2017-08-04 00:55:19 +02:00
|
|
|
// Copyright 2017 the Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 2014 The Chromium 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/extension_function_details.h"
|
|
|
|
|
2019-03-22 23:11:51 +01:00
|
|
|
#include "libcef/browser/browser_context.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "libcef/browser/extensions/browser_extensions_util.h"
|
|
|
|
#include "libcef/browser/extensions/extension_system.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
2017-09-28 15:40:26 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "base/task/thread_pool.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
|
2017-09-28 15:40:26 +02:00
|
|
|
#include "chrome/browser/extensions/extension_tab_util.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "chrome/browser/profiles/profile.h"
|
2017-09-28 15:40:26 +02:00
|
|
|
#include "content/public/browser/favicon_status.h"
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "extensions/browser/extension_function.h"
|
|
|
|
#include "extensions/browser/extension_function_dispatcher.h"
|
|
|
|
#include "extensions/common/error_utils.h"
|
|
|
|
|
|
|
|
using content::RenderViewHost;
|
2019-03-13 22:27:37 +01:00
|
|
|
using content::WebContents;
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
|
|
|
namespace keys = extensions::tabs_constants;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefGetExtensionLoadFileCallbackImpl
|
|
|
|
: public CefGetExtensionResourceCallback {
|
|
|
|
public:
|
|
|
|
CefGetExtensionLoadFileCallbackImpl(
|
|
|
|
const std::string& file,
|
|
|
|
CefExtensionFunctionDetails::LoadFileCallback callback)
|
|
|
|
: file_(file), callback_(std::move(callback)) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefGetExtensionLoadFileCallbackImpl(
|
|
|
|
const CefGetExtensionLoadFileCallbackImpl&) = delete;
|
|
|
|
CefGetExtensionLoadFileCallbackImpl& operator=(
|
|
|
|
const CefGetExtensionLoadFileCallbackImpl&) = delete;
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
~CefGetExtensionLoadFileCallbackImpl() {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// The callback is still pending. Cancel it now.
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
RunNow(file_, std::move(callback_), nullptr);
|
|
|
|
} else {
|
2020-03-04 01:29:39 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(
|
|
|
|
&CefGetExtensionLoadFileCallbackImpl::RunNow,
|
|
|
|
file_, std::move(callback_), nullptr));
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue(CefRefPtr<CefStreamReader> stream) override {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// Always continue asynchronously.
|
2020-03-04 01:29:39 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(
|
|
|
|
&CefGetExtensionLoadFileCallbackImpl::RunNow,
|
|
|
|
file_, std::move(callback_), stream));
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(
|
|
|
|
&CefGetExtensionLoadFileCallbackImpl::Continue,
|
|
|
|
this, stream));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override { Continue(nullptr); }
|
|
|
|
|
|
|
|
void Disconnect() { callback_.Reset(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void RunNow(const std::string& file,
|
|
|
|
CefExtensionFunctionDetails::LoadFileCallback callback,
|
|
|
|
CefRefPtr<CefStreamReader> stream) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
std::move(callback).Run(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
base::ThreadPool::PostTaskAndReplyWithResult(
|
2017-08-04 00:55:19 +02:00
|
|
|
FROM_HERE,
|
2021-04-21 00:52:34 +02:00
|
|
|
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
|
2017-08-04 00:55:19 +02:00
|
|
|
base::BindOnce(LoadFileFromStream, file, stream), std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<std::string> LoadFileFromStream(
|
|
|
|
const std::string& file,
|
|
|
|
CefRefPtr<CefStreamReader> stream) {
|
2019-03-13 22:27:37 +01:00
|
|
|
CEF_REQUIRE_BLOCKING();
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
// Move to the end of the stream.
|
|
|
|
stream->Seek(0, SEEK_END);
|
2023-06-01 16:06:15 +02:00
|
|
|
const int64_t size = stream->Tell();
|
2017-08-04 00:55:19 +02:00
|
|
|
if (size == 0) {
|
|
|
|
LOG(WARNING) << "Extension resource " << file << " is empty.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<std::string> result(new std::string());
|
|
|
|
result->resize(size);
|
|
|
|
|
|
|
|
// Move to the beginning of the stream.
|
|
|
|
stream->Seek(0, SEEK_SET);
|
|
|
|
|
|
|
|
// Read all stream contents into the string.
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t read, offset = 0;
|
2017-08-04 00:55:19 +02:00
|
|
|
do {
|
|
|
|
read =
|
|
|
|
static_cast<int>(stream->Read(&(*result)[offset], 1, size - offset));
|
|
|
|
offset += read;
|
|
|
|
} while (read > 0 && offset < size);
|
|
|
|
|
|
|
|
if (offset != size) {
|
|
|
|
LOG(WARNING) << "Extension resource " << file << " read failed; expected "
|
|
|
|
<< size << ", got " << offset << " bytes.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string file_;
|
|
|
|
CefExtensionFunctionDetails::LoadFileCallback callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefGetExtensionLoadFileCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefExtensionFunctionDetails::CefExtensionFunctionDetails(
|
2019-10-01 15:55:16 +02:00
|
|
|
ExtensionFunction* function)
|
2017-08-04 00:55:19 +02:00
|
|
|
: function_(function) {}
|
|
|
|
|
|
|
|
CefExtensionFunctionDetails::~CefExtensionFunctionDetails() {}
|
|
|
|
|
|
|
|
Profile* CefExtensionFunctionDetails::GetProfile() const {
|
|
|
|
return Profile::FromBrowserContext(function_->browser_context());
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> CefExtensionFunctionDetails::GetSenderBrowser()
|
2017-08-04 00:55:19 +02:00
|
|
|
const {
|
|
|
|
content::WebContents* web_contents = function_->GetSenderWebContents();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (web_contents) {
|
2020-09-22 21:54:02 +02:00
|
|
|
return AlloyBrowserHostImpl::GetBrowserForContents(web_contents);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> CefExtensionFunctionDetails::GetCurrentBrowser()
|
2017-08-04 00:55:19 +02:00
|
|
|
const {
|
|
|
|
// Start with the browser hosting the extension.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetSenderBrowser();
|
2017-08-04 00:55:19 +02:00
|
|
|
if (browser && browser->client()) {
|
|
|
|
CefRefPtr<CefExtensionHandler> handler = GetCefExtension()->GetHandler();
|
|
|
|
if (handler) {
|
|
|
|
// Give the handler an opportunity to specify a different browser.
|
2018-06-08 18:53:10 +02:00
|
|
|
CefRefPtr<CefBrowser> active_browser =
|
|
|
|
handler->GetActiveBrowser(GetCefExtension(), browser.get(),
|
|
|
|
function_->include_incognito_information());
|
2017-08-04 00:55:19 +02:00
|
|
|
if (active_browser && active_browser != browser) {
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> active_browser_impl =
|
|
|
|
static_cast<AlloyBrowserHostImpl*>(active_browser.get());
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Make sure we're operating in the same CefBrowserContext.
|
|
|
|
if (CefBrowserContext::FromBrowserContext(
|
|
|
|
browser->GetBrowserContext()) ==
|
|
|
|
CefBrowserContext::FromBrowserContext(
|
2017-08-04 00:55:19 +02:00
|
|
|
active_browser_impl->GetBrowserContext())) {
|
|
|
|
browser = active_browser_impl;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Browser with tabId "
|
|
|
|
<< active_browser->GetIdentifier()
|
|
|
|
<< " cannot be accessed because is uses a different "
|
|
|
|
"CefRequestContext";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// May be null during startup/shutdown.
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefExtensionFunctionDetails::CanAccessBrowser(
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> target) const {
|
2017-08-04 00:55:19 +02:00
|
|
|
DCHECK(target);
|
|
|
|
|
|
|
|
// Start with the browser hosting the extension.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetSenderBrowser();
|
2017-08-04 00:55:19 +02:00
|
|
|
if (browser == target) {
|
|
|
|
// A sender can always access itself.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser && browser->client()) {
|
|
|
|
CefRefPtr<CefExtensionHandler> handler = GetCefExtension()->GetHandler();
|
|
|
|
if (handler) {
|
2018-06-08 18:53:10 +02:00
|
|
|
return handler->CanAccessBrowser(
|
|
|
|
GetCefExtension(), browser.get(),
|
|
|
|
function_->include_incognito_information(), target);
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to allowing access.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl>
|
2017-08-04 00:55:19 +02:00
|
|
|
CefExtensionFunctionDetails::GetBrowserForTabIdFirstTime(
|
|
|
|
int tab_id,
|
|
|
|
std::string* error_message) const {
|
|
|
|
DCHECK(!get_browser_called_first_time_);
|
|
|
|
get_browser_called_first_time_ = true;
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser;
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2017-09-28 15:40:26 +02:00
|
|
|
if (tab_id >= 0) {
|
2017-08-04 00:55:19 +02:00
|
|
|
// May be an invalid tabId or in the wrong BrowserContext.
|
|
|
|
browser = GetBrowserForTabId(tab_id, function_->browser_context());
|
|
|
|
if (!browser || !browser->web_contents() || !CanAccessBrowser(browser)) {
|
|
|
|
if (error_message) {
|
|
|
|
*error_message = ErrorUtils::FormatErrorMessage(
|
2019-06-05 16:15:45 +02:00
|
|
|
keys::kTabNotFoundError, base::NumberToString(tab_id));
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// May return NULL during shutdown.
|
|
|
|
browser = GetCurrentBrowser();
|
|
|
|
if (!browser || !browser->web_contents()) {
|
|
|
|
if (error_message) {
|
|
|
|
*error_message = keys::kNoCurrentWindowError;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl>
|
2017-08-04 00:55:19 +02:00
|
|
|
CefExtensionFunctionDetails::GetBrowserForTabIdAgain(
|
|
|
|
int tab_id,
|
|
|
|
std::string* error_message) const {
|
|
|
|
DCHECK_GE(tab_id, 0);
|
|
|
|
DCHECK(get_browser_called_first_time_);
|
|
|
|
|
|
|
|
// May return NULL during shutdown.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser =
|
2017-08-04 00:55:19 +02:00
|
|
|
GetBrowserForTabId(tab_id, function_->browser_context());
|
|
|
|
if (!browser || !browser->web_contents()) {
|
|
|
|
if (error_message) {
|
|
|
|
*error_message = ErrorUtils::FormatErrorMessage(
|
2019-06-05 16:15:45 +02:00
|
|
|
keys::kTabNotFoundError, base::NumberToString(tab_id));
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefExtensionFunctionDetails::LoadFile(const std::string& file,
|
|
|
|
LoadFileCallback callback) const {
|
|
|
|
// Start with the browser hosting the extension.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetSenderBrowser();
|
2017-08-04 00:55:19 +02:00
|
|
|
if (browser && browser->client()) {
|
|
|
|
CefRefPtr<CefExtensionHandler> handler = GetCefExtension()->GetHandler();
|
|
|
|
if (handler) {
|
|
|
|
CefRefPtr<CefGetExtensionLoadFileCallbackImpl> cef_callback(
|
|
|
|
new CefGetExtensionLoadFileCallbackImpl(file, std::move(callback)));
|
|
|
|
if (handler->GetExtensionResource(GetCefExtension(), browser.get(), file,
|
|
|
|
cef_callback)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
cef_callback->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-28 15:40:26 +02:00
|
|
|
CefExtensionFunctionDetails::OpenTabParams::OpenTabParams() {}
|
|
|
|
|
|
|
|
CefExtensionFunctionDetails::OpenTabParams::~OpenTabParams() {}
|
|
|
|
|
2022-09-26 21:30:45 +02:00
|
|
|
std::unique_ptr<api::tabs::Tab> CefExtensionFunctionDetails::OpenTab(
|
2017-09-28 15:40:26 +02:00
|
|
|
const OpenTabParams& params,
|
|
|
|
bool user_gesture,
|
|
|
|
std::string* error_message) const {
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> sender_browser = GetSenderBrowser();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!sender_browser) {
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// windowId defaults to "current" window.
|
|
|
|
int window_id = extension_misc::kCurrentWindowId;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.window_id.has_value()) {
|
2017-09-28 15:40:26 +02:00
|
|
|
window_id = *params.window_id;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// CEF doesn't have the concept of windows containing tab strips so we'll
|
|
|
|
// select an "active browser" for BrowserContext sharing instead.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> active_browser =
|
2017-09-28 15:40:26 +02:00
|
|
|
GetBrowserForTabIdFirstTime(window_id, error_message);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!active_browser) {
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// If an opener browser was specified then we expect it to exist.
|
|
|
|
int opener_browser_id = -1;
|
2022-09-26 21:30:45 +02:00
|
|
|
if (params.opener_tab_id.has_value() && *params.opener_tab_id >= 0) {
|
2017-09-28 15:40:26 +02:00
|
|
|
if (GetBrowserForTabIdAgain(*params.opener_tab_id, error_message)) {
|
|
|
|
opener_browser_id = *params.opener_tab_id;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GURL url;
|
2022-09-26 21:30:45 +02:00
|
|
|
if (params.url.has_value()) {
|
2023-02-27 19:52:38 +01:00
|
|
|
auto url_expected = ExtensionTabUtil::PrepareURLForNavigation(
|
2023-04-04 20:00:13 +02:00
|
|
|
*params.url, function()->extension(), function()->browser_context());
|
2023-02-27 19:52:38 +01:00
|
|
|
if (url_expected.has_value()) {
|
|
|
|
url = *url_expected;
|
|
|
|
} else {
|
|
|
|
*error_message = std::move(url_expected.error());
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to foreground for the new tab. The presence of 'active' property
|
|
|
|
// will override this default.
|
|
|
|
bool active = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.active.has_value()) {
|
2017-09-28 15:40:26 +02:00
|
|
|
active = *params.active;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// CEF doesn't use the index value but we let the client see/modify it.
|
|
|
|
int index = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.index.has_value()) {
|
2017-09-28 15:40:26 +02:00
|
|
|
index = *params.index;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
auto cef_browser_context = CefBrowserContext::FromBrowserContext(
|
|
|
|
active_browser->GetBrowserContext());
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// A CEF representation should always exist.
|
|
|
|
CefRefPtr<CefExtension> cef_extension =
|
2020-07-01 02:57:00 +02:00
|
|
|
cef_browser_context->GetExtension(function()->extension()->id());
|
2017-09-28 15:40:26 +02:00
|
|
|
DCHECK(cef_extension);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_extension) {
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// Always use the same request context that the extension was registered with.
|
|
|
|
// GetLoaderContext() will return NULL for internal extensions.
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
cef_extension->GetLoaderContext();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!request_context) {
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserCreateParams create_params;
|
2021-04-15 01:28:22 +02:00
|
|
|
create_params.url = url.spec();
|
2017-09-28 15:40:26 +02:00
|
|
|
create_params.request_context = request_context;
|
|
|
|
create_params.window_info.reset(new CefWindowInfo);
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-01-15 14:36:24 +01:00
|
|
|
create_params.window_info->SetAsPopup(nullptr, CefString());
|
2017-09-28 15:40:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Start with the active browser's settings.
|
|
|
|
create_params.client = active_browser->GetClient();
|
|
|
|
create_params.settings = active_browser->settings();
|
|
|
|
|
|
|
|
CefRefPtr<CefExtensionHandler> handler = cef_extension->GetHandler();
|
2022-09-26 21:30:45 +02:00
|
|
|
if (handler &&
|
2017-09-28 15:40:26 +02:00
|
|
|
handler->OnBeforeBrowser(cef_extension, sender_browser.get(),
|
2021-04-15 01:28:22 +02:00
|
|
|
active_browser.get(), index, create_params.url,
|
|
|
|
active, *create_params.window_info,
|
|
|
|
create_params.client, create_params.settings)) {
|
2017-09-28 15:40:26 +02:00
|
|
|
// Cancel the browser creation.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
if (active_browser->is_views_hosted()) {
|
2017-09-28 15:40:26 +02:00
|
|
|
// The new browser will also be Views hosted.
|
2022-04-08 22:48:56 +02:00
|
|
|
create_params.popup_with_views_hosted_opener = true;
|
2017-09-28 15:40:26 +02:00
|
|
|
create_params.window_info.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browser creation may fail under certain rare circumstances.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> new_browser =
|
|
|
|
AlloyBrowserHostImpl::Create(create_params);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!new_browser) {
|
2017-09-28 15:40:26 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
// Return data about the newly created tab.
|
2019-10-01 15:55:16 +02:00
|
|
|
auto extension = function()->extension();
|
|
|
|
auto web_contents = new_browser->web_contents();
|
2017-09-28 15:40:26 +02:00
|
|
|
auto result = CreateTabObject(new_browser, opener_browser_id, active, index);
|
2019-10-01 15:55:16 +02:00
|
|
|
auto scrub_tab_behavior = ExtensionTabUtil::GetScrubTabBehavior(
|
|
|
|
extension, extensions::Feature::Context::UNSPECIFIED_CONTEXT,
|
|
|
|
web_contents);
|
2022-09-26 21:30:45 +02:00
|
|
|
ExtensionTabUtil::ScrubTabForExtension(extension, web_contents, &result,
|
2019-11-12 17:11:44 +01:00
|
|
|
scrub_tab_behavior);
|
2022-09-26 21:30:45 +02:00
|
|
|
return base::WrapUnique(new api::tabs::Tab(std::move(result)));
|
2017-09-28 15:40:26 +02:00
|
|
|
}
|
|
|
|
|
2022-09-26 21:30:45 +02:00
|
|
|
api::tabs::Tab CefExtensionFunctionDetails::CreateTabObject(
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> new_browser,
|
2017-09-28 15:40:26 +02:00
|
|
|
int opener_browser_id,
|
|
|
|
bool active,
|
|
|
|
int index) const {
|
|
|
|
content::WebContents* contents = new_browser->web_contents();
|
|
|
|
|
|
|
|
bool is_loading = contents->IsLoading();
|
2022-09-26 21:30:45 +02:00
|
|
|
api::tabs::Tab tab_object;
|
|
|
|
tab_object.id = new_browser->GetIdentifier();
|
|
|
|
tab_object.index = index;
|
|
|
|
tab_object.window_id = *tab_object.id;
|
|
|
|
tab_object.status = is_loading ? api::tabs::TAB_STATUS_LOADING
|
|
|
|
: api::tabs::TAB_STATUS_COMPLETE;
|
|
|
|
tab_object.active = active;
|
|
|
|
tab_object.selected = true;
|
|
|
|
tab_object.highlighted = true;
|
|
|
|
tab_object.pinned = false;
|
2018-06-08 18:53:10 +02:00
|
|
|
// TODO(extensions): Use RecentlyAudibleHelper to populate |audible|.
|
2022-09-26 21:30:45 +02:00
|
|
|
tab_object.discarded = false;
|
|
|
|
tab_object.auto_discardable = false;
|
|
|
|
tab_object.muted_info = CreateMutedInfo(contents);
|
|
|
|
tab_object.incognito = false;
|
2017-09-28 15:40:26 +02:00
|
|
|
gfx::Size contents_size = contents->GetContainerBounds().size();
|
2022-09-26 21:30:45 +02:00
|
|
|
tab_object.width = contents_size.width();
|
|
|
|
tab_object.height = contents_size.height();
|
|
|
|
tab_object.url = contents->GetURL().spec();
|
|
|
|
tab_object.title = base::UTF16ToUTF8(contents->GetTitle());
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
|
|
|
|
if (entry && entry->GetFavicon().valid) {
|
2022-09-26 21:30:45 +02:00
|
|
|
tab_object.fav_icon_url = entry->GetFavicon().url.spec();
|
2017-09-28 15:40:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (opener_browser_id >= 0) {
|
2022-09-26 21:30:45 +02:00
|
|
|
tab_object.opener_tab_id = opener_browser_id;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-09-28 15:40:26 +02:00
|
|
|
|
|
|
|
return tab_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2022-09-26 21:30:45 +02:00
|
|
|
api::tabs::MutedInfo CefExtensionFunctionDetails::CreateMutedInfo(
|
|
|
|
content::WebContents* contents) {
|
2017-09-28 15:40:26 +02:00
|
|
|
DCHECK(contents);
|
2022-09-26 21:30:45 +02:00
|
|
|
api::tabs::MutedInfo info;
|
|
|
|
info.muted = contents->IsAudioMuted();
|
|
|
|
// TODO(cef): Maybe populate |info.reason|.
|
2017-09-28 15:40:26 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
CefRefPtr<CefExtension> CefExtensionFunctionDetails::GetCefExtension() const {
|
|
|
|
if (!cef_extension_) {
|
|
|
|
cef_extension_ =
|
2020-07-01 02:57:00 +02:00
|
|
|
CefBrowserContext::FromBrowserContext(function_->browser_context())
|
2017-08-04 00:55:19 +02:00
|
|
|
->GetExtension(function_->extension_id());
|
|
|
|
DCHECK(cef_extension_);
|
|
|
|
}
|
|
|
|
return cef_extension_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace extensions
|