}
DownloadItem::~DownloadItem() {
- Destroy();
-}
-
-void DownloadItem::Destroy() {
- if (download_item_) {
- download_item_->RemoveObserver(this);
- auto iter = g_download_item_objects.find(download_item_->GetId());
- if (iter != g_download_item_objects.end())
- g_download_item_objects.erase(iter);
- download_item_ = nullptr;
- }
-}
-
-bool DownloadItem::IsDestroyed() const {
- return download_item_ == nullptr;
+ if (download_item_)
+ OnDownloadDestroyed(download_item_);
}
void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) {
download_item_->IsDone() ? Emit("done", item->GetState()) : Emit("updated");
}
-void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) {
- Destroy();
+void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download_item) {
+ download_item_->RemoveObserver(this);
+ auto iter = g_download_item_objects.find(download_item_->GetId());
+ if (iter != g_download_item_objects.end())
+ g_download_item_objects.erase(iter);
+ download_item_ = nullptr;
}
int64 DownloadItem::GetReceivedBytes() {
mate::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
+ .MakeDestroyable()
.SetMethod("pause", &DownloadItem::Pause)
.SetMethod("resume", &DownloadItem::Resume)
.SetMethod("cancel", &DownloadItem::Cancel)
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
- bool IsDestroyed() const override;
-
- void Destroy();
content::DownloadItem* download_item_;
}
GlobalShortcut::~GlobalShortcut() {
-}
-
-void GlobalShortcut::Destroy() {
UnregisterAll();
}
GlobalShortcut();
~GlobalShortcut() override;
- // mate::TrackableObject:
- void Destroy() override;
-
// mate::Wrappable implementations:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
Menu::~Menu() {
}
-void Menu::Destroy() {
- model_.reset();
-}
-
-bool Menu::IsDestroyed() const {
- return !model_;
-}
-
void Menu::AfterInit(v8::Isolate* isolate) {
mate::Dictionary wrappable(isolate, GetWrapper(isolate));
mate::Dictionary delegate;
void Menu::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
+ .MakeDestroyable()
.SetMethod("insertItem", &Menu::InsertItemAt)
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
Menu();
~Menu() override;
- // mate::TrackableObject:
- void Destroy() override;
-
// mate::Wrappable:
- bool IsDestroyed() const override;
void AfterInit(v8::Isolate* isolate) override;
// ui::SimpleMenuModel::Delegate:
protected:
MenuMac();
- void Destroy() override;
void Popup(Window* window) override;
void PopupAt(Window* window, int x, int y) override;
MenuMac::MenuMac() {
}
-void MenuMac::Destroy() {
- menu_controller_.reset();
- Menu::Destroy();
-}
-
void MenuMac::Popup(Window* window) {
NativeWindow* native_window = window->window();
if (!native_window)
}
PowerMonitor::~PowerMonitor() {
-}
-
-void PowerMonitor::Destroy() {
base::PowerMonitor::Get()->RemoveObserver(this);
}
PowerMonitor();
~PowerMonitor() override;
- // mate::TrackableObject:
- void Destroy() override;
-
// base::PowerObserver implementations:
void OnPowerStateChange(bool on_battery_power) override;
void OnSuspend() override;
PowerSaveBlocker::~PowerSaveBlocker() {
}
-void PowerSaveBlocker::Destroy() {
- power_save_blocker_types_.clear();
- power_save_blocker_.reset();
-}
-
void PowerSaveBlocker::UpdatePowerSaveBlocker() {
if (power_save_blocker_types_.empty()) {
power_save_blocker_.reset();
PowerSaveBlocker();
~PowerSaveBlocker() override;
- // mate::TrackableObject:
- void Destroy() override;
-
// mate::Wrappable implementations:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
Session::~Session() {
content::BrowserContext::GetDownloadManager(browser_context())->
RemoveObserver(this);
- Destroy();
}
void Session::OnDownloadCreated(content::DownloadManager* manager,
}
}
-bool Session::IsDestroyed() const {
- return !browser_context_;
-}
-
-void Session::Destroy() {
- browser_context_ = nullptr;
-}
-
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(browser_context(), url, callback);
}
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
+ .MakeDestroyable()
.SetMethod("resolveProxy", &Session::ResolveProxy)
.SetMethod("clearCache", &Session::ClearCache)
.SetMethod("clearStorageData", &Session::ClearStorageData)
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
- bool IsDestroyed() const override;
private:
- // mate::TrackableObject:
- void Destroy() override;
-
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
void ClearCache(const net::CompletionCallback& callback);
void ClearStorageData(mate::Arguments* args);
Emit("drag-end");
}
-bool Tray::IsDestroyed() const {
- return !tray_icon_;
-}
-
-void Tray::Destroy() {
- tray_icon_.reset();
-}
-
void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) {
tray_icon_->SetImage(image);
}
void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
- .SetMethod("destroy", &Tray::Destroy, true)
- .SetMethod("isDestroyed", &Tray::IsDestroyed, true)
+ .MakeDestroyable()
.SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip)
void OnDragExited() override;
void OnDragEnded() override;
- // mate::Wrappable:
- bool IsDestroyed() const override;
-
- // mate::TrackableObject:
- void Destroy() override;
-
void SetImage(mate::Arguments* args, const gfx::Image& image);
void SetPressedImage(mate::Arguments* args, const gfx::Image& image);
void SetToolTip(mate::Arguments* args, const std::string& tool_tip);
}
WebContents::~WebContents() {
- Destroy();
+ if (type_ == WEB_VIEW && managed_web_contents()) {
+ // When force destroying the "destroyed" event is not emitted.
+ WebContentsDestroyed();
+
+ guest_delegate_->Destroy();
+
+ Observe(nullptr);
+ DestroyWebContents();
+ }
}
bool WebContents::AddMessageToConsole(content::WebContents* source,
details.is_in_page, details.did_replace_entry);
}
-void WebContents::Destroy() {
- session_.Reset();
- if (type_ == WEB_VIEW && managed_web_contents()) {
- // When force destroying the "destroyed" event is not emitted.
- WebContentsDestroyed();
-
- guest_delegate_->Destroy();
-
- Observe(nullptr);
- DestroyWebContents();
- }
-}
-
int WebContents::GetID() const {
return web_contents()->GetRenderProcessHost()->GetID();
}
v8::Isolate* isolate) {
if (template_.IsEmpty())
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
- .SetMethod("destroy", &WebContents::Destroy, true)
- .SetMethod("isDestroyed", &WebContents::IsDestroyed, true)
+ .MakeDestroyable()
.SetMethod("getId", &WebContents::GetID)
.SetMethod("equal", &WebContents::Equal)
.SetMethod("_loadURL", &WebContents::LoadURL)
.SetMethod("replaceMisspelling", &WebContents::ReplaceMisspelling)
.SetMethod("focus", &WebContents::Focus)
.SetMethod("tabTraverse", &WebContents::TabTraverse)
- .SetMethod("_send", &WebContents::SendIPCMessage, true)
+ .SetMethod("_send", &WebContents::SendIPCMessage)
.SetMethod("sendInputEvent", &WebContents::SendInputEvent)
.SetMethod("beginFrameSubscription",
&WebContents::BeginFrameSubscription)
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
- .SetProperty("session", &WebContents::Session, true)
- .SetProperty("devToolsWebContents",
- &WebContents::DevToolsWebContents, true)
+ .SetProperty("session", &WebContents::Session)
+ .SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
.Build());
return mate::ObjectTemplateBuilder(
isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_));
}
-bool WebContents::IsDestroyed() const {
- return !web_contents();
-}
-
AtomBrowserContext* WebContents::GetBrowserContext() const {
return static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext());
}
static mate::Handle<WebContents> Create(
v8::Isolate* isolate, const mate::Dictionary& options);
- // mate::TrackableObject:
- void Destroy() override;
-
int GetID() const;
bool Equal(const WebContents* web_contents) const;
void LoadURL(const GURL& url, const mate::Dictionary& options);
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
- bool IsDestroyed() const override;
// content::WebContentsDelegate:
bool AddMessageToConsole(content::WebContents* source,
}
Window::~Window() {
- if (window_)
- Destroy();
+ if (!window_->IsClosed())
+ window_->CloseContents(nullptr);
}
void Window::WillCloseWindow(bool* prevent_default) {
}
void Window::OnWindowClosed() {
- if (api_web_contents_) {
- api_web_contents_->DestroyWebContents();
- api_web_contents_ = nullptr;
- web_contents_.Reset();
- }
+ api_web_contents_->DestroyWebContents();
RemoveFromWeakMap();
window_->RemoveObserver(this);
+ // We can not call Destroy here because we need to call Emit first, but we
+ // also do not want any method to be used, so just mark as destroyed here.
+ MarkDestroyed();
+
Emit("closed");
- // Clean up the resources after window has been closed.
- base::MessageLoop::current()->DeleteSoon(FROM_HERE, window_.release());
+ // Destroy the native class when window is closed.
+ base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure());
}
void Window::OnWindowBlur() {
return new Window(isolate, options);
}
-bool Window::IsDestroyed() const {
- return !window_ || window_->IsClosed();
-}
-
-void Window::Destroy() {
- if (window_)
- window_->CloseContents(nullptr);
-}
-
void Window::Close() {
window_->Close();
}
void Window::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype)
- .SetMethod("destroy", &Window::Destroy, true)
- .SetMethod("isDestroyed", &Window::IsDestroyed, true)
+ .MakeDestroyable()
.SetMethod("close", &Window::Close)
.SetMethod("focus", &Window::Focus)
.SetMethod("isFocused", &Window::IsFocused)
.SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection)
#endif
- .SetProperty("id", &Window::ID, true)
- .SetProperty("webContents", &Window::WebContents, true);
+ .SetProperty("id", &Window::ID)
+ .SetProperty("webContents", &Window::WebContents);
}
// static
void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override;
#endif
- // mate::Wrappable:
- bool IsDestroyed() const override;
-
private:
- // mate::TrackableObject:
- void Destroy() override;
-
// APIs for NativeWindow.
void Close();
void Focus();
TrackableObjectBase::TrackableObjectBase()
: weak_map_id_(0), wrapped_(nullptr), weak_factory_(this) {
- RegisterDestructionCallback(
- base::Bind(&TrackableObjectBase::Destroy, weak_factory_.GetWeakPtr()));
+ RegisterDestructionCallback(GetDestroyClosure());
}
TrackableObjectBase::~TrackableObjectBase() {
AttachAsUserData(wrapped_);
}
+void TrackableObjectBase::MarkDestroyed() {
+ GetWrapper(isolate())->SetAlignedPointerInInternalField(0, nullptr);
+}
+
+base::Closure TrackableObjectBase::GetDestroyClosure() {
+ return base::Bind(&TrackableObjectBase::Destroy, weak_factory_.GetWeakPtr());
+}
+
+void TrackableObjectBase::Destroy() {
+ delete this;
+}
+
void TrackableObjectBase::AttachAsUserData(base::SupportsUserData* wrapped) {
if (weak_map_id_ != 0) {
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
// Wrap TrackableObject into a class that SupportsUserData.
void AttachAsUserData(base::SupportsUserData* wrapped);
- // Subclasses should implement this to destroy their native types.
- virtual void Destroy() = 0;
-
protected:
~TrackableObjectBase() override;
// mate::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
+ // Mark the JS object as destroyed.
+ void MarkDestroyed();
+
+ // Returns a closure that can destroy the native class.
+ base::Closure GetDestroyClosure();
+
// Get the weak_map_id from SupportsUserData.
static int32_t GetIDFromWrappedClass(base::SupportsUserData* wrapped);
base::SupportsUserData* wrapped_;
private:
+ void Destroy();
+
base::WeakPtrFactory<TrackableObjectBase> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
-Subproject commit 93984941005bab194a2d47aff655d525c064efcb
+Subproject commit e859228db163c27410fb200c2df0715478fdf0d7