2015-07-16 23:40:01 +02:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/extensions/browser_extensions_util.h"
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2019-03-22 23:11:51 +01:00
|
|
|
#include "libcef/browser/browser_context.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "libcef/browser/browser_host_base.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/browser_info_manager.h"
|
2015-10-21 20:17:09 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
2015-10-12 22:45:14 +02:00
|
|
|
#include "libcef/common/extensions/extensions_util.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "libcef/common/frame_util.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "libcef/features/runtime_checks.h"
|
2015-10-12 22:45:14 +02:00
|
|
|
|
2019-07-17 20:47:27 +02:00
|
|
|
#include "chrome/browser/browser_process.h"
|
|
|
|
#include "chrome/browser/printing/print_preview_dialog_controller.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
#include "content/browser/browser_plugin/browser_plugin_embedder.h"
|
|
|
|
#include "content/browser/browser_plugin/browser_plugin_guest.h"
|
|
|
|
#include "content/browser/web_contents/web_contents_impl.h"
|
2015-08-17 21:48:57 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/browser_plugin_guest_manager.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "extensions/browser/extension_registry.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
2015-08-17 21:48:57 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool InsertWebContents(std::vector<content::WebContents*>* vector,
|
|
|
|
content::WebContents* web_contents) {
|
|
|
|
vector->push_back(web_contents);
|
|
|
|
return false; // Continue iterating.
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 21:48:57 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
content::WebContents* GetFullPageGuestForOwnerContents(
|
|
|
|
content::WebContents* owner) {
|
2015-07-16 23:40:01 +02:00
|
|
|
content::WebContentsImpl* owner_impl =
|
|
|
|
static_cast<content::WebContentsImpl*>(owner);
|
2015-08-17 21:48:57 +02:00
|
|
|
content::BrowserPluginEmbedder* plugin_embedder =
|
|
|
|
owner_impl->GetBrowserPluginEmbedder();
|
|
|
|
if (plugin_embedder) {
|
|
|
|
content::BrowserPluginGuest* plugin_guest =
|
|
|
|
plugin_embedder->GetFullPageGuest();
|
|
|
|
if (plugin_guest)
|
|
|
|
return plugin_guest->web_contents();
|
|
|
|
}
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-08-17 21:48:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllGuestsForOwnerContents(content::WebContents* owner,
|
|
|
|
std::vector<content::WebContents*>* guests) {
|
|
|
|
content::BrowserPluginGuestManager* plugin_guest_manager =
|
|
|
|
owner->GetBrowserContext()->GetGuestManager();
|
2021-06-04 03:34:56 +02:00
|
|
|
plugin_guest_manager->ForEachGuest(
|
|
|
|
owner, base::BindRepeating(InsertWebContents, guests));
|
2015-08-17 21:48:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
content::WebContents* GetOwnerForGuestContents(content::WebContents* guest) {
|
|
|
|
content::WebContentsImpl* guest_impl =
|
|
|
|
static_cast<content::WebContentsImpl*>(guest);
|
|
|
|
content::BrowserPluginGuest* plugin_guest =
|
|
|
|
guest_impl->GetBrowserPluginGuest();
|
2020-04-02 23:26:14 +02:00
|
|
|
if (plugin_guest) {
|
2020-10-08 21:54:42 +02:00
|
|
|
return plugin_guest->owner_web_contents();
|
2020-04-02 23:26:14 +02:00
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
|
|
|
|
// Maybe it's a print preview dialog.
|
|
|
|
auto print_preview_controller =
|
|
|
|
g_browser_process->print_preview_dialog_controller();
|
|
|
|
return print_preview_controller->GetInitiator(guest);
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> GetOwnerBrowserForGlobalId(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2019-05-24 22:23:43 +02:00
|
|
|
bool* is_guest_view) {
|
2015-10-21 20:17:09 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
// Use the non-thread-safe but potentially faster approach.
|
2017-01-23 18:36:54 +01:00
|
|
|
content::RenderFrameHost* host =
|
2021-08-19 23:07:44 +02:00
|
|
|
content::RenderFrameHost::FromID(global_id);
|
2015-10-21 20:17:09 +02:00
|
|
|
if (host)
|
|
|
|
return GetOwnerBrowserForHost(host, is_guest_view);
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-10-21 20:17:09 +02:00
|
|
|
} else {
|
|
|
|
// Use the thread-safe approach.
|
|
|
|
scoped_refptr<CefBrowserInfo> info =
|
2021-08-19 23:07:44 +02:00
|
|
|
CefBrowserInfoManager::GetInstance()->GetBrowserInfo(global_id,
|
|
|
|
is_guest_view);
|
2015-10-21 20:17:09 +02:00
|
|
|
if (info.get()) {
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser = info->browser();
|
2015-10-21 20:17:09 +02:00
|
|
|
if (!browser.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
LOG(WARNING) << "Found browser id " << info->browser_id()
|
2021-08-19 23:07:44 +02:00
|
|
|
<< " but no browser object matching frame "
|
|
|
|
<< frame_util::GetFrameDebugString(global_id);
|
2015-10-21 20:17:09 +02:00
|
|
|
}
|
|
|
|
return browser;
|
|
|
|
}
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-10-21 20:17:09 +02:00
|
|
|
}
|
2015-10-12 22:45:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> GetOwnerBrowserForHost(
|
2015-10-21 20:17:09 +02:00
|
|
|
content::RenderViewHost* host,
|
|
|
|
bool* is_guest_view) {
|
|
|
|
if (is_guest_view)
|
|
|
|
*is_guest_view = false;
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser =
|
|
|
|
CefBrowserHostBase::GetBrowserForHost(host);
|
2015-10-12 22:45:14 +02:00
|
|
|
if (!browser.get() && ExtensionsEnabled()) {
|
|
|
|
// Retrieve the owner browser, if any.
|
|
|
|
content::WebContents* owner = GetOwnerForGuestContents(
|
|
|
|
content::WebContents::FromRenderViewHost(host));
|
2015-10-21 20:17:09 +02:00
|
|
|
if (owner) {
|
2020-09-18 00:24:08 +02:00
|
|
|
browser = CefBrowserHostBase::GetBrowserForContents(owner);
|
2015-10-21 20:17:09 +02:00
|
|
|
if (browser.get() && is_guest_view)
|
|
|
|
*is_guest_view = true;
|
|
|
|
}
|
2015-10-12 22:45:14 +02:00
|
|
|
}
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> GetOwnerBrowserForHost(
|
2017-01-23 18:36:54 +01:00
|
|
|
content::RenderFrameHost* host,
|
|
|
|
bool* is_guest_view) {
|
|
|
|
if (is_guest_view)
|
|
|
|
*is_guest_view = false;
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser =
|
|
|
|
CefBrowserHostBase::GetBrowserForHost(host);
|
2017-01-23 18:36:54 +01:00
|
|
|
if (!browser.get() && ExtensionsEnabled()) {
|
|
|
|
// Retrieve the owner browser, if any.
|
|
|
|
content::WebContents* owner = GetOwnerForGuestContents(
|
|
|
|
content::WebContents::FromRenderFrameHost(host));
|
|
|
|
if (owner) {
|
2020-09-18 00:24:08 +02:00
|
|
|
browser = CefBrowserHostBase::GetBrowserForContents(owner);
|
2017-01-23 18:36:54 +01:00
|
|
|
if (browser.get() && is_guest_view)
|
|
|
|
*is_guest_view = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> GetBrowserForTabId(
|
2017-08-04 00:55:19 +02:00
|
|
|
int tab_id,
|
|
|
|
content::BrowserContext* browser_context) {
|
2020-09-18 00:24:08 +02:00
|
|
|
REQUIRE_ALLOY_RUNTIME();
|
2017-08-04 00:55:19 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(browser_context);
|
2017-09-28 15:40:26 +02:00
|
|
|
if (tab_id < 0 || !browser_context)
|
2017-08-04 00:55:19 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
auto cef_browser_context =
|
|
|
|
CefBrowserContext::FromBrowserContext(browser_context);
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
for (const auto& browser_info :
|
|
|
|
CefBrowserInfoManager::GetInstance()->GetBrowserInfoList()) {
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> current_browser =
|
|
|
|
static_cast<AlloyBrowserHostImpl*>(browser_info->browser().get());
|
2017-08-04 00:55:19 +02:00
|
|
|
if (current_browser && current_browser->GetIdentifier() == tab_id) {
|
2020-07-01 02:57:00 +02:00
|
|
|
// Make sure we're operating in the same CefBrowserContext.
|
|
|
|
if (CefBrowserContext::FromBrowserContext(
|
|
|
|
current_browser->GetBrowserContext()) == cef_browser_context) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return current_browser;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Browser with tabId " << tab_id
|
|
|
|
<< " cannot be accessed because is uses a different "
|
|
|
|
"CefRequestContext";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Extension* GetExtensionForUrl(content::BrowserContext* browser_context,
|
|
|
|
const GURL& url) {
|
|
|
|
ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context);
|
|
|
|
if (!registry)
|
|
|
|
return nullptr;
|
|
|
|
std::string extension_id = url.host();
|
|
|
|
return registry->enabled_extensions().GetByID(extension_id);
|
|
|
|
}
|
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
} // namespace extensions
|