use webcontents id to identify callbacks
This commit is contained in:
Родитель
3a154ab8ea
Коммит
2c59f4567e
|
@ -4,8 +4,8 @@ objectsRegistry = require './objects-registry.js'
|
||||||
v8Util = process.atomBinding 'v8_util'
|
v8Util = process.atomBinding 'v8_util'
|
||||||
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
|
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
|
||||||
|
|
||||||
# Weak reference to callback with their registry ID.
|
# Object mapping from webcontents id to their renderer callbacks weakmap.
|
||||||
rendererCallbacks = new IDWeakMap()
|
rendererRegistry = {}
|
||||||
|
|
||||||
# Convert a real value into meta data.
|
# Convert a real value into meta data.
|
||||||
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
||||||
|
@ -74,11 +74,18 @@ unwrapArgs = (sender, args) ->
|
||||||
returnValue = metaToValue meta.value
|
returnValue = metaToValue meta.value
|
||||||
-> returnValue
|
-> returnValue
|
||||||
when 'function'
|
when 'function'
|
||||||
|
webContentsId = sender.getId()
|
||||||
|
rendererCallbacks = rendererRegistry[webContentsId]
|
||||||
|
if not rendererCallbacks?
|
||||||
|
# Weak reference to callbacks with their ID
|
||||||
|
rendererCallbacks = new IDWeakMap()
|
||||||
|
rendererRegistry[webContentsId] = rendererCallbacks
|
||||||
|
|
||||||
if rendererCallbacks.has(meta.id)
|
if rendererCallbacks.has(meta.id)
|
||||||
return rendererCallbacks.get(meta.id)
|
return rendererCallbacks.get(meta.id)
|
||||||
|
|
||||||
rendererReleased = false
|
rendererReleased = false
|
||||||
objectsRegistry.once "clear-#{sender.getId()}", ->
|
objectsRegistry.once "clear-#{webContentsId}", ->
|
||||||
rendererReleased = true
|
rendererReleased = true
|
||||||
|
|
||||||
ret = ->
|
ret = ->
|
||||||
|
@ -109,6 +116,8 @@ callFunction = (event, func, caller, args) ->
|
||||||
|
|
||||||
# Send by BrowserWindow when its render view is deleted.
|
# Send by BrowserWindow when its render view is deleted.
|
||||||
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
|
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
|
||||||
|
if rendererRegistry.id?
|
||||||
|
delete rendererRegistry.id
|
||||||
objectsRegistry.clear id
|
objectsRegistry.clear id
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
|
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
|
||||||
|
|
|
@ -12,11 +12,11 @@ namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
IDWeakMap::IDWeakMap() {
|
IDWeakMap::IDWeakMap() : id_weak_map_(new atom::IDWeakMap) {
|
||||||
id_weak_map_.reset(new atom::IDWeakMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IDWeakMap::~IDWeakMap() {
|
IDWeakMap::~IDWeakMap() {
|
||||||
|
id_weak_map_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDWeakMap::Set(v8::Isolate* isolate,
|
void IDWeakMap::Set(v8::Isolate* isolate,
|
||||||
|
|
|
@ -22,7 +22,7 @@ class IDWeakMap : public mate::Wrappable {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
IDWeakMap();
|
IDWeakMap();
|
||||||
virtual ~IDWeakMap();
|
~IDWeakMap();
|
||||||
|
|
||||||
// mate::Wrappable:
|
// mate::Wrappable:
|
||||||
bool IsDestroyed() const override;
|
bool IsDestroyed() const override;
|
||||||
|
@ -34,7 +34,7 @@ class IDWeakMap : public mate::Wrappable {
|
||||||
bool Has(int32_t id);
|
bool Has(int32_t id);
|
||||||
void Remove(int32_t id);
|
void Remove(int32_t id);
|
||||||
|
|
||||||
scoped_ptr<atom::IDWeakMap> id_weak_map_;
|
atom::IDWeakMap* id_weak_map_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,8 +22,6 @@ class CallbacksRegistry
|
||||||
continue if location.indexOf('(native)') isnt -1
|
continue if location.indexOf('(native)') isnt -1
|
||||||
continue if location.indexOf('atom.asar') isnt -1
|
continue if location.indexOf('atom.asar') isnt -1
|
||||||
[x, filenameAndLine] = /([^/^\)]*)\)?$/gi.exec(location)
|
[x, filenameAndLine] = /([^/^\)]*)\)?$/gi.exec(location)
|
||||||
[x, line, column] = /(\d+):(\d+)/g.exec(filenameAndLine)
|
|
||||||
id += parseInt(line) + parseInt(column)
|
|
||||||
break
|
break
|
||||||
|
|
||||||
@callbacks[id] = callback
|
@callbacks[id] = callback
|
||||||
|
|
|
@ -32,15 +32,6 @@ IDWeakMap::IDWeakMap() : next_id_(0) {
|
||||||
IDWeakMap::~IDWeakMap() {
|
IDWeakMap::~IDWeakMap() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
|
||||||
int32_t id = GetNextID();
|
|
||||||
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
|
|
||||||
ObjectKey* key = new ObjectKey(id, this);
|
|
||||||
global->SetWeak(key, OnObjectGC, v8::WeakCallbackType::kParameter);
|
|
||||||
map_[id] = global;
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IDWeakMap::Set(v8::Isolate* isolate,
|
void IDWeakMap::Set(v8::Isolate* isolate,
|
||||||
int32_t id,
|
int32_t id,
|
||||||
v8::Local<v8::Object> object) {
|
v8::Local<v8::Object> object) {
|
||||||
|
@ -50,6 +41,12 @@ void IDWeakMap::Set(v8::Isolate* isolate,
|
||||||
map_[id] = global;
|
map_[id] = global;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
||||||
|
int32_t id = GetNextID();
|
||||||
|
Set(isolate, id, object);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
v8::MaybeLocal<v8::Object> IDWeakMap::Get(v8::Isolate* isolate, int32_t id) {
|
||||||
auto iter = map_.find(id);
|
auto iter = map_.find(id);
|
||||||
if (iter == map_.end())
|
if (iter == map_.end())
|
||||||
|
|
|
@ -19,12 +19,12 @@ class IDWeakMap {
|
||||||
IDWeakMap();
|
IDWeakMap();
|
||||||
~IDWeakMap();
|
~IDWeakMap();
|
||||||
|
|
||||||
// Adds |object| to WeakMap and returns its allocated |id|.
|
|
||||||
int32_t Add(v8::Isolate* isolate, v8::Local<v8::Object> object);
|
|
||||||
|
|
||||||
// Sets the object to WeakMap with the given |id|.
|
// Sets the object to WeakMap with the given |id|.
|
||||||
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
|
void Set(v8::Isolate* isolate, int32_t id, v8::Local<v8::Object> object);
|
||||||
|
|
||||||
|
// 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|.
|
// Gets the object from WeakMap by its |id|.
|
||||||
v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, int32_t id);
|
v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, int32_t id);
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче