• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Embedded Framework 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_HOST_BASE_H_
6 #define CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
7 #pragma once
8 
9 #include "include/cef_browser.h"
10 #include "include/cef_client.h"
11 #include "include/views/cef_browser_view.h"
12 #include "libcef/browser/browser_contents_delegate.h"
13 #include "libcef/browser/browser_info.h"
14 #include "libcef/browser/browser_platform_delegate.h"
15 #include "libcef/browser/devtools/devtools_manager.h"
16 #include "libcef/browser/frame_host_impl.h"
17 #include "libcef/browser/request_context_impl.h"
18 
19 #include "base/observer_list.h"
20 #include "base/synchronization/lock.h"
21 #include "extensions/common/mojom/view_type.mojom.h"
22 
23 namespace extensions {
24 class Extension;
25 }
26 
27 // Parameters that are passed to the runtime-specific Create methods.
28 struct CefBrowserCreateParams {
CefBrowserCreateParamsCefBrowserCreateParams29   CefBrowserCreateParams() {}
30 
31   // Copy constructor used with the chrome runtime only.
CefBrowserCreateParamsCefBrowserCreateParams32   CefBrowserCreateParams(const CefBrowserCreateParams& that) {
33     operator=(that);
34   }
35   CefBrowserCreateParams& operator=(const CefBrowserCreateParams& that) {
36     // Not all parameters can be copied.
37     client = that.client;
38     url = that.url;
39     settings = that.settings;
40     request_context = that.request_context;
41     extra_info = that.extra_info;
42 #if defined(TOOLKIT_VIEWS)
43     browser_view = that.browser_view;
44 #endif
45     return *this;
46   }
47 
48   // Platform-specific window creation info. Will be nullptr when creating a
49   // views-hosted browser. Currently used with the alloy runtime only.
50   std::unique_ptr<CefWindowInfo> window_info;
51 
52 #if defined(TOOLKIT_VIEWS)
53   // The BrowserView that will own a Views-hosted browser. Will be nullptr for
54   // popup browsers.
55   CefRefPtr<CefBrowserView> browser_view;
56 
57   // True if this browser is a popup and has a Views-hosted opener, in which
58   // case the BrowserView for this browser will be created later (from
59   // PopupWebContentsCreated).
60   bool popup_with_views_hosted_opener = false;
61 #endif
62 
63   // Client implementation. May be nullptr.
64   CefRefPtr<CefClient> client;
65 
66   // Initial URL to load. May be empty. If this is a valid extension URL then
67   // the browser will be created as an app view extension host.
68   CefString url;
69 
70   // Browser settings.
71   CefBrowserSettings settings;
72 
73   // Other browser that opened this DevTools browser. Will be nullptr for non-
74   // DevTools browsers. Currently used with the alloy runtime only.
75   CefRefPtr<CefBrowserHostBase> devtools_opener;
76 
77   // Request context to use when creating the browser. If nullptr the global
78   // request context will be used.
79   CefRefPtr<CefRequestContext> request_context;
80 
81   // Extra information that will be passed to
82   // CefRenderProcessHandler::OnBrowserCreated.
83   CefRefPtr<CefDictionaryValue> extra_info;
84 
85   // Used when explicitly creating the browser as an extension host via
86   // ProcessManager::CreateBackgroundHost. Currently used with the alloy
87   // runtime only.
88   const extensions::Extension* extension = nullptr;
89   extensions::mojom::ViewType extension_host_type =
90       extensions::mojom::ViewType::kInvalid;
91 };
92 
93 // Base class for CefBrowserHost implementations. Includes functionality that is
94 // shared by the alloy and chrome runtimes. All methods are thread-safe unless
95 // otherwise indicated.
96 class CefBrowserHostBase : public CefBrowserHost,
97                            public CefBrowser,
98                            public CefBrowserContentsDelegate::Observer {
99  public:
100   // Interface to implement for observers that wish to be informed of changes
101   // to the CefBrowserHostBase. All methods will be called on the UI thread.
102   class Observer : public base::CheckedObserver {
103    public:
104     // Called before |browser| is destroyed. Any references to |browser| should
105     // be cleared when this method is called.
106     virtual void OnBrowserDestroyed(CefBrowserHostBase* browser) = 0;
107 
108    protected:
~Observer()109     virtual ~Observer() {}
110   };
111 
112   // Create a new CefBrowserHost instance of the current runtime type with
113   // owned WebContents.
114   static CefRefPtr<CefBrowserHostBase> Create(
115       CefBrowserCreateParams& create_params);
116 
117   // Returns the browser associated with the specified RenderViewHost.
118   static CefRefPtr<CefBrowserHostBase> GetBrowserForHost(
119       const content::RenderViewHost* host);
120   // Returns the browser associated with the specified RenderFrameHost.
121   static CefRefPtr<CefBrowserHostBase> GetBrowserForHost(
122       const content::RenderFrameHost* host);
123   // Returns the browser associated with the specified WebContents.
124   static CefRefPtr<CefBrowserHostBase> GetBrowserForContents(
125       const content::WebContents* contents);
126   // Returns the browser associated with the specified global ID.
127   static CefRefPtr<CefBrowserHostBase> GetBrowserForGlobalId(
128       const content::GlobalRenderFrameHostId& global_id);
129 
130   CefBrowserHostBase(
131       const CefBrowserSettings& settings,
132       CefRefPtr<CefClient> client,
133       std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate,
134       scoped_refptr<CefBrowserInfo> browser_info,
135       CefRefPtr<CefRequestContextImpl> request_context);
136 
137   CefBrowserHostBase(const CefBrowserHostBase&) = delete;
138   CefBrowserHostBase& operator=(const CefBrowserHostBase&) = delete;
139 
140   // Called on the UI thread after the associated WebContents is created.
141   virtual void InitializeBrowser();
142 
143   // Called on the UI thread when the OS window hosting the browser is
144   // destroyed.
145   virtual void WindowDestroyed() = 0;
146 
147   // Called on the UI thread after the associated WebContents is destroyed.
148   // Also called from CefBrowserInfoManager::DestroyAllBrowsers if the browser
149   // was not properly shut down.
150   virtual void DestroyBrowser();
151 
152   // CefBrowserHost methods:
153   CefRefPtr<CefBrowser> GetBrowser() override;
154   CefRefPtr<CefClient> GetClient() override;
155   CefRefPtr<CefRequestContext> GetRequestContext() override;
156   bool HasView() override;
157   void StartDownload(const CefString& url) override;
158   void DownloadImage(const CefString& image_url,
159                      bool is_favicon,
160                      uint32 max_image_size,
161                      bool bypass_cache,
162                      CefRefPtr<CefDownloadImageCallback> callback) override;
163   void ReplaceMisspelling(const CefString& word) override;
164   void AddWordToDictionary(const CefString& word) override;
165   void SendKeyEvent(const CefKeyEvent& event) override;
166   void SendMouseClickEvent(const CefMouseEvent& event,
167                            MouseButtonType type,
168                            bool mouseUp,
169                            int clickCount) override;
170   void SendMouseMoveEvent(const CefMouseEvent& event, bool mouseLeave) override;
171   void SendMouseWheelEvent(const CefMouseEvent& event,
172                            int deltaX,
173                            int deltaY) override;
174   bool SendDevToolsMessage(const void* message, size_t message_size) override;
175   int ExecuteDevToolsMethod(int message_id,
176                             const CefString& method,
177                             CefRefPtr<CefDictionaryValue> params) override;
178   CefRefPtr<CefRegistration> AddDevToolsMessageObserver(
179       CefRefPtr<CefDevToolsMessageObserver> observer) override;
180   void GetNavigationEntries(CefRefPtr<CefNavigationEntryVisitor> visitor,
181                             bool current_only) override;
182   CefRefPtr<CefNavigationEntry> GetVisibleNavigationEntry() override;
183 
184   // CefBrowser methods:
185   bool IsValid() override;
186   CefRefPtr<CefBrowserHost> GetHost() override;
187   bool CanGoBack() override;
188   void GoBack() override;
189   bool CanGoForward() override;
190   void GoForward() override;
191   bool IsLoading() override;
192   void Reload() override;
193   void ReloadIgnoreCache() override;
194   void StopLoad() override;
195   int GetIdentifier() override;
196   bool IsSame(CefRefPtr<CefBrowser> that) override;
197   bool HasDocument() override;
198   bool IsPopup() override;
199   CefRefPtr<CefFrame> GetMainFrame() override;
200   CefRefPtr<CefFrame> GetFocusedFrame() override;
201   CefRefPtr<CefFrame> GetFrame(int64 identifier) override;
202   CefRefPtr<CefFrame> GetFrame(const CefString& name) override;
203   size_t GetFrameCount() override;
204   void GetFrameIdentifiers(std::vector<int64>& identifiers) override;
205   void GetFrameNames(std::vector<CefString>& names) override;
206 
207   // CefBrowserContentsDelegate::Observer methods:
208   void OnStateChanged(CefBrowserContentsState state_changed) override;
209   void OnWebContentsDestroyed(content::WebContents* web_contents) override;
210 
211   // Returns the frame associated with the specified RenderFrameHost.
212   CefRefPtr<CefFrame> GetFrameForHost(const content::RenderFrameHost* host);
213 
214   // Returns the frame associated with the specified global ID. See
215   // documentation on RenderFrameHost::GetFrameTreeNodeId() for why the global
216   // ID is preferred.
217   CefRefPtr<CefFrame> GetFrameForGlobalId(
218       const content::GlobalRenderFrameHostId& global_id);
219 
220   // Manage observer objects. The observer must either outlive this object or
221   // be removed before destruction. Must be called on the UI thread.
222   void AddObserver(Observer* observer);
223   void RemoveObserver(Observer* observer);
224   bool HasObserver(Observer* observer) const;
225 
226   // Methods called from CefFrameHostImpl.
227   void LoadMainFrameURL(const content::OpenURLParams& params);
228   void OnDidFinishLoad(CefRefPtr<CefFrameHostImpl> frame,
229                        const GURL& validated_url,
230                        int http_status_code);
231   virtual void OnSetFocus(cef_focus_source_t source) = 0;
232   void ViewText(const std::string& text);
233 
234   // Called from CefBrowserInfoManager::MaybeAllowNavigation.
235   virtual bool MaybeAllowNavigation(content::RenderFrameHost* opener,
236                                     bool is_guest_view,
237                                     const content::OpenURLParams& params);
238 
239   // Helpers for executing client callbacks. Must be called on the UI thread.
240   void OnAfterCreated();
241   void OnBeforeClose();
242   void OnBrowserDestroyed();
243 
244   // Thread-safe accessors.
settings()245   const CefBrowserSettings& settings() const { return settings_; }
client()246   CefRefPtr<CefClient> client() const { return client_; }
browser_info()247   scoped_refptr<CefBrowserInfo> browser_info() const { return browser_info_; }
248   int browser_id() const;
request_context()249   CefRefPtr<CefRequestContextImpl> request_context() const {
250     return request_context_;
251   }
is_views_hosted()252   bool is_views_hosted() const { return is_views_hosted_; }
253   SkColor GetBackgroundColor() const;
254 
255   // Returns true if windowless rendering is enabled.
256   virtual bool IsWindowless() const;
257 
258   // Accessors that must be called on the UI thread.
259   content::WebContents* GetWebContents() const;
260   content::BrowserContext* GetBrowserContext() const;
platform_delegate()261   CefBrowserPlatformDelegate* platform_delegate() const {
262     return platform_delegate_.get();
263   }
contents_delegate()264   CefBrowserContentsDelegate* contents_delegate() const {
265     return contents_delegate_.get();
266   }
267 
268 #if defined(TOOLKIT_VIEWS)
269   // Returns the Widget owner for the browser window. Only used with windowed
270   // rendering.
271   views::Widget* GetWindowWidget() const;
272 
273   // Returns the BrowserView associated with this browser. Only used with Views-
274   // based browsers.
275   CefRefPtr<CefBrowserView> GetBrowserView() const;
276 #endif
277 
278  protected:
279   bool EnsureDevToolsManager();
280   void InitializeDevToolsRegistrationOnUIThread(
281       CefRefPtr<CefRegistration> registration);
282 
283   // Called from LoadMainFrameURL to perform the actual navigation.
284   virtual bool Navigate(const content::OpenURLParams& params);
285 
286   // Thread-safe members.
287   CefBrowserSettings settings_;
288   CefRefPtr<CefClient> client_;
289   std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate_;
290   scoped_refptr<CefBrowserInfo> browser_info_;
291   CefRefPtr<CefRequestContextImpl> request_context_;
292   const bool is_views_hosted_;
293 
294   // Only accessed on the UI thread.
295   std::unique_ptr<CefBrowserContentsDelegate> contents_delegate_;
296 
297   // Observers that want to be notified of changes to this object.
298   // Only accessed on the UI thread.
299   base::ObserverList<Observer> observers_;
300 
301   // Volatile state accessed from multiple threads. All access must be protected
302   // by |state_lock_|.
303   base::Lock state_lock_;
304   bool is_loading_ = false;
305   bool can_go_back_ = false;
306   bool can_go_forward_ = false;
307   bool has_document_ = false;
308   bool is_fullscreen_ = false;
309   CefRefPtr<CefFrameHostImpl> focused_frame_;
310 
311   // Used for creating and managing DevTools instances.
312   std::unique_ptr<CefDevToolsManager> devtools_manager_;
313 
314  private:
315   IMPLEMENT_REFCOUNTING(CefBrowserHostBase);
316 };
317 
318 #endif  // CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
319