]> khamidou — GitHub mirrors - electron.git/commitdiff
Move webContents events away from window.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 25 Apr 2014 03:51:05 +0000 (11:51 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 25 Apr 2014 03:51:05 +0000 (11:51 +0800)
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/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_observer.h

index 782e5893f45904caeb95742508e405c4c6ef34c1..52c6735dfff055111c2cca1ba5e965241d42d049 100644 (file)
@@ -23,7 +23,22 @@ WebContents::WebContents(content::WebContents* web_contents)
 WebContents::~WebContents() {
 }
 
+void WebContents::RenderViewDeleted(content::RenderViewHost*) {
+  base::ListValue args;
+  args.AppendInteger(GetProcessID());
+  args.AppendInteger(GetRoutingID());
+  Emit("render-view-deleted", args);
+}
+
+void WebContents::RenderProcessGone(base::TerminationStatus status) {
+  Emit("crashed");
+}
+
 void WebContents::WebContentsDestroyed(content::WebContents*) {
+  // The RenderViewDeleted is not called when the WebContents is destroyed
+  // directly.
+  RenderViewDeleted(web_contents_->GetRenderViewHost());
+
   web_contents_ = NULL;
 }
 
index b15b604903adde0b24376016d77a9afe6dc71967..e9a22a00b720d023b93f7e66d2f91c8b32119d9b 100644 (file)
@@ -39,6 +39,8 @@ class WebContents : public mate::EventEmitter,
       v8::Isolate* isolate) OVERRIDE;
 
   // content::WebContentsObserver implementations:
+  virtual void RenderViewDeleted(content::RenderViewHost*) OVERRIDE;
+  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
   virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
 
  private:
index c0a9d1f5879604edd6f7cf42f266a563791e0a57..24bd64811e123cb903af002ade2765ae97047e12 100644 (file)
@@ -115,17 +115,6 @@ void Window::OnRendererResponsive() {
   Emit("responsive");
 }
 
-void Window::OnRenderViewDeleted(int process_id, int routing_id) {
-  base::ListValue args;
-  args.AppendInteger(process_id);
-  args.AppendInteger(routing_id);
-  Emit("render-view-deleted", args);
-}
-
-void Window::OnRendererCrashed() {
-  Emit("crashed");
-}
-
 // static
 mate::Wrappable* Window::New(mate::Arguments* args,
                              const base::DictionaryValue& options) {
@@ -425,8 +414,8 @@ void Window::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("blurWebView", &Window::BlurWebView)
       .SetMethod("isWebViewFocused", &Window::IsWebViewFocused)
       .SetMethod("capturePage", &Window::CapturePage)
-      .SetMethod("getWebContents", &Window::GetWebContents)
-      .SetMethod("getDevToolsWebContents", &Window::GetDevToolsWebContents)
+      .SetMethod("_getWebContents", &Window::GetWebContents)
+      .SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents)
       .SetMethod("loadUrl", &Window::LoadURL)
       .SetMethod("canGoBack", &Window::CanGoBack)
       .SetMethod("canGoForward", &Window::CanGoForward)
index d119f965724cdac7c8deeffa25f10f182a5894a8..1cf9169a9f3774c08fdba741ac24599eb2f0c536 100644 (file)
@@ -56,8 +56,6 @@ class Window : public mate::EventEmitter,
   virtual void OnWindowBlur() OVERRIDE;
   virtual void OnRendererUnresponsive() OVERRIDE;
   virtual void OnRendererResponsive() OVERRIDE;
-  virtual void OnRenderViewDeleted(int process_id, int routing_id) OVERRIDE;
-  virtual void OnRendererCrashed() OVERRIDE;
 
  private:
   // APIs for NativeWindow.
index 04b0a6f4ea332d9411ca1e3481bf3763cb11e248..8db7680ad2e21f7455238815e1c7913b7960d494 100644 (file)
@@ -14,19 +14,13 @@ BrowserWindow::_init = ->
     menu = app.getApplicationMenu()
     @setMenu menu if menu?
 
