Move webContents events away from window.

This commit is contained in:
Cheng Zhao 2014-04-25 11:51:05 +08:00
parent 9eeec9aa0b
commit 859606e88c
8 changed files with 34 additions and 51 deletions

View 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;
}

View 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:

View 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)

View 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.

View 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
@webContents = @getWebContents()
# And devToolsWebContents.
# 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())

View 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.

View 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;

View 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