Browse Source

Merge pull request #178 from atom/debug-devtools

Add BrowserWindow.debugDevTools() API
Cheng Zhao 11 years ago
parent
commit
a2ecb554cc

+ 1 - 1
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
 

+ 3 - 0
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',

+ 7 - 0
browser/api/atom_api_window.cc

@@ -414,6 +414,12 @@ void Window::InspectElement(const v8::FunctionCallbackInfo<v8::Value>& args) {
   self->window_->InspectElement(x, y);
 }
 
+// static
+void Window::DebugDevTools(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  UNWRAP_WINDOW_AND_CHECK;
+  self->window_->DebugDevTools();
+}
+
 // static
 void Window::FocusOnWebView(const v8::FunctionCallbackInfo<v8::Value>& args) {
   UNWRAP_WINDOW_AND_CHECK;
@@ -663,6 +669,7 @@ void Window::Initialize(v8::Handle<v8::Object> 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);

+ 1 - 0
browser/api/atom_api_window.h

@@ -86,6 +86,7 @@ class Window : public EventEmitter,
   static void CloseDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void IsDevToolsOpened(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void InspectElement(const v8::FunctionCallbackInfo<v8::Value>& args);
+  static void DebugDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void FocusOnWebView(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void BlurWebView(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void IsWebViewFocused(const v8::FunctionCallbackInfo<v8::Value>& args);

+ 63 - 0
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

+ 53 - 0
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<content::DevToolsAgentHost> devtools_agent_host_;
+  scoped_ptr<content::DevToolsClientHost> devtools_client_host_;
+
+  DISALLOW_COPY_AND_ASSIGN(DevToolsDelegate);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_DEVTOOLS_DELEGATE_H_

+ 19 - 0
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<brightray::InspectableWebContentsImpl*>(
+          inspectable_web_contents());
+  return inspectable_web_contents_impl->devtools_web_contents();
+}
+
 void NativeWindow::NotifyWindowClosed() {
   if (is_closed_)
     return;

+ 7 - 1
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<NativeWindow> weak_factory_;
 
+  scoped_ptr<DevToolsDelegate> devtools_delegate_;
   scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;