1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_REQUEST_CONTEXT_IMPL_H_ 6 #define CEF_LIBCEF_REQUEST_CONTEXT_IMPL_H_ 7 #pragma once 8 9 #include "include/cef_request_context.h" 10 #include "libcef/browser/browser_context.h" 11 #include "libcef/browser/media_router/media_router_impl.h" 12 #include "libcef/browser/net_service/cookie_manager_impl.h" 13 #include "libcef/browser/thread_util.h" 14 15 class CefBrowserContext; 16 17 // Implementation of the CefRequestContext interface. All methods are thread- 18 // safe unless otherwise indicated. Will be deleted on the UI thread. 19 class CefRequestContextImpl : public CefRequestContext { 20 public: 21 ~CefRequestContextImpl() override; 22 23 // Creates the singleton global RequestContext. Called from 24 // AlloyBrowserMainParts::PreMainMessageLoopRun. 25 static CefRefPtr<CefRequestContextImpl> CreateGlobalRequestContext( 26 const CefRequestContextSettings& settings); 27 28 // Returns a CefRequestContextImpl for the specified |request_context|. 29 // Will return the global context if |request_context| is NULL. 30 static CefRefPtr<CefRequestContextImpl> GetOrCreateForRequestContext( 31 CefRefPtr<CefRequestContext> request_context); 32 33 // Verify that the browser context can be directly accessed (e.g. on the UI 34 // thread and initialized). 35 bool VerifyBrowserContext() const; 36 37 // Returns the browser context object. Can only be called on the UI thread 38 // after the browser context has been initialized. 39 CefBrowserContext* GetBrowserContext(); 40 41 // If the context is fully initialized execute |callback|, otherwise 42 // store it until the context is fully initialized. 43 void ExecuteWhenBrowserContextInitialized(base::OnceClosure callback); 44 45 // Executes |callback| either synchronously or asynchronously after the 46 // browser context object has been initialized. If |task_runner| is NULL the 47 // callback will be executed on the originating thread. The resulting getter 48 // can only be executed on the UI thread. 49 using BrowserContextCallback = 50 base::OnceCallback<void(CefBrowserContext::Getter)>; 51 void GetBrowserContext( 52 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 53 BrowserContextCallback callback); 54 55 bool IsSame(CefRefPtr<CefRequestContext> other) override; 56 bool IsSharingWith(CefRefPtr<CefRequestContext> other) override; 57 bool IsGlobal() override; 58 CefRefPtr<CefRequestContextHandler> GetHandler() override; 59 CefString GetCachePath() override; 60 CefRefPtr<CefCookieManager> GetCookieManager( 61 CefRefPtr<CefCompletionCallback> callback) override; 62 bool RegisterSchemeHandlerFactory( 63 const CefString& scheme_name, 64 const CefString& domain_name, 65 CefRefPtr<CefSchemeHandlerFactory> factory) override; 66 bool ClearSchemeHandlerFactories() override; 67 void PurgePluginListCache(bool reload_pages) override; 68 bool HasPreference(const CefString& name) override; 69 CefRefPtr<CefValue> GetPreference(const CefString& name) override; 70 CefRefPtr<CefDictionaryValue> GetAllPreferences( 71 bool include_defaults) override; 72 bool CanSetPreference(const CefString& name) override; 73 bool SetPreference(const CefString& name, 74 CefRefPtr<CefValue> value, 75 CefString& error) override; 76 void ClearCertificateExceptions( 77 CefRefPtr<CefCompletionCallback> callback) override; 78 void ClearHttpAuthCredentials( 79 CefRefPtr<CefCompletionCallback> callback) override; 80 void CloseAllConnections(CefRefPtr<CefCompletionCallback> callback) override; 81 void ResolveHost(const CefString& origin, 82 CefRefPtr<CefResolveCallback> callback) override; 83 void LoadExtension(const CefString& root_directory, 84 CefRefPtr<CefDictionaryValue> manifest, 85 CefRefPtr<CefExtensionHandler> handler) override; 86 bool DidLoadExtension(const CefString& extension_id) override; 87 bool HasExtension(const CefString& extension_id) override; 88 bool GetExtensions(std::vector<CefString>& extension_ids) override; 89 CefRefPtr<CefExtension> GetExtension(const CefString& extension_id) override; 90 CefRefPtr<CefMediaRouter> GetMediaRouter( 91 CefRefPtr<CefCompletionCallback> callback) override; 92 settings()93 const CefRequestContextSettings& settings() const { return config_.settings; } 94 95 // Called from CefBrowserContentsDelegate::RenderFrameCreated or 96 // CefMimeHandlerViewGuestDelegate::OnGuestAttached when a render frame is 97 // created. 98 void OnRenderFrameCreated(int render_process_id, 99 int render_frame_id, 100 int frame_tree_node_id, 101 bool is_main_frame, 102 bool is_guest_view); 103 104 // Called from CefBrowserContentsDelegate::RenderFrameDeleted or 105 // CefMimeHandlerViewGuestDelegate::OnGuestDetached when a render frame is 106 // deleted. 107 void OnRenderFrameDeleted(int render_process_id, 108 int render_frame_id, 109 int frame_tree_node_id, 110 bool is_main_frame, 111 bool is_guest_view); 112 113 private: 114 friend class CefRequestContext; 115 116 struct Config { 117 // True if wrapping the global context. 118 bool is_global = false; 119 120 // |settings| or |other| will be set when creating a new CefRequestContext 121 // via the API. When wrapping an existing CefBrowserContext* both will be 122 // empty and Initialize(CefBrowserContext*) will be called immediately after 123 // CefRequestContextImpl construction. 124 CefRequestContextSettings settings; 125 CefRefPtr<CefRequestContextImpl> other; 126 127 // Optionally use this handler. 128 CefRefPtr<CefRequestContextHandler> handler; 129 130 // Used to uniquely identify CefRequestContext objects before an associated 131 // CefBrowserContext has been created. Should be set when a new 132 // CefRequestContext via the API. 133 int unique_id = -1; 134 }; 135 136 static CefRefPtr<CefRequestContextImpl> GetOrCreateRequestContext( 137 const Config& config); 138 139 explicit CefRequestContextImpl(const Config& config); 140 141 void Initialize(); 142 void BrowserContextInitialized(); 143 144 // Make sure the browser context exists. Only called on the UI thread. 145 void EnsureBrowserContext(); 146 147 void PurgePluginListCacheInternal( 148 bool reload_pages, 149 CefBrowserContext::Getter browser_context_getter); 150 void ClearCertificateExceptionsInternal( 151 CefRefPtr<CefCompletionCallback> callback, 152 CefBrowserContext::Getter browser_context_getter); 153 void ClearHttpAuthCredentialsInternal( 154 CefRefPtr<CefCompletionCallback> callback, 155 CefBrowserContext::Getter browser_context_getter); 156 void CloseAllConnectionsInternal( 157 CefRefPtr<CefCompletionCallback> callback, 158 CefBrowserContext::Getter browser_context_getter); 159 void ResolveHostInternal(const CefString& origin, 160 CefRefPtr<CefResolveCallback> callback, 161 CefBrowserContext::Getter browser_context_getter); 162 163 void InitializeCookieManagerInternal( 164 CefRefPtr<CefCookieManagerImpl> cookie_manager, 165 CefRefPtr<CefCompletionCallback> callback); 166 void InitializeMediaRouterInternal(CefRefPtr<CefMediaRouterImpl> media_router, 167 CefRefPtr<CefCompletionCallback> callback); 168 169 CefBrowserContext* browser_context() const; 170 171 // We must disassociate from this on destruction. 172 CefBrowserContext* browser_context_ = nullptr; 173 174 Config config_; 175 176 IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(CefRequestContextImpl); 177 DISALLOW_COPY_AND_ASSIGN(CefRequestContextImpl); 178 }; 179 180 #endif // CEF_LIBCEF_REQUEST_CONTEXT_IMPL_H_ 181