Merge pull request #2136 from atom/check-object-life

Check whether object is destroyed before calling its methods
This commit is contained in:
Cheng Zhao 2015-07-06 21:56:59 +08:00
commit 5e2481e631
8 changed files with 29 additions and 37 deletions

View file

@ -60,45 +60,36 @@ void Tray::OnBalloonClosed() {
Emit("balloon-closed"); Emit("balloon-closed");
} }
bool Tray::IsDestroyed() const {
return !tray_icon_;
}
void Tray::Destroy() { void Tray::Destroy() {
tray_icon_.reset(); tray_icon_.reset();
} }
void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) { void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetImage(image); tray_icon_->SetImage(image);
} }
void Tray::SetPressedImage(mate::Arguments* args, const gfx::Image& image) { void Tray::SetPressedImage(mate::Arguments* args, const gfx::Image& image) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetPressedImage(image); tray_icon_->SetPressedImage(image);
} }
void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) { void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetToolTip(tool_tip); tray_icon_->SetToolTip(tool_tip);
} }
void Tray::SetTitle(mate::Arguments* args, const std::string& title) { void Tray::SetTitle(mate::Arguments* args, const std::string& title) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetTitle(title); tray_icon_->SetTitle(title);
} }
void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) { void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetHighlightMode(highlight); tray_icon_->SetHighlightMode(highlight);
} }
void Tray::DisplayBalloon(mate::Arguments* args, void Tray::DisplayBalloon(mate::Arguments* args,
const mate::Dictionary& options) { const mate::Dictionary& options) {
if (!CheckTrayLife(args))
return;
gfx::Image icon; gfx::Image icon;
options.Get("icon", &icon); options.Get("icon", &icon);
base::string16 title, content; base::string16 title, content;
@ -112,25 +103,14 @@ void Tray::DisplayBalloon(mate::Arguments* args,
} }
void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
if (!CheckTrayLife(args))
return;
tray_icon_->SetContextMenu(menu->model()); tray_icon_->SetContextMenu(menu->model());
} }
bool Tray::CheckTrayLife(mate::Arguments* args) {
if (!tray_icon_) {
args->ThrowError("Tray is already destroyed");
return false;
} else {
return true;
}
}
// static // static
void Tray::BuildPrototype(v8::Isolate* isolate, void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) { v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("destroy", &Tray::Destroy) .SetMethod("destroy", &Tray::Destroy, true)
.SetMethod("setImage", &Tray::SetImage) .SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage) .SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip) .SetMethod("setToolTip", &Tray::SetToolTip)

View file

@ -47,6 +47,9 @@ class Tray : public mate::EventEmitter,
void OnBalloonClicked() override; void OnBalloonClicked() override;
void OnBalloonClosed() override; void OnBalloonClosed() override;
// mate::Wrappable:
bool IsDestroyed() const override;
void Destroy(); void Destroy();
void SetImage(mate::Arguments* args, const gfx::Image& image); void SetImage(mate::Arguments* args, const gfx::Image& image);
void SetPressedImage(mate::Arguments* args, const gfx::Image& image); void SetPressedImage(mate::Arguments* args, const gfx::Image& image);
@ -57,8 +60,6 @@ class Tray : public mate::EventEmitter,
void SetContextMenu(mate::Arguments* args, Menu* menu); void SetContextMenu(mate::Arguments* args, Menu* menu);
private: private:
bool CheckTrayLife(mate::Arguments* args);
scoped_ptr<TrayIcon> tray_icon_; scoped_ptr<TrayIcon> tray_icon_;
DISALLOW_COPY_AND_ASSIGN(Tray); DISALLOW_COPY_AND_ASSIGN(Tray);

View file

