Be safe on lifetime of webContents.

This commit is contained in:
Cheng Zhao 2014-04-25 11:22:51 +08:00
Родитель b02bcc0016
Коммит 9eeec9aa0b
3 изменённых файлов: 31 добавлений и 13 удалений

Просмотреть файл

@ -17,11 +17,20 @@ namespace api {
WebContents::WebContents(content::WebContents* web_contents) WebContents::WebContents(content::WebContents* web_contents)
: web_contents_(web_contents) { : web_contents_(web_contents) {
Observe(web_contents_);
} }
WebContents::~WebContents() { WebContents::~WebContents() {
} }
void WebContents::WebContentsDestroyed(content::WebContents*) {
web_contents_ = NULL;
}
bool WebContents::IsAlive() const {
return web_contents_ != NULL;
}
GURL WebContents::GetURL() const { GURL WebContents::GetURL() const {
return web_contents_->GetURL(); return web_contents_->GetURL();
} }
@ -62,6 +71,7 @@ void WebContents::ExecuteJavaScript(const string16& code) {
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate) return mate::ObjectTemplateBuilder(isolate)
.SetMethod("isAlive", &WebContents::IsAlive)
.SetMethod("getUrl", &WebContents::GetURL) .SetMethod("getUrl", &WebContents::GetURL)
.SetMethod("getTitle", &WebContents::GetTitle) .SetMethod("getTitle", &WebContents::GetTitle)
.SetMethod("isLoading", &WebContents::IsLoading) .SetMethod("isLoading", &WebContents::IsLoading)

Просмотреть файл

@ -6,23 +6,20 @@
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_ #define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/event_emitter.h"
#include "content/public/browser/web_contents_observer.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
class GURL;
namespace content {
class WebContents;
}
namespace atom { namespace atom {
namespace api { namespace api {
class WebContents : public mate::EventEmitter { class WebContents : public mate::EventEmitter,
public content::WebContentsObserver {
public: public:
static mate::Handle<WebContents> Create(v8::Isolate* isolate, static mate::Handle<WebContents> Create(v8::Isolate* isolate,
content::WebContents* web_contents); content::WebContents* web_contents);
bool IsAlive() const;
GURL GetURL() const; GURL GetURL() const;
string16 GetTitle() const; string16 GetTitle() const;
bool IsLoading() const; bool IsLoading() const;
@ -39,7 +36,10 @@ class WebContents : public mate::EventEmitter {
// mate::Wrappable implementations: // mate::Wrappable implementations:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate); v8::Isolate* isolate) OVERRIDE;
// content::WebContentsObserver implementations:
virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
private: private:
content::WebContents* web_contents_; // Weak. content::WebContents* web_contents_; // Weak.

Просмотреть файл

@ -15,15 +15,23 @@ BrowserWindow::_init = ->
@setMenu menu if menu? @setMenu menu if menu?
# Define getter for webContents. # Define getter for webContents.
@__webContents = null webContents = null
@__defineGetter__ 'webContents', -> @__defineGetter__ 'webContents', ->
@__webContents ?= @getWebContents() webContents ?= @getWebContents()
@__devToolsWebContents = null # Return null if webContents is destroyed.
webContents = null unless webContents?.isAlive()
webContents
# And devToolsWebContents.
devToolsWebContents = null
@__defineGetter__ 'devToolsWebContents', -> @__defineGetter__ 'devToolsWebContents', ->
if @isDevToolsOpened() if @isDevToolsOpened()
@__devToolsWebContents ?= @getDevToolsWebContents() # Get the 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()
else else
@__devToolsWebContents = null devToolsWebContents = null
# Remember the window. # Remember the window.
id = BrowserWindow.windows.add this id = BrowserWindow.windows.add this