From 618040efc124b17ce8805bad89ee92a15c3819ca Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 09:52:20 +0800 Subject: [PATCH 1/7] Add DebugDevTools API. --- atom.gyp | 1 + browser/native_window.cc | 44 ++++++++++++++++++++++++++++++++++++++++ browser/native_window.h | 11 ++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/atom.gyp b/atom.gyp index a17155bb3760..1b470d337cd7 100644 --- a/atom.gyp +++ b/atom.gyp @@ -359,6 +359,7 @@ 'include_dirs': [ '.', 'vendor', + 'vendor/brightray', # Include directories for uv and node. 'vendor/node/src', 'vendor/node/deps/http_parser', diff --git a/browser/native_window.cc b/browser/native_window.cc index 7c47c3122b18..caa191b06c39 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -20,6 +20,9 @@ #include "browser/browser.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" @@ -37,6 +40,7 @@ #include "ui/gfx/point.h" #include "ui/gfx/rect.h" #include "ui/gfx/size.h" +#include "vendor/brightray/browser/inspectable_web_contents_impl.h" #include "webkit/common/user_agent/user_agent_util.h" using content::NavigationEntry; @@ -165,6 +169,8 @@ bool NativeWindow::HasModalDialog() { void NativeWindow::OpenDevTools() { inspectable_web_contents()->ShowDevTools(); + + DebugDevTools(); } void NativeWindow::CloseDevTools() { @@ -183,6 +189,36 @@ void NativeWindow::InspectElement(int x, int y) { agent->InspectElement(x, y); } +void NativeWindow::DebugDevTools() { + if (!IsDevToolsOpened()) + return; + + base::DictionaryValue options; + NativeWindow* window = NativeWindow::Create(&options); + + brightray::InspectableWebContentsImpl* inspectable_web_contents_impl = + static_cast( + inspectable_web_contents()); + content::WebContents* devtools_web_contents = + inspectable_web_contents_impl->devtools_web_contents(); + + content::DevToolsAgentHost* agent_host = + content::DevToolsAgentHost::GetOrCreateFor( + devtools_web_contents->GetRenderViewHost()); + content::DevToolsClientHost* frontend_host = + content::DevToolsClientHost::CreateDevToolsFrontendHost( + devtools_web_contents, window); + content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( + agent_host, frontend_host); + + window->InitFromOptions(&options); + window->GetWebContents()->GetController().LoadURL( + GURL("chrome-devtools://devtools/devtools.html"), + content::Referrer(), + content::PAGE_TRANSITION_AUTO_TOPLEVEL, + std::string()); +} + void NativeWindow::FocusOnWebView() { GetWebContents()->GetRenderViewHost()->Focus(); } @@ -447,6 +483,14 @@ void NativeWindow::Observe(int type, } } +void NativeWindow::DispatchOnEmbedder(const std::string& message) { +} + +void NativeWindow::InspectedContentsClosing() { + // We are acting as devtools debugger, safe to delete here. + delete this; +} + void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, bool succeed, const SkBitmap& bitmap) { diff --git a/browser/native_window.h b/browser/native_window.h index 0b82b969c723..2561285cf13c 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -12,6 +12,7 @@ #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" @@ -49,7 +50,8 @@ struct DraggableRegion; class NativeWindow : public brightray::DefaultWebContentsDelegate, public content::WebContentsObserver, - public content::NotificationObserver { + public content::NotificationObserver, + public content::DevToolsFrontendHostDelegate { public: typedef base::Callback& buffer)> CapturePageCallback; @@ -127,6 +129,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void CloseDevTools(); virtual bool IsDevToolsOpened(); virtual void InspectElement(int x, int y); + virtual void DebugDevTools(); virtual void FocusOnWebView(); virtual void BlurWebView(); @@ -210,11 +213,15 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) OVERRIDE; virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; - // Implementations of content::NotificationObserver + // Implementations of content::NotificationObserver. virtual void Observe(int type, 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_; From 1f57994e2ac349a64824b9b112d7a3272216f012 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 10:09:32 +0800 Subject: [PATCH 2/7] Setup devtools client for the correct contents. --- browser/native_window.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index caa191b06c39..ee2a2bc8207b 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -207,16 +207,16 @@ void NativeWindow::DebugDevTools() { devtools_web_contents->GetRenderViewHost()); content::DevToolsClientHost* frontend_host = content::DevToolsClientHost::CreateDevToolsFrontendHost( - devtools_web_contents, window); + window->GetWebContents(), window); content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( agent_host, frontend_host); window->InitFromOptions(&options); window->GetWebContents()->GetController().LoadURL( - GURL("chrome-devtools://devtools/devtools.html"), - content::Referrer(), - content::PAGE_TRANSITION_AUTO_TOPLEVEL, - std::string()); + GURL("chrome-devtools://devtools/devtools.html"), + content::Referrer(), + content::PAGE_TRANSITION_AUTO_TOPLEVEL, + std::string()); } void NativeWindow::FocusOnWebView() { @@ -487,8 +487,8 @@ void NativeWindow::DispatchOnEmbedder(const std::string& message) { } void NativeWindow::InspectedContentsClosing() { - // We are acting as devtools debugger, safe to delete here. - delete this; + // We are acting as devtools debugger, safe to close here. + CloseImmediately(); } void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, From f2bef6c26d00fce789934446cbcecd402be33864 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 11:28:21 +0800 Subject: [PATCH 3/7] Setup devtools frontend. --- browser/native_window.cc | 22 ++++++++++++++++------ browser/native_window.h | 7 +++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index ee2a2bc8207b..a9d887c13212 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -196,21 +196,23 @@ 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(); - content::DevToolsAgentHost* agent_host = - content::DevToolsAgentHost::GetOrCreateFor( - devtools_web_contents->GetRenderViewHost()); - content::DevToolsClientHost* frontend_host = + // Setup devtools. + window->devtools_agent_host_ = content::DevToolsAgentHost::GetOrCreateFor( + devtools_web_contents->GetRenderViewHost()); + window->devtools_client_host_.reset( content::DevToolsClientHost::CreateDevToolsFrontendHost( - window->GetWebContents(), window); + window->GetWebContents(), window)); content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor( - agent_host, frontend_host); + window->devtools_agent_host_.get(), window->devtools_client_host_.get()); + // Done. window->InitFromOptions(&options); window->GetWebContents()->GetController().LoadURL( GURL("chrome-devtools://devtools/devtools.html"), @@ -434,6 +436,14 @@ 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(), diff --git a/browser/native_window.h b/browser/native_window.h index 2561285cf13c..bf31049cd7f7 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -30,6 +30,8 @@ class InspectableWebContents; namespace content { class BrowserContext; +class DevToolsAgentHost; +class DevToolsClientHost; class WebContents; } @@ -208,6 +210,8 @@ 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; @@ -265,6 +269,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, scoped_ptr dialog_manager_; scoped_ptr inspectable_web_contents_; + scoped_refptr devtools_agent_host_; + scoped_ptr devtools_client_host_; + DISALLOW_COPY_AND_ASSIGN(NativeWindow); }; From 99c0de6a1afd5f7795be0f85974dbd38c8a98b9c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 11:48:11 +0800 Subject: [PATCH 4/7] Disable stack dumping on MAC. It would prevent the system crash reporter. --- app/atom_main_delegate.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/atom_main_delegate.cc b/app/atom_main_delegate.cc index 5a529b26fe89..e50ae1c1cb4e 100644 --- a/app/atom_main_delegate.cc +++ b/app/atom_main_delegate.cc @@ -42,7 +42,7 @@ bool AtomMainDelegate::BasicStartupComplete(int* exit_code) { logging::SetLogItems(true, false, true, false); // Enable convient stack printing. -#if defined(DEBUG) +#if defined(DEBUG) && !defined(OS_MACOSX) base::debug::EnableInProcessStackDumping(); #endif From 8b9d35d84e68f1e771ebbed0128955a57f2568ec Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 11:53:13 +0800 Subject: [PATCH 5/7] 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); }; From eaedac2536c8992db6254136b86fb0babd925be9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 12:08:33 +0800 Subject: [PATCH 6/7] Add the debugDevTools JS API. --- browser/api/atom_api_window.cc | 7 +++++++ browser/api/atom_api_window.h | 1 + browser/native_window.cc | 18 +++++++++--------- browser/native_window.h | 4 +++- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index d5b6296831a7..f502425749ed 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -414,6 +414,12 @@ void Window::InspectElement(const v8::FunctionCallbackInfo& args) { self->window_->InspectElement(x, y); } +// static +void Window::DebugDevTools(const v8::FunctionCallbackInfo& args) { + UNWRAP_WINDOW_AND_CHECK; + self->window_->DebugDevTools(); +} + // static void Window::FocusOnWebView(const v8::FunctionCallbackInfo& args) { UNWRAP_WINDOW_AND_CHECK; @@ -663,6 +669,7 @@ void Window::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools", CloseDevTools); NODE_SET_PROTOTYPE_METHOD(t, "isDevToolsOpened", IsDevToolsOpened); NODE_SET_PROTOTYPE_METHOD(t, "inspectElement", InspectElement); + NODE_SET_PROTOTYPE_METHOD(t, "debugDevTools", DebugDevTools); NODE_SET_PROTOTYPE_METHOD(t, "focusOnWebView", FocusOnWebView); NODE_SET_PROTOTYPE_METHOD(t, "blurWebView", BlurWebView); NODE_SET_PROTOTYPE_METHOD(t, "isWebViewFocused", IsWebViewFocused); diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index 1526e4802a09..d53affaa7fce 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -86,6 +86,7 @@ class Window : public EventEmitter, static void CloseDevTools(const v8::FunctionCallbackInfo& args); static void IsDevToolsOpened(const v8::FunctionCallbackInfo& args); static void InspectElement(const v8::FunctionCallbackInfo& args); + static void DebugDevTools(const v8::FunctionCallbackInfo& args); static void FocusOnWebView(const v8::FunctionCallbackInfo& args); static void BlurWebView(const v8::FunctionCallbackInfo& args); static void IsWebViewFocused(const v8::FunctionCallbackInfo& args); diff --git a/browser/native_window.cc b/browser/native_window.cc index 603c9db1dd03..3afdea54586c 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -167,8 +167,6 @@ bool NativeWindow::HasModalDialog() { void NativeWindow::OpenDevTools() { inspectable_web_contents()->ShowDevTools(); - - DebugDevTools(); } void NativeWindow::CloseDevTools() { @@ -187,14 +185,16 @@ void NativeWindow::InspectElement(int x, int y) { agent->InspectElement(x, y); } -void NativeWindow::DebugDevTools() { - if (!IsDevToolsOpened()) - return; +scoped_ptr NativeWindow::DebugDevTools() { + scoped_ptr window; + if (IsDevToolsOpened()) { + base::DictionaryValue options; + window.reset(NativeWindow::Create(&options)); + window->devtools_delegate_.reset(new DevToolsDelegate( + window.get(), GetDevToolsWebContents())); + } - base::DictionaryValue options; - NativeWindow* window = NativeWindow::Create(&options); - window->devtools_delegate_.reset(new DevToolsDelegate( - window, GetDevToolsWebContents())); + return window.Pass(); } void NativeWindow::FocusOnWebView() { diff --git a/browser/native_window.h b/browser/native_window.h index 14268ccbd8d9..d17f41df846b 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -128,7 +128,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void CloseDevTools(); virtual bool IsDevToolsOpened(); virtual void InspectElement(int x, int y); - virtual void DebugDevTools(); + + // Creates a new window to debug the devtools. + virtual scoped_ptr DebugDevTools(); virtual void FocusOnWebView(); virtual void BlurWebView(); From f65f95e95c9afaa4b5fe62c2a3885e5caed98347 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 24 Feb 2014 12:17:10 +0800 Subject: [PATCH 7/7] The DevTools window should manage lifetime itself. --- browser/devtools_delegate.cc | 6 +++++- browser/devtools_delegate.h | 7 ++++++- browser/native_window.cc | 16 +++++++--------- browser/native_window.h | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/browser/devtools_delegate.cc b/browser/devtools_delegate.cc index 77b8923c519b..dc11dd35e12c 100644 --- a/browser/devtools_delegate.cc +++ b/browser/devtools_delegate.cc @@ -47,7 +47,7 @@ void DevToolsDelegate::DispatchOnEmbedder(const std::string& message) { } void DevToolsDelegate::InspectedContentsClosing() { - owner_window_->CloseImmediately(); + delete owner_window_; } void DevToolsDelegate::AboutToNavigateRenderView( @@ -56,4 +56,8 @@ void DevToolsDelegate::AboutToNavigateRenderView( owner_window_->GetWebContents()->GetRenderViewHost()); } +void DevToolsDelegate::OnWindowClosed() { + delete owner_window_; +} + } // namespace atom diff --git a/browser/devtools_delegate.h b/browser/devtools_delegate.h index d281e3b62a7e..d8a8be529748 100644 --- a/browser/devtools_delegate.h +++ b/browser/devtools_delegate.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_DEVTOOLS_DELEGATE_H_ #include "base/memory/scoped_ptr.h" +#include "browser/native_window_observer.h" #include "content/public/browser/devtools_frontend_host_delegate.h" #include "content/public/browser/web_contents_observer.h" @@ -19,7 +20,8 @@ namespace atom { class NativeWindow; class DevToolsDelegate : public content::DevToolsFrontendHostDelegate, - public content::WebContentsObserver { + public content::WebContentsObserver, + public NativeWindowObserver { public: DevToolsDelegate(NativeWindow* window, content::WebContents* target_web_contents); @@ -34,6 +36,9 @@ class DevToolsDelegate : public content::DevToolsFrontendHostDelegate, virtual void AboutToNavigateRenderView( content::RenderViewHost* render_view_host) OVERRIDE; + // Implementations of NativeWindowObserver. + virtual void OnWindowClosed() OVERRIDE; + private: NativeWindow* owner_window_; diff --git a/browser/native_window.cc b/browser/native_window.cc index 3afdea54586c..c54e32d57317 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -185,16 +185,14 @@ void NativeWindow::InspectElement(int x, int y) { agent->InspectElement(x, y); } -scoped_ptr NativeWindow::DebugDevTools() { - scoped_ptr window; - if (IsDevToolsOpened()) { - base::DictionaryValue options; - window.reset(NativeWindow::Create(&options)); - window->devtools_delegate_.reset(new DevToolsDelegate( - window.get(), GetDevToolsWebContents())); - } +void NativeWindow::DebugDevTools() { + if (!IsDevToolsOpened()) + return; - return window.Pass(); + base::DictionaryValue options; + NativeWindow* window = NativeWindow::Create(&options); + window->devtools_delegate_.reset(new DevToolsDelegate( + window, GetDevToolsWebContents())); } void NativeWindow::FocusOnWebView() { diff --git a/browser/native_window.h b/browser/native_window.h index d17f41df846b..b1eb6e695b46 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -130,7 +130,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void InspectElement(int x, int y); // Creates a new window to debug the devtools. - virtual scoped_ptr DebugDevTools(); + virtual void DebugDevTools(); virtual void FocusOnWebView(); virtual void BlurWebView();