2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-10-22 14:55:13 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-02-04 23:17:28 +00:00
|
|
|
#include "atom/browser/web_view_manager.h"
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-10-23 15:08:48 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2014-10-25 12:52:42 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2015-02-04 23:39:41 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-04-26 07:30:31 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-06-03 06:08:56 +00:00
|
|
|
WebViewManager* GetManagerFromWebContents(
|
|
|
|
const content::WebContents* web_contents) {
|
|
|
|
auto context = web_contents->GetBrowserContext();
|
2015-02-04 23:08:29 +00:00
|
|
|
if (!context)
|
2015-04-26 07:30:31 +00:00
|
|
|
return nullptr;
|
|
|
|
return static_cast<WebViewManager*>(context->GetGuestManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
2015-06-03 06:08:56 +00:00
|
|
|
bool WebViewManager::GetInfoForWebContents(
|
|
|
|
const content::WebContents* web_contents, WebViewInfo* info) {
|
|
|
|
auto manager = GetManagerFromWebContents(web_contents);
|
2015-06-03 05:45:06 +00:00
|
|
|
if (!manager)
|
|
|
|
return false;
|
|
|
|
base::AutoLock auto_lock(manager->lock_);
|
|
|
|
auto iter = manager->webview_info_map_.find(web_contents);
|
|
|
|
if (iter == manager->webview_info_map_.end())
|
|
|
|
return false;
|
|
|
|
*info = iter->second;
|
|
|
|
return true;
|
2015-02-04 23:08:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 14:55:13 +00:00
|
|
|
WebViewManager::WebViewManager(content::BrowserContext* context) {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebViewManager::~WebViewManager() {
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:08:48 +00:00
|
|
|
void WebViewManager::AddGuest(int guest_instance_id,
|
2014-12-09 22:38:43 +00:00
|
|
|
int element_instance_id,
|
2014-10-23 15:08:48 +00:00
|
|
|
content::WebContents* embedder,
|
2014-10-25 12:52:42 +00:00
|
|
|
content::WebContents* web_contents,
|
2015-02-04 23:52:59 +00:00
|
|
|
const WebViewInfo& info) {
|
2015-02-04 22:58:03 +00:00
|
|
|
base::AutoLock auto_lock(lock_);
|
2015-02-04 23:28:26 +00:00
|
|
|
web_contents_embdder_map_[guest_instance_id] = { web_contents, embedder };
|
2015-06-03 05:45:06 +00:00
|
|
|
webview_info_map_[web_contents] = info;
|
2014-12-09 22:38:43 +00:00
|
|
|
|
|
|
|
// Map the element in embedder to guest.
|
2015-04-21 10:56:08 +00:00
|
|
|
int owner_process_id = embedder->GetRenderProcessHost()->GetID();
|
|
|
|
ElementInstanceKey key(owner_process_id, element_instance_id);
|
2014-12-09 22:38:43 +00:00
|
|
|
element_instance_id_to_guest_map_[key] = guest_instance_id;
|
2014-10-23 15:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewManager::RemoveGuest(int guest_instance_id) {
|
2015-02-04 22:58:03 +00:00
|
|
|
base::AutoLock auto_lock(lock_);
|
2015-02-04 23:28:26 +00:00
|
|
|
if (!ContainsKey(web_contents_embdder_map_, guest_instance_id))
|
2014-12-15 19:22:14 +00:00
|
|
|
return;
|
|
|
|
|
2015-02-04 23:28:26 +00:00
|
|
|
auto web_contents = web_contents_embdder_map_[guest_instance_id].web_contents;
|
|
|
|
web_contents_embdder_map_.erase(guest_instance_id);
|
2015-06-03 05:45:06 +00:00
|
|
|
webview_info_map_.erase(web_contents);
|
2015-02-04 22:58:03 +00:00
|
|
|
|
2014-12-09 22:38:43 +00:00
|
|
|
// Remove the record of element in embedder too.
|
|
|
|
for (const auto& element : element_instance_id_to_guest_map_)
|
|
|
|
if (element.second == guest_instance_id) {
|
|
|
|
element_instance_id_to_guest_map_.erase(element.first);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-23 15:08:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:43:26 +00:00
|
|
|
content::WebContents* WebViewManager::GetGuestByInstanceID(
|
2015-04-21 10:56:08 +00:00
|
|
|
int owner_process_id,
|
2014-12-09 22:38:43 +00:00
|
|
|
int element_instance_id) {
|
2015-04-21 10:56:08 +00:00
|
|
|
ElementInstanceKey key(owner_process_id, element_instance_id);
|
2014-12-09 22:38:43 +00:00
|
|
|
if (!ContainsKey(element_instance_id_to_guest_map_, key))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
int guest_instance_id = element_instance_id_to_guest_map_[key];
|
2015-02-04 23:28:26 +00:00
|
|
|
if (ContainsKey(web_contents_embdder_map_, guest_instance_id))
|
|
|
|
return web_contents_embdder_map_[guest_instance_id].web_contents;
|
2014-10-23 15:08:48 +00:00
|
|
|
else
|
2014-12-07 15:43:26 +00:00
|
|
|
return nullptr;
|
2014-10-22 14:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebViewManager::ForEachGuest(content::WebContents* embedder_web_contents,
|
|
|
|
const GuestCallback& callback) {
|
2015-02-04 23:28:26 +00:00
|
|
|
for (auto& item : web_contents_embdder_map_)
|
2014-10-23 15:08:48 +00:00
|
|
|
if (item.second.embedder == embedder_web_contents &&
|
|
|
|
callback.Run(item.second.web_contents))
|
|
|
|
return true;
|
2014-10-22 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|