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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/web_view_manager.h"
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2017-12-18 02:46:23 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2014-10-25 12:52:42 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2016-05-25 17:28:35 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/atom_browser_context.h"
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
WebViewManager::WebViewManager() = default;
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
WebViewManager::~WebViewManager() = default;
|
2014-10-22 14:55:13 +00:00
|
|
|
|
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,
|
2015-09-03 00:47:58 +00:00
|
|
|
content::WebContents* web_contents) {
|
2018-04-18 01:55:30 +00:00
|
|
|
web_contents_embedder_map_[guest_instance_id] = {web_contents, embedder};
|
2014-12-09 22:38:43 +00:00
|
|
|
|
|
|
|
// Map the element in embedder to guest.
|
2017-12-18 02:46:23 +00:00
|
|
|
int owner_process_id = embedder->GetMainFrame()->GetProcess()->GetID();
|
2015-04-21 10:56:08 +00:00
|
|
|
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) {
|
2019-08-28 14:39:21 +00:00
|
|
|
if (web_contents_embedder_map_.erase(guest_instance_id) == 0)
|
2014-12-15 19:22:14 +00:00
|
|
|
return;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-25 17:13:12 +00:00
|
|
|
content::WebContents* WebViewManager::GetEmbedder(int guest_instance_id) {
|
2019-08-28 14:39:21 +00:00
|
|
|
const auto iter = web_contents_embedder_map_.find(guest_instance_id);
|
|
|
|
return iter == std::end(web_contents_embedder_map_) ? nullptr
|
|
|
|
: iter->second.embedder;
|
2016-05-25 17:13:12 +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) {
|
2019-08-28 14:39:21 +00:00
|
|
|
const ElementInstanceKey key(owner_process_id, element_instance_id);
|
|
|
|
const auto guest_iter = element_instance_id_to_guest_map_.find(key);
|
|
|
|
if (guest_iter == std::end(element_instance_id_to_guest_map_))
|
2014-12-09 22:38:43 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2019-08-28 14:39:21 +00:00
|
|
|
const int guest_instance_id = guest_iter->second;
|
|
|
|
const auto iter = web_contents_embedder_map_.find(guest_instance_id);
|
|
|
|
return iter == std::end(web_contents_embedder_map_)
|
|
|
|
? nullptr
|
|
|
|
: iter->second.web_contents;
|
2014-10-22 14:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebViewManager::ForEachGuest(content::WebContents* embedder_web_contents,
|
|
|
|
const GuestCallback& callback) {
|
2019-11-07 15:39:48 +00:00
|
|
|
for (auto& item : web_contents_embedder_map_) {
|
|
|
|
if (item.second.embedder != embedder_web_contents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto* guest_web_contents = item.second.web_contents;
|
|
|
|
if (guest_web_contents && callback.Run(guest_web_contents))
|
2014-10-23 15:08:48 +00:00
|
|
|
return true;
|
2019-11-07 15:39:48 +00:00
|
|
|
}
|
2014-10-22 14:55:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-25 17:13:12 +00:00
|
|
|
// static
|
2016-05-25 17:20:00 +00:00
|
|
|
WebViewManager* WebViewManager::GetWebViewManager(
|
|
|
|
content::WebContents* web_contents) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* context = web_contents->GetBrowserContext();
|
2016-05-25 17:13:12 +00:00
|
|
|
if (context) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* manager = context->GetGuestManager();
|
2016-05-25 17:22:43 +00:00
|
|
|
return static_cast<WebViewManager*>(manager);
|
2016-05-25 17:13:12 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|