• 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 FrameTreeNode ID.
127   static CefRefPtr<CefBrowserHostBase> GetBrowserForFrameTreeNode(
128       int frame_tree_node_id);
129   // Returns the browser associated with the specified frame routing IDs.
130   static CefRefPtr<CefBrowserHostBase> GetBrowserForFrameRoute(
131       int render_process_id,
132       int render_routing_id);
133 
134   CefBrowserHostBase(
135       const CefBrowserSettings& settings,
136       CefRefPtr<CefClient> client,
137       std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate,
138       scoped_refptr<CefBrowserInfo> browser_info,
139       CefRefPtr<CefRequestContextImpl> request_context);
140 
141   // Called on the UI thread after the associated WebContents is created.
142   virtual void InitializeBrowser();
143 
144   // Called on the UI thread when the OS window hosting the browser is
145   // destroyed.
146   virtual void WindowDestroyed() = 0;
147 
148   // Called on the UI thread after the associated WebContents is destroyed.
149   // Also called from CefBrowserInfoManager::DestroyAllBrowsers if the browser
150   // was not properly shut down.
151   virtual void DestroyBrowser();
152 
153   // CefBrowserHost methods:
154   CefRefPtr<CefBrowser> GetBrowser() override;
155   CefRefPtr<CefClient> GetClient() override;
156   CefRefPtr<CefRequestContext> GetRequestContext() override;
157   bool HasView() override;
158   void StartDownload(const CefString& url) override;
159   void DownloadImage(const CefString& image_url,
160                      bool is_favicon,
161                      uint32 max_image_size,
162                      bool bypass_cache,
163                      CefRefPtr<CefDownloadImageCallback> callback) override;
164   void ReplaceMisspelling(const CefString& word) override;
165   void AddWordToDictionary(const CefString& word) override;
166   void SendKeyEvent(const CefKeyEvent& event) override;
167   void SendMouseClickEvent(const CefMouseEvent& event,
168                            MouseButtonType type,
169                            bool mouseUp,
170                            int clickCount) override;
171   void SendMouseMoveEvent(const CefMouseEvent& event, bool mouseLeave) override;
172   void SendMouseWheelEvent(const CefMouseEvent& event,
173                            int deltaX,
174                            int deltaY) override;
175   bool SendDevToolsMessage(const void* message, size_t message_size) override;
176   int ExecuteDevToolsMethod(int message_id,
177                             const CefString& method,
178                             CefRefPtr<CefDictionaryValue> params) override;
179   CefRefPtr<CefRegistration> AddDevToolsMessageObserver(
180       CefRefPtr<CefDevToolsMessageObserver> observer) override;
181   void GetNavigationEntries(CefRefPtr<CefNavigationEntryVisitor> visitor,
182                             bool current_only) override;
183   CefRefPtr<CefNavigationEntry> GetVisibleNavigationEntry() override;
184 
185   // CefBrowser methods:
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 FrameTreeNode ID.
215   CefRefPtr<CefFrame> GetFrameForFrameTreeNode(int frame_tree_node_id);
216 
217   // Manage observer objects. The observer must either outlive this object or
218   // be removed before destruction. Must be called on the UI thread.
219   void AddObserver(Observer* observer);
220   void RemoveObserver(Observer* observer);
221   bool HasObserver(Observer* observer) const;
222 
223   // Methods called from CefFrameHostImpl.
224   void LoadMainFrameURL(const content::OpenURLParams& params);
225   void OnDidFinishLoad(CefRefPtr<CefFrameHostImpl> frame,
226                        const GURL& validated_url,
227                        int http_status_code);
228   virtual void OnSetFocus(cef_focus_source_t source) = 0;
229   void ViewText(const std::string& text);
230 
231   // Called from CefBrowserInfoManager::MaybeAllowNavigation.
232   virtual bool MaybeAllowNavigation(content::RenderFrameHost* opener,
233                                     bool is_guest_view,
234                                     const content::OpenURLParams& params);
235 
236   // Helpers for executing client callbacks. Must be called on the UI thread.
237   void OnAfterCreated();
238   void OnBeforeClose();
239   void OnBrowserDestroyed();
240 
241   // Thread-safe accessors.
settings()242   const CefBrowserSettings& settings() const { return settings_; }
client()243   CefRefPtr<CefClient> client() const { return client_; }
browser_info()244   scoped_refptr<CefBrowserInfo> browser_info() const { return browser_info_; }
245   int browser_id() const;
request_context()246   CefRefPtr<CefRequestContextImpl> request_context() const {
247     return request_context_;
248   }
is_views_hosted()249   bool is_views_hosted() const { return is_views_hosted_; }
250   SkColor GetBackgroundColor() const;
251 
252   // Returns true if windowless rendering is enabled.
253   virtual bool IsWindowless() const;
254 
255   // Accessors that must be called on the UI thread.
256   content::WebContents* GetWebContents() const;
257   content::BrowserContext* GetBrowserContext() const;
platform_delegate()258   CefBrowserPlatformDelegate* platform_delegate() const {
259     return platform_delegate_.get();
260   }
contents_delegate()261   CefBrowserContentsDelegate* contents_delegate() const {
262     return contents_delegate_.get();
263   }
264 
265 #if defined(TOOLKIT_VIEWS)
266   // Returns the Widget owner for the browser window. Only used with windowed
267   // rendering.
268   views::Widget* GetWindowWidget() const;
269 
270   // Returns the BrowserView associated with this browser. Only used with Views-
271   // based browsers.
272   CefRefPtr<CefBrowserView> GetBrowserView() const;
273 #endif
274 
275  protected:
276   bool EnsureDevToolsManager();
277   void InitializeDevToolsRegistrationOnUIThread(
278       CefRefPtr<CefRegistration> registration);
279 
280   // Called from LoadMainFrameURL to perform the actual navigation.
281   virtual bool Navigate(const content::OpenURLParams& params);
282 
283   // Thread-safe members.
284   CefBrowserSettings settings_;
285   CefRefPtr<CefClient> client_;
286   std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate_;
287   scoped_refptr<CefBrowserInfo> browser_info_;
288   CefRefPtr<CefRequestContextImpl> request_context_;
289   const bool is_views_hosted_;
290 
291   // Only accessed on the UI thread.
292   std::unique_ptr<CefBrowserContentsDelegate> contents_delegate_;
293 
294   // Observers that want to be notified of changes to this object.
295   // Only accessed on the UI thread.
296   base::ObserverList<Observer> observers_;
297 
298   // Volatile state accessed from multiple threads. All access must be protected
299   // by |state_lock_|.
300   base::Lock state_lock_;
301   bool is_loading_ = false;
302   bool can_go_back_ = false;
303   bool can_go_forward_ = false;
304   bool has_document_ = false;
305   bool is_fullscreen_ = false;
306   CefRefPtr<CefFrameHostImpl> focused_frame_;
307 
308   // Used for creating and managing DevTools instances.
309   std::unique_ptr<CefDevToolsManager> devtools_manager_;
310 
311  private:
312   IMPLEMENT_REFCOUNTING(CefBrowserHostBase);
313   DISALLOW_COPY_AND_ASSIGN(CefBrowserHostBase);
314 };
315 
316 #endif  // CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
317