diff --git a/brightray/brightray.gyp b/brightray/brightray.gyp index e7e6fcd66059..128d81ea77b8 100644 --- a/brightray/brightray.gyp +++ b/brightray/brightray.gyp @@ -35,8 +35,8 @@ 'browser/default_web_contents_delegate.cc', 'browser/default_web_contents_delegate.h', 'browser/default_web_contents_delegate_mac.mm', - 'browser/devtools_delegate.cc', - 'browser/devtools_delegate.h', + 'browser/devtools_ui.cc', + 'browser/devtools_ui.h', 'browser/download_manager_delegate.cc', 'browser/download_manager_delegate.h', 'browser/inspectable_web_contents.cc', @@ -66,6 +66,8 @@ 'browser/win/devtools_window.h', 'browser/win/inspectable_web_contents_view_win.cc', 'browser/win/inspectable_web_contents_view_win.h', + 'browser/web_ui_controller_factory.cc', + 'browser/web_ui_controller_factory.h', 'common/application_info.h', 'common/application_info_mac.mm', 'common/application_info_win.cc', diff --git a/brightray/browser/browser_main_parts.cc b/brightray/browser/browser_main_parts.cc index bb435057c056..15a7192d0093 100644 --- a/brightray/browser/browser_main_parts.cc +++ b/brightray/browser/browser_main_parts.cc @@ -5,9 +5,8 @@ #include "browser/browser_main_parts.h" #include "browser/browser_context.h" -#include "browser/devtools_delegate.h" +#include "browser/web_ui_controller_factory.h" -#include "content/public/browser/devtools_http_handler.h" #include "net/socket/tcp_listen_socket.h" namespace brightray { @@ -16,19 +15,14 @@ BrowserMainParts::BrowserMainParts() { } BrowserMainParts::~BrowserMainParts() { - devtools_http_handler_->Stop(); - devtools_http_handler_ = nullptr; } void BrowserMainParts::PreMainMessageLoopRun() { browser_context_.reset(CreateBrowserContext()); browser_context_->Initialize(); - // These two objects are owned by devtools_http_handler_. - auto delegate = new DevToolsDelegate; - auto factory = new net::TCPListenSocketFactory("127.0.0.1", 0); - - devtools_http_handler_ = content::DevToolsHttpHandler::Start(factory, std::string(), delegate); + web_ui_controller_factory_.reset(new WebUIControllerFactory(browser_context_.get())); + content::WebUIControllerFactory::RegisterFactory(web_ui_controller_factory_.get()); } BrowserContext* BrowserMainParts::CreateBrowserContext() { diff --git a/brightray/browser/browser_main_parts.h b/brightray/browser/browser_main_parts.h index 7120c619b0b3..ca80e557654f 100644 --- a/brightray/browser/browser_main_parts.h +++ b/brightray/browser/browser_main_parts.h @@ -9,13 +9,10 @@ #include "base/memory/scoped_ptr.h" #include "content/public/browser/browser_main_parts.h" -namespace content { -class DevToolsHttpHandler; -} - namespace brightray { class BrowserContext; +class WebUIControllerFactory; class BrowserMainParts : public content::BrowserMainParts { public: @@ -23,7 +20,6 @@ public: ~BrowserMainParts(); BrowserContext* browser_context() { return browser_context_.get(); } - content::DevToolsHttpHandler* devtools_http_handler() { return devtools_http_handler_; } protected: // Subclasses should override this to provide their own BrowserContxt implementation. The caller @@ -38,7 +34,7 @@ protected: private: scoped_ptr browser_context_; - content::DevToolsHttpHandler* devtools_http_handler_; + scoped_ptr web_ui_controller_factory_; DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); }; diff --git a/brightray/browser/devtools_delegate.cc b/brightray/browser/devtools_delegate.cc deleted file mode 100644 index ccbcd362aa92..000000000000 --- a/brightray/browser/devtools_delegate.cc +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2012 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 "devtools_delegate.h" - -namespace brightray { - -DevToolsDelegate::DevToolsDelegate() { -} - -DevToolsDelegate::~DevToolsDelegate() { -} - -std::string DevToolsDelegate::GetDiscoveryPageHTML() { - return std::string(); -} - -bool DevToolsDelegate::BundlesFrontendResources() { - return true; -} - -base::FilePath DevToolsDelegate::GetDebugFrontendDir() { - return base::FilePath(); -} - -std::string DevToolsDelegate::GetPageThumbnailData(const GURL&) { - return std::string(); -} - -content::RenderViewHost* DevToolsDelegate::CreateNewTarget() { - return nullptr; -} - -content::DevToolsHttpHandlerDelegate::TargetType DevToolsDelegate::GetTargetType(content::RenderViewHost*) { - return kTargetTypeTab; -} - -std::string DevToolsDelegate::GetViewDescription(content::RenderViewHost*) { - return std::string(); -} - -scoped_refptr DevToolsDelegate::CreateSocketForTethering( - net::StreamListenSocket::Delegate*, - std::string* name) { - return nullptr; -} - -} diff --git a/brightray/browser/devtools_delegate.h b/brightray/browser/devtools_delegate.h deleted file mode 100644 index 12dd2087ee73..000000000000 --- a/brightray/browser/devtools_delegate.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2012 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_BROWSER_DEVTOOLS_DELEGATE_H_ -#define BRIGHTRAY_BROWSER_DEVTOOLS_DELEGATE_H_ - -#include "content/public/browser/devtools_http_handler_delegate.h" - -namespace brightray { - -class DevToolsDelegate : public content::DevToolsHttpHandlerDelegate { -public: - DevToolsDelegate(); - ~DevToolsDelegate(); - -private: - virtual std::string GetDiscoveryPageHTML() OVERRIDE; - virtual bool BundlesFrontendResources() OVERRIDE; - virtual base::FilePath GetDebugFrontendDir() OVERRIDE; - virtual std::string GetPageThumbnailData(const GURL&) OVERRIDE; - virtual content::RenderViewHost* CreateNewTarget() OVERRIDE; - virtual TargetType GetTargetType(content::RenderViewHost*) OVERRIDE; - virtual std::string GetViewDescription(content::RenderViewHost*) OVERRIDE; - virtual scoped_refptr CreateSocketForTethering( - net::StreamListenSocket::Delegate*, - std::string* name) OVERRIDE; -}; - -} - -#endif diff --git a/brightray/browser/devtools_ui.cc b/brightray/browser/devtools_ui.cc new file mode 100644 index 000000000000..571dc8adb626 --- /dev/null +++ b/brightray/browser/devtools_ui.cc @@ -0,0 +1,102 @@ +// Copyright (c) 2012 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/devtools_ui.h" + +#include + +#include "browser/browser_context.h" + +#include "base/memory/ref_counted_memory.h" +#include "base/strings/string_util.h" +#include "base/strings/stringprintf.h" +#include "content/public/browser/devtools_http_handler.h" +#include "content/public/browser/url_data_source.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_ui.h" +#include "ui/base/resource/resource_bundle.h" + +using content::WebContents; + +namespace brightray { + +namespace { + +const char kChromeUIDevToolsBundledHost[] = "devtools"; + +std::string PathWithoutParams(const std::string& path) { + return GURL(std::string("chrome-devtools://devtools/") + path) + .path().substr(1); +} + +std::string GetMimeTypeForPath(const std::string& path) { + std::string filename = PathWithoutParams(path); + if (EndsWith(filename, ".html", false)) { + return "text/html"; + } else if (EndsWith(filename, ".css", false)) { + return "text/css"; + } else if (EndsWith(filename, ".js", false)) { + return "application/javascript"; + } else if (EndsWith(filename, ".png", false)) { + return "image/png"; + } else if (EndsWith(filename, ".gif", false)) { + return "image/gif"; + } else if (EndsWith(filename, ".manifest", false)) { + return "text/cache-manifest"; + } + NOTREACHED(); + return "text/plain"; +} + +class BundledDataSource : public content::URLDataSource { + public: + explicit BundledDataSource() { + } + + // content::URLDataSource implementation. + virtual std::string GetSource() const OVERRIDE { + return kChromeUIDevToolsBundledHost; + } + + virtual void StartDataRequest(const std::string& path, + int render_process_id, + int render_view_id, + const GotDataCallback& callback) OVERRIDE { + std::string filename = PathWithoutParams(path); + + int resource_id = + content::DevToolsHttpHandler::GetFrontendResourceId(filename); + + DLOG_IF(WARNING, -1 == resource_id) << "Unable to find dev tool resource: " + << filename << ". If you compiled with debug_devtools=1, try running" + " with --debug-devtools."; + const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + scoped_refptr bytes(rb.LoadDataResourceBytes( + resource_id)); + callback.Run(bytes); + } + + virtual std::string GetMimeType(const std::string& path) const OVERRIDE { + return GetMimeTypeForPath(path); + } + + virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE { + return false; + } + + private: + virtual ~BundledDataSource() {} + DISALLOW_COPY_AND_ASSIGN(BundledDataSource); +}; + +} + +DevToolsUI::DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui) + : WebUIController(web_ui), + browser_context_(browser_context) { + web_ui->SetBindings(0); + content::URLDataSource::Add(browser_context_, new BundledDataSource()); +} + +} diff --git a/brightray/browser/devtools_ui.h b/brightray/browser/devtools_ui.h new file mode 100644 index 000000000000..6ac205ab5488 --- /dev/null +++ b/brightray/browser/devtools_ui.h @@ -0,0 +1,28 @@ +// Copyright (c) 2011 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_BROWSER_DEVTOOLS_UI_H_ +#define BRIGHTRAY_BROWSER_DEVTOOLS_UI_H_ + +#include "base/compiler_specific.h" +#include "content/public/browser/web_ui_controller.h" + +namespace brightray { + +class BrowserContext; + +class DevToolsUI : public content::WebUIController { + public: + explicit DevToolsUI(BrowserContext* browser_context, content::WebUI* web_ui); + + private: + // Weak reference to the browser context. + BrowserContext* browser_context_; + + DISALLOW_COPY_AND_ASSIGN(DevToolsUI); +}; + +} + +#endif diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index 935026df3198..f143768ec348 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -25,8 +25,13 @@ namespace brightray { namespace { +const char kChromeUIDevToolsURL[] = "chrome-devtools://devtools/devtools.html"; const char kDockSidePref[] = "brightray.devtools.dockside"; +GURL GetDevToolsURL() { + return GURL(kChromeUIDevToolsURL); +} + } // Implemented separately on each platform. @@ -70,9 +75,7 @@ void InspectableWebContentsImpl::ShowDevTools() { agent_host_ = content::DevToolsAgentHost::GetOrCreateFor(web_contents_->GetRenderViewHost()); frontend_host_.reset(content::DevToolsClientHost::CreateDevToolsFrontendHost(devtools_web_contents_.get(), this)); - auto handler = BrowserClient::Get()->browser_main_parts()->devtools_http_handler(); - auto url = handler->GetFrontendURL(nullptr); - devtools_web_contents_->GetController().LoadURL(url, content::Referrer(), content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string()); + devtools_web_contents_->GetController().LoadURL(GetDevToolsURL(), content::Referrer(), content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string()); } view_->SetDockSide(dock_side_); diff --git a/brightray/browser/web_ui_controller_factory.cc b/brightray/browser/web_ui_controller_factory.cc new file mode 100644 index 000000000000..61f6bf993c5a --- /dev/null +++ b/brightray/browser/web_ui_controller_factory.cc @@ -0,0 +1,59 @@ +// Copyright (c) 2013 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/web_ui_controller_factory.h" + +#include "browser/browser_context.h" +#include "browser/devtools_ui.h" + +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_ui.h" +#include "content/public/common/url_constants.h" + +namespace brightray { + +namespace { + +const char kChromeUIDevToolsBundledHost[] = "devtools"; + +} + +WebUIControllerFactory::WebUIControllerFactory(BrowserContext* browser_context) + : browser_context_(browser_context) { + DCHECK(browser_context_); +} + +WebUIControllerFactory::~WebUIControllerFactory() { +} + +content::WebUI::TypeID WebUIControllerFactory::GetWebUIType( + content::BrowserContext* browser_context, const GURL& url) const { + if (url.host() == kChromeUIDevToolsBundledHost) { + return const_cast(this); + } + + return content::WebUI::kNoWebUI; +} + +bool WebUIControllerFactory::UseWebUIForURL( + content::BrowserContext* browser_context, const GURL& url) const { + return GetWebUIType(browser_context, url) != content::WebUI::kNoWebUI; +} + +bool WebUIControllerFactory::UseWebUIBindingsForURL( + content::BrowserContext* browser_context, const GURL& url) const { + return UseWebUIForURL(browser_context, url); +} + +content::WebUIController* WebUIControllerFactory::CreateWebUIControllerForURL( + content::WebUI* web_ui, const GURL& url) const { + DCHECK(browser_context_); + + if (url.host() == kChromeUIDevToolsBundledHost) + return new DevToolsUI(browser_context_, web_ui); + + return NULL; +} + +} diff --git a/brightray/browser/web_ui_controller_factory.h b/brightray/browser/web_ui_controller_factory.h new file mode 100644 index 000000000000..1ee3f9b2dfd0 --- /dev/null +++ b/brightray/browser/web_ui_controller_factory.h @@ -0,0 +1,42 @@ +// Copyright (c) 2013 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_BROWSER_WEB_UI_CONTROLLER_FACTORY_H_ +#define BRIGHTRAY_BROWSER_WEB_UI_CONTROLLER_FACTORY_H_ + +#include "base/basictypes.h" +#include "content/public/browser/web_ui.h" +#include "content/public/browser/web_ui_controller_factory.h" + +namespace brightray { + +class BrowserContext; + +class WebUIControllerFactory : public content::WebUIControllerFactory { + public: + WebUIControllerFactory(BrowserContext* browser_context); + virtual ~WebUIControllerFactory(); + + virtual content::WebUI::TypeID GetWebUIType(content::BrowserContext* browser_context, + const GURL& url) const OVERRIDE; + virtual bool UseWebUIForURL(content::BrowserContext* browser_context, + const GURL& url) const OVERRIDE; + virtual bool UseWebUIBindingsForURL(content::BrowserContext* browser_context, + const GURL& url) const OVERRIDE; + virtual content::WebUIController* CreateWebUIControllerForURL( + content::WebUI* web_ui, + const GURL& url) const OVERRIDE; + + static WebUIControllerFactory* GetInstance(); + + private: + // Weak reference to the browser context. + BrowserContext* browser_context_; + + DISALLOW_COPY_AND_ASSIGN(WebUIControllerFactory); +}; + +} + +#endif