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.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
|
|
|
|
#pragma once
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
#include <queue>
|
2013-07-15 22:25:59 +02:00
|
|
|
#include <set>
|
2019-05-24 22:23:43 +02:00
|
|
|
#include <unordered_map>
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "base/containers/unique_ptr_adapters.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/callback.h"
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2012-12-30 12:17:49 +01:00
|
|
|
#include "base/memory/ref_counted.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2015-10-21 20:17:09 +02:00
|
|
|
#include "base/synchronization/lock.h"
|
2019-03-19 10:42:54 +01:00
|
|
|
#include "base/values.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/internal/cef_ptr.h"
|
|
|
|
#include "cef/libcef/common/values_impl.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "content/public/browser/global_routing_id.h"
|
2021-07-25 19:31:39 +02:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
class CefBrowserHostBase;
|
2021-05-21 03:42:58 +02:00
|
|
|
class CefFrameHandler;
|
2019-05-24 22:23:43 +02:00
|
|
|
class CefFrameHostImpl;
|
2012-12-30 12:17:49 +01:00
|
|
|
|
|
|
|
// CefBrowserInfo is used to associate a browser ID and render view/process
|
2020-09-18 00:24:08 +02:00
|
|
|
// IDs with a particular CefBrowserHostBase. Render view/process IDs may change
|
|
|
|
// during the lifetime of a single CefBrowserHostBase.
|
2012-12-30 12:17:49 +01:00
|
|
|
//
|
2019-05-24 22:23:43 +02:00
|
|
|
// CefBrowserInfo objects are managed by CefBrowserInfoManager and should not be
|
|
|
|
// created directly.
|
2012-12-30 12:17:49 +01:00
|
|
|
class CefBrowserInfo : public base::RefCountedThreadSafe<CefBrowserInfo> {
|
|
|
|
public:
|
2019-03-19 10:42:54 +01:00
|
|
|
CefBrowserInfo(int browser_id,
|
|
|
|
bool is_popup,
|
2019-05-24 22:23:43 +02:00
|
|
|
bool is_windowless,
|
2024-04-17 18:01:26 +02:00
|
|
|
bool print_preview_enabled,
|
2019-03-19 10:42:54 +01:00
|
|
|
CefRefPtr<CefDictionaryValue> extra_info);
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefBrowserInfo(const CefBrowserInfo&) = delete;
|
|
|
|
CefBrowserInfo& operator=(const CefBrowserInfo&) = delete;
|
|
|
|
|
2019-03-13 22:27:37 +01:00
|
|
|
int browser_id() const { return browser_id_; }
|
2012-12-30 12:17:49 +01:00
|
|
|
bool is_popup() const { return is_popup_; }
|
2014-07-01 00:30:29 +02:00
|
|
|
bool is_windowless() const { return is_windowless_; }
|
2024-04-17 18:01:26 +02:00
|
|
|
bool print_preview_enabled() const { return print_preview_enabled_; }
|
2019-03-19 10:42:54 +01:00
|
|
|
CefRefPtr<CefDictionaryValue> extra_info() const { return extra_info_; }
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// May return nullptr if the browser has not yet been created (before
|
|
|
|
// SetBrowser) or if the browser has been destroyed (after BrowserDestroyed).
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser() const;
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Returns true if the browser has been created (after SetBrowser) and is not
|
|
|
|
// yet closing (before SetClosing or WebContentsDestroyed).
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
// Returns true if the browser is closing (after SetClosing or
|
|
|
|
// WebContentsDestroyed).
|
|
|
|
bool IsClosing() const;
|
|
|
|
|
|
|
|
// Called from CefBrowserHostBase constructor.
|
2020-09-18 00:24:08 +02:00
|
|
|
void SetBrowser(CefRefPtr<CefBrowserHostBase> browser);
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Called from CefBrowserHostBase::OnBeforeClose.
|
2021-09-15 20:40:53 +02:00
|
|
|
void SetClosing();
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
// Called from CefBrowserHostBase::DestroyWebContents.
|
|
|
|
void WebContentsDestroyed();
|
|
|
|
|
|
|
|
// Called from CefBrowserHostBase::DestroyBrowser.
|
|
|
|
void BrowserDestroyed();
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// Ensure that a frame record exists for |host|. Called for the main frame
|
|
|
|
// when the RenderView is created, or for a sub-frame when the associated
|
|
|
|
// RenderFrame is created in the renderer process.
|
2024-04-24 23:23:47 +02:00
|
|
|
// Called from CefBrowserContentsDelegate::RenderFrameCreated.
|
|
|
|
void MaybeCreateFrame(content::RenderFrameHost* host);
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2021-07-25 19:31:39 +02:00
|
|
|
// Used to track state changes such as entering/exiting the BackForwardCache.
|
|
|
|
// Called from CefBrowserContentsDelegate::RenderFrameHostStateChanged.
|
|
|
|
void FrameHostStateChanged(
|
|
|
|
content::RenderFrameHost* host,
|
|
|
|
content::RenderFrameHost::LifecycleState old_state,
|
|
|
|
content::RenderFrameHost::LifecycleState new_state);
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// Remove the frame record for |host|. Called for the main frame when the
|
|
|
|
// RenderView is destroyed, or for a sub-frame when the associated RenderFrame
|
|
|
|
// is destroyed in the renderer process.
|
2020-09-18 00:24:08 +02:00
|
|
|
// Called from CefBrowserContentsDelegate::RenderFrameDeleted or
|
2019-05-24 22:23:43 +02:00
|
|
|
// CefMimeHandlerViewGuestDelegate::OnGuestDetached.
|
|
|
|
void RemoveFrame(content::RenderFrameHost* host);
|
|
|
|
|
|
|
|
// Returns the main frame object. This object will remain valid until the
|
|
|
|
// browser is destroyed even though the indentifier may change with cross-
|
|
|
|
// origin navigations. Furthermore, calling LoadURL on this object will always
|
|
|
|
// behave as expected because the call is routed through the browser's
|
|
|
|
// NavigationController.
|
|
|
|
CefRefPtr<CefFrameHostImpl> GetMainFrame();
|
|
|
|
|
|
|
|
// Creates a temporary sub-frame object for situations during navigation or
|
|
|
|
// resource loading where a RFH does not yet exist. If |parent_frame_id|
|
|
|
|
// is invalid the current main frame will be specified as the parent.
|
|
|
|
// Temporary frame objects are not tracked but will be implicitly detached
|
|
|
|
// on browser destruction.
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> CreateTempSubFrame(
|
|
|
|
const content::GlobalRenderFrameHostId& parent_global_id);
|
2019-05-24 22:23:43 +02:00
|
|
|
|
|
|
|
// Returns the frame object matching the specified host or nullptr if no match
|
2024-04-24 23:23:47 +02:00
|
|
|
// is found. Must be called on the UI thread.
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> GetFrameForHost(
|
|
|
|
const content::RenderFrameHost* host,
|
2021-05-19 02:45:05 +02:00
|
|
|
bool prefer_speculative = false) const;
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2024-01-26 03:12:43 +01:00
|
|
|
// Returns the frame object matching the specified ID/token or nullptr if no
|
2024-04-24 23:23:47 +02:00
|
|
|
// match is found. Safe to call from any thread.
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> GetFrameForGlobalId(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2021-05-19 02:45:05 +02:00
|
|
|
bool prefer_speculative = false) const;
|
2024-01-26 03:12:43 +01:00
|
|
|
CefRefPtr<CefFrameHostImpl> GetFrameForGlobalToken(
|
|
|
|
const content::GlobalRenderFrameHostToken& global_token,
|
|
|
|
bool prefer_speculative = false) const;
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2024-04-24 23:23:47 +02:00
|
|
|
// Returns all non-speculative frame objects that currently exist. Safe to
|
|
|
|
// call from any thread.
|
2021-12-06 21:40:25 +01:00
|
|
|
using FrameHostList = std::set<CefRefPtr<CefFrameHostImpl>>;
|
2019-05-24 22:23:43 +02:00
|
|
|
FrameHostList GetAllFrames() const;
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
class NavigationLock final : public base::RefCounted<NavigationLock> {
|
|
|
|
private:
|
|
|
|
friend class CefBrowserInfo;
|
|
|
|
friend class base::RefCounted<NavigationLock>;
|
|
|
|
|
2024-01-21 20:18:09 +01:00
|
|
|
// All usage is via friend declaration. NOLINTNEXTLINE
|
2020-09-18 00:24:08 +02:00
|
|
|
NavigationLock();
|
2024-01-21 20:18:09 +01:00
|
|
|
// All usage is via friend declaration. NOLINTNEXTLINE
|
2020-09-18 00:24:08 +02:00
|
|
|
~NavigationLock();
|
|
|
|
|
|
|
|
base::OnceClosure pending_action_;
|
|
|
|
base::WeakPtrFactory<NavigationLock> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Block navigation actions on NavigationLock life span. Must be called on the
|
|
|
|
// UI thread.
|
|
|
|
scoped_refptr<NavigationLock> CreateNavigationLock();
|
|
|
|
|
|
|
|
// Returns true if navigation actions are currently blocked. If this method
|
|
|
|
// returns true the most recent |pending_action| will be executed on the UI
|
|
|
|
// thread once the navigation lock is released. Must be called on the UI
|
|
|
|
// thread.
|
|
|
|
bool IsNavigationLocked(base::OnceClosure pending_action);
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
using FrameNotifyOnceAction =
|
|
|
|
base::OnceCallback<void(CefRefPtr<CefFrameHandler>)>;
|
|
|
|
|
|
|
|
// Specifies a CefFrameHandler notification action whose execution may need
|
|
|
|
// to be blocked on release of a potentially held NotificationStateLock. If no
|
|
|
|
// CefFrameHandler exists then the action will be discarded without executing.
|
|
|
|
// If the NotificationStateLock is not currently held then the action will be
|
|
|
|
// executed immediately.
|
|
|
|
void MaybeExecuteFrameNotification(FrameNotifyOnceAction pending_action);
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
void MaybeNotifyDraggableRegionsChanged(
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame,
|
|
|
|
std::vector<CefDraggableRegion> draggable_regions);
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
private:
|
|
|
|
friend class base::RefCountedThreadSafe<CefBrowserInfo>;
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
virtual ~CefBrowserInfo();
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
struct FrameInfo {
|
|
|
|
~FrameInfo();
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
inline bool IsCurrentMainFrame() const {
|
2021-07-25 19:31:39 +02:00
|
|
|
return frame_ && is_main_frame_ && !is_speculative_ && !is_in_bfcache_;
|
2021-05-21 03:42:58 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
content::GlobalRenderFrameHostId global_id_;
|
2019-05-24 22:23:43 +02:00
|
|
|
bool is_main_frame_;
|
|
|
|
bool is_speculative_;
|
2021-07-25 19:31:39 +02:00
|
|
|
bool is_in_bfcache_ = false;
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> frame_;
|
|
|
|
};
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
void SetMainFrame(CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame);
|
|
|
|
|
|
|
|
void MaybeNotifyFrameCreated(CefRefPtr<CefFrameHostImpl> frame);
|
|
|
|
void MaybeNotifyFrameDetached(CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame);
|
|
|
|
void MaybeNotifyMainFrameChanged(CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefFrameHostImpl> old_frame,
|
|
|
|
CefRefPtr<CefFrameHostImpl> new_frame);
|
|
|
|
|
|
|
|
void RemoveAllFrames(CefRefPtr<CefBrowserHostBase> old_browser);
|
2014-11-12 20:25:15 +01:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
const int browser_id_;
|
|
|
|
const bool is_popup_;
|
|
|
|
const bool is_windowless_;
|
|
|
|
const bool print_preview_enabled_;
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefDictionaryValue> extra_info_;
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// Navigation will be blocked while |navigation_lock_| exists.
|
|
|
|
// Only accessed on the UI thread.
|
|
|
|
base::WeakPtr<NavigationLock> navigation_lock_;
|
|
|
|
|
2021-05-21 03:42:58 +02:00
|
|
|
// Used instead of |base::AutoLock(lock_)| in situations that might generate
|
|
|
|
// CefFrameHandler notifications. Any notifications passed to
|
|
|
|
// MaybeExecuteFrameNotification() will be queued until the lock is released,
|
2021-09-16 10:01:55 +02:00
|
|
|
// and then executed in order. Only accessed on the UI thread.
|
2021-05-21 03:42:58 +02:00
|
|
|
class NotificationStateLock final {
|
|
|
|
public:
|
|
|
|
explicit NotificationStateLock(CefBrowserInfo* browser_info);
|
|
|
|
~NotificationStateLock();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class CefBrowserInfo;
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<CefBrowserInfo> browser_info_;
|
2021-05-21 03:42:58 +02:00
|
|
|
CefRefPtr<CefFrameHandler> frame_handler_;
|
2024-06-14 19:01:45 +02:00
|
|
|
std::unique_ptr<base::MovableAutoLock> browser_info_lock_scope_;
|
2021-05-21 03:42:58 +02:00
|
|
|
std::queue<FrameNotifyOnceAction> queue_;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable base::Lock notification_lock_;
|
|
|
|
|
|
|
|
// These members must be protected by |notification_lock_|.
|
2024-05-11 17:48:38 +02:00
|
|
|
raw_ptr<NotificationStateLock> notification_state_lock_ = nullptr;
|
2021-05-21 03:42:58 +02:00
|
|
|
CefRefPtr<CefFrameHandler> frame_handler_;
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
mutable base::Lock lock_;
|
2012-12-30 12:17:49 +01:00
|
|
|
|
|
|
|
// The below members must be protected by |lock_|.
|
2013-07-15 22:25:59 +02:00
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser_;
|
2019-05-24 22:23:43 +02:00
|
|
|
|
|
|
|
// Owner of FrameInfo structs.
|
2021-08-19 23:07:44 +02:00
|
|
|
using FrameInfoSet =
|
|
|
|
std::set<std::unique_ptr<FrameInfo>, base::UniquePtrComparator>;
|
2019-05-24 22:23:43 +02:00
|
|
|
FrameInfoSet frame_info_set_;
|
2012-12-30 12:17:49 +01:00
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
// Map a global ID to one frame. These IDs are guaranteed to uniquely
|
|
|
|
// identify a RFH for its complete lifespan. See documentation on
|
|
|
|
// RenderFrameHost::GetFrameTreeNodeId() for background.
|
|
|
|
using FrameIDMap = std::unordered_map<content::GlobalRenderFrameHostId,
|
|
|
|
FrameInfo*,
|
|
|
|
content::GlobalRenderFrameHostIdHasher>;
|
2019-05-24 22:23:43 +02:00
|
|
|
FrameIDMap frame_id_map_;
|
2017-10-26 20:17:00 +02:00
|
|
|
|
2024-01-26 03:12:43 +01:00
|
|
|
// Map of global token to global ID.
|
|
|
|
using FrameTokenToIdMap = std::map<content::GlobalRenderFrameHostToken,
|
|
|
|
content::GlobalRenderFrameHostId>;
|
|
|
|
FrameTokenToIdMap frame_token_to_id_map_;
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
// The current main frame.
|
|
|
|
CefRefPtr<CefFrameHostImpl> main_frame_;
|
2019-03-19 10:42:54 +01:00
|
|
|
|
2021-09-15 20:40:53 +02:00
|
|
|
// True if the browser is currently closing.
|
|
|
|
bool is_closing_ = false;
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
// Only accessed on the UI thread.
|
|
|
|
std::vector<CefDraggableRegion> draggable_regions_;
|
2012-12-30 12:17:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_BROWSER_INFO_H_
|