]> khamidou — GitHub mirrors - electron.git/commitdiff
Move devtools API to WebContents
authorCheng Zhao <zcbenz@gmail.com>
Thu, 1 Oct 2015 03:14:19 +0000 (11:14 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 1 Oct 2015 03:25:31 +0000 (11:25 +0800)
12 files changed:
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/api/lib/browser-window.coffee
atom/browser/common_web_contents_delegate.cc
atom/browser/common_web_contents_delegate.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_observer.h
atom/browser/native_window_views.cc
atom/browser/native_window_views.h

index bdeb4ed4f873b94adef22da0a37caeef926dd645..90c5a5146c3b541d332b9163212d010a482f1b70 100644 (file)
@@ -26,6 +26,7 @@
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
+#include "brightray/browser/inspectable_web_contents_view.h"
 #include "chrome/browser/printing/print_view_manager_basic.h"
 #include "chrome/browser/printing/print_preview_message_handler.h"
 #include "content/common/view_messages.h"
@@ -228,6 +229,8 @@ WebContents::WebContents(v8::Isolate* isolate,
   AttachAsUserData(web_contents);
   InitWithWebContents(web_contents);
 
+  managed_web_contents()->GetView()->SetDelegate(this);
+
   // Save the preferences in C++.
   base::DictionaryValue web_preferences;
   mate::ConvertFromV8(isolate, options.GetHandle(), &web_preferences);
@@ -491,6 +494,28 @@ void WebContents::DidUpdateFaviconURL(
   Emit("page-favicon-updated", unique_urls);
 }
 
+void WebContents::DevToolsFocused() {
+  Emit("devtools-focused");
+}
+
+void WebContents::DevToolsOpened() {
+  v8::Locker locker(isolate());
+  v8::HandleScope handle_scope(isolate());
+  auto handle = WebContents::CreateFrom(
+      isolate(), managed_web_contents()->GetDevToolsWebContents());
+  devtools_web_contents_.Reset(isolate(), handle.ToV8());
+
+  Emit("devtools-opened");
+}
+
+void WebContents::DevToolsClosed() {
+  v8::Locker locker(isolate());
+  v8::HandleScope handle_scope(isolate());
+  devtools_web_contents_.Reset();
+
+  Emit("devtools-closed");
+}
+
 bool WebContents::OnMessageReceived(const IPC::Message& message) {
   bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(WebContents, message)
@@ -698,10 +723,6 @@ void WebContents::InspectServiceWorker() {
   }
 }
 
-v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
-  return v8::Local<v8::Value>::New(isolate, session_);
-}
-
 void WebContents::HasServiceWorker(
     const base::Callback<void(bool)>& callback) {
   auto context = GetServiceWorkerContext(web_contents());
@@ -893,6 +914,17 @@ v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
   return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
 }
 
+v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
+  return v8::Local<v8::Value>::New(isolate, session_);
+}
+
+v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
+  if (devtools_web_contents_.IsEmpty())
+    return v8::Null(isolate);
+  else
+    return v8::Local<v8::Value>::New(isolate, devtools_web_contents_);
+}
+
 mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
   if (template_.IsEmpty())
@@ -957,7 +989,9 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
         .SetMethod("_printToPDF", &WebContents::PrintToPDF)
         .SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
         .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
