1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_ 7 #pragma once 8 9 #include <set> 10 #include <vector> 11 12 #include "include/cef_request_context_handler.h" 13 #include "libcef/browser/iothread_state.h" 14 #include "libcef/browser/request_context_handler_map.h" 15 16 #include "base/callback.h" 17 #include "base/files/file_path.h" 18 #include "base/memory/weak_ptr.h" 19 #include "base/task/sequenced_task_runner_helpers.h" 20 #include "chrome/common/plugin.mojom.h" 21 #include "services/network/public/mojom/network_context.mojom.h" 22 #include "third_party/abseil-cpp/absl/types/optional.h" 23 #include "url/origin.h" 24 25 /* 26 // Classes used in request processing (network, storage, service, etc.): 27 // 28 // WC = WebContents 29 // Content API representation of a browser. Created by BHI or the system (for 30 // popups) and owned by BHI. Keeps a pointer to the content::BrowserContext. 31 // 32 // BHI = AlloyBrowserHostImpl 33 // Implements the CefBrowser and CefBrowserHost interfaces which are exposed 34 // to clients. References an RCI instance. Owns a WC. Lifespan is controlled 35 // by client references and CefBrowserInfoManager (until the browser has 36 // closed). 37 // 38 // RCI = CefRequestContextImpl 39 // Implements the CefRequestContext interface which is exposed to clients. 40 // Creates or references a BC. Lifespan is controlled by client references and 41 // BrowserMainParts (for the global RCI). 42 // 43 // BC = CefBrowserContext 44 // Is/owns the content::BrowserContext which is the entry point from WC. 45 // Owns the IOTS and creates the SPI indirectly. Potentially shared by 46 // multiple RCI. Deletes itself when no longer needed by RCI. 47 // 48 // SPI = content::StoragePartitionImpl 49 // Owns storage-related objects like Quota, IndexedDB, Cache, etc. Created by 50 // StoragePartitionImplMap::Get(). Life span is controlled indirectly by BC. 51 // 52 // IOTS = CefIOThreadState 53 // Stores state for access on the IO thread. Life span is controlled by BC. 54 // 55 // 56 // Relationship diagram: 57 // ref = reference (CefRefPtr/scoped_refptr) 58 // own = ownership (std::unique_ptr) 59 // ptr = raw pointer 60 // 61 // BHI -ref-> RCI -ptr-> BC -own-> SPI, IOTS 62 // ^ 63 // BHI -own-> WC -ptr--/ 64 // 65 // 66 // How shutdown works: 67 // 1. AlloyBrowserHostImpl::DestroyBrowser is called on the UI thread after the 68 // browser is closed and deletes the WebContents. 69 // 1. AlloyBrowserHostImpl is destroyed on any thread when the last reference 70 // is released. 71 // 2. CefRequestContextImpl is destroyed (possibly asynchronously) on the UI 72 // thread when the last reference is released. 73 // 3. CefBrowserContext is destroyed on the UI thread when no longer needed 74 // by any CefRequestContextImpl (via RemoveCefRequestContext). 75 // 4. CefIOThreadState is destroyed asynchronously on the IO thread after 76 // the owning CefBrowserContext is destroyed. 77 */ 78 79 namespace content { 80 class BrowserContext; 81 struct GlobalRenderFrameHostId; 82 } // namespace content 83 84 class CefMediaRouterManager; 85 class CefRequestContextImpl; 86 class Profile; 87 88 // Main entry point for configuring behavior on a per-RequestContext basis. The 89 // content::BrowserContext represented by this class is passed to 90 // WebContents::Create in AlloyBrowserHostImpl::CreateInternal. Only accessed on 91 // the UI thread unless otherwise indicated. 92 class CefBrowserContext { 93 public: 94 CefBrowserContext(const CefBrowserContext&) = delete; 95 CefBrowserContext& operator=(const CefBrowserContext&) = delete; 96 97 // Returns the existing instance, if any, associated with the specified 98 // |cache_path|. 99 static CefBrowserContext* FromCachePath(const base::FilePath& cache_path); 100 101 // Returns the existing instance, if any, associated with the specified IDs. 102 // See comments on IsAssociatedContext() for usage. 103 static CefBrowserContext* FromGlobalId( 104 const content::GlobalRenderFrameHostId& global_id, 105 bool require_frame_match); 106 107 // Returns the underlying CefBrowserContext if any. 108 static CefBrowserContext* FromBrowserContext( 109 const content::BrowserContext* context); 110 static CefBrowserContext* FromProfile(const Profile* profile); 111 112 // Returns all existing CefBrowserContext. 113 static std::vector<CefBrowserContext*> GetAll(); 114 115 // Returns the content and chrome layer representations of the context. 116 virtual content::BrowserContext* AsBrowserContext() = 0; 117 virtual Profile* AsProfile() = 0; 118 119 // Returns true if the context is fully initialized. 120 virtual bool IsInitialized() const = 0; 121 122 // If the context is fully initialized execute |callback|, otherwise 123 // store it until the context is fully initialized. 124 virtual void StoreOrTriggerInitCallback(base::OnceClosure callback) = 0; 125 126 // Called from CefRequestContextImpl to track associated objects. This 127 // object will delete itself when the count reaches zero. 128 void AddCefRequestContext(CefRequestContextImpl* context); 129 virtual void RemoveCefRequestContext(CefRequestContextImpl* context); 130 131 // Called from CefRequestContextImpl::OnRenderFrameCreated. 132 void OnRenderFrameCreated(CefRequestContextImpl* request_context, 133 const content::GlobalRenderFrameHostId& global_id, 134 bool is_main_frame, 135 bool is_guest_view); 136 137 // Called from CefRequestContextImpl::OnRenderFrameDeleted. 138 void OnRenderFrameDeleted(CefRequestContextImpl* request_context, 139 const content::GlobalRenderFrameHostId& global_id, 140 bool is_main_frame, 141 bool is_guest_view); 142 143 // Returns the handler that matches the specified IDs. Pass -1 for unknown 144 // values. If |require_frame_match| is true only exact matches will be 145 // returned. If |require_frame_match| is false, and there is not an exact 146 // match, then the first handler for the same |render_process_id| will be 147 // returned. 148 CefRefPtr<CefRequestContextHandler> GetHandler( 149 const content::GlobalRenderFrameHostId& global_id, 150 bool require_frame_match) const; 151 152 // Returns true if this context is associated with the specified IDs. Pass -1 153 // for unknown values. If |require_frame_match| is true only exact matches 154 // will qualify. If |require_frame_match| is false, and there is not an exact 155 // match, then any match for |render_process_id| will qualify. 156 bool IsAssociatedContext(const content::GlobalRenderFrameHostId& global_id, 157 bool require_frame_match) const; 158 159 // Called from CefRequestContextImpl methods of the same name. 160 void RegisterSchemeHandlerFactory(const CefString& scheme_name, 161 const CefString& domain_name, 162 CefRefPtr<CefSchemeHandlerFactory> factory); 163 void ClearSchemeHandlerFactories(); 164 // TODO(chrome-runtime): Make these extension methods pure virtual. 165 virtual void LoadExtension(const CefString& root_directory, 166 CefRefPtr<CefDictionaryValue> manifest, 167 CefRefPtr<CefExtensionHandler> handler, 168 CefRefPtr<CefRequestContext> loader_context); 169 virtual bool GetExtensions(std::vector<CefString>& extension_ids); 170 virtual CefRefPtr<CefExtension> GetExtension(const CefString& extension_id); 171 172 // Called from CefExtensionImpl::Unload(). 173 virtual bool UnloadExtension(const CefString& extension_id); 174 175 // Returns true if this context supports print preview. 176 virtual bool IsPrintPreviewSupported() const; 177 178 network::mojom::NetworkContext* GetNetworkContext(); 179 180 CefMediaRouterManager* GetMediaRouterManager(); 181 182 using CookieableSchemes = absl::optional<std::vector<std::string>>; 183 184 // Returns the schemes associated with this context specifically, or the 185 // global configuration if unset. 186 CookieableSchemes GetCookieableSchemes() const; 187 static CookieableSchemes GetGlobalCookieableSchemes(); 188 189 // These accessors are safe to call from any thread because the values don't 190 // change during this object's lifespan. settings()191 const CefRequestContextSettings& settings() const { return settings_; } cache_path()192 base::FilePath cache_path() const { return cache_path_; } iothread_state()193 scoped_refptr<CefIOThreadState> iothread_state() const { 194 return iothread_state_; 195 } 196 197 // Used to hold a WeakPtr reference to this this object. The Getter returns 198 // nullptr if this object has already been destroyed. 199 using Getter = base::RepeatingCallback<CefBrowserContext*()>; getter()200 Getter getter() const { return getter_; } 201 202 protected: 203 explicit CefBrowserContext(const CefRequestContextSettings& settings); 204 virtual ~CefBrowserContext(); 205 206 // Will be called immediately after this object is created. 207 virtual void Initialize(); 208 209 // Will be called immediately before this object is deleted. 210 virtual void Shutdown(); 211 212 // Members initialized during construction or Initialize() are safe to access 213 // from any thread. 214 const CefRequestContextSettings settings_; 215 base::FilePath cache_path_; 216 217 private: 218 // For DeleteSoon(). 219 friend class base::DeleteHelper<CefBrowserContext>; 220 221 scoped_refptr<CefIOThreadState> iothread_state_; 222 CookieableSchemes cookieable_schemes_; 223 std::unique_ptr<CefMediaRouterManager> media_router_manager_; 224 225 // CefRequestContextImpl objects referencing this object. 226 std::set<CefRequestContextImpl*> request_context_set_; 227 228 // Map IDs to CefRequestContextHandler objects. 229 CefRequestContextHandlerMap handler_map_; 230 231 // Set of global IDs associated with this context. 232 using RenderIdSet = std::set<content::GlobalRenderFrameHostId>; 233 RenderIdSet render_id_set_; 234 235 #if DCHECK_IS_ON() 236 bool is_shutdown_ = false; 237 #endif 238 239 Getter getter_; 240 base::WeakPtrFactory<CefBrowserContext> weak_ptr_factory_; 241 }; 242 243 #endif // CEF_LIBCEF_BROWSER_BROWSER_CONTEXT_IMPL_H_ 244