From 74db9da5e128379496bc150f54c8efc98a1949eb Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 28 Oct 2014 17:08:25 -0700 Subject: [PATCH] Add inspect command to skydb This CL adds an "inspect" command to skydb that injects the inspector module into the page and prints some instructions for opening the inspector. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/690433004 --- engine/public/web/WebView.h | 3 +++ engine/web/WebViewImpl.cpp | 12 +++++++++++ engine/web/WebViewImpl.h | 1 + tools/debugger/debugger.cc | 20 ++++++++++++++---- tools/debugger/debugger.mojom | 1 + tools/debugger/prompt/prompt.cc | 21 +++++++++++++++---- viewer/BUILD.gn | 3 +++ viewer/DEPS | 1 + viewer/document_view.cc | 2 ++ viewer/document_view.h | 2 ++ viewer/services/inspector.mojom | 11 ++++++++++ viewer/services/inspector_impl.cc | 28 +++++++++++++++++++++++++ viewer/services/inspector_impl.h | 35 +++++++++++++++++++++++++++++++ 13 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 viewer/services/inspector.mojom create mode 100644 viewer/services/inspector_impl.cc create mode 100644 viewer/services/inspector_impl.h diff --git a/engine/public/web/WebView.h b/engine/public/web/WebView.h index bc0b3a55833..6e97abc0019 100644 --- a/engine/public/web/WebView.h +++ b/engine/public/web/WebView.h @@ -128,6 +128,9 @@ public: virtual WebFrame* mainFrame() = 0; + virtual void injectModule(const WebString& path) = 0; + + // Focus --------------------------------------------------------------- virtual WebFrame* focusedFrame() = 0; diff --git a/engine/web/WebViewImpl.cpp b/engine/web/WebViewImpl.cpp index 954be58c5fd..f1eb3427c8f 100644 --- a/engine/web/WebViewImpl.cpp +++ b/engine/web/WebViewImpl.cpp @@ -50,6 +50,7 @@ #include "core/frame/FrameView.h" #include "core/frame/LocalFrame.h" #include "core/frame/Settings.h" +#include "core/html/HTMLLinkElement.h" #include "core/html/HTMLMediaElement.h" #include "core/html/ime/InputMethodContext.h" #include "core/loader/FrameLoader.h" @@ -1659,6 +1660,17 @@ WebFrame* WebViewImpl::focusedFrame() return WebFrame::fromFrame(focusedCoreFrame()); } +void WebViewImpl::injectModule(const WebString& path) +{ + RefPtr document = m_page->mainFrame()->document(); + RefPtr link = HTMLLinkElement::create(*document, false); + link->setAttribute(HTMLNames::relAttr, "import"); + link->setAttribute(HTMLNames::hrefAttr, path); + if (!document->documentElement()) + return; + document->documentElement()->appendChild(link.release()); +} + void WebViewImpl::setFocusedFrame(WebFrame* frame) { if (!frame) { diff --git a/engine/web/WebViewImpl.h b/engine/web/WebViewImpl.h index 2b510d96c2a..70f3ebd3e3c 100644 --- a/engine/web/WebViewImpl.h +++ b/engine/web/WebViewImpl.h @@ -118,6 +118,7 @@ public: // WebView methods: virtual void setMainFrame(WebFrame*) override; + virtual void injectModule(const WebString&) override; virtual void setSpellCheckClient(WebSpellCheckClient*) override; virtual WebSettings* settings() override; virtual WebString pageEncoding() const override; diff --git a/tools/debugger/debugger.cc b/tools/debugger/debugger.cc index 52d00447dbc..e9a77a2f5cf 100644 --- a/tools/debugger/debugger.cc +++ b/tools/debugger/debugger.cc @@ -16,6 +16,7 @@ #include "mojo/services/window_manager/window_manager_delegate.h" #include "sky/tools/debugger/focus_rules.h" #include "sky/tools/debugger/debugger.mojom.h" +#include "sky/viewer/services/inspector.mojom.h" namespace sky { @@ -58,7 +59,7 @@ class SkyDebugger : public mojo::ApplicationDelegate, mojo::ViewManager* view_manager, mojo::View* root, mojo::ServiceProviderImpl* exported_services, - scoped_ptr remote_service_provider) override { + scoped_ptr imported_services) override { view_manager_ = view_manager; root_ = root; @@ -104,10 +105,19 @@ class SkyDebugger : public mojo::ApplicationDelegate, // We can get Navigate commands before we've actually been // embedded into the view and content_ created. // Just save the last one. - if (content_) - content_->Embed(url); - else + if (content_) { + scoped_ptr exported_services( + new mojo::ServiceProviderImpl()); + viewer_services_ = content_->Embed(url, exported_services.Pass()); + } else { pending_url_ = url; + } + } + + virtual void InjectInspector() override { + InspectorServicePtr inspector_service; + mojo::ConnectToService(viewer_services_.get(), &inspector_service); + inspector_service->Inject(); } scoped_ptr window_manager_app_; @@ -117,6 +127,8 @@ class SkyDebugger : public mojo::ApplicationDelegate, mojo::View* content_; std::string pending_url_; + scoped_ptr viewer_services_; + DISALLOW_COPY_AND_ASSIGN(SkyDebugger); }; diff --git a/tools/debugger/debugger.mojom b/tools/debugger/debugger.mojom index 067a160569a..df3399e27c5 100644 --- a/tools/debugger/debugger.mojom +++ b/tools/debugger/debugger.mojom @@ -6,6 +6,7 @@ module sky { interface Debugger { NavigateToURL(string url); + InjectInspector(); }; } diff --git a/tools/debugger/prompt/prompt.cc b/tools/debugger/prompt/prompt.cc index d411c82618f..6bcc464b346 100644 --- a/tools/debugger/prompt/prompt.cc +++ b/tools/debugger/prompt/prompt.cc @@ -73,6 +73,10 @@ class Prompt : public mojo::ApplicationDelegate { Reload(); return true; } + if (command == "inspect") { + Inspect(); + return true; + } if (command.size() == 1) { char c = command[0]; if (c == 'h') @@ -111,16 +115,25 @@ class Prompt : public mojo::ApplicationDelegate { << "Sky Debugger" << std::endl << "============" << std::endl << "Type a URL to load in the debugger, enter to reload." << std::endl - << "Commands: help -- Help" << std::endl - << " trace -- Capture a trace" << std::endl - << " reload -- Reload the current page" << std::endl - << " q -- Quit" << std::endl; + << "Commands: help -- Help" << std::endl + << " trace -- Capture a trace" << std::endl + << " reload -- Reload the current page" << std::endl + << " inspect -- Inspect the current page" << std::endl + << " q -- Quit" << std::endl; } void Reload() { debugger_->NavigateToURL(url_); } + void Inspect() { + debugger_->InjectInspector(); + std::cout + << "Open the following URL in Chrome:" << std::endl + << "chrome-devtools://devtools/bundled/devtools.html?ws=localhost:9898" + << std::endl; + } + void Quit() { std::cout << "quitting" << std::endl; exit(0); diff --git a/viewer/BUILD.gn b/viewer/BUILD.gn index 854fbca67f4..cf27e7cf0d2 100644 --- a/viewer/BUILD.gn +++ b/viewer/BUILD.gn @@ -34,6 +34,8 @@ shared_library("viewer") { "script/script_runner.h", "services/tracing_impl.cc", "services/tracing_impl.h", + "services/inspector_impl.cc", + "services/inspector_impl.h", "viewer.cc", ] @@ -78,6 +80,7 @@ mojom("bindings") { sources = [ "test_observer.mojom", "services/tracing.mojom", + "services/inspector.mojom", ] } diff --git a/viewer/DEPS b/viewer/DEPS index 599ed9d7c56..c15c66c7581 100644 --- a/viewer/DEPS +++ b/viewer/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+cc", + "+mojo/public", "+mojo/application", "+mojo/cc", "+mojo/converters/geometry", diff --git a/viewer/document_view.cc b/viewer/document_view.cc index 2265ae1930a..d5754d75405 100644 --- a/viewer/document_view.cc +++ b/viewer/document_view.cc @@ -75,6 +75,7 @@ DocumentView::DocumentView( web_view_(NULL), root_(NULL), view_manager_client_factory_(shell, this), + inspector_service_factory_(this), compositor_thread_(compositor_thread), weak_factory_(this) { mojo::ServiceProviderImpl* exported_services = new mojo::ServiceProviderImpl(); @@ -102,6 +103,7 @@ void DocumentView::OnEmbed( root_ = root; imported_services_ = imported_services.Pass(); navigator_host_.set_service_provider(imported_services_.get()); + exported_services->AddService(&inspector_service_factory_); Load(response_.Pass()); diff --git a/viewer/document_view.h b/viewer/document_view.h index 305a97ffb69..665d9329815 100644 --- a/viewer/document_view.h +++ b/viewer/document_view.h @@ -16,6 +16,7 @@ #include "mojo/services/public/interfaces/network/url_loader.mojom.h" #include "sky/engine/public/web/WebFrameClient.h" #include "sky/engine/public/web/WebViewClient.h" +#include "sky/viewer/services/inspector_impl.h" namespace base { class MessageLoopProxy; @@ -98,6 +99,7 @@ class DocumentView : public blink::WebViewClient, blink::WebView* web_view_; mojo::View* root_; mojo::ViewManagerClientFactory view_manager_client_factory_; + InspectorServiceFactory inspector_service_factory_; scoped_ptr web_layer_tree_view_impl_; scoped_refptr compositor_thread_; scoped_ptr script_runner_; diff --git a/viewer/services/inspector.mojom b/viewer/services/inspector.mojom new file mode 100644 index 00000000000..95b3eb58549 --- /dev/null +++ b/viewer/services/inspector.mojom @@ -0,0 +1,11 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +module sky { + +interface InspectorService { + Inject(); +}; + +} diff --git a/viewer/services/inspector_impl.cc b/viewer/services/inspector_impl.cc new file mode 100644 index 00000000000..a5343fa9600 --- /dev/null +++ b/viewer/services/inspector_impl.cc @@ -0,0 +1,28 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/viewer/services/inspector_impl.h" + +#include "sky/engine/public/web/WebDocument.h" +#include "sky/engine/public/web/WebElement.h" +#include "sky/engine/public/web/WebFrame.h" +#include "sky/engine/public/web/WebView.h" +#include "sky/viewer/document_view.h" + +namespace sky { + +InspectorServiceImpl::InspectorServiceImpl(DocumentView* view) + : view_(view->GetWeakPtr()) { +} + +InspectorServiceImpl::~InspectorServiceImpl() { +} + +void InspectorServiceImpl::Inject() { + if (!view_) + return; + view_->web_view()->injectModule("/sky/framework/inspector/inspector.sky"); +} + +} // namespace sky diff --git a/viewer/services/inspector_impl.h b/viewer/services/inspector_impl.h new file mode 100644 index 00000000000..1db6cde60e0 --- /dev/null +++ b/viewer/services/inspector_impl.h @@ -0,0 +1,35 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_VIEWER_SERVICES_INSPECTOR_IMPL_H_ +#define SKY_VIEWER_SERVICES_INSPECTOR_IMPL_H_ + +#include "base/basictypes.h" +#include "base/memory/weak_ptr.h" +#include "mojo/public/cpp/application/interface_factory_impl.h" +#include "sky/viewer/services/inspector.mojom.h" + +namespace sky { +class DocumentView; + +class InspectorServiceImpl : public mojo::InterfaceImpl { + public: + explicit InspectorServiceImpl(DocumentView*); + virtual ~InspectorServiceImpl(); + + private: + // Overridden from InspectorService: + void Inject() override; + + base::WeakPtr view_; + + DISALLOW_COPY_AND_ASSIGN(InspectorServiceImpl); +}; + +typedef mojo::InterfaceFactoryImplWithContext< + InspectorServiceImpl, DocumentView> InspectorServiceFactory; + +} // namespace sky + +#endif // SKY_VIEWER_SERVICES_INSPECTOR_IMPL_H_