electron/shell/browser/web_view_manager.cc

87 lines
2.8 KiB
C++
Raw Normal View History

// 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"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
2016-05-25 17:28:35 +00:00
#include "content/public/browser/web_contents.h"
2014-10-22 14:55:13 +00:00
namespace atom {
2018-04-18 01:55:30 +00:00
WebViewManager::WebViewManager() {}
2014-10-22 14:55:13 +00:00
2018-04-18 01:55:30 +00:00
WebViewManager::~WebViewManager() {}
2014-10-22 14:55:13 +00:00
2014-10-23 15:08:48 +00:00
void WebViewManager::AddGuest(int guest_instance_id,
int element_instance_id,
2014-10-23 15:08:48 +00:00
content::WebContents* embedder,
content::WebContents* web_contents) {
2018-04-18 01:55:30 +00:00
web_contents_embedder_map_[guest_instance_id] = {web_contents, embedder};
// Map the element in embedder to guest.
int owner_process_id = embedder->GetMainFrame()->GetProcess()->GetID();
2015-04-21 10:56:08 +00:00
ElementInstanceKey key(owner_process_id, element_instance_id);
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) {
2016-11-30 07:30:03 +00:00
if (!base::ContainsKey(web_contents_embedder_map_, guest_instance_id))
return;
2015-08-06 15:01:05 +00:00
web_contents_embedder_map_.erase(guest_instance_id);
2015-02-04 22:58:03 +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
}
content::WebContents* WebViewManager::GetEmbedder(int guest_instance_id) {
2016-11-30 07:30:03 +00:00
if (base::ContainsKey(web_contents_embedder_map_, guest_instance_id))
return web_contents_embedder_map_[guest_instance_id].embedder;
else
return nullptr;
}
content::WebContents* WebViewManager::GetGuestByInstanceID(
2015-04-21 10:56:08 +00:00
int owner_process_id,
int element_instance_id) {
2015-04-21 10:56:08 +00:00
ElementInstanceKey key(owner_process_id, element_instance_id);
2016-11-30 07:30:03 +00:00
if (!base::ContainsKey(element_instance_id_to_guest_map_, key))
return nullptr;
int guest_instance_id = element_instance_id_to_guest_map_[key];
2016-11-30 07:30:03 +00:00
if (base::ContainsKey(web_contents_embedder_map_, guest_instance_id))
2015-08-06 15:01:05 +00:00
return web_contents_embedder_map_[guest_instance_id].web_contents;
2014-10-23 15:08:48 +00:00
else
return nullptr;
2014-10-22 14:55:13 +00:00
}
bool WebViewManager::ForEachGuest(content::WebContents* embedder_web_contents,
const GuestCallback& callback) {
2015-08-06 15:01:05 +00:00
for (auto& item : web_contents_embedder_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;
}
// static
2016-05-25 17:20:00 +00:00
WebViewManager* WebViewManager::GetWebViewManager(
content::WebContents* web_contents) {
auto* context = web_contents->GetBrowserContext();
if (context) {
auto* manager = context->GetGuestManager();
2016-05-25 17:22:43 +00:00
return static_cast<WebViewManager*>(manager);
} else {
return nullptr;
}
}
2014-10-22 14:55:13 +00:00
} // namespace atom