2020-09-18 00:24:08 +02:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/chrome/chrome_browser_host_impl.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/notreached.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_platform_delegate.h"
|
|
|
|
#include "cef/libcef/browser/chrome/browser_platform_delegate_chrome.h"
|
|
|
|
#include "cef/libcef/browser/chrome/views/chrome_browser_frame.h"
|
|
|
|
#include "cef/libcef/browser/chrome/views/chrome_browser_view.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/browser/views/browser_view_impl.h"
|
|
|
|
#include "cef/libcef/common/net/url_util.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "chrome/browser/profiles/profile.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "chrome/browser/ui/browser_commands.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "chrome/browser/ui/browser_navigator.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "chrome/browser/ui/browser_tabstrip.h"
|
|
|
|
#include "chrome/browser/ui/browser_window.h"
|
|
|
|
#include "chrome/browser/ui/tabs/tab_strip_model.h"
|
2023-03-06 21:42:12 +01:00
|
|
|
#include "chrome/browser/ui/views/frame/contents_web_view.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "chrome/common/pref_names.h"
|
2021-02-18 02:58:25 +01:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::Create(
|
2020-09-25 03:40:47 +02:00
|
|
|
const CefBrowserCreateParams& params) {
|
2023-11-14 18:16:43 +01:00
|
|
|
auto browser = CreateBrowser(params, std::nullopt);
|
2021-02-18 02:58:25 +01:00
|
|
|
|
2021-04-15 01:28:22 +02:00
|
|
|
GURL url = url_util::MakeGURL(params.url, /*fixup=*/true);
|
2024-11-05 20:58:45 +01:00
|
|
|
const bool url_is_empty = url.is_empty();
|
|
|
|
if (url_is_empty) {
|
2020-09-25 03:40:47 +02:00
|
|
|
// Chrome will navigate to kChromeUINewTabURL by default. We want to keep
|
|
|
|
// the current CEF behavior of not navigating at all. Use a special URL that
|
|
|
|
// will be recognized in HandleNonNavigationAboutURL.
|
|
|
|
url = GURL("chrome://ignore/");
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// Add a new tab. This will indirectly create a new tab WebContents and
|
2020-09-25 03:40:47 +02:00
|
|
|
// call ChromeBrowserDelegate::OnWebContentsCreated to create the associated
|
2020-09-18 00:24:08 +02:00
|
|
|
// ChromeBrowserHostImpl.
|
2021-04-02 22:53:17 +02:00
|
|
|
chrome::AddTabAt(browser, url, /*index=*/TabStripModel::kNoTab,
|
|
|
|
/*foreground=*/true);
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// The new tab WebContents.
|
|
|
|
auto web_contents = browser->tab_strip_model()->GetActiveWebContents();
|
2020-09-25 03:40:47 +02:00
|
|
|
CHECK(web_contents);
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// The associated ChromeBrowserHostImpl.
|
|
|
|
auto browser_host =
|
|
|
|
ChromeBrowserHostImpl::GetBrowserForContents(web_contents);
|
2020-09-25 03:40:47 +02:00
|
|
|
CHECK(browser_host);
|
|
|
|
|
2024-11-05 20:58:45 +01:00
|
|
|
if (!url_is_empty) {
|
|
|
|
// Match Alloy-style behavior of requesting focus after initial navigation.
|
|
|
|
browser_host->OnSetFocus(FOCUS_SOURCE_NAVIGATION);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
return browser_host;
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::FromBaseChecked(
|
|
|
|
CefRefPtr<CefBrowserHostBase> host_base) {
|
|
|
|
if (!host_base) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
CHECK(host_base->IsChromeStyle());
|
|
|
|
return static_cast<ChromeBrowserHostImpl*>(host_base.get());
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::GetBrowserForHost(
|
|
|
|
const content::RenderViewHost* host) {
|
2024-04-17 18:01:26 +02:00
|
|
|
return FromBaseChecked(CefBrowserHostBase::GetBrowserForHost(host));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::GetBrowserForHost(
|
|
|
|
const content::RenderFrameHost* host) {
|
2024-04-17 18:01:26 +02:00
|
|
|
return FromBaseChecked(CefBrowserHostBase::GetBrowserForHost(host));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::GetBrowserForContents(
|
|
|
|
const content::WebContents* contents) {
|
2024-04-17 18:01:26 +02:00
|
|
|
return FromBaseChecked(CefBrowserHostBase::GetBrowserForContents(contents));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::GetBrowserForGlobalId(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id) {
|
2024-04-17 18:01:26 +02:00
|
|
|
return FromBaseChecked(CefBrowserHostBase::GetBrowserForGlobalId(global_id));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 22:22:07 +01:00
|
|
|
// static
|
|
|
|
CefRefPtr<ChromeBrowserHostImpl> ChromeBrowserHostImpl::GetBrowserForBrowser(
|
|
|
|
const Browser* browser) {
|
2024-01-12 00:32:08 +01:00
|
|
|
// Return the ChromeBrowserHostImpl that is currently active.
|
|
|
|
// Views-hosted Browsers will contain a single ChromeBrowserHostImpl.
|
|
|
|
// Otherwise, there will be a ChromeBrowserHostImpl per Tab/WebContents.
|
|
|
|
// |contents| may be nullptr during Browser initialization or destruction.
|
|
|
|
auto contents = browser->tab_strip_model()->GetActiveWebContents();
|
|
|
|
if (!contents) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return GetBrowserForContents(contents);
|
2022-03-21 22:22:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
ChromeBrowserHostImpl::~ChromeBrowserHostImpl() = default;
|
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
void ChromeBrowserHostImpl::AddNewContents(
|
2023-11-14 18:16:43 +01:00
|
|
|
std::unique_ptr<content::WebContents> contents,
|
|
|
|
std::optional<Browser::CreateParams> browser_create_params) {
|
2021-04-02 22:53:17 +02:00
|
|
|
DCHECK(contents);
|
|
|
|
DCHECK(!browser_);
|
|
|
|
|
|
|
|
// We should already be associated with the WebContents.
|
|
|
|
DCHECK_EQ(GetWebContents(), contents.get());
|
|
|
|
|
|
|
|
CefBrowserCreateParams params;
|
|
|
|
params.request_context = request_context();
|
|
|
|
params.browser_view = GetBrowserView();
|
|
|
|
|
|
|
|
// Create the new Browser representation.
|
2023-11-14 18:16:43 +01:00
|
|
|
auto browser = CreateBrowser(params, std::move(browser_create_params));
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
// Add the WebContents to the Browser.
|
|
|
|
browser->tab_strip_model()->AddWebContents(
|
|
|
|
std::move(contents), /*index=*/TabStripModel::kNoTab,
|
|
|
|
ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL,
|
2022-08-23 03:37:40 +02:00
|
|
|
AddTabTypes::ADD_ACTIVE);
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
SetBrowser(browser);
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
void ChromeBrowserHostImpl::OnWebContentsDestroyed(
|
|
|
|
content::WebContents* web_contents) {
|
2024-05-13 23:36:09 +02:00
|
|
|
DestroyWebContents(web_contents);
|
2024-04-23 22:06:00 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
if (!is_destroying_browser_) {
|
2024-04-23 22:06:00 +02:00
|
|
|
// Destroy the browser asynchronously to allow the current call stack
|
|
|
|
// to unwind (we may have been called via the TabStripModel owned by the
|
|
|
|
// Browser).
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&ChromeBrowserHostImpl::DestroyBrowser, this));
|
2023-10-13 19:48:01 +02:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::OnSetFocus(cef_focus_source_t source) {
|
2020-09-25 03:40:47 +02:00
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(&ChromeBrowserHostImpl::OnSetFocus,
|
|
|
|
this, source));
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2024-07-18 19:50:37 +02:00
|
|
|
if (contents_delegate_.OnSetFocus(source)) {
|
2020-09-25 03:40:47 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (platform_delegate_) {
|
2022-04-08 22:48:56 +02:00
|
|
|
platform_delegate_->SetFocus(true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-04-08 22:48:56 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (browser_) {
|
|
|
|
const int tab_index = GetCurrentTabIndex();
|
|
|
|
if (tab_index != TabStripModel::kNoTab) {
|
|
|
|
chrome::SelectNumberedTab(browser_, tab_index);
|
|
|
|
}
|
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::CloseBrowser(bool force_close) {
|
2024-10-17 17:05:30 +02:00
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(&ChromeBrowserHostImpl::CloseBrowser,
|
|
|
|
this, force_close));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!force_close) {
|
|
|
|
TryCloseBrowser();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Always do this asynchronously because TabStripModel is not re-entrant.
|
2024-10-17 17:05:30 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&ChromeBrowserHostImpl::DoCloseBrowser, this));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChromeBrowserHostImpl::TryCloseBrowser() {
|
2024-10-17 17:05:30 +02:00
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
DCHECK(false) << "called on invalid thread";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* web_contents = GetWebContents()) {
|
|
|
|
// This check works as follows:
|
|
|
|
// 1. Returns false if the main frame is ready to close
|
|
|
|
// (IsPageReadyToBeClosed returns true).
|
|
|
|
// 2. Otherwise returns true if any frame in the frame tree needs to run
|
|
|
|
// beforeunload or unload-time event handlers.
|
|
|
|
// 3. Otherwise returns false.
|
|
|
|
if (web_contents->NeedToFireBeforeUnloadOrUnloadEvents()) {
|
|
|
|
// Will result in a call to Browser::BeforeUnloadFired and, if the close
|
|
|
|
// isn't canceled, Browser::CloseContents which indirectly calls
|
|
|
|
// TabStripModel::CloseWebContentsAt (similar to DoCloseBrowser but
|
|
|
|
// without CLOSE_USER_GESTURE). Additional calls to DispatchBeforeUnload
|
|
|
|
// while the unload is pending will be ignored.
|
|
|
|
web_contents->DispatchBeforeUnload(/*auto_cancel=*/false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseBrowser(/*force_close=*/true);
|
2021-02-18 02:58:25 +01:00
|
|
|
return true;
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefWindowHandle ChromeBrowserHostImpl::GetWindowHandle() {
|
2022-04-08 22:48:56 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
// Always return the most up-to-date window handle for a views-hosted
|
|
|
|
// browser since it may change if the view is re-parented.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (platform_delegate_) {
|
2022-04-08 22:48:56 +02:00
|
|
|
return platform_delegate_->GetHostWindowHandle();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-04-08 22:48:56 +02:00
|
|
|
}
|
2022-04-19 19:45:03 +02:00
|
|
|
return host_window_handle_;
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefWindowHandle ChromeBrowserHostImpl::GetOpenerWindowHandle() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return kNullWindowHandle;
|
|
|
|
}
|
|
|
|
|
2022-02-17 19:17:29 +01:00
|
|
|
void ChromeBrowserHostImpl::Find(const CefString& searchText,
|
2020-09-18 00:24:08 +02:00
|
|
|
bool forward,
|
|
|
|
bool matchCase,
|
|
|
|
bool findNext) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::StopFinding(bool clearSelection) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::WasResized() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::WasHidden(bool hidden) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::NotifyScreenInfoChanged() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::Invalidate(PaintElementType type) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SendExternalBeginFrame() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SendTouchEvent(const CefTouchEvent& event) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SendCaptureLostEvent() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChromeBrowserHostImpl::GetWindowlessFrameRate() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SetWindowlessFrameRate(int frame_rate) {}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::ImeSetComposition(
|
|
|
|
const CefString& text,
|
|
|
|
const std::vector<CefCompositionUnderline>& underlines,
|
|
|
|
const CefRange& replacement_range,
|
|
|
|
const CefRange& selection_range) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::ImeCommitText(const CefString& text,
|
|
|
|
const CefRange& replacement_range,
|
|
|
|
int relative_cursor_pos) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
void ChromeBrowserHostImpl::ImeFinishComposingText(bool keep_selection) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::ImeCancelComposition() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragTargetDragEnter(
|
|
|
|
CefRefPtr<CefDragData> drag_data,
|
|
|
|
const CefMouseEvent& event,
|
|
|
|
DragOperationsMask allowed_ops) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragTargetDragOver(const CefMouseEvent& event,
|
|
|
|
DragOperationsMask allowed_ops) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragTargetDragLeave() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragTargetDrop(const CefMouseEvent& event) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragSourceSystemDragEnded() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::DragSourceEndedAt(int x,
|
|
|
|
int y,
|
|
|
|
DragOperationsMask op) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SetAudioMuted(bool mute) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChromeBrowserHostImpl::IsAudioMuted() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SetAutoResizeEnabled(bool enabled,
|
|
|
|
const CefSize& min_size,
|
|
|
|
const CefSize& max_size) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:16:43 +01:00
|
|
|
bool ChromeBrowserHostImpl::CanExecuteChromeCommand(int command_id) {
|
|
|
|
// Verify that this method is being called on the UI thread.
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
DCHECK(false) << "called on invalid thread";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_) {
|
|
|
|
return chrome::SupportsCommand(browser_, command_id) &&
|
|
|
|
chrome::IsCommandEnabled(browser_, command_id);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::ExecuteChromeCommand(
|
|
|
|
int command_id,
|
|
|
|
cef_window_open_disposition_t disposition) {
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&ChromeBrowserHostImpl::ExecuteChromeCommand,
|
|
|
|
this, command_id, disposition));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_) {
|
|
|
|
chrome::ExecuteCommandWithDisposition(
|
|
|
|
browser_, command_id, static_cast<WindowOpenDisposition>(disposition));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:28:27 +02:00
|
|
|
ChromeBrowserView* ChromeBrowserHostImpl::chrome_browser_view() const {
|
|
|
|
if (browser_ && is_views_hosted_) {
|
|
|
|
return static_cast<ChromeBrowserView*>(browser_->window());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
bool ChromeBrowserHostImpl::Navigate(const content::OpenURLParams& params) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (GetCurrentTabIndex() == TabStripModel::kNoTab) {
|
|
|
|
// We can't navigate via the Browser because we don't have a current tab.
|
|
|
|
return CefBrowserHostBase::Navigate(params);
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (browser_) {
|
2021-04-15 01:28:22 +02:00
|
|
|
GURL gurl = params.url;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!url_util::FixupGURL(gurl)) {
|
2021-02-28 21:23:42 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-02-28 21:23:42 +01:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// This is generally equivalent to calling Browser::OpenURL, except:
|
|
|
|
// 1. It doesn't trigger a call to CefRequestHandler::OnOpenURLFromTab, and
|
|
|
|
// 2. It navigates in this CefBrowserHost's WebContents instead of
|
|
|
|
// (a) creating a new WebContents, or (b) using the Browser's active
|
|
|
|
// WebContents (which may not be the same), and
|
|
|
|
// 3. There is no risk of triggering chrome's popup blocker.
|
2021-02-28 21:23:42 +01:00
|
|
|
NavigateParams nav_params(browser_, gurl, params.transition);
|
2020-09-25 03:40:47 +02:00
|
|
|
nav_params.FillNavigateParamsFromOpenURLParams(params);
|
|
|
|
|
|
|
|
// Always navigate in the current tab.
|
|
|
|
nav_params.disposition = WindowOpenDisposition::CURRENT_TAB;
|
|
|
|
nav_params.source_contents = GetWebContents();
|
|
|
|
|
2022-08-23 03:37:40 +02:00
|
|
|
nav_params.tabstrip_add_types = AddTabTypes::ADD_NONE;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (params.user_gesture) {
|
2020-09-25 03:40:47 +02:00
|
|
|
nav_params.window_action = NavigateParams::SHOW_WINDOW;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
::Navigate(&nav_params);
|
|
|
|
return true;
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
return false;
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ChromeBrowserHostImpl::ChromeBrowserHostImpl(
|
|
|
|
const CefBrowserSettings& settings,
|
|
|
|
CefRefPtr<CefClient> client,
|
2020-09-25 03:40:47 +02:00
|
|
|
std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate,
|
2020-09-18 00:24:08 +02:00
|
|
|
scoped_refptr<CefBrowserInfo> browser_info,
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context)
|
2020-09-25 03:40:47 +02:00
|
|
|
: CefBrowserHostBase(settings,
|
|
|
|
client,
|
|
|
|
std::move(platform_delegate),
|
|
|
|
browser_info,
|
|
|
|
request_context) {}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
// static
|
|
|
|
Browser* ChromeBrowserHostImpl::CreateBrowser(
|
2023-11-14 18:16:43 +01:00
|
|
|
const CefBrowserCreateParams& params,
|
|
|
|
std::optional<Browser::CreateParams> browser_create_params) {
|
|
|
|
Browser::CreateParams chrome_params = [¶ms, &browser_create_params]() {
|
|
|
|
if (!browser_create_params.has_value()) {
|
|
|
|
auto* profile = CefRequestContextImpl::GetProfile(params.request_context);
|
|
|
|
return Browser::CreateParams(profile, /*user_gesture=*/false);
|
|
|
|
} else {
|
|
|
|
return std::move(*browser_create_params);
|
|
|
|
}
|
|
|
|
}();
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
// Pass |params| to cef::BrowserDelegate::Create from the Browser constructor.
|
|
|
|
chrome_params.cef_params = base::MakeRefCounted<DelegateCreateParams>(params);
|
|
|
|
|
|
|
|
// Configure Browser creation to use the existing Views-based
|
|
|
|
// Widget/BrowserFrame (ChromeBrowserFrame) and BrowserView/BrowserWindow
|
|
|
|
// (ChromeBrowserView). See views/chrome_browser_frame.h for related
|
|
|
|
// documentation.
|
|
|
|
ChromeBrowserView* chrome_browser_view = nullptr;
|
|
|
|
if (params.browser_view) {
|
2023-11-14 18:16:43 +01:00
|
|
|
if (chrome_params.type == Browser::TYPE_NORMAL) {
|
|
|
|
// Don't show most controls.
|
|
|
|
chrome_params.type = Browser::TYPE_POPUP;
|
|
|
|
// Don't show title bar or address.
|
|
|
|
chrome_params.trusted_source = true;
|
|
|
|
}
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
auto view_impl =
|
|
|
|
static_cast<CefBrowserViewImpl*>(params.browser_view.get());
|
|
|
|
|
2023-09-07 19:28:27 +02:00
|
|
|
chrome_browser_view = view_impl->chrome_browser_view();
|
2021-04-02 22:53:17 +02:00
|
|
|
chrome_params.window = chrome_browser_view;
|
|
|
|
|
|
|
|
auto chrome_widget =
|
|
|
|
static_cast<ChromeBrowserFrame*>(chrome_browser_view->GetWidget());
|
|
|
|
chrome_browser_view->set_frame(chrome_widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Browser. This will indirectly create the ChomeBrowserDelegate.
|
|
|
|
// The same params will be used to create a new Browser if the tab is dragged
|
|
|
|
// out of the existing Browser. The returned Browser is owned by the
|
|
|
|
// associated BrowserView.
|
|
|
|
auto browser = Browser::Create(chrome_params);
|
|
|
|
|
2021-11-16 23:24:58 +01:00
|
|
|
bool show_browser = true;
|
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
if (chrome_browser_view) {
|
|
|
|
// Initialize the BrowserFrame and BrowserView and create the controls that
|
|
|
|
// require access to the Browser.
|
2023-09-07 19:28:27 +02:00
|
|
|
chrome_browser_view->InitBrowser(base::WrapUnique(browser));
|
2021-11-16 23:24:58 +01:00
|
|
|
|
2023-03-06 21:42:12 +01:00
|
|
|
// Don't set theme colors in ContentsWebView::UpdateBackgroundColor.
|
|
|
|
chrome_browser_view->contents_web_view()->SetBackgroundVisible(false);
|
|
|
|
|
2021-11-16 23:24:58 +01:00
|
|
|
// Don't show the browser by default.
|
|
|
|
show_browser = false;
|
2021-04-02 22:53:17 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 23:24:58 +01:00
|
|
|
if (show_browser) {
|
|
|
|
browser->window()->Show();
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::Attach(content::WebContents* web_contents,
|
2023-11-14 18:16:43 +01:00
|
|
|
bool is_devtools_popup,
|
2024-04-17 18:01:26 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> opener) {
|
2020-09-18 00:24:08 +02:00
|
|
|
DCHECK(web_contents);
|
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
if (opener) {
|
2024-11-09 01:05:04 +01:00
|
|
|
opener_id_ = opener->GetIdentifier();
|
|
|
|
|
2021-04-02 22:53:17 +02:00
|
|
|
// Give the opener browser's platform delegate an opportunity to modify the
|
|
|
|
// new browser's platform delegate.
|
2024-04-17 18:01:26 +02:00
|
|
|
opener->platform_delegate()->PopupWebContentsCreated(
|
2021-04-02 22:53:17 +02:00
|
|
|
settings_, client_, web_contents, platform_delegate_.get(),
|
2023-11-14 18:16:43 +01:00
|
|
|
is_devtools_popup);
|
2021-04-02 22:53:17 +02:00
|
|
|
}
|
2021-02-18 02:58:25 +01:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
platform_delegate_->WebContentsCreated(web_contents,
|
|
|
|
/*own_web_contents=*/false);
|
2024-07-18 19:50:37 +02:00
|
|
|
contents_delegate_.ObserveWebContents(web_contents);
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
// Associate the platform delegate with this browser.
|
|
|
|
platform_delegate_->BrowserCreated(this);
|
|
|
|
|
|
|
|
// Associate the base class with the WebContents.
|
2020-09-18 00:24:08 +02:00
|
|
|
InitializeBrowser();
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
// Notify that the browser has been created. These must be delivered in the
|
|
|
|
// expected order.
|
|
|
|
|
2023-11-14 18:16:43 +01:00
|
|
|
if (opener) {
|
2022-04-08 22:48:56 +02:00
|
|
|
// 1. Notify the opener browser's platform delegate. With Views this will
|
|
|
|
// result in a call to CefBrowserViewDelegate::OnPopupBrowserViewCreated().
|
|
|
|
// We want to call this method first because the implementation will often
|
|
|
|
// create the Widget for the new popup browser. Without that Widget
|
|
|
|
// CefBrowserHost::GetWindowHandle() will return kNullWindowHandle in
|
|
|
|
// OnAfterCreated(), which breaks client expectations (e.g. clients expect
|
|
|
|
// everything about the browser to be valid at that time).
|
2024-04-17 18:01:26 +02:00
|
|
|
opener->platform_delegate()->PopupBrowserCreated(platform_delegate_.get(),
|
|
|
|
this, is_devtools_popup);
|
2022-04-08 22:48:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Notify the browser's LifeSpanHandler. This must always be the first
|
2021-04-02 22:53:17 +02:00
|
|
|
// notification for the browser.
|
|
|
|
{
|
|
|
|
// The WebContents won't be added to the Browser's TabStripModel until later
|
|
|
|
// in the current call stack. Block navigation until that time.
|
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
|
|
|
OnAfterCreated();
|
|
|
|
}
|
|
|
|
|
2022-04-08 22:48:56 +02:00
|
|
|
// 3. Notify the platform delegate. With Views this will result in a call to
|
2021-04-02 22:53:17 +02:00
|
|
|
// CefBrowserViewDelegate::OnBrowserCreated().
|
|
|
|
platform_delegate_->NotifyBrowserCreated();
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeBrowserHostImpl::SetBrowser(Browser* browser) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2023-11-14 18:16:43 +01:00
|
|
|
if (browser == browser_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
browser_ = browser;
|
2020-09-25 03:40:47 +02:00
|
|
|
static_cast<CefBrowserPlatformDelegateChrome*>(platform_delegate_.get())
|
|
|
|
->set_chrome_browser(browser);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser_) {
|
2024-02-08 19:41:18 +01:00
|
|
|
// We expect the Browser and CefRequestContext to have the same Profile.
|
|
|
|
CHECK_EQ(browser_->profile(),
|
|
|
|
request_context()->GetBrowserContext()->AsProfile());
|
|
|
|
|
2022-04-19 19:45:03 +02:00
|
|
|
host_window_handle_ = platform_delegate_->GetHostWindowHandle();
|
2023-11-14 18:16:43 +01:00
|
|
|
} else {
|
|
|
|
host_window_handle_ = kNullWindowHandle;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
void ChromeBrowserHostImpl::WindowDestroyed() {
|
|
|
|
CEF_REQUIRE_UIT();
|
2023-09-07 19:28:27 +02:00
|
|
|
if (auto view = chrome_browser_view()) {
|
|
|
|
view->Destroyed();
|
2021-02-18 02:58:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
platform_delegate_->CloseHostWindow();
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:49:50 +02:00
|
|
|
bool ChromeBrowserHostImpl::WillBeDestroyed() const {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
// TODO(chrome): Modify this to support DoClose(), see issue #3294.
|
|
|
|
return !!browser_;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void ChromeBrowserHostImpl::DestroyBrowser() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
is_destroying_browser_ = true;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// If the WebContents still exists at this point, close the Browser and
|
|
|
|
// WebContents first. See comments on CefBrowserHostBase::DestroyBrowser.
|
|
|
|
if (GetWebContents()) {
|
|
|
|
// Triggers a call to OnWebContentsDestroyed.
|
2024-10-17 17:05:30 +02:00
|
|
|
DoCloseBrowser();
|
2024-05-13 23:36:09 +02:00
|
|
|
DCHECK(!GetWebContents());
|
2023-10-13 19:48:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Disassociate the platform delegate from this browser.
|
|
|
|
platform_delegate_->BrowserDestroyed(this);
|
|
|
|
|
2023-10-13 19:48:01 +02:00
|
|
|
// Clean up UI thread state.
|
|
|
|
browser_ = nullptr;
|
|
|
|
weak_ptr_factory_.InvalidateWeakPtrs();
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserHostBase::DestroyBrowser();
|
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
|
2024-10-17 17:05:30 +02:00
|
|
|
void ChromeBrowserHostImpl::DoCloseBrowser() {
|
2020-09-25 03:40:47 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (browser_) {
|
|
|
|
// Like chrome::CloseTab() but specifying the WebContents.
|
|
|
|
const int tab_index = GetCurrentTabIndex();
|
|
|
|
if (tab_index != TabStripModel::kNoTab) {
|
2024-05-13 23:36:09 +02:00
|
|
|
// This will trigger destruction of the Browser and WebContents.
|
2020-09-25 03:40:47 +02:00
|
|
|
browser_->tab_strip_model()->CloseWebContentsAt(
|
2022-08-23 03:37:40 +02:00
|
|
|
tab_index, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB |
|
|
|
|
TabCloseTypes::CLOSE_USER_GESTURE);
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChromeBrowserHostImpl::GetCurrentTabIndex() const {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (browser_) {
|
|
|
|
return browser_->tab_strip_model()->GetIndexOfWebContents(GetWebContents());
|
|
|
|
}
|
|
|
|
return TabStripModel::kNoTab;
|
|
|
|
}
|