diff --git a/app/atom_main_delegate.cc b/app/atom_main_delegate.cc index 5a529b26fe..e50ae1c1cb 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 diff --git a/atom.gyp b/atom.gyp index a17155bb37..a419ae9427 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', @@ -359,6 +361,7 @@ 'include_dirs': [ '.', 'vendor', + 'vendor/brightray', # Include directories for uv and node. 'vendor/node/src', 'vendor/node/deps/http_parser', diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index d5b6296831..f502425749 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 1526e4802a..d53affaa7f 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/devtools_delegate.cc b/browser/devtools_delegate.cc new file mode 100644 index 0000000000..dc11dd35e1 --- /dev/null +++ b/browser/devtools_delegate.cc @@ -0,0 +1,63 @@ +// 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() { + delete owner_window_; +} + +void DevToolsDelegate::AboutToNavigateRenderView( + content::RenderViewHost* render_view_host) { + content::DevToolsClientHost::SetupDevToolsFrontendClient( + owner_window_->GetWebContents()->GetRenderViewHost()); +} + +void DevToolsDelegate::OnWindowClosed() { + delete owner_window_; +} + +} // namespace atom diff --git a/browser/devtools_delegate.h b/browser/devtools_delegate.h new file mode 100644 index 0000000000..d8a8be5297 --- /dev/null +++ b/browser/devtools_delegate.h @@ -0,0 +1,53 @@ +// 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 "browser/native_window_observer.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 NativeWindowObserver { + 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; + + // Implementations of NativeWindowObserver. + virtual void OnWindowClosed() 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 7c47c3122b..c54e32d573 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -18,6 +18,7 @@ #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/invalidate_type.h" @@ -37,6 +38,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; @@ -183,6 +185,16 @@ 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); + window->devtools_delegate_.reset(new DevToolsDelegate( + window, GetDevToolsWebContents())); +} + void NativeWindow::FocusOnWebView() { GetWebContents()->GetRenderViewHost()->Focus(); } @@ -269,6 +281,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; diff --git a/browser/native_window.h b/browser/native_window.h index 0b82b969c7..b1eb6e695b 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -45,6 +45,7 @@ class Message; namespace atom { class AtomJavaScriptDialogManager; +class DevToolsDelegate; struct DraggableRegion; class NativeWindow : public brightray::DefaultWebContentsDelegate, @@ -128,6 +129,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual bool IsDevToolsOpened(); virtual void InspectElement(int x, int y); + // Creates a new window to debug the devtools. + virtual void DebugDevTools(); + virtual void FocusOnWebView(); virtual void BlurWebView(); virtual bool IsWebViewFocused(); @@ -149,6 +153,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,7 +215,7 @@ 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; @@ -255,6 +260,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, base::WeakPtrFactory weak_factory_; + scoped_ptr devtools_delegate_; scoped_ptr dialog_manager_; scoped_ptr inspectable_web_contents_;