@ -740,8 +740,8 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
if (template_.IsEmpty()) if (template_.IsEmpty())
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate) template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
.SetMethod("destroy", &WebContents::Destroy) .SetMethod("destroy", &WebContents::Destroy, true)
.SetMethod("isAlive", &WebContents::IsAlive) .SetMethod("isAlive", &WebContents::IsAlive, true)
.SetMethod("getId", &WebContents::GetID) .SetMethod("getId", &WebContents::GetID)
.SetMethod("equal", &WebContents::Equal) .SetMethod("equal", &WebContents::Equal)
.SetMethod("_loadUrl", &WebContents::LoadURL) .SetMethod("_loadUrl", &WebContents::LoadURL)
@ -775,7 +775,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
.SetMethod("unselect", &WebContents::Unselect) .SetMethod("unselect", &WebContents::Unselect)
.SetMethod("replace", &WebContents::Replace) .SetMethod("replace", &WebContents::Replace)
.SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling) .SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling)
.SetMethod("_send", &WebContents::SendIPCMessage) .SetMethod("_send", &WebContents::SendIPCMessage, true)
.SetMethod("setSize", &WebContents::SetSize) .SetMethod("setSize", &WebContents::SetSize)
.SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency)
.SetMethod("isGuest", &WebContents::IsGuest) .SetMethod("isGuest", &WebContents::IsGuest)
@ -792,6 +792,10 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_)); isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_));
} }
bool WebContents::IsDestroyed() const {
return !IsAlive();
}
void WebContents::OnRendererMessage(const base::string16& channel, void WebContents::OnRendererMessage(const base::string16& channel,
const base::ListValue& args) { const base::ListValue& args) {
// webContents.emit(channel, new Event(), args...); // webContents.emit(channel, new Event(), args...);

View file

@ -111,6 +111,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// mate::Wrappable: // mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder( mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override; v8::Isolate* isolate) override;
bool IsDestroyed() const override;
// content::WebContentsDelegate: // content::WebContentsDelegate:
bool AddMessageToConsole(content::WebContents* source, bool AddMessageToConsole(content::WebContents* source,

View file

@ -177,6 +177,10 @@ mate::Wrappable* Window::New(v8::Isolate* isolate,
return new Window(isolate, options); return new Window(isolate, options);
} }
bool Window::IsDestroyed() const {
return !window_ || window_->IsClosed();
}
void Window::Destroy() { void Window::Destroy() {
window_->CloseContents(nullptr); window_->CloseContents(nullptr);
} }
@ -477,7 +481,7 @@ v8::Local<v8::Value> Window::DevToolsWebContents(v8::Isolate* isolate) {
void Window::BuildPrototype(v8::Isolate* isolate, void Window::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) { v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("destroy", &Window::Destroy) .SetMethod("destroy", &Window::Destroy, true)
.SetMethod("close", &Window::Close) .SetMethod("close", &Window::Close)
.SetMethod("isClosed", &Window::IsClosed) .SetMethod("isClosed", &Window::IsClosed)
.SetMethod("focus", &Window::Focus) .SetMethod("focus", &Window::Focus)
@ -540,9 +544,9 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("showDefinitionForSelection", .SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection) &Window::ShowDefinitionForSelection)
#endif #endif
.SetProperty("id", &Window::ID) .SetProperty("id", &Window::ID, true)
.SetProperty("webContents", &Window::WebContents) .SetProperty("webContents", &Window::WebContents, true)
.SetProperty("devToolsWebContents", &Window::DevToolsWebContents); .SetProperty("devToolsWebContents", &Window::DevToolsWebContents, true);
} }
} // namespace api } // namespace api

View file

@ -73,6 +73,9 @@ class Window : public mate::TrackableObject<Window>,
void OnDevToolsClosed() override; void OnDevToolsClosed() override;
void OnExecuteWindowsCommand(const std::string& command_name) override; void OnExecuteWindowsCommand(const std::string& command_name) override;
// mate::Wrappable:
bool IsDestroyed() const override;
private: private:
// APIs for NativeWindow. // APIs for NativeWindow.
void Destroy(); void Destroy();

View file

@ -21,9 +21,8 @@ createGuest = (embedder, url, frameName, options) ->
# guest is closed by user then we should prevent |embedder| from double # guest is closed by user then we should prevent |embedder| from double
# closing guest. # closing guest.
closedByEmbedder = -> closedByEmbedder = ->
embedder.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED', guest.id
guest.removeListener 'closed', closedByUser guest.removeListener 'closed', closedByUser
guest.destroy() unless guest.isClosed() guest.destroy()
closedByUser = -> closedByUser = ->
embedder.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED', guest.id embedder.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED', guest.id
embedder.removeListener 'render-view-deleted', closedByEmbedder embedder.removeListener 'render-view-deleted', closedByEmbedder

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit cc4e2fcd94b5a22e6720f0fba1c586a89640f1f6 Subproject commit 41cd6d13c9c9be164f427864277f3cc36b69eb39