Merge pull request #2046 from atom/id-weak-map

Track native JS objects in C++
This commit is contained in:
Cheng Zhao 2015-06-24 20:01:42 +08:00
commit bd4d6dcda2
21 changed files with 461 additions and 127 deletions

View file

@ -133,6 +133,14 @@ void App::OnWillFinishLaunching() {
} }
void App::OnFinishLaunching() { void App::OnFinishLaunching() {
// Create the defaultSession.
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
auto browser_context = static_cast<AtomBrowserContext*>(
AtomBrowserMainParts::Get()->browser_context());
auto handle = Session::CreateFrom(isolate(), browser_context);
default_session_.Reset(isolate(), handle.ToV8());
Emit("ready"); Emit("ready");
} }
@ -173,13 +181,10 @@ void App::SetAppUserModelId(const std::string& app_id) {
} }
v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) { v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
if (default_session_.IsEmpty()) { if (default_session_.IsEmpty())
auto browser_context = static_cast<AtomBrowserContext*>( return v8::Null(isolate);
AtomBrowserMainParts::Get()->browser_context()); else
auto handle = Session::Create(isolate, browser_context); return v8::Local<v8::Value>::New(isolate, default_session_);
default_session_.Reset(isolate, handle.ToV8());
}
return v8::Local<v8::Value>::New(isolate, default_session_);
} }
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(

View file

@ -68,6 +68,7 @@ class ResolveProxyHelper {
Session::Session(AtomBrowserContext* browser_context) Session::Session(AtomBrowserContext* browser_context)
: browser_context_(browser_context) { : browser_context_(browser_context) {
AttachAsUserData(browser_context);
} }
Session::~Session() { Session::~Session() {
@ -93,9 +94,13 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
} }
// static // static
mate::Handle<Session> Session::Create( mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate, v8::Isolate* isolate,
AtomBrowserContext* browser_context) { AtomBrowserContext* browser_context) {
auto existing = TrackableObject::FromWrappedClass(isolate, browser_context);
if (existing)
return mate::CreateHandle(isolate, static_cast<Session*>(existing));
return mate::CreateHandle(isolate, new Session(browser_context)); return mate::CreateHandle(isolate, new Session(browser_context));
} }

View file

@ -7,9 +7,9 @@
#include <string> #include <string>
#include "atom/browser/api/trackable_object.h"
#include "base/callback.h" #include "base/callback.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "native_mate/wrappable.h"
class GURL; class GURL;
@ -19,12 +19,13 @@ class AtomBrowserContext;
namespace api { namespace api {
class Session: public mate::Wrappable { class Session: public mate::TrackableObject<Session> {
public: public:
using ResolveProxyCallback = base::Callback<void(std::string)>; using ResolveProxyCallback = base::Callback<void(std::string)>;
static mate::Handle<Session> Create(v8::Isolate* isolate, // Gets or creates Session from the |browser_context|.
AtomBrowserContext* browser_context); static mate::Handle<Session> CreateFrom(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
protected: protected:
explicit Session(AtomBrowserContext* browser_context); explicit Session(AtomBrowserContext* browser_context);

View file

@ -155,6 +155,7 @@ WebContents::WebContents(content::WebContents* web_contents)
auto_size_enabled_(false), auto_size_enabled_(false),
is_full_page_plugin_(false), is_full_page_plugin_(false),
inspectable_web_contents_(nullptr) { inspectable_web_contents_(nullptr) {
AttachAsUserData(web_contents);
} }
WebContents::WebContents(const mate::Dictionary& options) WebContents::WebContents(const mate::Dictionary& options)
@ -176,6 +177,7 @@ WebContents::WebContents(const mate::Dictionary& options)
if (options.Get("embedder", &embedder) && embedder) if (options.Get("embedder", &embedder) && embedder)
owner_window = NativeWindow::FromWebContents(embedder->web_contents()); owner_window = NativeWindow::FromWebContents(embedder->web_contents());
AttachAsUserData(web_contents);
InitWithWebContents(web_contents, owner_window); InitWithWebContents(web_contents, owner_window);
inspectable_web_contents_ = managed_web_contents(); inspectable_web_contents_ = managed_web_contents();
@ -422,6 +424,7 @@ void WebContents::WebContentsDestroyed() {
// The RenderViewDeleted was not called when the WebContents is destroyed. // The RenderViewDeleted was not called when the WebContents is destroyed.
RenderViewDeleted(web_contents()->GetRenderViewHost()); RenderViewDeleted(web_contents()->GetRenderViewHost());
Emit("destroyed"); Emit("destroyed");
RemoveFromWeakMap();
} }
void WebContents::NavigationEntryCommitted( void WebContents::NavigationEntryCommitted(
@ -608,7 +611,7 @@ void WebContents::InspectServiceWorker() {
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) { v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
if (session_.IsEmpty()) { if (session_.IsEmpty()) {
mate::Handle<api::Session> handle = Session::Create( mate::Handle<api::Session> handle = Session::CreateFrom(
isolate, isolate,
static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext())); static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
session_.Reset(isolate, handle.ToV8()); session_.Reset(isolate, handle.ToV8());
@ -873,6 +876,13 @@ gfx::Size WebContents::GetDefaultSize() const {
// static // static
mate::Handle<WebContents> WebContents::CreateFrom( mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents) { v8::Isolate* isolate, brightray::InspectableWebContents* web_contents) {
// We have an existing WebContents object in JS.
auto existing = TrackableObject::FromWrappedClass(
isolate, web_contents->GetWebContents());
if (existing)
return mate::CreateHandle(isolate, static_cast<WebContents*>(existing));
// Otherwise create a new WebContents wrapper object.
auto handle = mate::CreateHandle(isolate, new WebContents(web_contents)); auto handle = mate::CreateHandle(isolate, new WebContents(web_contents));
g_wrap_web_contents.Run(handle.ToV8()); g_wrap_web_contents.Run(handle.ToV8());
return handle; return handle;
@ -881,6 +891,12 @@ mate::Handle<WebContents> WebContents::CreateFrom(
// static // static
mate::Handle<WebContents> WebContents::CreateFrom( mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents) { v8::Isolate* isolate, content::WebContents* web_contents) {
// We have an existing WebContents object in JS.
auto existing = TrackableObject::FromWrappedClass(isolate, web_contents);
if (existing)
return mate::CreateHandle(isolate, static_cast<WebContents*>(existing));
// Otherwise create a new WebContents wrapper object.
auto handle = mate::CreateHandle(isolate, new WebContents(web_contents)); auto handle = mate::CreateHandle(isolate, new WebContents(web_contents));
g_wrap_web_contents.Run(handle.ToV8()); g_wrap_web_contents.Run(handle.ToV8());
return handle; return handle;

View file

@ -8,7 +8,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/common_web_contents_delegate.h" #include "atom/browser/common_web_contents_delegate.h"
#include "content/public/browser/browser_plugin_guest_delegate.h" #include "content/public/browser/browser_plugin_guest_delegate.h"
#include "content/public/common/favicon_url.h" #include "content/public/common/favicon_url.h"
@ -46,7 +46,7 @@ struct SetSizeParams {
scoped_ptr<gfx::Size> normal_size; scoped_ptr<gfx::Size> normal_size;
}; };
class WebContents : public mate::EventEmitter, class WebContents : public mate::TrackableObject<WebContents>,
public content::BrowserPluginGuestDelegate, public content::BrowserPluginGuestDelegate,
public CommonWebContentsDelegate, public CommonWebContentsDelegate,
public content::WebContentsObserver, public content::WebContentsObserver,
@ -240,7 +240,6 @@ class WebContents : public mate::EventEmitter,
// Returns the default size of the guestview. // Returns the default size of the guestview.
gfx::Size GetDefaultSize() const; gfx::Size GetDefaultSize() const;
v8::Global<v8::Value> session_; v8::Global<v8::Value> session_;
// Stores whether the contents of the guest can be transparent. // Stores whether the contents of the guest can be transparent.

View file

@ -49,6 +49,13 @@ Window::~Window() {
Destroy(); Destroy();
} }
void Window::AfterInit(v8::Isolate* isolate) {
mate::TrackableObject<Window>::AfterInit(isolate);
auto web_contents = window_->managed_web_contents();
auto handle = WebContents::CreateFrom(isolate, web_contents);
web_contents_.Reset(isolate, handle.ToV8());
}
void Window::OnPageTitleUpdated(bool* prevent_default, void Window::OnPageTitleUpdated(bool* prevent_default,
const std::string& title) { const std::string& title) {
*prevent_default = Emit("page-title-updated", title); *prevent_default = Emit("page-title-updated", title);
@ -72,6 +79,7 @@ void Window::WillCloseWindow(bool* prevent_default) {
void Window::OnWindowClosed() { void Window::OnWindowClosed() {
Emit("closed"); Emit("closed");
RemoveFromWeakMap();
window_->RemoveObserver(this); window_->RemoveObserver(this);
} }
@ -401,8 +409,20 @@ void Window::SetOverlayIcon(const gfx::Image& overlay,
window_->SetOverlayIcon(overlay, description); window_->SetOverlayIcon(overlay, description);
} }
void Window::SetMenu(ui::SimpleMenuModel* menu) { void Window::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
window_->SetMenu(menu); mate::Handle<Menu> menu;
if (value->IsObject() &&
mate::V8ToString(value->ToObject()->GetConstructorName()) == "Menu" &&
mate::ConvertFromV8(isolate, value, &menu)) {
menu_.Reset(isolate, menu.ToV8());
window_->SetMenu(menu->model());
} else if (value->IsNull()) {
menu_.Reset();
window_->SetMenu(nullptr);
} else {
isolate->ThrowException(v8::Exception::TypeError(
mate::StringToV8(isolate, "Invalid Menu")));
}
} }
void Window::SetAutoHideMenuBar(bool auto_hide) { void Window::SetAutoHideMenuBar(bool auto_hide) {
@ -435,13 +455,15 @@ bool Window::IsVisibleOnAllWorkspaces() {
return window_->IsVisibleOnAllWorkspaces(); return window_->IsVisibleOnAllWorkspaces();
} }
int32_t Window::ID() const {
return weak_map_id();
}
v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) { v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) {
if (web_contents_.IsEmpty()) { if (web_contents_.IsEmpty())
auto handle = return v8::Null(isolate);
WebContents::CreateFrom(isolate, window_->managed_web_contents()); else
web_contents_.Reset(isolate, handle.ToV8()); return v8::Local<v8::Value>::New(isolate, web_contents_);
}
return v8::Local<v8::Value>::New(isolate, web_contents_);
} }
v8::Local<v8::Value> Window::DevToolsWebContents(v8::Isolate* isolate) { v8::Local<v8::Value> Window::DevToolsWebContents(v8::Isolate* isolate) {
@ -505,7 +527,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("capturePage", &Window::CapturePage) .SetMethod("capturePage", &Window::CapturePage)
.SetMethod("setProgressBar", &Window::SetProgressBar) .SetMethod("setProgressBar", &Window::SetProgressBar)
.SetMethod("setOverlayIcon", &Window::SetOverlayIcon) .SetMethod("setOverlayIcon", &Window::SetOverlayIcon)
.SetMethod("_setMenu", &Window::SetMenu) .SetMethod("setMenu", &Window::SetMenu)
.SetMethod("setAutoHideMenuBar", &Window::SetAutoHideMenuBar) .SetMethod("setAutoHideMenuBar", &Window::SetAutoHideMenuBar)
.SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide) .SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide)
.SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility) .SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility)
@ -518,6 +540,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("showDefinitionForSelection", .SetMethod("showDefinitionForSelection",
&Window::ShowDefinitionForSelection) &Window::ShowDefinitionForSelection)
#endif #endif
.SetProperty("id", &Window::ID)
.SetProperty("webContents", &Window::WebContents) .SetProperty("webContents", &Window::WebContents)
.SetProperty("devToolsWebContents", &Window::DevToolsWebContents); .SetProperty("devToolsWebContents", &Window::DevToolsWebContents);
} }
@ -529,14 +552,21 @@ void Window::BuildPrototype(v8::Isolate* isolate,
namespace { namespace {
using atom::api::Window;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) { v8::Local<v8::Context> context, void* priv) {
using atom::api::Window;
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
v8::Local<v8::Function> constructor = mate::CreateConstructor<Window>( v8::Local<v8::Function> constructor = mate::CreateConstructor<Window>(
isolate, "BrowserWindow", base::Bind(&Window::New)); isolate, "BrowserWindow", base::Bind(&Window::New));
mate::Dictionary browser_window(isolate, constructor);
browser_window.SetMethod("fromId",
&mate::TrackableObject<Window>::FromWeakMapID);
browser_window.SetMethod("getAllWindows",
&mate::TrackableObject<Window>::GetAll);
mate::Dictionary dict(isolate, exports); mate::Dictionary dict(isolate, exports);
dict.Set("BrowserWindow", static_cast<v8::Local<v8::Value>>(constructor)); dict.Set("BrowserWindow", browser_window);
} }
} // namespace } // namespace

View file

@ -10,8 +10,8 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/native_window_observer.h" #include "atom/browser/native_window_observer.h"
#include "atom/browser/api/event_emitter.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
class GURL; class GURL;
@ -25,10 +25,6 @@ class Arguments;
class Dictionary; class Dictionary;
} }
namespace ui {
class SimpleMenuModel;
}
namespace atom { namespace atom {
class NativeWindow; class NativeWindow;
@ -37,7 +33,7 @@ namespace api {
class WebContents; class WebContents;
class Window : public mate::EventEmitter, class Window : public mate::TrackableObject<Window>,
public NativeWindowObserver { public NativeWindowObserver {
public: public:
static mate::Wrappable* New(v8::Isolate* isolate, static mate::Wrappable* New(v8::Isolate* isolate,
@ -52,6 +48,9 @@ class Window : public mate::EventEmitter,
explicit Window(const mate::Dictionary& options); explicit Window(const mate::Dictionary& options);
virtual ~Window(); virtual ~Window();
// mate::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
// NativeWindowObserver: // NativeWindowObserver:
void OnPageTitleUpdated(bool* prevent_default, void OnPageTitleUpdated(bool* prevent_default,
const std::string& title) override; const std::string& title) override;
@ -134,7 +133,7 @@ class Window : public mate::EventEmitter,
void SetProgressBar(double progress); void SetProgressBar(double progress);
void SetOverlayIcon(const gfx::Image& overlay, void SetOverlayIcon(const gfx::Image& overlay,
const std::string& description); const std::string& description);
void SetMenu(ui::SimpleMenuModel* menu); void SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> menu);
void SetAutoHideMenuBar(bool auto_hide); void SetAutoHideMenuBar(bool auto_hide);
bool IsMenuBarAutoHide(); bool IsMenuBarAutoHide();
void SetMenuBarVisibility(bool visible); void SetMenuBarVisibility(bool visible);
@ -147,11 +146,13 @@ class Window : public mate::EventEmitter,
void SetVisibleOnAllWorkspaces(bool visible); void SetVisibleOnAllWorkspaces(bool visible);
bool IsVisibleOnAllWorkspaces(); bool IsVisibleOnAllWorkspaces();
int32_t ID() const;
v8::Local<v8::Value> WebContents(v8::Isolate* isolate); v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate); v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
v8::Global<v8::Value> web_contents_; v8::Global<v8::Value> web_contents_;
v8::Global<v8::Value> devtools_web_contents_; v8::Global<v8::Value> devtools_web_contents_;
v8::Global<v8::Value> menu_;
scoped_ptr<NativeWindow> window_; scoped_ptr<NativeWindow> window_;

View file

@ -1,25 +1,16 @@
EventEmitter = require('events').EventEmitter EventEmitter = require('events').EventEmitter
IDWeakMap = require 'id-weak-map'
app = require 'app' app = require 'app'
ipc = require 'ipc' ipc = require 'ipc'
BrowserWindow = process.atomBinding('window').BrowserWindow BrowserWindow = process.atomBinding('window').BrowserWindow
BrowserWindow::__proto__ = EventEmitter.prototype BrowserWindow::__proto__ = EventEmitter.prototype
# Store all created windows in the weak map.
BrowserWindow.windows = new IDWeakMap
BrowserWindow::_init = -> BrowserWindow::_init = ->
# Simulate the application menu on platforms other than OS X. # Simulate the application menu on platforms other than OS X.
if process.platform isnt 'darwin' if process.platform isnt 'darwin'
menu = app.getApplicationMenu() menu = app.getApplicationMenu()
@setMenu menu if menu? @setMenu menu if menu?
# Remember the window ID.
Object.defineProperty this, 'id',
value: BrowserWindow.windows.add(this)
enumerable: true
# Make new windows requested by links behave like "window.open" # Make new windows requested by links behave like "window.open"
@on '-new-window', (event, url, frameName) => @on '-new-window', (event, url, frameName) =>
event.sender = @webContents event.sender = @webContents
@ -36,21 +27,6 @@ BrowserWindow::_init = ->
@on 'focus', (event) => @on 'focus', (event) =>
app.emit 'browser-window-focus', event, this app.emit 'browser-window-focus', event, this
# Remove the window from weak map immediately when it's destroyed, since we
# could be iterating windows before GC happened.
@once 'closed', =>
BrowserWindow.windows.remove @id if BrowserWindow.windows.has @id
BrowserWindow::setMenu = (menu) ->
throw new TypeError('Invalid menu') unless menu is null or menu?.constructor?.name is 'Menu'
@menu = menu # Keep a reference of menu in case of GC.
@_setMenu menu
BrowserWindow.getAllWindows = ->
windows = BrowserWindow.windows
windows.get key for key in windows.keys()
BrowserWindow.getFocusedWindow = -> BrowserWindow.getFocusedWindow = ->
windows = BrowserWindow.getAllWindows() windows = BrowserWindow.getAllWindows()
return window for window in windows when window.isFocused() return window for window in windows when window.isFocused()
@ -63,9 +39,6 @@ BrowserWindow.fromDevToolsWebContents = (webContents) ->
windows = BrowserWindow.getAllWindows() windows = BrowserWindow.getAllWindows()
return window for window in windows when window.devToolsWebContents?.equal webContents return window for window in windows when window.devToolsWebContents?.equal webContents
BrowserWindow.fromId = (id) ->
BrowserWindow.windows.get id if BrowserWindow.windows.has id
# Helpers. # Helpers.
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
BrowserWindow::send = -> @webContents.send.apply @webContents, arguments BrowserWindow::send = -> @webContents.send.apply @webContents, arguments

View file

@ -0,0 +1,68 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "base/bind.h"
#include "base/supports_user_data.h"
namespace mate {
namespace {
const char* kTrackedObjectKey = "TrackedObjectKey";
class IDUserData : public base::SupportsUserData::Data {
public:
explicit IDUserData(int32_t id) : id_(id) {}
operator int32_t() const { return id_; }
private:
int32_t id_;
DISALLOW_COPY_AND_ASSIGN(IDUserData);
};
} // namespace
TrackableObjectBase::TrackableObjectBase()
: weak_map_id_(0), wrapped_(nullptr) {
}
TrackableObjectBase::~TrackableObjectBase() {
}
void TrackableObjectBase::AfterInit(v8::Isolate* isolate) {
if (wrapped_)
AttachAsUserData(wrapped_);
}
void TrackableObjectBase::AttachAsUserData(base::SupportsUserData* wrapped) {
if (weak_map_id_ != 0) {
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
wrapped_ = nullptr;
} else {
// If the TrackableObjectBase is not ready yet then delay SetUserData until
// AfterInit is called.
wrapped_ = wrapped;
}
}
// static
int32_t TrackableObjectBase::GetIDFromWrappedClass(base::SupportsUserData* w) {
auto id = static_cast<IDUserData*>(w->GetUserData(kTrackedObjectKey));
if (id)
return *id;
else
return 0;
}
// static
void TrackableObjectBase::RegisterDestructionCallback(void (*c)()) {
atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(base::Bind(c));
}
} // namespace mate

View file

@ -0,0 +1,116 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_TRACKABLE_OBJECT_H_
#define ATOM_BROWSER_API_TRACKABLE_OBJECT_H_
#include <vector>
#include "atom/browser/api/event_emitter.h"
#include "atom/common/id_weak_map.h"
namespace base {
class SupportsUserData;
}
namespace mate {
// Users should use TrackableObject instead.
class TrackableObjectBase : public mate::EventEmitter {
public:
TrackableObjectBase();
// The ID in weak map.
int32_t weak_map_id() const { return weak_map_id_; }
// Wrap TrackableObject into a class that SupportsUserData.
void AttachAsUserData(base::SupportsUserData* wrapped);
protected:
~TrackableObjectBase() override;
// mate::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
// Get the weak_map_id from SupportsUserData.
static int32_t GetIDFromWrappedClass(base::SupportsUserData* wrapped);
// Register a callback that should be destroyed before JavaScript environment
// gets destroyed.
static void RegisterDestructionCallback(void (*callback)());
int32_t weak_map_id_;
base::SupportsUserData* wrapped_;
private:
DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
};
// All instances of TrackableObject will be kept in a weak map and can be got
// from its ID.
template<typename T>
class TrackableObject : public TrackableObjectBase {
public:
// Finds out the TrackableObject from its ID in weak map.
static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) {
v8::MaybeLocal<v8::Object> object = weak_map_.Get(isolate, id);
if (object.IsEmpty())
return nullptr;
T* self = nullptr;
mate::ConvertFromV8(isolate, object.ToLocalChecked(), &self);
return self;
}
// Finds out the TrackableObject from the class it wraps.
static T* FromWrappedClass(v8::Isolate* isolate,
base::SupportsUserData* wrapped) {
int32_t id = GetIDFromWrappedClass(wrapped);
if (!id)
return nullptr;
return FromWeakMapID(isolate, id);
}
// Returns all objects in this class's weak map.
static std::vector<v8::Local<v8::Object>> GetAll(v8::Isolate* isolate) {
return weak_map_.Values(isolate);
}
TrackableObject() {
RegisterDestructionCallback(&TrackableObject<T>::ReleaseAllWeakReferences);
}
// Removes this instance from the weak map.
void RemoveFromWeakMap() {
if (weak_map_.Has(weak_map_id()))
weak_map_.Remove(weak_map_id());
}
protected:
~TrackableObject() override {
RemoveFromWeakMap();
}
void AfterInit(v8::Isolate* isolate) override {
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
TrackableObjectBase::AfterInit(isolate);
}
private:
// Releases all weak references in weak map, called when app is terminating.
static void ReleaseAllWeakReferences() {
weak_map_.Clear();
}
static atom::IDWeakMap weak_map_;
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
};
template<typename T>
atom::IDWeakMap TrackableObject<T>::weak_map_;
} // namespace mate
#endif // ATOM_BROWSER_API_TRACKABLE_OBJECT_H_

View file

@ -4,6 +4,7 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
@ -36,6 +37,8 @@ AtomBrowserMainParts::AtomBrowserMainParts()
} }
AtomBrowserMainParts::~AtomBrowserMainParts() { AtomBrowserMainParts::~AtomBrowserMainParts() {
for (const auto& callback : destruction_callbacks_)
callback.Run();
} }
// static // static
@ -44,6 +47,11 @@ AtomBrowserMainParts* AtomBrowserMainParts::Get() {
return self_; return self_;
} }
void AtomBrowserMainParts::RegisterDestructionCallback(
const base::Closure& callback) {
destruction_callbacks_.push_back(callback);
}
brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() { brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() {
return new AtomBrowserContext(); return new AtomBrowserContext();
} }

View file

@ -5,6 +5,9 @@
#ifndef ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_ #ifndef ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
#define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_ #define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
#include <list>
#include "base/callback.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "brightray/browser/browser_main_parts.h" #include "brightray/browser/browser_main_parts.h"
@ -24,6 +27,10 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
static AtomBrowserMainParts* Get(); static AtomBrowserMainParts* Get();
// Register a callback that should be destroyed before JavaScript environment
// gets destroyed.
void RegisterDestructionCallback(const base::Closure& callback);
Browser* browser() { return browser_.get(); } Browser* browser() { return browser_.get(); }
protected: protected:
@ -53,6 +60,9 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
base::Timer gc_timer_; base::Timer gc_timer_;
// List of callbacks should be executed before destroying JS env.
std::list<base::Closure> destruction_callbacks_;
static AtomBrowserMainParts* self_; static AtomBrowserMainParts* self_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts); DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);

View file

@ -48,17 +48,14 @@ ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, args...) ->
event.returnValue = createGuest event.sender, args... event.returnValue = createGuest event.sender, args...
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', (event, guestId) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', (event, guestId) ->
return unless BrowserWindow.windows.has guestId BrowserWindow.fromId(guestId)?.destroy()
BrowserWindow.windows.get(guestId).destroy()
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, method, args...) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, method, args...) ->
return unless BrowserWindow.windows.has guestId BrowserWindow.fromId(guestId)?[method] args...
BrowserWindow.windows.get(guestId)[method] args...
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin) ->
return unless BrowserWindow.windows.has guestId guestContents = BrowserWindow.fromId(guestId)?.webContents
guestContents = BrowserWindow.windows.get(guestId).webContents if guestContents?.getUrl().indexOf(targetOrigin) is 0 or targetOrigin is '*'
if guestContents.getUrl().indexOf(targetOrigin) is 0 or targetOrigin is '*'
guestContents.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', message, targetOrigin guestContents.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', message, targetOrigin
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, message, targetOrigin) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, message, targetOrigin) ->
@ -67,5 +64,4 @@ ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, mess
embedder.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', message, targetOrigin embedder.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', message, targetOrigin
ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) -> ipc.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) ->
return unless BrowserWindow.windows.has guestId BrowserWindow.fromId(guestId)?.webContents?[method] args...
BrowserWindow.windows.get(guestId).webContents?[method] args...

