Make Session trackable

This commit is contained in:
Cheng Zhao 2015-06-24 15:37:04 +08:00
parent 1023b67d59
commit 0ebd4d04ad
7 changed files with 27 additions and 16 deletions

View file

@ -176,7 +176,7 @@ v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
if (default_session_.IsEmpty()) { if (default_session_.IsEmpty()) {
auto browser_context = static_cast<AtomBrowserContext*>( auto browser_context = static_cast<AtomBrowserContext*>(
AtomBrowserMainParts::Get()->browser_context()); AtomBrowserMainParts::Get()->browser_context());
auto handle = Session::Create(isolate, browser_context); auto handle = Session::CreateFrom(isolate, browser_context);
default_session_.Reset(isolate, handle.ToV8()); default_session_.Reset(isolate, handle.ToV8());
} }
return v8::Local<v8::Value>::New(isolate, default_session_); return v8::Local<v8::Value>::New(isolate, default_session_);

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 {
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();
@ -608,7 +610,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());
@ -784,11 +786,6 @@ bool WebContents::IsGuest() const {
return is_guest(); return is_guest();
} }
void WebContents::AfterInit(v8::Isolate* isolate) {
mate::TrackableObject::AfterInit(isolate);
AttachAsUserData(web_contents());
}
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
if (template_.IsEmpty()) if (template_.IsEmpty())

View file

@ -142,7 +142,6 @@ class WebContents : public mate::TrackableObject,
~WebContents(); ~WebContents();
// mate::Wrappable: // mate::Wrappable:
void AfterInit(v8::Isolate* isolate);
mate::ObjectTemplateBuilder GetObjectTemplateBuilder( mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override; v8::Isolate* isolate) override;
@ -241,7 +240,6 @@ class WebContents : public mate::TrackableObject,
// 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

@ -54,7 +54,7 @@ void TrackableObject::ReleaseAllWeakReferences() {
weak_map_.Clear(); weak_map_.Clear();
} }
TrackableObject::TrackableObject() : weak_map_id_(0) { TrackableObject::TrackableObject() : weak_map_id_(0), wrapped_(nullptr) {
} }
TrackableObject::~TrackableObject() { TrackableObject::~TrackableObject() {
@ -63,10 +63,19 @@ TrackableObject::~TrackableObject() {
void TrackableObject::AfterInit(v8::Isolate* isolate) { void TrackableObject::AfterInit(v8::Isolate* isolate) {
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate)); weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
if (wrapped_)
AttachAsUserData(wrapped_);
} }
void TrackableObject::AttachAsUserData(base::SupportsUserData* wrapped) { void TrackableObject::AttachAsUserData(base::SupportsUserData* wrapped) {
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_)); if (weak_map_id_ != 0) {
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
wrapped_ = nullptr;
} else {
// If the TrackableObject is not ready yet then delay SetUserData until
// AfterInit is called.
wrapped_ = wrapped;
}
} }
} // namespace mate } // namespace mate

View file

@ -45,6 +45,7 @@ class TrackableObject : public mate::EventEmitter {
static atom::IDWeakMap weak_map_; static atom::IDWeakMap weak_map_;
int32_t weak_map_id_; int32_t weak_map_id_;
base::SupportsUserData* wrapped_;
DISALLOW_COPY_AND_ASSIGN(TrackableObject); DISALLOW_COPY_AND_ASSIGN(TrackableObject);
}; };