From c874219101b188cd2e4f615fdb7753f47ad5aea3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 9 Jan 2015 17:14:52 -0800 Subject: [PATCH] Use the new devtools API --- brightray/brightray.gyp | 6 +- brightray/browser/browser_client.cc | 4 +- brightray/browser/browser_main_parts.cc | 21 +++-- brightray/browser/browser_main_parts.h | 4 +- brightray/browser/devtools_delegate.h | 73 ---------------- ...legate.cc => devtools_manager_delegate.cc} | 87 ++++++++++--------- brightray/browser/devtools_manager_delegate.h | 43 +++++++++ brightray/browser/remote_debugging_server.cc | 44 ++++++++++ brightray/browser/remote_debugging_server.h | 31 +++++++ 9 files changed, 189 insertions(+), 124 deletions(-) delete mode 100644 brightray/browser/devtools_delegate.h rename brightray/browser/{devtools_delegate.cc => devtools_manager_delegate.cc} (75%) create mode 100644 brightray/browser/devtools_manager_delegate.h create mode 100644 brightray/browser/remote_debugging_server.cc create mode 100644 brightray/browser/remote_debugging_server.h diff --git a/brightray/brightray.gyp b/brightray/brightray.gyp index ad90b1def86..736d169ec6c 100644 --- a/brightray/brightray.gyp +++ b/brightray/brightray.gyp @@ -43,10 +43,10 @@ 'browser/default_web_contents_delegate_mac.mm', 'browser/devtools_contents_resizing_strategy.cc', 'browser/devtools_contents_resizing_strategy.h', - 'browser/devtools_delegate.cc', - 'browser/devtools_delegate.h', 'browser/devtools_embedder_message_dispatcher.cc', 'browser/devtools_embedder_message_dispatcher.h', + 'browser/devtools_manager_delegate.cc', + 'browser/devtools_manager_delegate.h', 'browser/devtools_ui.cc', 'browser/devtools_ui.h', 'browser/inspectable_web_contents.cc', @@ -73,6 +73,8 @@ 'browser/notification_presenter_mac.mm', 'browser/linux/notification_presenter_linux.h', 'browser/linux/notification_presenter_linux.cc', + 'browser/remote_debugging_server.cc', + 'browser/remote_debugging_server.h', 'browser/url_request_context_getter.cc', 'browser/url_request_context_getter.h', 'browser/views/inspectable_web_contents_view_views.h', diff --git a/brightray/browser/browser_client.cc b/brightray/browser/browser_client.cc index a1c6d2f7719..b391cffaf8d 100644 --- a/brightray/browser/browser_client.cc +++ b/brightray/browser/browser_client.cc @@ -6,7 +6,7 @@ #include "browser/browser_context.h" #include "browser/browser_main_parts.h" -#include "browser/devtools_delegate.h" +#include "browser/devtools_manager_delegate.h" #include "browser/media/media_capture_devices_dispatcher.h" #include "browser/notification_presenter.h" @@ -98,7 +98,7 @@ base::FilePath BrowserClient::GetDefaultDownloadDirectory() { } content::DevToolsManagerDelegate* BrowserClient::GetDevToolsManagerDelegate() { - return new DevToolsManagerDelegate(browser_context()); + return new DevToolsManagerDelegate; } } // namespace brightray diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index 67d50d92306..351bee9c988 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -4,10 +4,12 @@ #include "browser/browser_main_parts.h" -#include "base/command_line.h" #include "browser/browser_context.h" -#include "browser/devtools_delegate.h" +#include "browser/remote_debugging_server.h" #include "browser/web_ui_controller_factory.h" + +#include "base/command_line.h" +#include "base/strings/string_number_conversions.h" #include "content/public/common/content_switches.h" #include "net/proxy/proxy_resolver_v8.h" @@ -121,10 +123,17 @@ void BrowserMainParts::PreMainMessageLoopRun() { new WebUIControllerFactory(browser_context_.get())); content::WebUIControllerFactory::RegisterFactory( web_ui_controller_factory_.get()); - if (CommandLine::ForCurrentProcess()->HasSwitch( - switches::kRemoteDebuggingPort)) { - devtools_delegate_.reset(new brightray::DevToolsDelegate( - browser_context())); + + // --remote-debugging-port + base::CommandLine* command_line = CommandLine::ForCurrentProcess(); + if (command_line->HasSwitch(switches::kRemoteDebuggingPort)) { + std::string port_str = command_line->GetSwitchValueASCII(switches::kRemoteDebuggingPort); + int port; + if (base::StringToInt(port_str, &port) && port >= 0 && port < 65535) + remote_debugging_server_.reset( + new RemoteDebuggingServer("127.0.0.1", static_cast(port))); + else + DLOG(WARNING) << "Invalid http debugger port number " << port; } } diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index a959b228f51..3e52af4bd50 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -25,7 +25,7 @@ namespace brightray { class BrowserContext; class WebUIControllerFactory; -class DevToolsDelegate; +class RemoteDebuggingServer; class BrowserMainParts : public content::BrowserMainParts { public: @@ -58,7 +58,7 @@ class BrowserMainParts : public content::BrowserMainParts { scoped_ptr browser_context_; scoped_ptr web_ui_controller_factory_; - scoped_ptr devtools_delegate_; + scoped_ptr remote_debugging_server_; #if defined(TOOLKIT_VIEWS) scoped_ptr views_delegate_; diff --git a/brightray/browser/devtools_delegate.h b/brightray/browser/devtools_delegate.h deleted file mode 100644 index b7fcc7b60c9..00000000000 --- a/brightray/browser/devtools_delegate.h +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 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-CHROMIUM file. - -#ifndef BRIGHTRAY_DEVTOOLS_DELEGATE_H_ -#define BRIGHTRAY_DEVTOOLS_DELEGATE_H_ - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "content/public/browser/devtools_http_handler_delegate.h" -#include "content/public/browser/devtools_manager_delegate.h" - -namespace content { -class BrowserContext; -class DevToolsHttpHandler; -} - -namespace brightray { - -class DevToolsDelegate : public content::DevToolsHttpHandlerDelegate { - public: - explicit DevToolsDelegate(content::BrowserContext* browser_context); - virtual ~DevToolsDelegate(); - - // Stops http server. - void Stop(); - - // DevToolsHttpProtocolHandler::Delegate overrides. - std::string GetDiscoveryPageHTML() override; - bool BundlesFrontendResources() override; - base::FilePath GetDebugFrontendDir() override; - scoped_ptr CreateSocketForTethering( - net::StreamListenSocket::Delegate* delegate, - std::string* name) override; - - content::DevToolsHttpHandler* devtools_http_handler() { - return devtools_http_handler_; - } - - private: - content::BrowserContext* browser_context_; - content::DevToolsHttpHandler* devtools_http_handler_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsDelegate); -}; - -class DevToolsManagerDelegate : public content::DevToolsManagerDelegate { - public: - explicit DevToolsManagerDelegate(content::BrowserContext* browser_context); - virtual ~DevToolsManagerDelegate(); - - // DevToolsManagerDelegate implementation. - void Inspect(content::BrowserContext* browser_context, - content::DevToolsAgentHost* agent_host) override {} - void DevToolsAgentStateChanged(content::DevToolsAgentHost* agent_host, - bool attached) override {} - base::DictionaryValue* HandleCommand( - content::DevToolsAgentHost* agent_host, - base::DictionaryValue* command) override; - scoped_ptr CreateNewTarget( - const GURL& url) override; - void EnumerateTargets(TargetCallback callback) override; - std::string GetPageThumbnailData(const GURL& url) override; - - private: - content::BrowserContext* browser_context_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsManagerDelegate); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_DEVTOOLS_DELEGATE_H_ diff --git a/brightray/browser/devtools_delegate.cc b/brightray/browser/devtools_manager_delegate.cc similarity index 75% rename from brightray/browser/devtools_delegate.cc rename to brightray/browser/devtools_manager_delegate.cc index 3cf57c4810e..2165b9c8524 100644 --- a/brightray/browser/devtools_delegate.cc +++ b/brightray/browser/devtools_manager_delegate.cc @@ -2,12 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE-CHROMIUM file. -#include "browser/devtools_delegate.h" +#include "browser/devtools_manager_delegate.h" #include #include "base/bind.h" #include "base/command_line.h" +#include "base/files/file_path.h" #include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" @@ -32,6 +33,8 @@ using content::BrowserContext; using content::DevToolsTarget; using content::DevToolsHttpHandler; +namespace brightray { + namespace { // A hack here: @@ -85,9 +88,9 @@ class Target : public content::DevToolsTarget { public: explicit Target(scoped_refptr agent_host); - virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); } - virtual std::string GetParentId() const OVERRIDE { return std::string(); } - virtual std::string GetType() const OVERRIDE { + std::string GetId() const override { return agent_host_->GetId(); } + std::string GetParentId() const override { return std::string(); } + std::string GetType() const override { switch (agent_host_->GetType()) { case DevToolsAgentHost::TYPE_WEB_CONTENTS: return kTargetTypePage; @@ -98,23 +101,19 @@ class Target : public content::DevToolsTarget { } return kTargetTypeOther; } - virtual std::string GetTitle() const OVERRIDE { - return agent_host_->GetTitle(); - } - virtual std::string GetDescription() const OVERRIDE { return std::string(); } - virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); } - virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; } - virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { + std::string GetTitle() const override { return agent_host_->GetTitle(); } + std::string GetDescription() const override { return std::string(); } + GURL GetURL() const override { return agent_host_->GetURL(); } + GURL GetFaviconURL() const override { return favicon_url_; } + base::TimeTicks GetLastActivityTime() const override { return last_activity_time_; } - virtual bool IsAttached() const OVERRIDE { - return agent_host_->IsAttached(); - } - virtual scoped_refptr GetAgentHost() const OVERRIDE { + bool IsAttached() const override { return agent_host_->IsAttached(); } + scoped_refptr GetAgentHost() const override { return agent_host_; } - virtual bool Activate() const OVERRIDE; - virtual bool Close() const OVERRIDE; + bool Activate() const override; + bool Close() const override; private: scoped_refptr agent_host_; @@ -141,27 +140,31 @@ bool Target::Close() const { return agent_host_->Close(); } -} // namespace - -namespace brightray { - // DevToolsDelegate -------------------------------------------------------- -DevToolsDelegate::DevToolsDelegate(content::BrowserContext* browser_context) - : browser_context_(browser_context) { - std::string frontend_url; - devtools_http_handler_ = DevToolsHttpHandler::Start( - CreateSocketFactory(), frontend_url, this, base::FilePath()); +class DevToolsDelegate : public content::DevToolsHttpHandlerDelegate { + public: + DevToolsDelegate(); + virtual ~DevToolsDelegate(); + + // content::DevToolsHttpHandlerDelegate. + std::string GetDiscoveryPageHTML() override; + bool BundlesFrontendResources() override; + base::FilePath GetDebugFrontendDir() override; + scoped_ptr CreateSocketForTethering( + net::StreamListenSocket::Delegate* delegate, + std::string* name) override; + + private: + DISALLOW_COPY_AND_ASSIGN(DevToolsDelegate); +}; + +DevToolsDelegate::DevToolsDelegate() { } DevToolsDelegate::~DevToolsDelegate() { } -void DevToolsDelegate::Stop() { - // The call below destroys this. - devtools_http_handler_->Stop(); -} - std::string DevToolsDelegate::GetDiscoveryPageHTML() { return ResourceBundle::GetSharedInstance().GetRawDataResource( kIDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string(); @@ -182,11 +185,20 @@ DevToolsDelegate::CreateSocketForTethering( return scoped_ptr(); } +} // namespace + // DevToolsManagerDelegate --------------------------------------------------- -DevToolsManagerDelegate::DevToolsManagerDelegate( - content::BrowserContext* browser_context) - : browser_context_(browser_context) { +// static +content::DevToolsHttpHandler* +DevToolsManagerDelegate::CreateHttpHandler() { + return DevToolsHttpHandler::Start(CreateSocketFactory(), + std::string(), + new DevToolsDelegate, + base::FilePath()); +} + +DevToolsManagerDelegate::DevToolsManagerDelegate() { } DevToolsManagerDelegate::~DevToolsManagerDelegate() { @@ -210,11 +222,8 @@ DevToolsManagerDelegate::CreateNewTarget(const GURL& url) { void DevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) { TargetList targets; - content::DevToolsAgentHost::List agents = - content::DevToolsAgentHost::GetOrCreateAll(); - for (content::DevToolsAgentHost::List::iterator it = agents.begin(); - it != agents.end(); ++it) { - targets.push_back(new Target(*it)); + for (const auto& agent_host : DevToolsAgentHost::GetOrCreateAll()) { + targets.push_back(new Target(agent_host)); } callback.Run(targets); } diff --git a/brightray/browser/devtools_manager_delegate.h b/brightray/browser/devtools_manager_delegate.h new file mode 100644 index 00000000000..529f1ff7b89 --- /dev/null +++ b/brightray/browser/devtools_manager_delegate.h @@ -0,0 +1,43 @@ +// Copyright (c) 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-CHROMIUM file. + +#ifndef BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_ +#define BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "content/public/browser/devtools_http_handler_delegate.h" +#include "content/public/browser/devtools_manager_delegate.h" + +namespace content { +class DevToolsHttpHandler; +} + +namespace brightray { + +class DevToolsManagerDelegate : public content::DevToolsManagerDelegate { + public: + static content::DevToolsHttpHandler* CreateHttpHandler(); + + DevToolsManagerDelegate(); + virtual ~DevToolsManagerDelegate(); + + // DevToolsManagerDelegate implementation. + void Inspect(content::BrowserContext* browser_context, + content::DevToolsAgentHost* agent_host) override {} + void DevToolsAgentStateChanged(content::DevToolsAgentHost* agent_host, + bool attached) override {} + base::DictionaryValue* HandleCommand(content::DevToolsAgentHost* agent_host, + base::DictionaryValue* command) override; + scoped_ptr CreateNewTarget(const GURL& url) override; + void EnumerateTargets(TargetCallback callback) override; + std::string GetPageThumbnailData(const GURL& url) override; + + private: + DISALLOW_COPY_AND_ASSIGN(DevToolsManagerDelegate); +}; + +} // namespace brightray + +#endif // BROWSER_DEVTOOLS_MANAGER_DELEGATE_H_ diff --git a/brightray/browser/remote_debugging_server.cc b/brightray/browser/remote_debugging_server.cc new file mode 100644 index 00000000000..9aa004c313a --- /dev/null +++ b/brightray/browser/remote_debugging_server.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2015 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-CHROMIUM file. + +#include "browser/remote_debugging_server.h" + +#include "browser/devtools_manager_delegate.h" + +#include "base/files/file_util.h" +#include "content/public/browser/devtools_http_handler.h" +#include "net/socket/tcp_server_socket.h" + +namespace brightray { + +namespace { + +class TCPServerSocketFactory + : public content::DevToolsHttpHandler::ServerSocketFactory { + public: + TCPServerSocketFactory(const std::string& address, uint16 port, int backlog) + : content::DevToolsHttpHandler::ServerSocketFactory(address, port, backlog) {} + + private: + // content::DevToolsHttpHandler::ServerSocketFactory: + scoped_ptr Create() const override { + return scoped_ptr(new net::TCPServerSocket(NULL, net::NetLog::Source())); + } + + DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); +}; + +} // namespace + +RemoteDebuggingServer::RemoteDebuggingServer(const std::string& ip, uint16 port) { + scoped_ptr factory( + new TCPServerSocketFactory(ip, port, 1)); + devtools_http_handler_ = DevToolsManagerDelegate::CreateHttpHandler(); +} + +RemoteDebuggingServer::~RemoteDebuggingServer() { + devtools_http_handler_->Stop(); +} + +} // namespace brightray diff --git a/brightray/browser/remote_debugging_server.h b/brightray/browser/remote_debugging_server.h new file mode 100644 index 00000000000..ff0e5bcf497 --- /dev/null +++ b/brightray/browser/remote_debugging_server.h @@ -0,0 +1,31 @@ +// Copyright (c) 2015 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-CHROMIUM file. + +#ifndef BROWSER_REMOTE_DEBUGGING_SERVER_H_ +#define BROWSER_REMOTE_DEBUGGING_SERVER_H_ + +#include + +#include "base/basictypes.h" + +namespace content { +class DevToolsHttpHandler; +} + +namespace brightray { + +class RemoteDebuggingServer { +public: + RemoteDebuggingServer(const std::string& ip, uint16 port); + virtual ~RemoteDebuggingServer(); + +private: + content::DevToolsHttpHandler* devtools_http_handler_; + + DISALLOW_COPY_AND_ASSIGN(RemoteDebuggingServer); +}; + +} // namespace brightray + +#endif // BROWSER_REMOTE_DEBUGGING_SERVER_H_