WebContents::WebContents(content::WebContents* web_contents)
: web_contents_(web_contents) {
+ Observe(web_contents_);
}
WebContents::~WebContents() {
}
+void WebContents::WebContentsDestroyed(content::WebContents*) {
+ web_contents_ = NULL;
+}
+
+bool WebContents::IsAlive() const {
+ return web_contents_ != NULL;
+}
+
GURL WebContents::GetURL() const {
return web_contents_->GetURL();
}
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
+ .SetMethod("isAlive", &WebContents::IsAlive)
.SetMethod("getUrl", &WebContents::GetURL)
.SetMethod("getTitle", &WebContents::GetTitle)
.SetMethod("isLoading", &WebContents::IsLoading)
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
#include "atom/browser/api/event_emitter.h"
+#include "content/public/browser/web_contents_observer.h"
#include "native_mate/handle.h"
-class GURL;
-
-namespace content {
-class WebContents;
-}
-
namespace atom {
namespace api {
-class WebContents : public mate::EventEmitter {
+class WebContents : public mate::EventEmitter,
+ public content::WebContentsObserver {
public:
static mate::Handle<WebContents> Create(v8::Isolate* isolate,
content::WebContents* web_contents);
+ bool IsAlive() const;
GURL GetURL() const;
string16 GetTitle() const;
bool IsLoading() const;
// mate::Wrappable implementations:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
- v8::Isolate* isolate);
+ v8::Isolate* isolate) OVERRIDE;
+
+ // content::WebContentsObserver implementations:
+ virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
private:
content::WebContents* web_contents_; // Weak.
@setMenu menu if menu?
# Define getter for webContents.
- @__webContents = null
+ webContents = null
@__defineGetter__ 'webContents', ->
- @__webContents ?= @getWebContents()
- @__devToolsWebContents = null
+ webContents ?= @getWebContents()
+ # Return null if webContents is destroyed.
+ webContents = null unless webContents?.isAlive()
+ webContents
+
+ # And devToolsWebContents.
+ devToolsWebContents = null
@__defineGetter__ 'devToolsWebContents', ->
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
- @__devToolsWebContents = null
+ devToolsWebContents = null
# Remember the window.
id = BrowserWindow.windows.add this