if (default_session_.IsEmpty()) {
auto browser_context = static_cast<AtomBrowserContext*>(
AtomBrowserMainParts::Get()->browser_context());
- auto handle = Session::Create(isolate, browser_context);
+ auto handle = Session::CreateFrom(isolate, browser_context);
default_session_.Reset(isolate, handle.ToV8());
}
return v8::Local<v8::Value>::New(isolate, default_session_);
Session::Session(AtomBrowserContext* browser_context)
: browser_context_(browser_context) {
+ AttachAsUserData(browser_context);
}
Session::~Session() {
}
// static
-mate::Handle<Session> Session::Create(
+mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate,
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));
}
#include <string>
+#include "atom/browser/api/trackable_object.h"
#include "base/callback.h"
#include "native_mate/handle.h"
-#include "native_mate/wrappable.h"
class GURL;
namespace api {
-class Session: public mate::Wrappable {
+class Session: public mate::TrackableObject {
public:
using ResolveProxyCallback = base::Callback<void(std::string)>;
- static mate::Handle<Session> Create(v8::Isolate* isolate,
- AtomBrowserContext* browser_context);
+ // Gets or creates Session from the |browser_context|.
+ static mate::Handle<Session> CreateFrom(
+ v8::Isolate* isolate, AtomBrowserContext* browser_context);
protected:
explicit Session(AtomBrowserContext* browser_context);
auto_size_enabled_(false),
is_full_page_plugin_(false),
inspectable_web_contents_(nullptr) {
+ AttachAsUserData(web_contents);
}
WebContents::WebContents(const mate::Dictionary& options)
if (options.Get("embedder", &embedder) && embedder)
owner_window = NativeWindow::FromWebContents(embedder->web_contents());
+ AttachAsUserData(web_contents);
InitWithWebContents(web_contents, owner_window);
inspectable_web_contents_ = managed_web_contents();
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
if (session_.IsEmpty()) {
- mate::Handle<api::Session> handle = Session::Create(
+ mate::Handle<api::Session> handle = Session::CreateFrom(
isolate,
static_cast<AtomBrowserContext*>(web_contents()->GetBrowserContext()));
session_.Reset(isolate, handle.ToV8());
return is_guest();
}
-void WebContents::AfterInit(v8::Isolate* isolate) {
- mate::TrackableObject::AfterInit(isolate);
- AttachAsUserData(web_contents());
-}
-
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
if (template_.IsEmpty())
~WebContents();
// mate::Wrappable:
- void AfterInit(v8::Isolate* isolate);
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
// Returns the default size of the guestview.
gfx::Size GetDefaultSize() const;
-
v8::Global<v8::Value> session_;
// Stores whether the contents of the guest can be transparent.
weak_map_.Clear();
}
-TrackableObject::TrackableObject() : weak_map_id_(0) {
+TrackableObject::TrackableObject() : weak_map_id_(0), wrapped_(nullptr) {
}
TrackableObject::~TrackableObject() {
void TrackableObject::AfterInit(v8::Isolate* isolate) {
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
+ if (wrapped_)
+ AttachAsUserData(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
static atom::IDWeakMap weak_map_;
int32_t weak_map_id_;
+ base::SupportsUserData* wrapped_;
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
};