Make sure each C++ WebContents has only one JS WebContents
This commit is contained in:
parent
552a12d2ee
commit
1023b67d59
4 changed files with 21 additions and 2 deletions
|
@ -784,6 +784,11 @@ 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())
|
||||||
|
@ -873,6 +878,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 +893,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;
|
||||||
|
|
|
@ -142,6 +142,7 @@ 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;
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ void TrackableObject::AfterInit(v8::Isolate* isolate) {
|
||||||
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TrackableObject::Attach(base::SupportsUserData* wrapped) {
|
void TrackableObject::AttachAsUserData(base::SupportsUserData* wrapped) {
|
||||||
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
|
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ class TrackableObject : public mate::EventEmitter {
|
||||||
~TrackableObject() override;
|
~TrackableObject() override;
|
||||||
|
|
||||||
// Wrap TrackableObject into a class that SupportsUserData.
|
// Wrap TrackableObject into a class that SupportsUserData.
|
||||||
void Attach(base::SupportsUserData* wrapped);
|
void AttachAsUserData(base::SupportsUserData* wrapped);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static atom::IDWeakMap weak_map_;
|
static atom::IDWeakMap weak_map_;
|
||||||
|
|
Loading…
Add table
Reference in a new issue