From 63c94b7a2565f38a2e922420be0da2223f56f663 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 17 Jun 2015 17:54:27 +0200 Subject: Update remote inspector for Chromium 44 Handle changes in modules, API and ownership of inspector related classes. Change-Id: I8faa7f22b50828edeac450cc34c151fbb2c45a0c Reviewed-by: Jocelyn Turcotte (Woboq GmbH) --- src/core/dev_tools_http_handler_delegate_qt.cpp | 122 +++++++++++++----------- src/core/dev_tools_http_handler_delegate_qt.h | 39 ++++---- src/core/qtwebengine.gypi | 2 + src/core/web_engine_context.cpp | 2 +- src/core/web_engine_context.h | 5 +- 5 files changed, 91 insertions(+), 79 deletions(-) diff --git a/src/core/dev_tools_http_handler_delegate_qt.cpp b/src/core/dev_tools_http_handler_delegate_qt.cpp index 964d6ad38..2afd75e6e 100644 --- a/src/core/dev_tools_http_handler_delegate_qt.cpp +++ b/src/core/dev_tools_http_handler_delegate_qt.cpp @@ -49,9 +49,11 @@ #include "base/files/file_path.h" #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" +#include "components/devtools_discovery/devtools_discovery_manager.h" +#include "components/devtools_discovery/devtools_target_descriptor.h" +#include "components/devtools_http_handler/devtools_http_handler.h" #include "content/public/browser/devtools_agent_host.h" -#include "content/public/browser/devtools_http_handler.h" -#include "content/public/browser/devtools_target.h" +#include "content/public/browser/devtools_frontend_host.h" #include "content/public/browser/favicon_status.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/render_view_host.h" @@ -59,14 +61,18 @@ #include "content/public/browser/web_contents_delegate.h" #include "content/public/common/content_switches.h" #include "net/base/ip_endpoint.h" +#include "net/base/net_errors.h" #include "net/socket/stream_listen_socket.h" #include "net/socket/tcp_server_socket.h" using namespace content; +using namespace devtools_discovery; +using namespace devtools_http_handler; namespace { const char kTargetTypePage[] = "page"; +const char kTargetTypeSharedWorker[] = "worker"; const char kTargetTypeServiceWorker[] = "service_worker"; const char kTargetTypeOther[] = "other"; @@ -74,15 +80,29 @@ class TCPServerSocketFactory : public DevToolsHttpHandler::ServerSocketFactory { public: TCPServerSocketFactory(const std::string& address, int port, int backlog) - : DevToolsHttpHandler::ServerSocketFactory(address, port, backlog) {} + : m_address(address), m_port(port), m_backlog(backlog) + {} private: - scoped_ptr Create() const override { - return scoped_ptr(new net::TCPServerSocket(NULL, net::NetLog::Source())); - } - DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); + scoped_ptr CreateForHttpServer() override { + scoped_ptr socket(new net::TCPServerSocket(nullptr, net::NetLog::Source())); + if (socket->ListenWithAddressAndPort(m_address, m_port, m_backlog) != net::OK) + return scoped_ptr(); + + return socket; + } + + const std::string m_address; + int m_port; + int m_backlog; + DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); }; -class Target : public content::DevToolsTarget { +class DevToolsDiscoveryProviderQt : public DevToolsDiscoveryManager::Provider { +public: + DevToolsTargetDescriptor::List GetDescriptors() override; +}; + +class Target : public DevToolsTargetDescriptor { public: explicit Target(scoped_refptr agent_host); @@ -92,6 +112,8 @@ public: switch (agent_host_->GetType()) { case DevToolsAgentHost::TYPE_WEB_CONTENTS: return kTargetTypePage; + case DevToolsAgentHost::TYPE_SHARED_WORKER: + return kTargetTypeSharedWorker; case DevToolsAgentHost::TYPE_SERVICE_WORKER: return kTargetTypeServiceWorker; default: @@ -141,17 +163,40 @@ bool Target::Close() const { return agent_host_->Close(); } +DevToolsTargetDescriptor::List DevToolsDiscoveryProviderQt::GetDescriptors() +{ + DevToolsTargetDescriptor::List targets; + for (const auto& agent_host : DevToolsAgentHost::GetOrCreateAll()) { + targets.push_back(new Target(agent_host)); + } + return targets; +} + } // namespace namespace QtWebEngineCore { +scoped_ptr createDevToolsHttpHandler() +{ + DevToolsHttpHandlerDelegateQt *delegate = new DevToolsHttpHandlerDelegateQt(); + if (!delegate->isValid()) { + delete delegate; + return nullptr; + } + scoped_ptr factory(new TCPServerSocketFactory(delegate->bindAddress().toStdString(), delegate->port(), 1)); + // Ownership of the delegate is taken over the devtools http handler. + scoped_ptr handler(new DevToolsHttpHandler(factory.Pass(), std::string(), delegate, base::FilePath(), base::FilePath(), std::string(), std::string())); + DevToolsDiscoveryManager::GetInstance()->AddProvider(scoped_ptr(new DevToolsDiscoveryProviderQt())); + return handler; +} + DevToolsHttpHandlerDelegateQt::DevToolsHttpHandlerDelegateQt() - : m_devtoolsHttpHandler(0) - , m_bindAddress(QLatin1String("127.0.0.1")) + : m_bindAddress(QLatin1String("127.0.0.1")) , m_port(0) + , m_valid(false) { const QString inspectorEnv = QString::fromUtf8(qgetenv("QTWEBENGINE_REMOTE_DEBUGGING")); - const CommandLine &commandLine = *CommandLine::ForCurrentProcess(); + const base::CommandLine &commandLine = *base::CommandLine::ForCurrentProcess(); QString portStr; if (commandLine.HasSwitch(switches::kRemoteDebuggingPort)) { @@ -166,26 +211,16 @@ DevToolsHttpHandlerDelegateQt::DevToolsHttpHandlerDelegateQt() } else return; - bool ok = false; - m_port = portStr.toInt(&ok); - if (ok && m_port > 0 && m_port < 65535) { - scoped_ptr factory(new TCPServerSocketFactory(m_bindAddress.toStdString(), m_port, 1)); - m_devtoolsHttpHandler = DevToolsHttpHandler::Start(factory.Pass(), std::string(), this, base::FilePath()); - } else + m_port = portStr.toInt(&m_valid); + m_valid = m_valid && m_port > 0 && m_port < 65535; + if (!m_valid) qWarning("Invalid port given for the inspector server \"%s\". Examples of valid input: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's network interface).", qPrintable(portStr)); } -DevToolsHttpHandlerDelegateQt::~DevToolsHttpHandlerDelegateQt() -{ - // Stop() takes care of deleting the DevToolsHttpHandler. - if (m_devtoolsHttpHandler) - m_devtoolsHttpHandler->Stop(); -} - -void DevToolsHttpHandlerDelegateQt::Initialized(const net::IPEndPoint& ip_address) +void DevToolsHttpHandlerDelegateQt::Initialized(const net::IPEndPoint *ip_address) { - if (ip_address.address().size()) { - QString addressAndPort = QString::fromStdString(ip_address.ToString()); + if (ip_address && ip_address->address().size()) { + QString addressAndPort = QString::fromStdString(ip_address->ToString()); qWarning("Remote debugging server started successfully. Try pointing a Chromium-based browser to http://%s", qPrintable(addressAndPort)); } else qWarning("Couldn't start the inspector server on bind address \"%s\" and port \"%d\". In case of invalid input, try something like: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's interface).", qPrintable(m_bindAddress), m_port); @@ -203,42 +238,19 @@ std::string DevToolsHttpHandlerDelegateQt::GetDiscoveryPageHTML() return html; } -bool DevToolsHttpHandlerDelegateQt::BundlesFrontendResources() -{ - return true; -} - -base::FilePath DevToolsHttpHandlerDelegateQt::GetDebugFrontendDir() -{ - return base::FilePath(); -} - -scoped_ptr DevToolsHttpHandlerDelegateQt::CreateSocketForTethering(net::StreamListenSocket::Delegate* delegate, std::string* name) -{ - return scoped_ptr(); -} - -base::DictionaryValue* DevToolsManagerDelegateQt::HandleCommand(DevToolsAgentHost *, base::DictionaryValue *) { - return 0; -} - -std::string DevToolsManagerDelegateQt::GetPageThumbnailData(const GURL& url) +std::string DevToolsHttpHandlerDelegateQt::GetPageThumbnailData(const GURL& url) { return std::string(); } -scoped_ptr DevToolsManagerDelegateQt::CreateNewTarget(const GURL &) +std::string DevToolsHttpHandlerDelegateQt::GetFrontendResource(const std::string &path) { - return scoped_ptr(); + return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); } -void DevToolsManagerDelegateQt::EnumerateTargets(TargetCallback callback) +base::DictionaryValue* DevToolsManagerDelegateQt::HandleCommand(DevToolsAgentHost *, base::DictionaryValue *) { - TargetList targets; - for (const auto& agent_host : DevToolsAgentHost::GetOrCreateAll()) { - targets.push_back(new Target(agent_host)); - } - callback.Run(targets); + return 0; } } //namespace QtWebEngineCore diff --git a/src/core/dev_tools_http_handler_delegate_qt.h b/src/core/dev_tools_http_handler_delegate_qt.h index 902e99507..b998359fd 100644 --- a/src/core/dev_tools_http_handler_delegate_qt.h +++ b/src/core/dev_tools_http_handler_delegate_qt.h @@ -37,43 +37,43 @@ #ifndef DEV_TOOLS_HTTP_HANDLER_DELEGATE_QT_H #define DEV_TOOLS_HTTP_HANDLER_DELEGATE_QT_H -#include "content/public/browser/devtools_http_handler_delegate.h" +#include "components/devtools_http_handler/devtools_http_handler_delegate.h" #include "content/public/browser/devtools_manager_delegate.h" +#include "net/socket/stream_listen_socket.h" #include #include // needed for Q_DECL_OVERRIDE -namespace net { -class StreamListenSocket; -} - namespace content { class BrowserContext; +} + +namespace devtools_http_handler { class DevToolsHttpHandler; -class RenderViewHost; } namespace QtWebEngineCore { -class DevToolsHttpHandlerDelegateQt : public content::DevToolsHttpHandlerDelegate { -public: +scoped_ptr createDevToolsHttpHandler(); +class DevToolsHttpHandlerDelegateQt : public devtools_http_handler::DevToolsHttpHandlerDelegate { +public: DevToolsHttpHandlerDelegateQt(); - virtual ~DevToolsHttpHandlerDelegateQt(); - // content::DevToolsHttpHandlerDelegate Overrides - virtual void Initialized(const net::IPEndPoint &ip_address) Q_DECL_OVERRIDE; - virtual std::string GetDiscoveryPageHTML() Q_DECL_OVERRIDE; - virtual bool BundlesFrontendResources() Q_DECL_OVERRIDE; - virtual base::FilePath GetDebugFrontendDir() Q_DECL_OVERRIDE; - // Requests the list of all inspectable targets. - // The caller gets the ownership of the returned targets. - virtual scoped_ptr CreateSocketForTethering(net::StreamListenSocket::Delegate *delegate, std::string *name) Q_DECL_OVERRIDE; + bool isValid() const { return m_valid; } + QString bindAddress() const { return m_bindAddress; } + int port() const { return m_port; } + + // devtools_http_handler::DevToolsHttpHandlerDelegate Overrides + void Initialized(const net::IPEndPoint *ip_address) Q_DECL_OVERRIDE; + std::string GetDiscoveryPageHTML() Q_DECL_OVERRIDE; + std::string GetFrontendResource(const std::string&) Q_DECL_OVERRIDE; + std::string GetPageThumbnailData(const GURL &url) Q_DECL_OVERRIDE; private: - content::DevToolsHttpHandler *m_devtoolsHttpHandler; QString m_bindAddress; int m_port; + bool m_valid; }; class DevToolsManagerDelegateQt : public content::DevToolsManagerDelegate { @@ -81,9 +81,6 @@ public: void Inspect(content::BrowserContext *browser_context, content::DevToolsAgentHost *agent_host) Q_DECL_OVERRIDE { } void DevToolsAgentStateChanged(content::DevToolsAgentHost *agent_host, bool attached) Q_DECL_OVERRIDE { } base::DictionaryValue *HandleCommand(content::DevToolsAgentHost *agent_host, base::DictionaryValue *command) Q_DECL_OVERRIDE; - scoped_ptr CreateNewTarget(const GURL &url) Q_DECL_OVERRIDE; - void EnumerateTargets(TargetCallback callback) Q_DECL_OVERRIDE; - std::string GetPageThumbnailData(const GURL &url) Q_DECL_OVERRIDE; }; } // namespace QtWebEngineCore diff --git a/src/core/qtwebengine.gypi b/src/core/qtwebengine.gypi index 15da6b6ec..2c433f614 100644 --- a/src/core/qtwebengine.gypi +++ b/src/core/qtwebengine.gypi @@ -8,6 +8,8 @@ 'dependencies': [ '<(chromium_src_dir)/base/base.gyp:base', '<(chromium_src_dir)/base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', + '<(chromium_src_dir)/components/components.gyp:devtools_discovery', + '<(chromium_src_dir)/components/components.gyp:devtools_http_handler', '<(chromium_src_dir)/components/components.gyp:error_page_renderer', '<(chromium_src_dir)/components/components.gyp:visitedlink_browser', '<(chromium_src_dir)/components/components.gyp:visitedlink_renderer', diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index c1d0a61aa..605a89f37 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -249,7 +249,7 @@ WebEngineContext::WebEngineContext() m_runLoop.reset(new base::RunLoop); m_runLoop->BeforeRun(); - m_devtools.reset(new DevToolsHttpHandlerDelegateQt); + m_devtools = createDevToolsHttpHandler(); // Force the initialization of MediaCaptureDevicesDispatcher on the UI // thread to avoid a thread check assertion in its constructor when it // first gets referenced on the IO thread. diff --git a/src/core/web_engine_context.h b/src/core/web_engine_context.h index f900c93d8..f78791f27 100644 --- a/src/core/web_engine_context.h +++ b/src/core/web_engine_context.h @@ -41,6 +41,8 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "components/devtools_http_handler/devtools_http_handler.h" #include @@ -59,7 +61,6 @@ namespace QtWebEngineCore { class BrowserContextAdapter; class ContentMainDelegateQt; -class DevToolsHttpHandlerDelegateQt; class SurfaceFactoryQt; } // namespace @@ -81,7 +82,7 @@ private: scoped_ptr m_browserRunner; QObject* m_globalQObject; QExplicitlySharedDataPointer m_defaultBrowserContext; - scoped_ptr m_devtools; + scoped_ptr m_devtools; }; #endif // WEB_ENGINE_CONTEXT_H -- cgit v1.2.3