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