View file

@ -1,5 +1,5 @@
EventEmitter = require('events').EventEmitter EventEmitter = require('events').EventEmitter
IDWeakMap = require 'id-weak-map' IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
v8Util = process.atomBinding 'v8_util' v8Util = process.atomBinding 'v8_util'
# Class to reference all objects. # Class to reference all objects.

View file

@ -4,9 +4,6 @@
#include "atom/common/api/atom_api_id_weak_map.h" #include "atom/common/api/atom_api_id_weak_map.h"
#include <algorithm>
#include "base/logging.h"
#include "native_mate/constructor.h" #include "native_mate/constructor.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
@ -16,53 +13,37 @@ namespace atom {
namespace api { namespace api {
IDWeakMap::IDWeakMap() IDWeakMap::IDWeakMap() {
: next_id_(0) {
} }
IDWeakMap::~IDWeakMap() { IDWeakMap::~IDWeakMap() {
} }
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) { int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
int32_t key = GetNextID(); return map_.Add(isolate, object);
object->SetHiddenValue(mate::StringToV8(isolate, "IDWeakMapKey"),
mate::Converter<int32_t>::ToV8(isolate, key));
map_[key] = new mate::RefCountedPersistent<v8::Object>(isolate, object);
map_[key]->SetWeak(this, WeakCallback);
return key;
} }
v8::Local<v8::Value> IDWeakMap::Get(v8::Isolate* isolate, int32_t key) { v8::Local<v8::Value> IDWeakMap::Get(v8::Isolate* isolate, int32_t key) {
if (!Has(key)) { v8::MaybeLocal<v8::Object> result = map_.Get(isolate, key);
node::ThrowError(isolate, "Invalid key"); if (result.IsEmpty()) {
isolate->ThrowException(v8::Exception::Error(
mate::StringToV8(isolate, "Invalid key")));
return v8::Undefined(isolate); return v8::Undefined(isolate);
} else {
return result.ToLocalChecked();
} }
return map_[key]->NewHandle();
} }
bool IDWeakMap::Has(int32_t key) const { bool IDWeakMap::Has(int32_t key) const {
return map_.find(key) != map_.end(); return map_.Has(key);
} }
std::vector<int32_t> IDWeakMap::Keys() const { std::vector<int32_t> IDWeakMap::Keys() const {
std::vector<int32_t> keys; return map_.Keys();
keys.reserve(map_.size());
for (auto it = map_.begin(); it != map_.end(); ++it)
keys.push_back(it->first);
return keys;
} }
void IDWeakMap::Remove(int32_t key) { void IDWeakMap::Remove(int32_t key) {
if (Has(key)) map_.Remove(key);
map_.erase(key);
else
LOG(WARNING) << "Object with key " << key << " is being GCed for twice.";
}
int IDWeakMap::GetNextID() {
return ++next_id_;
} }
// static // static
@ -76,14 +57,6 @@ void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
.SetMethod("remove", &IDWeakMap::Remove); .SetMethod("remove", &IDWeakMap::Remove);
} }
// static
void IDWeakMap::WeakCallback(
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data) {
int32_t key = data.GetValue()->GetHiddenValue(
mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value();
data.GetParameter()->Remove(key);
}
} // namespace api } // namespace api
} // namespace atom } // namespace atom
@ -99,7 +72,7 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
isolate, isolate,
"IDWeakMap", "IDWeakMap",
base::Bind(&mate::NewOperatorFactory<IDWeakMap>)); base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
exports->Set(mate::StringToV8(isolate, "IDWeakMap"), constructor); exports->Set(mate::StringToSymbol(isolate, "IDWeakMap"), constructor);
} }
} // namespace } // namespace

