diff --git a/atom/browser/api/atom_api_web_view_manager.cc b/atom/browser/api/atom_api_web_view_manager.cc index bd5f47141f75..fa235293732b 100644 --- a/atom/browser/api/atom_api_web_view_manager.cc +++ b/atom/browser/api/atom_api_web_view_manager.cc @@ -51,18 +51,40 @@ struct Converter { namespace { +atom::WebViewManager* GetWebViewManager(content::WebContents* web_contents) { + auto context = web_contents->GetBrowserContext(); + if (context) { + auto manager = context->GetGuestManager(); + return static_cast(manager); + } + return nullptr; +} + +void AddGuest(int guest_instance_id, + int element_instance_id, + content::WebContents* embedder, + content::WebContents* guest_web_contents, + atom::WebViewManager::WebViewInfo info) { + auto manager = GetWebViewManager(embedder); + if (manager) { + info.guest_instance_id = guest_instance_id; + info.embedder = embedder; + manager->AddGuest(guest_instance_id, element_instance_id, embedder, + guest_web_contents, info); + } +} + +void RemoveGuest(content::WebContents* embedder, int guest_instance_id) { + auto manager = GetWebViewManager(embedder); + if (manager) + manager->RemoveGuest(guest_instance_id); +} + void Initialize(v8::Handle exports, v8::Handle unused, v8::Handle context, void* priv) { - using atom::WebViewManager; - auto manager = static_cast( - atom::AtomBrowserContext::Get()->GetGuestManager()); mate::Dictionary dict(context->GetIsolate(), exports); - dict.SetMethod("addGuest", - base::Bind(&WebViewManager::AddGuest, - base::Unretained(manager))); - dict.SetMethod("removeGuest", - base::Bind(&WebViewManager::RemoveGuest, - base::Unretained(manager))); + dict.SetMethod("addGuest", &AddGuest); + dict.SetMethod("removeGuest", &RemoveGuest); } } // namespace diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index 09f2d06ef2aa..d3ab506a0242 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -39,7 +39,7 @@ createGuest = (embedder, params) -> # Destroy guest when the embedder is gone or navigated. destroyEvents = ['destroyed', 'crashed', 'did-navigate-to-different-page'] destroy = -> - destroyGuest id if guestInstances[id]? + destroyGuest embedder, id if guestInstances[id]? embedder.once event, destroy for event in destroyEvents guest.once 'destroyed', -> embedder.removeListener event, destroy for event in destroyEvents @@ -90,7 +90,7 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) -> return unless oldGuestInstanceId != guestInstanceId return unless guestInstances[oldGuestInstanceId]? - destroyGuest oldGuestInstanceId + destroyGuest embedder, oldGuestInstanceId webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest, nodeIntegration: params.nodeintegration @@ -103,8 +103,8 @@ attachGuest = (embedder, elementInstanceId, guestInstanceId, params) -> reverseEmbedderElementsMap[guestInstanceId] = key # Destroy an existing guest instance. -destroyGuest = (id) -> - webViewManager.removeGuest id +destroyGuest = (embedder, id) -> + webViewManager.removeGuest embedder, id guestInstances[id].guest.destroy() delete guestInstances[id] @@ -120,7 +120,7 @@ ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_ATTACH_GUEST', (event, elementInstanceId, attachGuest event.sender, elementInstanceId, guestInstanceId, params ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', (event, id) -> - destroyGuest id + destroyGuest event.sender, id ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_SET_AUTO_SIZE', (event, id, params) -> guestInstances[id]?.guest.setAutoSize params.enableAutoSize, params.min, params.max diff --git a/atom/browser/web_view_manager.cc b/atom/browser/web_view_manager.cc index 78395da6ae1e..1fda008d0dc5 100644 --- a/atom/browser/web_view_manager.cc +++ b/atom/browser/web_view_manager.cc @@ -34,13 +34,11 @@ void WebViewManager::AddGuest(int guest_instance_id, int element_instance_id, content::WebContents* embedder, content::WebContents* web_contents, - WebViewInfo info) { + const WebViewInfo& info) { base::AutoLock auto_lock(lock_); web_contents_embdder_map_[guest_instance_id] = { web_contents, embedder }; int guest_process_id = web_contents->GetRenderProcessHost()->GetID(); - info.guest_instance_id = guest_instance_id; - info.embedder = embedder; webview_info_map_[guest_process_id] = info; // Map the element in embedder to guest. diff --git a/atom/browser/web_view_manager.h b/atom/browser/web_view_manager.h index ad7d1e4cb291..f87284c39e82 100644 --- a/atom/browser/web_view_manager.h +++ b/atom/browser/web_view_manager.h @@ -41,7 +41,7 @@ class WebViewManager : public content::BrowserPluginGuestManager { int element_instance_id, content::WebContents* embedder, content::WebContents* web_contents, - WebViewInfo info); + const WebViewInfo& info); void RemoveGuest(int guest_instance_id); // Looks up the information for the embedder for a given render