2012-12-30 12:17:49 +01:00
|
|
|
// Copyright (c) 2012 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/browser_info.h"
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "base/logging.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_host_base.h"
|
|
|
|
#include "cef/libcef/browser/browser_info_manager.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/common/frame_util.h"
|
|
|
|
#include "cef/libcef/common/values_impl.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "content/browser/renderer_host/frame_tree_node.h"
|
|
|
|
#include "content/browser/renderer_host/render_frame_host_impl.h"
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2015-10-21 20:17:09 +02:00
|
|
|
#include "ipc/ipc_message.h"
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefBrowserInfo::FrameInfo::~FrameInfo() {
|
2021-05-21 03:42:58 +02:00
|
|
|
#if DCHECK_IS_ON()
|
|
|
|
if (frame_ && !IsCurrentMainFrame()) {
|
|
|
|
// Should already be Detached.
|
2024-01-26 03:12:43 +01:00
|
|
|
DCHECK(frame_->IsDetached());
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
#endif
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefBrowserInfo::CefBrowserInfo(int browser_id,
|
|
|
|
bool is_popup,
|
|
|
|
bool is_windowless,
|
2024-04-17 18:01:26 +02:00
|
|
|
bool print_preview_enabled,
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefDictionaryValue> extra_info)
|
|
|
|
: browser_id_(browser_id),
|
|
|
|
is_popup_(is_popup),
|
|
|
|
is_windowless_(is_windowless),
|
2024-04-17 18:01:26 +02:00
|
|
|
print_preview_enabled_(print_preview_enabled),
|
2019-05-24 22:23:43 +02:00
|
|
|
extra_info_(extra_info) {
|
|
|
|
DCHECK_GT(browser_id, 0);
|
2023-11-14 18:16:43 +01:00
|
|
|
|
|
|
|
if (extra_info_ && !extra_info_->IsReadOnly()) {
|
|
|
|
// |extra_info_| should always be read-only to avoid accidental future
|
|
|
|
// modification. Take a copy instead of modifying the passed-in object for
|
|
|
|
// backwards compatibility.
|
|
|
|
extra_info_ = extra_info_->Copy(/*exclude_empty_children=*/false);
|
|
|
|
auto extra_info_impl =
|
|
|
|
static_cast<CefDictionaryValueImpl*>(extra_info_.get());
|
|
|
|
extra_info_impl->MarkReadOnly();
|
|
|
|
}
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
CefBrowserInfo::~CefBrowserInfo() {
|
|
|
|
DCHECK(frame_info_set_.empty());
|
|
|
|
}
|
2014-01-02 23:41:11 +01:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> CefBrowserInfo::browser() const {
|
2019-05-24 22:23:43 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2024-05-13 23:36:09 +02:00
|
|
|
return browser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserInfo::IsValid() const {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return browser_ && !is_closing_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserInfo::IsClosing() const {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return is_closing_;
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserInfo::SetBrowser(CefRefPtr<CefBrowserHostBase> browser) {
|
2024-05-13 23:36:09 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
DCHECK(browser);
|
|
|
|
DCHECK(!browser_);
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
browser_ = browser;
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Cache the associated frame handler.
|
|
|
|
if (auto client = browser->GetClient()) {
|
|
|
|
frame_handler_ = client->GetFrameHandler();
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
2024-05-13 23:36:09 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
void CefBrowserInfo::SetClosing() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
|
|
|
// In most cases WebContentsDestroyed will be called first, except if the
|
|
|
|
// browser still exits at CefShitdown.
|
|
|
|
if (!is_closing_) {
|
|
|
|
is_closing_ = true;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
void CefBrowserInfo::WebContentsDestroyed() {
|
|
|
|
NotificationStateLock lock_scope(this);
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Always called before BrowserDestroyed.
|
|
|
|
DCHECK(browser_);
|
|
|
|
|
|
|
|
// We want GetMainFrame() to return nullptr at this point, but browser()
|
|
|
|
// should still be valid so as not to interfere with the net_service
|
|
|
|
// DestructionObserver.
|
|
|
|
if (!is_closing_) {
|
|
|
|
is_closing_ = true;
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2024-05-13 23:36:09 +02:00
|
|
|
|
|
|
|
RemoveAllFrames(browser_);
|
|
|
|
|
|
|
|
// Any future calls to MaybeExecuteFrameNotification will now fail.
|
|
|
|
// NotificationStateLock already took a reference for the delivery of any
|
|
|
|
// notifications that are currently queued due to RemoveAllFrames.
|
|
|
|
frame_handler_ = nullptr;
|
2014-01-02 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
void CefBrowserInfo::BrowserDestroyed() {
|
2021-09-15 20:40:53 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2024-05-13 23:36:09 +02:00
|
|
|
|
|
|
|
// Always called after SetClosing and WebContentsDestroyed.
|
|
|
|
DCHECK(is_closing_);
|
|
|
|
DCHECK(frame_info_set_.empty());
|
|
|
|
|
|
|
|
DCHECK(browser_);
|
|
|
|
browser_ = nullptr;
|
2021-09-15 20:40:53 +02:00
|
|
|
}
|
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
void CefBrowserInfo::MaybeCreateFrame(content::RenderFrameHost* host) {
|
2019-05-24 22:23:43 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2024-04-24 21:38:49 +02:00
|
|
|
if (CefBrowserInfoManager::IsExcludedFrameHost(host)) {
|
|
|
|
// Don't create a FrameHost for an excluded type.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto global_id = host->GetGlobalId();
|
2019-05-24 22:23:43 +02:00
|
|
|
const bool is_main_frame = (host->GetParent() == nullptr);
|
|
|
|
|
|
|
|
// A speculative RFH will be created in response to a browser-initiated
|
|
|
|
// cross-origin navigation (e.g. via LoadURL) and eventually either discarded
|
|
|
|
// or swapped in based on whether the navigation is committed. We'll create a
|
|
|
|
// frame object for the speculative RFH so that it can be found by
|
|
|
|
// frame/routing ID. However, we won't replace the main frame with a
|
|
|
|
// speculative RFH until after it's swapped in, and we'll generally prefer to
|
|
|
|
// return a non-speculative RFH for the same node ID if one exists.
|
|
|
|
const bool is_speculative = (static_cast<content::RenderFrameHostImpl*>(host)
|
|
|
|
->frame_tree_node()
|
|
|
|
->render_manager()
|
|
|
|
->current_frame_host() != host);
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
NotificationStateLock lock_scope(this);
|
2019-05-24 22:23:43 +02:00
|
|
|
DCHECK(browser_);
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto it = frame_id_map_.find(global_id);
|
2019-05-24 22:23:43 +02:00
|
|
|
if (it != frame_id_map_.end()) {
|
|
|
|
auto info = it->second;
|
|
|
|
|
|
|
|
#if DCHECK_IS_ON()
|
|
|
|
// Check that the frame info hasn't changed unexpectedly.
|
2021-08-19 23:07:44 +02:00
|
|
|
DCHECK_EQ(info->global_id_, global_id);
|
2019-05-24 22:23:43 +02:00
|
|
|
DCHECK_EQ(info->is_main_frame_, is_main_frame);
|
|
|
|
#endif
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Update the associated RFH, which may have changed.
|
|
|
|
info->frame_->MaybeReAttach(this, host, /*require_detached=*/false);
|
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
if (info->is_speculative_ && !is_speculative) {
|
2019-05-24 22:23:43 +02:00
|
|
|
// Upgrade the frame info from speculative to non-speculative.
|
|
|
|
if (info->is_main_frame_) {
|
2021-05-19 02:45:05 +02:00
|
|
|
// Set the main frame object.
|
2021-05-21 03:42:58 +02:00
|
|
|
SetMainFrame(browser_, info->frame_);
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
|
|
|
info->is_speculative_ = false;
|
|
|
|
}
|
2017-10-26 20:17:00 +02:00
|
|
|
return;
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
auto frame_info = new FrameInfo;
|
2021-08-19 23:07:44 +02:00
|
|
|
frame_info->global_id_ = global_id;
|
2019-05-24 22:23:43 +02:00
|
|
|
frame_info->is_main_frame_ = is_main_frame;
|
|
|
|
frame_info->is_speculative_ = is_speculative;
|
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
// Create a new frame object.
|
|
|
|
frame_info->frame_ = new CefFrameHostImpl(this, host);
|
|
|
|
MaybeNotifyFrameCreated(frame_info->frame_);
|
|
|
|
if (is_main_frame && !is_speculative) {
|
|
|
|
SetMainFrame(browser_, frame_info->frame_);
|
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2024-04-24 23:23:47 +02:00
|
|
|
// Check that the frame info hasn't changed unexpectedly.
|
|
|
|
DCHECK(host->GetGlobalFrameToken() == *frame_info->frame_->frame_token());
|
|
|
|
DCHECK_EQ(frame_info->is_main_frame_, frame_info->frame_->IsMain());
|
2019-05-24 22:23:43 +02:00
|
|
|
#endif
|
2014-01-02 23:41:11 +01:00
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
browser_->request_context()->OnRenderFrameCreated(global_id, is_main_frame);
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// Populate the lookup maps.
|
2021-08-19 23:07:44 +02:00
|
|
|
frame_id_map_.insert(std::make_pair(global_id, frame_info));
|
2024-01-26 03:12:43 +01:00
|
|
|
frame_token_to_id_map_.insert(
|
|
|
|
std::make_pair(host->GetGlobalFrameToken(), global_id));
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// And finally set the ownership.
|
|
|
|
frame_info_set_.insert(base::WrapUnique(frame_info));
|
2014-01-02 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2021-07-25 19:31:39 +02:00
|
|
|
void CefBrowserInfo::FrameHostStateChanged(
|
|
|
|
content::RenderFrameHost* host,
|
|
|
|
content::RenderFrameHost::LifecycleState old_state,
|
|
|
|
content::RenderFrameHost::LifecycleState new_state) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
if ((old_state == content::RenderFrameHost::LifecycleState::kPrerendering ||
|
|
|
|
old_state ==
|
|
|
|
content::RenderFrameHost::LifecycleState::kInBackForwardCache) &&
|
|
|
|
new_state == content::RenderFrameHost::LifecycleState::kActive) {
|
|
|
|
if (auto frame = GetFrameForHost(host)) {
|
|
|
|
// Update the associated RFH, which may have changed.
|
2024-05-13 23:36:09 +02:00
|
|
|
frame->MaybeReAttach(this, host, /*require_detached=*/true);
|
2021-09-15 13:40:08 +02:00
|
|
|
|
2022-04-05 19:22:32 +02:00
|
|
|
if (frame->IsMain()) {
|
2021-09-15 13:40:08 +02:00
|
|
|
// Update the main frame object.
|
|
|
|
NotificationStateLock lock_scope(this);
|
|
|
|
SetMainFrame(browser_, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update draggable regions.
|
|
|
|
frame->MaybeSendDidStopLoading();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update BackForwardCache state.
|
2021-07-25 19:31:39 +02:00
|
|
|
bool added_to_bfcache =
|
|
|
|
new_state ==
|
|
|
|
content::RenderFrameHost::LifecycleState::kInBackForwardCache;
|
|
|
|
bool removed_from_bfcache =
|
|
|
|
old_state ==
|
|
|
|
content::RenderFrameHost::LifecycleState::kInBackForwardCache;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!added_to_bfcache && !removed_from_bfcache) {
|
2021-07-25 19:31:39 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-07-25 19:31:39 +02:00
|
|
|
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
auto it = frame_id_map_.find(host->GetGlobalId());
|
2024-04-24 21:38:49 +02:00
|
|
|
if (it != frame_id_map_.end()) {
|
|
|
|
DCHECK((!it->second->is_in_bfcache_ && added_to_bfcache) ||
|
|
|
|
(it->second->is_in_bfcache_ && removed_from_bfcache));
|
|
|
|
it->second->is_in_bfcache_ = added_to_bfcache;
|
|
|
|
}
|
2021-07-25 19:31:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
void CefBrowserInfo::RemoveFrame(content::RenderFrameHost* host) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
NotificationStateLock lock_scope(this);
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto global_id = host->GetGlobalId();
|
|
|
|
auto it = frame_id_map_.find(global_id);
|
2024-04-24 21:38:49 +02:00
|
|
|
if (it == frame_id_map_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-24 22:23:43 +02:00
|
|
|
|
|
|
|
auto frame_info = it->second;
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
browser_->request_context()->OnRenderFrameDeleted(global_id,
|
|
|
|
frame_info->is_main_frame_);
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// Remove from the lookup maps.
|
|
|
|
frame_id_map_.erase(it);
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2024-01-26 03:12:43 +01:00
|
|
|
{
|
|
|
|
auto it2 = frame_token_to_id_map_.find(host->GetGlobalFrameToken());
|
|
|
|
DCHECK(it2 != frame_token_to_id_map_.end());
|
|
|
|
frame_token_to_id_map_.erase(it2);
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// And finally delete the frame info.
|
2019-11-06 19:17:18 +01:00
|
|
|
{
|
|
|
|
auto it2 = frame_info_set_.find(frame_info);
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2024-10-22 20:48:06 +02:00
|
|
|
// Explicitly Detach everything.
|
2022-09-26 21:30:45 +02:00
|
|
|
const auto& other_frame_info = *it2;
|
2024-10-22 20:48:06 +02:00
|
|
|
if (other_frame_info->frame_) {
|
|
|
|
const bool is_current_main_frame = other_frame_info->IsCurrentMainFrame();
|
2022-09-26 21:30:45 +02:00
|
|
|
if (other_frame_info->frame_->Detach(
|
2024-10-22 20:48:06 +02:00
|
|
|
CefFrameHostImpl::DetachReason::RENDER_FRAME_DELETED,
|
|
|
|
is_current_main_frame)) {
|
|
|
|
DCHECK(!is_current_main_frame);
|
2022-09-26 21:30:45 +02:00
|
|
|
MaybeNotifyFrameDetached(browser_, other_frame_info->frame_);
|
2022-08-16 19:10:43 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 19:17:18 +01:00
|
|
|
frame_info_set_.erase(it2);
|
|
|
|
}
|
2013-07-15 22:25:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> CefBrowserInfo::GetMainFrame() {
|
2021-09-16 10:01:55 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2021-05-21 03:42:58 +02:00
|
|
|
// Early exit if called post-destruction.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browser_ || is_closing_) {
|
2021-05-21 03:42:58 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
|
|
|
|
CHECK(main_frame_);
|
2019-05-24 22:23:43 +02:00
|
|
|
return main_frame_;
|
|
|
|
}
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> CefBrowserInfo::CreateTempSubFrame(
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& parent_global_id) {
|
|
|
|
CefRefPtr<CefFrameHostImpl> parent = GetFrameForGlobalId(parent_global_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!parent) {
|
2019-05-24 22:23:43 +02:00
|
|
|
parent = GetMainFrame();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
// Intentionally not notifying for temporary frames.
|
2024-01-26 03:12:43 +01:00
|
|
|
return new CefFrameHostImpl(this, parent->frame_token());
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> CefBrowserInfo::GetFrameForHost(
|
|
|
|
const content::RenderFrameHost* host,
|
2021-05-19 02:45:05 +02:00
|
|
|
bool prefer_speculative) const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!host) {
|
2019-05-24 22:23:43 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
return GetFrameForGlobalId(
|
2024-04-24 23:23:47 +02:00
|
|
|
const_cast<content::RenderFrameHost*>(host)->GetGlobalId(),
|
2021-08-19 23:07:44 +02:00
|
|
|
prefer_speculative);
|
2012-12-30 12:17:49 +01:00
|
|
|
}
|
2015-10-21 20:17:09 +02:00
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> CefBrowserInfo::GetFrameForGlobalId(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2021-05-19 02:45:05 +02:00
|
|
|
bool prefer_speculative) const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_util::IsValidGlobalId(global_id)) {
|
2019-05-24 22:23:43 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-21 20:17:09 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto it = frame_id_map_.find(global_id);
|
2019-05-24 22:23:43 +02:00
|
|
|
if (it != frame_id_map_.end()) {
|
|
|
|
const auto info = it->second;
|
|
|
|
|
2021-05-19 02:45:05 +02:00
|
|
|
if (info->is_speculative_ && !prefer_speculative) {
|
2019-05-24 22:23:43 +02:00
|
|
|
if (info->is_main_frame_ && main_frame_) {
|
|
|
|
// Always prefer the non-speculative main frame.
|
|
|
|
return main_frame_;
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
LOG(WARNING) << "Returning a speculative frame for "
|
|
|
|
<< frame_util::GetFrameDebugString(global_id);
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(info->frame_);
|
|
|
|
return info->frame_;
|
|
|
|
}
|
2015-10-21 20:17:09 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
return nullptr;
|
2015-10-21 20:17:09 +02:00
|
|
|
}
|
|
|
|
|
2024-01-26 03:12:43 +01:00
|
|
|
CefRefPtr<CefFrameHostImpl> CefBrowserInfo::GetFrameForGlobalToken(
|
|
|
|
const content::GlobalRenderFrameHostToken& global_token,
|
|
|
|
bool prefer_speculative) const {
|
|
|
|
if (!frame_util::IsValidGlobalToken(global_token)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
content::GlobalRenderFrameHostId global_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
const auto it = frame_token_to_id_map_.find(global_token);
|
|
|
|
if (it == frame_token_to_id_map_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
global_id = it->second;
|
|
|
|
}
|
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
return GetFrameForGlobalId(global_id, prefer_speculative);
|
2024-01-26 03:12:43 +01:00
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefBrowserInfo::FrameHostList CefBrowserInfo::GetAllFrames() const {
|
2015-10-21 20:17:09 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2019-05-24 22:23:43 +02:00
|
|
|
FrameHostList frames;
|
|
|
|
for (const auto& info : frame_info_set_) {
|
2021-07-25 19:31:39 +02:00
|
|
|
if (info->frame_ && !info->is_speculative_ && !info->is_in_bfcache_) {
|
2019-05-24 22:23:43 +02:00
|
|
|
frames.insert(info->frame_);
|
2021-07-25 19:31:39 +02:00
|
|
|
}
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserInfo::NavigationLock::NavigationLock() : weak_ptr_factory_(this) {}
|
|
|
|
|
|
|
|
CefBrowserInfo::NavigationLock::~NavigationLock() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (pending_action_) {
|
|
|
|
CEF_POST_TASK(CEF_UIT, std::move(pending_action_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<CefBrowserInfo::NavigationLock>
|
|
|
|
CefBrowserInfo::CreateNavigationLock() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
scoped_refptr<NavigationLock> lock;
|
|
|
|
if (!navigation_lock_) {
|
|
|
|
lock = new NavigationLock();
|
|
|
|
navigation_lock_ = lock->weak_ptr_factory_.GetWeakPtr();
|
|
|
|
} else {
|
|
|
|
lock = navigation_lock_.get();
|
|
|
|
}
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserInfo::IsNavigationLocked(base::OnceClosure pending_action) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (navigation_lock_) {
|
|
|
|
navigation_lock_->pending_action_ = std::move(pending_action);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
void CefBrowserInfo::MaybeExecuteFrameNotification(
|
|
|
|
FrameNotifyOnceAction pending_action) {
|
|
|
|
CefRefPtr<CefFrameHandler> frame_handler;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope_(notification_lock_);
|
|
|
|
if (!frame_handler_) {
|
|
|
|
// No notifications will be executed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notification_state_lock_) {
|
|
|
|
// Queue the notification until the lock is released.
|
|
|
|
notification_state_lock_->queue_.push(std::move(pending_action));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_handler = frame_handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute immediately if not locked.
|
|
|
|
std::move(pending_action).Run(frame_handler);
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
void CefBrowserInfo::MaybeNotifyDraggableRegionsChanged(
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame,
|
|
|
|
std::vector<CefDraggableRegion> draggable_regions) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(frame->IsMain());
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (draggable_regions == draggable_regions_) {
|
2021-09-15 13:40:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-09-15 13:40:08 +02:00
|
|
|
|
|
|
|
draggable_regions_ = std::move(draggable_regions);
|
|
|
|
|
|
|
|
if (auto client = browser->GetClient()) {
|
|
|
|
if (auto handler = client->GetDragHandler()) {
|
|
|
|
handler->OnDraggableRegionsChanged(browser.get(), frame,
|
|
|
|
draggable_regions_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
// Passing in |browser| here because |browser_| may already be cleared.
|
|
|
|
void CefBrowserInfo::SetMainFrame(CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame) {
|
|
|
|
lock_.AssertAcquired();
|
|
|
|
DCHECK(browser);
|
|
|
|
DCHECK(!frame || frame->IsMain());
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
if (frame && main_frame_ &&
|
|
|
|
frame->GetIdentifier() == main_frame_->GetIdentifier()) {
|
|
|
|
// Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> old_frame;
|
|
|
|
if (main_frame_) {
|
|
|
|
old_frame = main_frame_;
|
2024-10-22 20:48:06 +02:00
|
|
|
if (old_frame->Detach(CefFrameHostImpl::DetachReason::NEW_MAIN_FRAME,
|
|
|
|
/*is_current_main_frame=*/false)) {
|
2021-07-25 19:31:39 +02:00
|
|
|
MaybeNotifyFrameDetached(browser, old_frame);
|
2022-08-16 19:10:43 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main_frame_ = frame;
|
|
|
|
|
|
|
|
MaybeNotifyMainFrameChanged(browser, old_frame, main_frame_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserInfo::MaybeNotifyFrameCreated(
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// Never notify for temporary objects.
|
|
|
|
DCHECK(!frame->is_temporary());
|
|
|
|
|
|
|
|
MaybeExecuteFrameNotification(base::BindOnce(
|
|
|
|
[](scoped_refptr<CefBrowserInfo> self, CefRefPtr<CefFrameHostImpl> frame,
|
|
|
|
CefRefPtr<CefFrameHandler> handler) {
|
|
|
|
if (auto browser = self->browser()) {
|
|
|
|
handler->OnFrameCreated(browser, frame);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scoped_refptr<CefBrowserInfo>(this), frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passing in |browser| here because |browser_| may already be cleared.
|
|
|
|
void CefBrowserInfo::MaybeNotifyFrameDetached(
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// Never notify for temporary objects.
|
|
|
|
DCHECK(!frame->is_temporary());
|
|
|
|
|
|
|
|
MaybeExecuteFrameNotification(base::BindOnce(
|
|
|
|
[](CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame,
|
|
|
|
CefRefPtr<CefFrameHandler> handler) {
|
|
|
|
handler->OnFrameDetached(browser, frame);
|
|
|
|
},
|
|
|
|
browser, frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passing in |browser| here because |browser_| may already be cleared.
|
|
|
|
void CefBrowserInfo::MaybeNotifyMainFrameChanged(
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> old_frame,
|
|
|
|
CefRefPtr<CefFrameHostImpl> new_frame) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// Never notify for temporary objects.
|
|
|
|
DCHECK(!old_frame || !old_frame->is_temporary());
|
|
|
|
DCHECK(!new_frame || !new_frame->is_temporary());
|
|
|
|
|
|
|
|
MaybeExecuteFrameNotification(base::BindOnce(
|
|
|
|
[](CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> old_frame,
|
|
|
|
CefRefPtr<CefFrameHostImpl> new_frame,
|
|
|
|
CefRefPtr<CefFrameHandler> handler) {
|
|
|
|
handler->OnMainFrameChanged(browser, old_frame, new_frame);
|
|
|
|
},
|
|
|
|
browser, old_frame, new_frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserInfo::RemoveAllFrames(
|
|
|
|
CefRefPtr<CefBrowserHostBase> old_browser) {
|
2019-05-24 22:23:43 +02:00
|
|
|
lock_.AssertAcquired();
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
// Make sure any callbacks will see the correct state (e.g. like
|
|
|
|
// CefBrowser::GetMainFrame returning nullptr and CefBrowser::IsValid
|
|
|
|
// returning false).
|
2024-05-13 23:36:09 +02:00
|
|
|
DCHECK(is_closing_);
|
2021-05-21 03:42:58 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// Clear the lookup maps.
|
|
|
|
frame_id_map_.clear();
|
2024-01-26 03:12:43 +01:00
|
|
|
frame_token_to_id_map_.clear();
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2024-10-22 20:48:06 +02:00
|
|
|
// Explicitly Detach everything.
|
2019-05-31 11:50:12 +02:00
|
|
|
for (auto& info : frame_info_set_) {
|
2024-10-22 20:48:06 +02:00
|
|
|
if (info->frame_) {
|
|
|
|
const bool is_current_main_frame = info->IsCurrentMainFrame();
|
2022-08-16 19:10:43 +02:00
|
|
|
if (info->frame_->Detach(
|
2024-10-22 20:48:06 +02:00
|
|
|
CefFrameHostImpl::DetachReason::BROWSER_DESTROYED,
|
|
|
|
is_current_main_frame)) {
|
|
|
|
DCHECK(!is_current_main_frame);
|
2021-07-25 19:31:39 +02:00
|
|
|
MaybeNotifyFrameDetached(old_browser, info->frame_);
|
2022-08-16 19:10:43 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (main_frame_) {
|
2021-05-21 03:42:58 +02:00
|
|
|
SetMainFrame(old_browser, nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// And finally delete the frame info.
|
|
|
|
frame_info_set_.clear();
|
2015-10-21 20:17:09 +02:00
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
|
|
|
|
CefBrowserInfo::NotificationStateLock::NotificationStateLock(
|
|
|
|
CefBrowserInfo* browser_info)
|
|
|
|
: browser_info_(browser_info) {
|
2021-09-16 10:01:55 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
// Take the navigation state lock.
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope_(browser_info_->notification_lock_);
|
|
|
|
CHECK(!browser_info_->notification_state_lock_);
|
|
|
|
browser_info_->notification_state_lock_ = this;
|
|
|
|
// We may need this on destruction, and the original might be cleared.
|
|
|
|
frame_handler_ = browser_info_->frame_handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take the browser info state lock.
|
2024-01-20 23:48:57 +01:00
|
|
|
browser_info_lock_scope_ =
|
2024-06-14 19:01:45 +02:00
|
|
|
std::make_unique<base::MovableAutoLock>(browser_info_->lock_);
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserInfo::NotificationStateLock::~NotificationStateLock() {
|
2021-09-16 10:01:55 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
// Unlock in reverse order.
|
|
|
|
browser_info_lock_scope_.reset();
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope_(browser_info_->notification_lock_);
|
|
|
|
CHECK_EQ(this, browser_info_->notification_state_lock_);
|
|
|
|
browser_info_->notification_state_lock_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!queue_.empty()) {
|
|
|
|
DCHECK(frame_handler_);
|
|
|
|
|
2021-09-16 10:01:55 +02:00
|
|
|
// Don't navigate while inside callbacks.
|
|
|
|
auto nav_lock = browser_info_->CreateNavigationLock();
|
2021-05-21 03:42:58 +02:00
|
|
|
|
|
|
|
// Empty the queue of pending actions. Any of these actions might result in
|
|
|
|
// the acquisition of a new NotificationStateLock.
|
|
|
|
while (!queue_.empty()) {
|
|
|
|
std::move(queue_.front()).Run(frame_handler_);
|
|
|
|
queue_.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|