-  # Define getter for webContents.
-  webContents = null
-  @__defineGetter__ 'webContents', ->
-    webContents ?= @getWebContents()
-    # Return null if webContents is destroyed.
-    webContents = null unless webContents?.isAlive()
-    webContents
-
-  # And devToolsWebContents.
+  @webContents = @getWebContents()
+
+  # Define getter for devToolsWebContents.
   devToolsWebContents = null
   @__defineGetter__ 'devToolsWebContents', ->
     if @isDevToolsOpened()
-      # Get the new devToolsWebContents if previous one has been destroyed, it
+      # Get a new devToolsWebContents if previous one has been destroyed, it
       # could happen when the devtools has been closed and then reopened.
       devToolsWebContents = null unless devToolsWebContents?.isAlive()
       devToolsWebContents ?= @getDevToolsWebContents()
@@ -43,12 +37,23 @@ BrowserWindow::_init = ->
 
   # Tell the rpc server that a render view has been deleted and we need to
   # release all objects owned by it.
-  @on 'render-view-deleted', (event, processId, routingId) ->
+  @webContents.on 'render-view-deleted', (event, processId, routingId) ->
     process.emit 'ATOM_BROWSER_RELEASE_RENDER_VIEW', processId, routingId
 
 BrowserWindow::toggleDevTools = ->
   if @isDevToolsOpened() then @closeDevTools() else @openDevTools()
 
+BrowserWindow::getWebContents = ->
+  webContents = @_getWebContents()
+  webContents.__proto__ = EventEmitter.prototype
+  webContents
+
+BrowserWindow::getDevToolsWebContents = ->
+  webContents = @_getDevToolsWebContents()
+  webContents.__proto__ = EventEmitter.prototype
+  webContents = null unless webContents.isAlive()
+  webContents
+
 BrowserWindow::restart = ->
   @loadUrl(@getUrl())
 
index c695b6aaac6483212d30f2599b7158a471dee039..8e1daa4dfa0b3626a97b9a30989868a3bcaf3c81 100644 (file)
@@ -300,14 +300,6 @@ void NativeWindow::DestroyWebContents() {
   if (!inspectable_web_contents_)
     return;
 
-  // The OnRenderViewDeleted is not called when the WebContents is destroyed
-  // directly (e.g. when closing the window), so we make sure it's always
-  // emitted to users by sending it before window is closed..
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
-                    OnRenderViewDeleted(
-                        GetWebContents()->GetRenderProcessHost()->GetID(),
-                        GetWebContents()->GetRoutingID()));
-
   inspectable_web_contents_.reset();
 }
 
@@ -477,16 +469,6 @@ void NativeWindow::RendererResponsive(content::WebContents* source) {
   FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive());
 }
 
-void NativeWindow::RenderViewDeleted(content::RenderViewHost* rvh) {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
-                    OnRenderViewDeleted(rvh->GetProcess()->GetID(),
-                                        rvh->GetRoutingID()));
-}
-
-void NativeWindow::RenderProcessGone(base::TerminationStatus status) {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererCrashed());
-}
-
 void NativeWindow::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
   // Do nothing, we override this method just to avoid compilation error since
   // there are two virtual functions named BeforeUnloadFired.
index 7c97bfe28006b9e13da12fdc24b1a50fbcd71b85..ba8beecb78ebdcaff8b46f8911148860755273ef 100644 (file)
@@ -229,8 +229,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   virtual void RendererResponsive(content::WebContents* source) OVERRIDE;
 
   // Implementations of content::WebContentsObserver.
-  virtual void RenderViewDeleted(content::RenderViewHost*) OVERRIDE;
-  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
   virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) OVERRIDE;
   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
 
index 5d697480050fb4274f6f62820b63bec6fee115b1..52700e5bfe65c598cce76a4caf822c05a563efda 100644 (file)
@@ -34,12 +34,6 @@ class NativeWindowObserver {
 
   // Called when renderer recovers.
   virtual void OnRendererResponsive() {}
-
-  // Called when a render view has been deleted.
-  virtual void OnRenderViewDeleted(int process_id, int routing_id) {}
-
-  // Called when renderer has crashed.
-  virtual void OnRendererCrashed() {}
 };
 
 }  // namespace atom