From 8b9d35d84e68f1e771ebbed0128955a57f2568ec Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 11:53:13 +0800 Subject: [PATCH] Separate devtools code out. --- atom.gyp | 2 ++ browser/devtools_delegate.cc | 59 ++++++++++++++++++++++++++++++++++++ browser/devtools_delegate.h | 48 +++++++++++++++++++++++++++++ browser/native_window.cc | 53 ++++++-------------------------- browser/native_window.h | 18 +++-------- 5 files changed, 123 insertions(+), 57 deletions(-) create mode 100644 browser/devtools_delegate.cc create mode 100644 browser/devtools_delegate.h diff --git a/atom.gyp b/atom.gyp index 1b470d337cd7..a419ae94278a 100644 --- a/atom.gyp +++ b/atom.gyp @@ -90,6 +90,8 @@ 'browser/browser_mac.mm', 'browser/browser_win.cc', 'browser/browser_observer.h', + 'browser/devtools_delegate.cc', + 'browser/devtools_delegate.h', 'browser/native_window.cc', 'browser/native_window.h', 'browser/native_window_gtk.cc', diff --git a/browser/devtools_delegate.cc b/browser/devtools_delegate.cc new file mode 100644 index 000000000000..77b8923c519b --- /dev/null +++ b/browser/devtools_delegate.cc @@ -0,0 +1,59 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "browser/devtools_delegate.h" + +#include "base/values.h" +#include "browser/native_window.h" +#include "content/public/browser/devtools_agent_host.h" +#include "content/public/browser/devtools_client_host.h" +#include "content/public/browser/devtools_http_handler.h" +#include "content/public/browser/devtools_manager.h" +#include "content/public/browser/web_contents.h" + +namespace atom { + +DevToolsDelegate::DevToolsDelegate(NativeWindow* window, + content::WebContents* target_web_contents) + : content::WebContentsObserver(window->GetWebContents()), + owner_window_(window) { + content::WebContents* web_contents = window->GetWebContents(); + + // Setup devtools. + devtools_agent_host_ = content::DevToolsAgentHost::GetOrCreateFor( + target_web_contents->GetRenderViewHost()); + devtools_client_host_.reset( + content::DevToolsClientHost::CreateDevToolsFrontendHost(web_contents, + this)); + content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( + devtools_agent_host_.get(), devtools_client_host_.get()); + + // Go! + base::DictionaryValue options; + options.SetString("title", "DevTools Debugger"); + window->InitFromOptions(&options); + web_contents->GetController().LoadURL( + GURL("chrome-devtools://devtools/devtools.html"), + content::Referrer(), + content::PAGE_TRANSITION_AUTO_TOPLEVEL, + std::string()); +} + +DevToolsDelegate::~DevToolsDelegate() { +} + +void DevToolsDelegate::DispatchOnEmbedder(const std::string& message) { +} + +void DevToolsDelegate::InspectedContentsClosing() { + owner_window_->CloseImmediately(); +} + +void DevToolsDelegate::AboutToNavigateRenderView( + content::RenderViewHost* render_view_host) { + content::DevToolsClientHost::SetupDevToolsFrontendClient( + owner_window_->GetWebContents()->GetRenderViewHost()); +} + +} // namespace atom diff --git a/browser/devtools_delegate.h b/browser/devtools_delegate.h new file mode 100644 index 000000000000..d281e3b62a7e --- /dev/null +++ b/browser/devtools_delegate.h @@ -0,0 +1,48 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_DEVTOOLS_DELEGATE_H_ +#define ATOM_BROWSER_DEVTOOLS_DELEGATE_H_ + +#include "base/memory/scoped_ptr.h" +#include "content/public/browser/devtools_frontend_host_delegate.h" +#include "content/public/browser/web_contents_observer.h" + +namespace content { +class DevToolsAgentHost; +class DevToolsClientHost; +} + +namespace atom { + +class NativeWindow; + +class DevToolsDelegate : public content::DevToolsFrontendHostDelegate, + public content::WebContentsObserver { + public: + DevToolsDelegate(NativeWindow* window, + content::WebContents* target_web_contents); + virtual ~DevToolsDelegate(); + + protected: + // Implementations of content::DevToolsFrontendHostDelegate. + virtual void DispatchOnEmbedder(const std::string& message) OVERRIDE; + virtual void InspectedContentsClosing() OVERRIDE; + + // Implementations of content::WebContentsObserver. + virtual void AboutToNavigateRenderView( + content::RenderViewHost* render_view_host) OVERRIDE; + + private: + NativeWindow* owner_window_; + + scoped_refptr devtools_agent_host_; + scoped_ptr devtools_client_host_; + + DISALLOW_COPY_AND_ASSIGN(DevToolsDelegate); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_DEVTOOLS_DELEGATE_H_ diff --git a/browser/native_window.cc b/browser/native_window.cc index a9d887c13212..603c9db1dd03 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -18,11 +18,9 @@ #include "browser/atom_browser_main_parts.h" #include "browser/atom_javascript_dialog_manager.h" #include "browser/browser.h" +#include "browser/devtools_delegate.h" #include "browser/window_list.h" #include "content/public/browser/devtools_agent_host.h" -#include "content/public/browser/devtools_client_host.h" -#include "content/public/browser/devtools_http_handler.h" -#include "content/public/browser/devtools_manager.h" #include "content/public/browser/invalidate_type.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/notification_details.h" @@ -195,30 +193,8 @@ void NativeWindow::DebugDevTools() { base::DictionaryValue options; NativeWindow* window = NativeWindow::Create(&options); - - // Receive devtool's web contents. - brightray::InspectableWebContentsImpl* inspectable_web_contents_impl = - static_cast( - inspectable_web_contents()); - content::WebContents* devtools_web_contents = - inspectable_web_contents_impl->devtools_web_contents(); - - // Setup devtools. - window->devtools_agent_host_ = content::DevToolsAgentHost::GetOrCreateFor( - devtools_web_contents->GetRenderViewHost()); - window->devtools_client_host_.reset( - content::DevToolsClientHost::CreateDevToolsFrontendHost( - window->GetWebContents(), window)); - content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( - window->devtools_agent_host_.get(), window->devtools_client_host_.get()); - - // Done. - window->InitFromOptions(&options); - window->GetWebContents()->GetController().LoadURL( - GURL("chrome-devtools://devtools/devtools.html"), - content::Referrer(), - content::PAGE_TRANSITION_AUTO_TOPLEVEL, - std::string()); + window->devtools_delegate_.reset(new DevToolsDelegate( + window, GetDevToolsWebContents())); } void NativeWindow::FocusOnWebView() { @@ -307,6 +283,13 @@ content::WebContents* NativeWindow::GetWebContents() const { return inspectable_web_contents_->GetWebContents(); } +content::WebContents* NativeWindow::GetDevToolsWebContents() const { + brightray::InspectableWebContentsImpl* inspectable_web_contents_impl = + static_cast( + inspectable_web_contents()); + return inspectable_web_contents_impl->devtools_web_contents(); +} + void NativeWindow::NotifyWindowClosed() { if (is_closed_) return; @@ -436,14 +419,6 @@ void NativeWindow::RendererResponsive(content::WebContents* source) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive()); } -void NativeWindow::AboutToNavigateRenderView( - content::RenderViewHost* render_view_host) { - // Setup devtools frontend if we are devtools window. - if (devtools_client_host_) - content::DevToolsClientHost::SetupDevToolsFrontendClient( - GetWebContents()->GetRenderViewHost()); -} - void NativeWindow::RenderViewDeleted(content::RenderViewHost* rvh) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRenderViewDeleted(rvh->GetProcess()->GetID(), @@ -493,14 +468,6 @@ void NativeWindow::Observe(int type, } } -void NativeWindow::DispatchOnEmbedder(const std::string& message) { -} - -void NativeWindow::InspectedContentsClosing() { - // We are acting as devtools debugger, safe to close here. - CloseImmediately(); -} - void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, bool succeed, const SkBitmap& bitmap) { diff --git a/browser/native_window.h b/browser/native_window.h index bf31049cd7f7..14268ccbd8d9 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -12,7 +12,6 @@ #include "base/memory/weak_ptr.h" #include "base/observer_list.h" #include "browser/native_window_observer.h" -#include "content/public/browser/devtools_frontend_host_delegate.h" #include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/web_contents_observer.h" @@ -30,8 +29,6 @@ class InspectableWebContents; namespace content { class BrowserContext; -class DevToolsAgentHost; -class DevToolsClientHost; class WebContents; } @@ -48,12 +45,12 @@ class Message; namespace atom { class AtomJavaScriptDialogManager; +class DevToolsDelegate; struct DraggableRegion; class NativeWindow : public brightray::DefaultWebContentsDelegate, public content::WebContentsObserver, - public content::NotificationObserver, - public content::DevToolsFrontendHostDelegate { + public content::NotificationObserver { public: typedef base::Callback& buffer)> CapturePageCallback; @@ -154,6 +151,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void CloseWebContents(); content::WebContents* GetWebContents() const; + content::WebContents* GetDevToolsWebContents() const; void AddObserver(NativeWindowObserver* obs) { observers_.AddObserver(obs); @@ -210,8 +208,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void RendererResponsive(content::WebContents* source) OVERRIDE; // Implementations of content::WebContentsObserver. - virtual void AboutToNavigateRenderView( - content::RenderViewHost* render_view_host) OVERRIDE; virtual void RenderViewDeleted(content::RenderViewHost*) OVERRIDE; virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) OVERRIDE; @@ -222,10 +218,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; - // Implementations of content::DevToolsFrontendHostDelegate. - virtual void DispatchOnEmbedder(const std::string& message) OVERRIDE; - virtual void InspectedContentsClosing() OVERRIDE; - // Whether window has standard frame. bool has_frame_; @@ -266,12 +258,10 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, base::WeakPtrFactory weak_factory_; + scoped_ptr devtools_delegate_; scoped_ptr dialog_manager_; scoped_ptr inspectable_web_contents_; - scoped_refptr devtools_agent_host_; - scoped_ptr devtools_client_host_; - DISALLOW_COPY_AND_ASSIGN(NativeWindow); };