Add mate::TrackableObject
This commit is contained in:
parent
15f350edcb
commit
7f0658efa7
7 changed files with 95 additions and 2 deletions
|
@ -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,
|
||||||
public content::BrowserPluginGuestDelegate,
|
public content::BrowserPluginGuestDelegate,
|
||||||
public CommonWebContentsDelegate,
|
public CommonWebContentsDelegate,
|
||||||
public content::WebContentsObserver,
|
public content::WebContentsObserver,
|
||||||
|
|
39
atom/browser/api/trackable_object.cc
Normal file
39
atom/browser/api/trackable_object.cc
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
atom::IDWeakMap TrackableObject::weak_map_;
|
||||||
|
|
||||||
|
// static
|
||||||
|
TrackableObject* TrackableObject::FromWeakMapID(v8::Isolate* isolate,
|
||||||
|
int32_t id) {
|
||||||
|
v8::MaybeLocal<v8::Object> object = weak_map_.Get(isolate, id);
|
||||||
|
if (object.IsEmpty())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
TrackableObject* self = nullptr;
|
||||||
|
mate::ConvertFromV8(isolate, object.ToLocalChecked(), &self);
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void TrackableObject::ReleaseAllWeakReferences() {
|
||||||
|
weak_map_.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
TrackableObject::TrackableObject() : weak_map_id_(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
TrackableObject::~TrackableObject() {
|
||||||
|
weak_map_.Remove(weak_map_id_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TrackableObject::AfterInit(v8::Isolate* isolate) {
|
||||||
|
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mate
|
43
atom/browser/api/trackable_object.h
Normal file
43
atom/browser/api/trackable_object.h
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// 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 "atom/browser/api/event_emitter.h"
|
||||||
|
#include "atom/common/id_weak_map.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
// All instances of TrackableObject will be kept in a weak map and can be got
|
||||||
|
// from its ID.
|
||||||
|
class TrackableObject : public mate::EventEmitter {
|
||||||
|
public:
|
||||||
|
// Find out the TrackableObject from its ID in weak map:
|
||||||
|
static TrackableObject* FromWeakMapID(v8::Isolate* isolate, int32_t id);
|
||||||
|
|
||||||
|
// Releases all weak references in weak map, called when app is terminating.
|
||||||
|
static void ReleaseAllWeakReferences();
|
||||||
|
|
||||||
|
// mate::Wrappable:
|
||||||
|
void AfterInit(v8::Isolate* isolate) override;
|
||||||
|
|
||||||
|
// The ID in weak map.
|
||||||
|
int32_t weak_map_id() const { return weak_map_id_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
TrackableObject();
|
||||||
|
~TrackableObject() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static atom::IDWeakMap weak_map_;
|
||||||
|
|
||||||
|
int32_t weak_map_id_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_API_TRACKABLE_OBJECT_H_
|
|
@ -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,7 @@ AtomBrowserMainParts::AtomBrowserMainParts()
|
||||||
}
|
}
|
||||||
|
|
||||||
AtomBrowserMainParts::~AtomBrowserMainParts() {
|
AtomBrowserMainParts::~AtomBrowserMainParts() {
|
||||||
|
mate::TrackableObject::ReleaseAllWeakReferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -55,6 +55,10 @@ void IDWeakMap::Remove(int32_t id) {
|
||||||
map_.erase(iter);
|
map_.erase(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IDWeakMap::Clear() {
|
||||||
|
map_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
int32_t IDWeakMap::GetNextID() {
|
int32_t IDWeakMap::GetNextID() {
|
||||||
return ++next_id_;
|
return ++next_id_;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,9 @@ class IDWeakMap {
|
||||||
// Remove object with |id| in the WeakMap.
|
// Remove object with |id| in the WeakMap.
|
||||||
void Remove(int32_t key);
|
void Remove(int32_t key);
|
||||||
|
|
||||||
|
// Clears the weak map.
|
||||||
|
void Clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Returns next available ID.
|
// Returns next available ID.
|
||||||
int32_t GetNextID();
|
int32_t GetNextID();
|
||||||
|
|
|
@ -96,6 +96,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',
|
||||||
|
|
Loading…
Reference in a new issue