2019-03-22 23:11:51 +01:00
|
|
|
// Copyright (c) 2011 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.
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-03-22 23:11:51 +01:00
|
|
|
#ifndef CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_
|
2012-04-03 03:34:16 +02:00
|
|
|
#pragma once
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
#include <optional>
|
2019-10-01 15:55:16 +02:00
|
|
|
#include <set>
|
2020-07-01 02:57:00 +02:00
|
|
|
#include <vector>
|
2019-10-01 15:55:16 +02:00
|
|
|
|
2019-03-22 23:11:51 +01:00
|
|
|
#include "base/files/file_path.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/callback.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2021-11-10 22:57:31 +01:00
|
|
|
#include "base/task/sequenced_task_runner_helpers.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_request_context_handler.h"
|
|
|
|
#include "cef/libcef/browser/iothread_state.h"
|
|
|
|
#include "cef/libcef/browser/request_context_handler_map.h"
|
2024-04-30 03:09:17 +02:00
|
|
|
#include "cef/libcef/features/features.h"
|
2019-10-01 15:55:16 +02:00
|
|
|
#include "chrome/common/plugin.mojom.h"
|
2020-07-01 02:57:00 +02:00
|
|
|
#include "services/network/public/mojom/network_context.mojom.h"
|
2024-04-17 18:01:26 +02:00
|
|
|
#include "ui/base/page_transition_types.h"
|
2019-10-01 15:55:16 +02:00
|
|
|
#include "url/origin.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-02-14 00:17:08 +01:00
|
|
|
/*
|
|
|
|
// Classes used in request processing (network, storage, service, etc.):
|
|
|
|
//
|
|
|
|
// WC = WebContents
|
|
|
|
// Content API representation of a browser. Created by BHI or the system (for
|
2020-07-01 02:57:00 +02:00
|
|
|
// popups) and owned by BHI. Keeps a pointer to the content::BrowserContext.
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
2020-09-22 21:54:02 +02:00
|
|
|
// BHI = AlloyBrowserHostImpl
|
2015-02-14 00:17:08 +01:00
|
|
|
// Implements the CefBrowser and CefBrowserHost interfaces which are exposed
|
2020-07-01 02:57:00 +02:00
|
|
|
// to clients. References an RCI instance. Owns a WC. Lifespan is controlled
|
|
|
|
// by client references and CefBrowserInfoManager (until the browser has
|
|
|
|
// closed).
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
|
|
|
// RCI = CefRequestContextImpl
|
|
|
|
// Implements the CefRequestContext interface which is exposed to clients.
|
2020-07-01 02:57:00 +02:00
|
|
|
// Creates or references a BC. Lifespan is controlled by client references and
|
|
|
|
// BrowserMainParts (for the global RCI).
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
2019-03-22 23:11:51 +01:00
|
|
|
// BC = CefBrowserContext
|
2020-07-01 02:57:00 +02:00
|
|
|
// Is/owns the content::BrowserContext which is the entry point from WC.
|
|
|
|
// Owns the IOTS and creates the SPI indirectly. Potentially shared by
|
|
|
|
// multiple RCI. Deletes itself when no longer needed by RCI.
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
2016-08-24 11:28:52 +02:00
|
|
|
// SPI = content::StoragePartitionImpl
|
|
|
|
// Owns storage-related objects like Quota, IndexedDB, Cache, etc. Created by
|
2020-07-01 02:57:00 +02:00
|
|
|
// StoragePartitionImplMap::Get(). Life span is controlled indirectly by BC.
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
2020-07-01 02:57:00 +02:00
|
|
|
// IOTS = CefIOThreadState
|
|
|
|
// Stores state for access on the IO thread. Life span is controlled by BC.
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Relationship diagram:
|
|
|
|
// ref = reference (CefRefPtr/scoped_refptr)
|
2017-02-14 23:27:19 +01:00
|
|
|
// own = ownership (std::unique_ptr)
|
2015-02-14 00:17:08 +01:00
|
|
|
// ptr = raw pointer
|
|
|
|
//
|
2020-07-01 02:57:00 +02:00
|
|
|
// BHI -ref-> RCI -ptr-> BC -own-> SPI, IOTS
|
|
|
|
// ^
|
|
|
|
// BHI -own-> WC -ptr--/
|
2015-02-14 00:17:08 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// How shutdown works:
|
2020-09-22 21:54:02 +02:00
|
|
|
// 1. AlloyBrowserHostImpl::DestroyBrowser is called on the UI thread after the
|
2020-07-01 02:57:00 +02:00
|
|
|
// browser is closed and deletes the WebContents.
|
2020-09-22 21:54:02 +02:00
|
|
|
// 1. AlloyBrowserHostImpl is destroyed on any thread when the last reference
|
2020-07-01 02:57:00 +02:00
|
|
|
// is released.
|
2017-02-14 23:27:19 +01:00
|
|
|
// 2. CefRequestContextImpl is destroyed (possibly asynchronously) on the UI
|
2020-07-01 02:57:00 +02:00
|
|
|
// thread when the last reference is released.
|
|
|
|
// 3. CefBrowserContext is destroyed on the UI thread when no longer needed
|
|
|
|
// by any CefRequestContextImpl (via RemoveCefRequestContext).
|
|
|
|
// 4. CefIOThreadState is destroyed asynchronously on the IO thread after
|
|
|
|
// the owning CefBrowserContext is destroyed.
|
2015-02-14 00:17:08 +01:00
|
|
|
*/
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
namespace content {
|
|
|
|
class BrowserContext;
|
2021-08-19 23:07:44 +02:00
|
|
|
struct GlobalRenderFrameHostId;
|
|
|
|
} // namespace content
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
class CefMediaRouterManager;
|
|
|
|
class CefRequestContextImpl;
|
|
|
|
class Profile;
|
|
|
|
|
|
|
|
// Main entry point for configuring behavior on a per-RequestContext basis. The
|
|
|
|
// content::BrowserContext represented by this class is passed to
|
2020-09-22 21:54:02 +02:00
|
|
|
// WebContents::Create in AlloyBrowserHostImpl::CreateInternal. Only accessed on
|
2020-07-01 02:57:00 +02:00
|
|
|
// the UI thread unless otherwise indicated.
|
|
|
|
class CefBrowserContext {
|
2012-04-03 03:34:16 +02:00
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
CefBrowserContext(const CefBrowserContext&) = delete;
|
|
|
|
CefBrowserContext& operator=(const CefBrowserContext&) = delete;
|
|
|
|
|
2019-03-22 23:11:51 +01:00
|
|
|
// Returns the existing instance, if any, associated with the specified
|
|
|
|
// |cache_path|.
|
2020-07-01 02:57:00 +02:00
|
|
|
static CefBrowserContext* FromCachePath(const base::FilePath& cache_path);
|
2019-03-22 23:11:51 +01:00
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
// Returns the existing instance, if any, associated with the specified IDs.
|
|
|
|
// See comments on IsAssociatedContext() for usage.
|
2021-08-19 23:07:44 +02:00
|
|
|
static CefBrowserContext* FromGlobalId(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
|
|
|
bool require_frame_match);
|
2019-10-01 15:55:16 +02:00
|
|
|
|
2024-02-08 19:41:18 +01:00
|
|
|
// Returns the underlying CefBrowserContext, if any.
|
2020-07-01 02:57:00 +02:00
|
|
|
static CefBrowserContext* FromBrowserContext(
|
|
|
|
const content::BrowserContext* context);
|
2021-04-07 00:09:45 +02:00
|
|
|
static CefBrowserContext* FromProfile(const Profile* profile);
|
2019-03-22 23:11:51 +01:00
|
|
|
|
|
|
|
// Returns all existing CefBrowserContext.
|
|
|
|
static std::vector<CefBrowserContext*> GetAll();
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Returns the content and chrome layer representations of the context.
|
|
|
|
virtual content::BrowserContext* AsBrowserContext() = 0;
|
|
|
|
virtual Profile* AsProfile() = 0;
|
2019-03-22 23:11:51 +01:00
|
|
|
|
2021-04-15 01:28:22 +02:00
|
|
|
// Returns true if the context is fully initialized.
|
|
|
|
virtual bool IsInitialized() const = 0;
|
|
|
|
|
|
|
|
// If the context is fully initialized execute |callback|, otherwise
|
|
|
|
// store it until the context is fully initialized.
|
|
|
|
virtual void StoreOrTriggerInitCallback(base::OnceClosure callback) = 0;
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Called from CefRequestContextImpl to track associated objects. This
|
|
|
|
// object will delete itself when the count reaches zero.
|
2019-03-22 23:11:51 +01:00
|
|
|
void AddCefRequestContext(CefRequestContextImpl* context);
|
2020-07-01 02:57:00 +02:00
|
|
|
virtual void RemoveCefRequestContext(CefRequestContextImpl* context);
|
2016-01-12 01:19:51 +01:00
|
|
|
|
2019-03-24 00:40:32 +01:00
|
|
|
// Called from CefRequestContextImpl::OnRenderFrameCreated.
|
|
|
|
void OnRenderFrameCreated(CefRequestContextImpl* request_context,
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2024-04-24 23:23:47 +02:00
|
|
|
bool is_main_frame);
|
2019-03-24 00:40:32 +01:00
|
|
|
|
|
|
|
// Called from CefRequestContextImpl::OnRenderFrameDeleted.
|
|
|
|
void OnRenderFrameDeleted(CefRequestContextImpl* request_context,
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2024-04-24 23:23:47 +02:00
|
|
|
bool is_main_frame);
|
2016-10-17 20:14:44 +02:00
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
// Returns the handler that matches the specified IDs. Pass -1 for unknown
|
|
|
|
// values. If |require_frame_match| is true only exact matches will be
|
|
|
|
// returned. If |require_frame_match| is false, and there is not an exact
|
|
|
|
// match, then the first handler for the same |render_process_id| will be
|
|
|
|
// returned.
|
|
|
|
CefRefPtr<CefRequestContextHandler> GetHandler(
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
bool require_frame_match) const;
|
|
|
|
|
|
|
|
// Returns true if this context is associated with the specified IDs. Pass -1
|
|
|
|
// for unknown values. If |require_frame_match| is true only exact matches
|
|
|
|
// will qualify. If |require_frame_match| is false, and there is not an exact
|
|
|
|
// match, then any match for |render_process_id| will qualify.
|
2021-08-19 23:07:44 +02:00
|
|
|
bool IsAssociatedContext(const content::GlobalRenderFrameHostId& global_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
bool require_frame_match) const;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Called from CefRequestContextImpl methods of the same name.
|
2020-07-01 02:57:00 +02:00
|
|
|
void RegisterSchemeHandlerFactory(const CefString& scheme_name,
|
|
|
|
const CefString& domain_name,
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory);
|
|
|
|
void ClearSchemeHandlerFactories();
|
2024-04-30 03:09:17 +02:00
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
|
2020-07-01 02:57:00 +02:00
|
|
|
virtual void LoadExtension(const CefString& root_directory,
|
|
|
|
CefRefPtr<CefDictionaryValue> manifest,
|
|
|
|
CefRefPtr<CefExtensionHandler> handler,
|
|
|
|
CefRefPtr<CefRequestContext> loader_context);
|
|
|
|
virtual bool GetExtensions(std::vector<CefString>& extension_ids);
|
|
|
|
virtual CefRefPtr<CefExtension> GetExtension(const CefString& extension_id);
|
|
|
|
|
|
|
|
// Called from CefExtensionImpl::Unload().
|
|
|
|
virtual bool UnloadExtension(const CefString& extension_id);
|
2024-04-30 03:09:17 +02:00
|
|
|
#endif
|
2020-07-01 02:57:00 +02:00
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
// Called from AlloyBrowserHostImpl::DidFinishNavigation to update the table
|
|
|
|
// of visited links.
|
|
|
|
virtual void AddVisitedURLs(const GURL& url,
|
|
|
|
const std::vector<GURL>& redirect_chain,
|
|
|
|
ui::PageTransition transition) = 0;
|
2020-07-01 02:57:00 +02:00
|
|
|
|
2019-05-18 19:41:14 +02:00
|
|
|
network::mojom::NetworkContext* GetNetworkContext();
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
CefMediaRouterManager* GetMediaRouterManager();
|
|
|
|
|
2024-02-08 19:41:18 +01:00
|
|
|
// Returns any CefRequestContext associated with this object, or nullptr if
|
|
|
|
// none exist. If |prefer_no_handler| is true this method will try to return a
|
|
|
|
// context without an associated CefRequestContextHandler. Use
|
|
|
|
// CefRequestContextImpl::GetOrCreateForBrowserContext() instead of calling
|
|
|
|
// this method directly.
|
|
|
|
CefRefPtr<CefRequestContextImpl> GetAnyRequestContext(
|
|
|
|
bool prefer_no_handler) const;
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
using CookieableSchemes = std::optional<std::vector<std::string>>;
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
// Returns the schemes associated with this context specifically, or the
|
|
|
|
// global configuration if unset.
|
|
|
|
CookieableSchemes GetCookieableSchemes() const;
|
2021-04-09 20:34:45 +02:00
|
|
|
static CookieableSchemes GetGlobalCookieableSchemes();
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// These accessors are safe to call from any thread because the values don't
|
|
|
|
// change during this object's lifespan.
|
|
|
|
const CefRequestContextSettings& settings() const { return settings_; }
|
|
|
|
base::FilePath cache_path() const { return cache_path_; }
|
2021-04-07 22:58:43 +02:00
|
|
|
scoped_refptr<CefIOThreadState> iothread_state() const {
|
|
|
|
return iothread_state_;
|
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Used to hold a WeakPtr reference to this this object. The Getter returns
|
|
|
|
// nullptr if this object has already been destroyed.
|
2020-03-19 16:34:15 +01:00
|
|
|
using Getter = base::RepeatingCallback<CefBrowserContext*()>;
|
|
|
|
Getter getter() const { return getter_; }
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
protected:
|
|
|
|
explicit CefBrowserContext(const CefRequestContextSettings& settings);
|
|
|
|
virtual ~CefBrowserContext();
|
|
|
|
|
|
|
|
// Will be called immediately after this object is created.
|
|
|
|
virtual void Initialize();
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Will be called immediately before this object is deleted.
|
|
|
|
virtual void Shutdown();
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// Members initialized during construction or Initialize() are safe to access
|
|
|
|
// from any thread.
|
2020-04-23 23:45:04 +02:00
|
|
|
const CefRequestContextSettings settings_;
|
2019-03-22 23:11:51 +01:00
|
|
|
base::FilePath cache_path_;
|
2017-04-20 21:28:17 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
private:
|
2021-07-23 21:55:22 +02:00
|
|
|
// For DeleteSoon().
|
|
|
|
friend class base::DeleteHelper<CefBrowserContext>;
|
|
|
|
|
2021-04-07 22:58:43 +02:00
|
|
|
scoped_refptr<CefIOThreadState> iothread_state_;
|
2020-09-04 21:08:55 +02:00
|
|
|
CookieableSchemes cookieable_schemes_;
|
2020-03-19 16:34:15 +01:00
|
|
|
std::unique_ptr<CefMediaRouterManager> media_router_manager_;
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
// CefRequestContextImpl objects referencing this object.
|
2024-05-13 23:36:09 +02:00
|
|
|
std::set<raw_ptr<CefRequestContextImpl>> request_context_set_;
|
2020-07-01 02:57:00 +02:00
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
// Map IDs to CefRequestContextHandler objects.
|
|
|
|
CefRequestContextHandlerMap handler_map_;
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
// Set of global IDs associated with this context.
|
2021-12-06 21:40:25 +01:00
|
|
|
using RenderIdSet = std::set<content::GlobalRenderFrameHostId>;
|
2019-10-01 15:55:16 +02:00
|
|
|
RenderIdSet render_id_set_;
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
#if DCHECK_IS_ON()
|
|
|
|
bool is_shutdown_ = false;
|
|
|
|
#endif
|
|
|
|
|
2020-03-19 16:34:15 +01:00
|
|
|
Getter getter_;
|
|
|
|
base::WeakPtrFactory<CefBrowserContext> weak_ptr_factory_;
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
2019-03-22 23:11:51 +01:00
|
|
|
#endif // CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_
|