From 1023b67d59f43ca6d92cf29d19c512fda6ccde6e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 24 Jun 2015 15:09:50 +0800 Subject: [PATCH] Make sure each C++ WebContents has only one JS WebContents --- atom/browser/api/atom_api_web_contents.cc | 18 ++++++++++++++++++ atom/browser/api/atom_api_web_contents.h | 1 + atom/browser/api/trackable_object.cc | 2 +- atom/browser/api/trackable_object.h | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index c64db2810ec5..0aace7a3f81f 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -784,6 +784,11 @@ bool WebContents::IsGuest() const { 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()) @@ -873,6 +878,13 @@ gfx::Size WebContents::GetDefaultSize() const { // static mate::Handle WebContents::CreateFrom( 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(existing)); + + // Otherwise create a new WebContents wrapper object. auto handle = mate::CreateHandle(isolate, new WebContents(web_contents)); g_wrap_web_contents.Run(handle.ToV8()); return handle; @@ -881,6 +893,12 @@ mate::Handle WebContents::CreateFrom( // static mate::Handle WebContents::CreateFrom( 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(existing)); + + // Otherwise create a new WebContents wrapper object. auto handle = mate::CreateHandle(isolate, new WebContents(web_contents)); g_wrap_web_contents.Run(handle.ToV8()); return handle; diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index d0179d7eb091..c109b1903af8 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -142,6 +142,7 @@ class WebContents : public mate::TrackableObject, ~WebContents(); // mate::Wrappable: + void AfterInit(v8::Isolate* isolate); mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; diff --git a/atom/browser/api/trackable_object.cc b/atom/browser/api/trackable_object.cc index 61f36fd904c4..417d5f6a170b 100644 --- a/atom/browser/api/trackable_object.cc +++ b/atom/browser/api/trackable_object.cc @@ -65,7 +65,7 @@ void TrackableObject::AfterInit(v8::Isolate* 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_)); } diff --git a/atom/browser/api/trackable_object.h b/atom/browser/api/trackable_object.h index b09fc35be8da..67e7ff45156c 100644 --- a/atom/browser/api/trackable_object.h +++ b/atom/browser/api/trackable_object.h @@ -39,7 +39,7 @@ class TrackableObject : public mate::EventEmitter { ~TrackableObject() override; // Wrap TrackableObject into a class that SupportsUserData. - void Attach(base::SupportsUserData* wrapped); + void AttachAsUserData(base::SupportsUserData* wrapped); private: static atom::IDWeakMap weak_map_;