-        .SetProperty("session", &WebContents::Session)
+        .SetProperty("session", &WebContents::Session, true)
+        .SetProperty("devToolsWebContents",
+                     &WebContents::DevToolsWebContents, true)
         .Build());
 
   return mate::ObjectTemplateBuilder(
index 91750ac6136c198f4791e0b4431ad1d51303531b..fa32d812e684bdd107cab111eb0f3bb1746f4bdc 100644 (file)
@@ -83,7 +83,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void DisableDeviceEmulation();
   void InspectElement(int x, int y);
   void InspectServiceWorker();
-  v8::Local<v8::Value> Session(v8::Isolate* isolate);
   void HasServiceWorker(const base::Callback<void(bool)>&);
   void UnregisterServiceWorker(const base::Callback<void(bool)>&);
   void SetAudioMuted(bool muted);
@@ -135,6 +134,10 @@ class WebContents : public mate::TrackableObject<WebContents>,
   // Returns the web preferences of current WebContents.
   v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
 
+  // Properties.
+  v8::Local<v8::Value> Session(v8::Isolate* isolate);
+  v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
+
  protected:
   explicit WebContents(content::WebContents* web_contents);
   WebContents(v8::Isolate* isolate, const mate::Dictionary& options);
@@ -218,6 +221,11 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void PluginCrashed(const base::FilePath& plugin_path,
                      base::ProcessId plugin_pid) override;
 
+  // brightray::InspectableWebContentsViewDelegate:
+  void DevToolsFocused() override;
+  void DevToolsOpened() override;
+  void DevToolsClosed() override;
+
  private:
   enum Type {
     BROWSER_WINDOW,  // Used by BrowserWindow.
@@ -237,6 +245,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
                              IPC::Message* message);
 
   v8::Global<v8::Value> session_;
+  v8::Global<v8::Value> devtools_web_contents_;
 
   scoped_ptr<WebViewGuestDelegate> guest_delegate_;
 
index 4d866d18503c788b1abaef289e81260243d6d6a8..7536c708768a345b3e398d5d8145c159a87d8f01 100644 (file)
@@ -184,28 +184,6 @@ void Window::OnRendererResponsive() {
   Emit("responsive");
 }
 
-void Window::OnDevToolsFocus() {
-  Emit("devtools-focused");
-}
-
-void Window::OnDevToolsOpened() {
-  v8::Locker locker(isolate());
-  v8::HandleScope handle_scope(isolate());
-  auto handle = WebContents::CreateFrom(
-      isolate(), api_web_contents_->GetDevToolsWebContents());
-  devtools_web_contents_.Reset(isolate(), handle.ToV8());
-
-  Emit("devtools-opened");
-}
-
-void Window::OnDevToolsClosed() {
-  v8::Locker locker(isolate());
-  v8::HandleScope handle_scope(isolate());
-  devtools_web_contents_.Reset();
-
-  Emit("devtools-closed");
-}
-
 void Window::OnExecuteWindowsCommand(const std::string& command_name) {
   Emit("app-command", command_name);
 }
@@ -540,13 +518,6 @@ v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) {
     return v8::Local<v8::Value>::New(isolate, web_contents_);
 }
 
-v8::Local<v8::Value> Window::DevToolsWebContents(v8::Isolate* isolate) {
-  if (devtools_web_contents_.IsEmpty())
-    return v8::Null(isolate);
-  else
-    return v8::Local<v8::Value>::New(isolate, devtools_web_contents_);
-}
-
 // static
 void Window::BuildPrototype(v8::Isolate* isolate,
                             v8::Local<v8::ObjectTemplate> prototype) {
@@ -618,8 +589,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
                  &Window::ShowDefinitionForSelection)
 #endif
       .SetProperty("id", &Window::ID, true)
-      .SetProperty("webContents", &Window::WebContents, true)
-      .SetProperty("devToolsWebContents", &Window::DevToolsWebContents, true);
+      .SetProperty("webContents", &Window::WebContents, true);
 }
 
 }  // namespace api
index d60bf0d87ea0360684eecdf5116750c401745101..8cf26fe58e8a7f9aaa11378fb0d9beaba8a361d2 100644 (file)
@@ -69,9 +69,6 @@ class Window : public mate::TrackableObject<Window>,
   void OnWindowLeaveHtmlFullScreen() override;
   void OnRendererUnresponsive() override;
   void OnRendererResponsive() override;
-  void OnDevToolsFocus() override;
-  void OnDevToolsOpened() override;
-  void OnDevToolsClosed() override;
   void OnExecuteWindowsCommand(const std::string& command_name) override;
 
   // mate::Wrappable:
@@ -150,10 +147,8 @@ class Window : public mate::TrackableObject<Window>,
 
   int32_t ID() const;
   v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
-  v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
 
   v8::Global<v8::Value> web_contents_;
-  v8::Global<v8::Value> devtools_web_contents_;
   v8::Global<v8::Value> menu_;
 
   api::WebContents* api_web_contents_;
index 2a92cfc55f8808b35df523944cba76b65e725dce..6ffba50d34cb7fc2aea897ed1f433e132d70dc81 100644 (file)
@@ -48,6 +48,15 @@ BrowserWindow::_init = ->
   # Notify the creation of the window.
   app.emit 'browser-window-created', {}, this
 
