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"
|
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"
|
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_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"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "extensions/browser/extension_registry.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
namespace {
|
2015-08-17 21:48:57 +02:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
content::WebContents* GetOwnerForBrowserPluginGuest(
|
|
|
|
const content::WebContents* guest) {
|
|
|
|
auto* guest_impl = static_cast<const content::WebContentsImpl*>(guest);
|
2015-08-17 21:48:57 +02:00
|
|
|
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
|
|
|
}
|
2024-04-17 18:01:26 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
content::WebContents* GetInitiatorForPrintPreviewDialog(
|
|
|
|
const content::WebContents* guest) {
|
2019-07-17 20:47:27 +02:00
|
|
|
auto print_preview_controller =
|
|
|
|
g_browser_process->print_preview_dialog_controller();
|
2024-04-17 18:01:26 +02:00
|
|
|
return print_preview_controller->GetInitiator(
|
|
|
|
const_cast<content::WebContents*>(guest));
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
} // namespace
|
2015-10-12 22:45:14 +02:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
content::WebContents* GetOwnerForGuestContents(
|
|
|
|
const content::WebContents* guest) {
|
|
|
|
// Maybe it's a guest view. This occurs while loading the PDF viewer.
|
|
|
|
if (auto* owner = GetOwnerForBrowserPluginGuest(guest)) {
|
|
|
|
return owner;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-21 20:17:09 +02:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
// Maybe it's a print preview dialog. This occurs while loading the print
|
|
|
|
// preview dialog.
|
|
|
|
if (auto* initiator = GetInitiatorForPrintPreviewDialog(guest)) {
|
|
|
|
// Maybe the dialog is parented to a guest view. This occurs while loading
|
|
|
|
// the print preview dialog from inside the PDF viewer.
|
|
|
|
if (auto* owner = GetOwnerForBrowserPluginGuest(initiator)) {
|
|
|
|
return owner;
|
2015-10-21 20:17:09 +02:00
|
|
|
}
|
2024-04-17 18:01:26 +02:00
|
|
|
return initiator;
|
2015-10-12 22:45:14 +02:00
|
|
|
}
|
2024-04-17 18:01:26 +02:00
|
|
|
|
|
|
|
return nullptr;
|
2015-10-12 22:45:14 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
bool IsBrowserPluginGuest(const content::WebContents* web_contents) {
|
|
|
|
return !!GetOwnerForBrowserPluginGuest(web_contents);
|
|
|
|
}
|
2017-01-23 18:36:54 +01:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
bool IsPrintPreviewDialog(const content::WebContents* web_contents) {
|
|
|
|
return !!GetInitiatorForPrintPreviewDialog(web_contents);
|
2017-01-23 18:36:54 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (tab_id < 0 || !browser_context) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
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()) {
|
2024-04-17 18:01:26 +02:00
|
|
|
auto current_browser =
|
|
|
|
AlloyBrowserHostImpl::FromBaseChecked(browser_info->browser());
|
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);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!registry) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
std::string extension_id = url.host();
|
|
|
|
return registry->enabled_extensions().GetByID(extension_id);
|
|
|
|
}
|
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
} // namespace extensions
|