View file

@ -6,11 +6,9 @@
#ifndef ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_ #ifndef ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_ #define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
#include <map>
#include <vector> #include <vector>
#include "base/basictypes.h" #include "atom/common/id_weak_map.h"
#include "native_mate/scoped_persistent.h"
#include "native_mate/wrappable.h" #include "native_mate/wrappable.h"
namespace atom { namespace atom {
@ -33,16 +31,8 @@ class IDWeakMap : public mate::Wrappable {
bool Has(int32_t key) const; bool Has(int32_t key) const;
std::vector<int32_t> Keys() const; std::vector<int32_t> Keys() const;
void Remove(int32_t key); void Remove(int32_t key);
int GetNextID();
static void WeakCallback( atom::IDWeakMap map_;
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
int32_t next_id_;
typedef scoped_refptr<mate::RefCountedPersistent<v8::Object> >
RefCountedV8Object;
std::map<int32_t, RefCountedV8Object> map_;
DISALLOW_COPY_AND_ASSIGN(IDWeakMap); DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
}; };

View file

@ -1,3 +0,0 @@
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
module.exports = IDWeakMap

View file

@ -0,0 +1,82 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/id_weak_map.h"
#include <utility>
#include "native_mate/converter.h"
namespace atom {
IDWeakMap::IDWeakMap() : next_id_(0) {
}
IDWeakMap::~IDWeakMap() {
}
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
int32_t id = GetNextID();
object->SetHiddenValue(mate::StringToSymbol(isolate, "IDWeakMapKey"),
mate::Converter<int32_t>::ToV8(isolate, id));
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
global->SetWeak(this, &WeakCallback);
map_.emplace(id, global);
return id;
}
v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
auto iter = map_.find(id);
if (iter == map_.end())
return v8::MaybeLocal<v8::Object>();
else
return v8::Local<v8::Object>::New(isolate, *iter->second);
}
bool IDWeakMap::Has(int32_t id) const {
return map_.find(id) != map_.end();
}
std::vector<int32_t> IDWeakMap::Keys() const {
std::vector<int32_t> keys;
keys.reserve(map_.size());
for (const auto& iter : map_)
keys.emplace_back(iter.first);
return keys;
}
std::vector<v8::Local<v8::Object>> IDWeakMap::Values(v8::Isolate* isolate) {
std::vector<v8::Local<v8::Object>> keys;
keys.reserve(map_.size());
for (const auto& iter : map_)
keys.emplace_back(v8::Local<v8::Object>::New(isolate, *iter.second));
return keys;
}
void IDWeakMap::Remove(int32_t id) {
auto iter = map_.find(id);
if (iter == map_.end())
LOG(WARNING) << "Removing unexist object with ID " << id;
else
map_.erase(iter);
}
void IDWeakMap::Clear() {
map_.clear();
}
int32_t IDWeakMap::GetNextID() {
return ++next_id_;
}
// static
void IDWeakMap::WeakCallback(
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data) {
int32_t id = data.GetValue()->GetHiddenValue(
mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value();
data.GetParameter()->Remove(id);
}
} // namespace atom

61
atom/common/id_weak_map.h Normal file
View file

@ -0,0 +1,61 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_ID_WEAK_MAP_H_
#define ATOM_COMMON_ID_WEAK_MAP_H_
#include <unordered_map>
#include <vector>
#include "base/memory/linked_ptr.h"
#include "v8/include/v8.h"
namespace atom {
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
class IDWeakMap {
public:
IDWeakMap();
~IDWeakMap();
// Adds |object| to WeakMap and returns its allocated |id|.
int32_t Add(v8::Isolate* isolate, v8::Local<v8::Object> object);
// Gets the object from WeakMap by its |id|.
v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, int32_t id);
// Whethere there is an object with |id| in this WeakMap.
bool Has(int32_t id) const;
// Returns IDs of all available objects.
std::vector<int32_t> Keys() const;
// Returns all objects.
std::vector<v8::Local<v8::Object>> Values(v8::Isolate* isolate);
// Remove object with |id| in the WeakMap.
void Remove(int32_t key);
// Clears the weak map.
void Clear();
private:
// Returns next available ID.
int32_t GetNextID();
static void WeakCallback(
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
// ID of next stored object.
int32_t next_id_;
// Map of stored objects.
std::unordered_map<int32_t, linked_ptr<v8::Global<v8::Object>>> map_;
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
};
} // namespace atom
#endif // ATOM_COMMON_ID_WEAK_MAP_H_

View file

@ -34,7 +34,6 @@
'atom/common/api/lib/callbacks-registry.coffee', 'atom/common/api/lib/callbacks-registry.coffee',
'atom/common/api/lib/clipboard.coffee', 'atom/common/api/lib/clipboard.coffee',
'atom/common/api/lib/crash-reporter.coffee', 'atom/common/api/lib/crash-reporter.coffee',
'atom/common/api/lib/id-weak-map.coffee',
'atom/common/api/lib/native-image.coffee', 'atom/common/api/lib/native-image.coffee',
'atom/common/api/lib/shell.coffee', 'atom/common/api/lib/shell.coffee',
'atom/common/lib/init.coffee', 'atom/common/lib/init.coffee',
@ -100,6 +99,8 @@
'atom/browser/api/event.h', 'atom/browser/api/event.h',
'atom/browser/api/event_emitter.cc', 'atom/browser/api/event_emitter.cc',
'atom/browser/api/event_emitter.h', 'atom/browser/api/event_emitter.h',
'atom/browser/api/trackable_object.cc',
'atom/browser/api/trackable_object.h',
'atom/browser/auto_updater.cc', 'atom/browser/auto_updater.cc',
'atom/browser/auto_updater.h', 'atom/browser/auto_updater.h',
'atom/browser/auto_updater_delegate.h', 'atom/browser/auto_updater_delegate.h',
@ -257,6 +258,8 @@
'atom/common/event_emitter_caller.cc', 'atom/common/event_emitter_caller.cc',
'atom/common/event_emitter_caller.h', 'atom/common/event_emitter_caller.h',
'atom/common/google_api_key.h', 'atom/common/google_api_key.h',
'atom/common/id_weak_map.cc',
'atom/common/id_weak_map.h',
'atom/common/linux/application_info.cc', 'atom/common/linux/application_info.cc',
'atom/common/native_mate_converters/accelerator_converter.cc', 'atom/common/native_mate_converters/accelerator_converter.cc',
'atom/common/native_mate_converters/accelerator_converter.h', 'atom/common/native_mate_converters/accelerator_converter.h',

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit cad1fa50a95ca4185c435846e4868d5bd6cc94df Subproject commit cc4e2fcd94b5a22e6720f0fba1c586a89640f1f6