+  # Be compatible with old APIs.
+  @webContents.on 'devtools-focused', => @emit 'devtools-focused'
+  @webContents.on 'devtools-opened', => @emit 'devtools-opened'
+  @webContents.on 'devtools-closed', => @emit 'devtools-closed'
+  Object.defineProperty this, 'devToolsWebContents',
+    enumerable: true,
+    configurable: false,
+    get: -> @webContents.devToolsWebContents
+
 BrowserWindow.getFocusedWindow = ->
   windows = BrowserWindow.getAllWindows()
   return window for window in windows when window.isFocused()
index 3cef7c6d68cca365ed5f4ab50307eb06a56e2fc6..518d4f650a63dfff366a2f4ca7299f3c166e2731 100644 (file)
 #include "content/public/browser/render_view_host.h"
 #include "storage/browser/fileapi/isolated_context.h"
 
+#if defined(TOOLKIT_VIEWS)
+#include "atom/browser/native_window_views.h"
+#endif
+
+#if defined(USE_X11)
+#include "atom/browser/browser.h"
+#endif
+
 using content::BrowserThread;
 
 namespace atom {
@@ -355,6 +363,23 @@ void CommonWebContentsDelegate::OnDevToolsAppendToFile(
       "DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr);
 }
 
+#if defined(TOOLKIT_VIEWS)
+gfx::ImageSkia CommonWebContentsDelegate::GetDevToolsWindowIcon() {
+  if (!owner_window())
+    return gfx::ImageSkia();
+  return static_cast<views::WidgetDelegate*>(static_cast<NativeWindowViews*>(
+      owner_window()))->GetWindowAppIcon();
+}
+#endif
+
+#if defined(USE_X11)
+void CommonWebContentsDelegate::GetDevToolsWindowWMClass(
+    std::string* name, std::string* class_name) {
+  *class_name = Browser::Get()->GetName();
+  *name = base::StringToLowerASCII(*class_name);
+}
+#endif
+
 void CommonWebContentsDelegate::SetHtmlApiFullscreen(bool enter_fullscreen) {
   // Window is already in fullscreen mode, save the state.
   if (enter_fullscreen && owner_window_->IsFullscreen()) {
index 495b5501a0d1cebaff926eb313a149d739699310..3a5be180dcc11a5ca9dcb3bca6d87adcf7dc8dc0 100644 (file)
@@ -12,6 +12,7 @@
 #include "brightray/browser/default_web_contents_delegate.h"
 #include "brightray/browser/inspectable_web_contents_impl.h"
 #include "brightray/browser/inspectable_web_contents_delegate.h"
+#include "brightray/browser/inspectable_web_contents_view_delegate.h"
 
 namespace atom {
 
@@ -21,7 +22,8 @@ class WebDialogHelper;
 
 class CommonWebContentsDelegate
     : public brightray::DefaultWebContentsDelegate,
-      public brightray::InspectableWebContentsDelegate {
+      public brightray::InspectableWebContentsDelegate,
+      public brightray::InspectableWebContentsViewDelegate {
  public:
   CommonWebContentsDelegate();
   virtual ~CommonWebContentsDelegate();
@@ -86,6 +88,15 @@ class CommonWebContentsDelegate
   void DevToolsRemoveFileSystem(
       const base::FilePath& file_system_path) override;
 
+  // brightray::InspectableWebContentsViewDelegate:
+#if defined(TOOLKIT_VIEWS)
+  gfx::ImageSkia GetDevToolsWindowIcon() override;
+#endif
+#if defined(USE_X11)
+  void GetDevToolsWindowWMClass(
+      std::string* name, std::string* class_name) override;
+#endif
+
  private:
   // Callback for when DevToolsSaveToFile has completed.
   void OnDevToolsSaveToFile(const std::string& url);
index c3620bba70527fb594814622f9cb4d2b93aa1176..b3ac71d210cfd4c6619d8036d139d5e6ea529fb7 100644 (file)
@@ -76,8 +76,6 @@ NativeWindow::NativeWindow(
       aspect_ratio_(0.0),
       inspectable_web_contents_(inspectable_web_contents),
       weak_factory_(this) {
-  inspectable_web_contents->GetView()->SetDelegate(this);
-
   options.Get(switches::kFrame, &has_frame_);
   options.Get(switches::kTransparent, &transparent_);
   options.Get(switches::kEnableLargerThanScreen, &enable_larger_than_screen_);
@@ -418,18 +416,6 @@ void NativeWindow::NotifyWindowExecuteWindowsCommand(
                     OnExecuteWindowsCommand(command));
 }
 
-void NativeWindow::DevToolsFocused() {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnDevToolsFocus());
-}
-
-void NativeWindow::DevToolsOpened() {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnDevToolsOpened());
-}
-
-void NativeWindow::DevToolsClosed() {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnDevToolsClosed());
-}
-
 void NativeWindow::RenderViewCreated(
     content::RenderViewHost* render_view_host) {
   if (!transparent_)
index 5c8d8c73b0fee8b9d2dba8bab7f6b5b4388062ff..6350ad247c125e0558f49e368fcdae35dc8f1d1d 100644 (file)
@@ -15,7 +15,6 @@
 #include "base/memory/scoped_ptr.h"
 #include "base/memory/weak_ptr.h"
 #include "base/observer_list.h"
-#include "brightray/browser/inspectable_web_contents_view_delegate.h"
 #include "content/public/browser/readback_types.h"
 #include "content/public/browser/web_contents_observer.h"
 #include "content/public/browser/web_contents_user_data.h"
@@ -50,8 +49,7 @@ namespace atom {
 
 struct DraggableRegion;
 
-class NativeWindow : public content::WebContentsObserver,
-                     public brightray::InspectableWebContentsViewDelegate {
+class NativeWindow : public content::WebContentsObserver {
  public:
   using CapturePageCallback = base::Callback<void(const SkBitmap& bitmap)>;
 
@@ -234,11 +232,6 @@ class NativeWindow : public content::WebContentsObserver,
   NativeWindow(brightray::InspectableWebContents* inspectable_web_contents,
                const mate::Dictionary& options);
 
-  // brightray::InspectableWebContentsViewDelegate:
-  void DevToolsFocused() override;
-  void DevToolsOpened() override;
-  void DevToolsClosed() override;
-
   // content::WebContentsObserver:
   void RenderViewCreated(content::RenderViewHost* render_view_host) override;
   void BeforeUnloadDialogCancelled() override;
index 5b0a0c56b3de7334708d87b3ceb17248de1e63cf..33ab1ecb6b3d102ba102e3e29ccbd183ca249009 100644 (file)
@@ -55,11 +55,6 @@ class NativeWindowObserver {
   virtual void OnWindowEnterHtmlFullScreen() {}
   virtual void OnWindowLeaveHtmlFullScreen() {}
 
-  // Redirect devtools events.
-  virtual void OnDevToolsFocus() {}
-  virtual void OnDevToolsOpened() {}
-  virtual void OnDevToolsClosed() {}
-
   // Called when renderer is hung.
   virtual void OnRendererUnresponsive() {}
 
index 34c2f62263193532cb837a48dbf8fd28987ab2d2..0fa2a99c27fc7500377aa294fd275b3202ba0fc2 100644 (file)
@@ -823,18 +823,6 @@ bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
 }
 #endif
 
-gfx::ImageSkia NativeWindowViews::GetDevToolsWindowIcon() {
-  return GetWindowAppIcon();
-}
-
-#if defined(USE_X11)
-void NativeWindowViews::GetDevToolsWindowWMClass(
-    std::string* name, std::string* class_name) {
-  *class_name = Browser::Get()->GetName();
-  *name = base::StringToLowerASCII(*class_name);
-}
-#endif
-
 #if defined(OS_WIN)
 bool NativeWindowViews::PreHandleMSG(
     UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) {
index 70c722c7d9cc4428bcd295426e87ac1b104f1493..71e122d3814f10c04913f26e67d424839309aaee 100644 (file)
@@ -131,13 +131,6 @@ class NativeWindowViews : public NativeWindow,
   bool ExecuteWindowsCommand(int command_id) override;
 #endif
 
-  // brightray::InspectableWebContentsViewDelegate:
-  gfx::ImageSkia GetDevToolsWindowIcon() override;
-#if defined(USE_X11)
-  void GetDevToolsWindowWMClass(
-      std::string* name, std::string* class_name) override;
-#endif
-
 #if defined(OS_WIN)
   // MessageHandlerDelegate:
   bool PreHandleMSG(