1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. 2 // Portions copyright (c) 2011 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 #ifndef CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_HOST_IMPL_H_ 7 #define CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_HOST_IMPL_H_ 8 #pragma once 9 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 #include "include/cef_browser.h" 15 #include "include/cef_client.h" 16 #include "include/cef_frame.h" 17 #include "libcef/browser/browser_host_base.h" 18 #include "libcef/browser/browser_info.h" 19 #include "libcef/browser/file_dialog_manager.h" 20 #include "libcef/browser/frame_host_impl.h" 21 #include "libcef/browser/javascript_dialog_manager.h" 22 #include "libcef/browser/menu_manager.h" 23 #include "libcef/browser/request_context_impl.h" 24 25 #include "base/synchronization/lock.h" 26 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents_delegate.h" 28 #include "content/public/browser/web_contents_observer.h" 29 #include "extensions/common/mojom/view_type.mojom-forward.h" 30 31 class CefAudioCapturer; 32 class CefBrowserInfo; 33 class SiteInstance; 34 35 // CefBrowser implementation for the alloy runtime. Method calls are delegated 36 // to the CefPlatformDelegate or the WebContents as appropriate. All methods are 37 // thread-safe unless otherwise indicated. 38 // 39 // WebContentsDelegate: Interface for handling WebContents delegations. There is 40 // a one-to-one relationship between AlloyBrowserHostImpl and WebContents 41 // instances. 42 // 43 // WebContentsObserver: Interface for observing WebContents notifications and 44 // IPC messages. There is a one-to-one relationship between WebContents and 45 // RenderViewHost instances. IPC messages received by the RenderViewHost will be 46 // forwarded to this WebContentsObserver implementation via WebContents. IPC 47 // messages sent using AlloyBrowserHostImpl::Send() will be forwarded to the 48 // RenderViewHost (after posting to the UI thread if necessary). Use 49 // WebContentsObserver::routing_id() when sending IPC messages. 50 class AlloyBrowserHostImpl : public CefBrowserHostBase, 51 public content::WebContentsDelegate, 52 public content::WebContentsObserver { 53 public: 54 // Used for handling the response to command messages. 55 class CommandResponseHandler : public virtual CefBaseRefCounted { 56 public: 57 virtual void OnResponse(const std::string& response) = 0; 58 }; 59 60 ~AlloyBrowserHostImpl() override; 61 62 // Create a new AlloyBrowserHostImpl instance with owned WebContents. 63 static CefRefPtr<AlloyBrowserHostImpl> Create( 64 CefBrowserCreateParams& create_params); 65 66 // Returns the browser associated with the specified RenderViewHost. 67 static CefRefPtr<AlloyBrowserHostImpl> GetBrowserForHost( 68 const content::RenderViewHost* host); 69 // Returns the browser associated with the specified RenderFrameHost. 70 static CefRefPtr<AlloyBrowserHostImpl> GetBrowserForHost( 71 const content::RenderFrameHost* host); 72 // Returns the browser associated with the specified WebContents. 73 static CefRefPtr<AlloyBrowserHostImpl> GetBrowserForContents( 74 const content::WebContents* contents); 75 // Returns the browser associated with the specified FrameTreeNode ID. 76 static CefRefPtr<AlloyBrowserHostImpl> GetBrowserForFrameTreeNode( 77 int frame_tree_node_id); 78 // Returns the browser associated with the specified frame routing IDs. 79 static CefRefPtr<AlloyBrowserHostImpl> GetBrowserForFrameRoute( 80 int render_process_id, 81 int render_routing_id); 82 83 // CefBrowserHost methods. 84 void CloseBrowser(bool force_close) override; 85 bool TryCloseBrowser() override; 86 void SetFocus(bool focus) override; 87 CefWindowHandle GetWindowHandle() override; 88 CefWindowHandle GetOpenerWindowHandle() override; 89 double GetZoomLevel() override; 90 void SetZoomLevel(double zoomLevel) override; 91 void RunFileDialog(FileDialogMode mode, 92 const CefString& title, 93 const CefString& default_file_path, 94 const std::vector<CefString>& accept_filters, 95 int selected_accept_filter, 96 CefRefPtr<CefRunFileDialogCallback> callback) override; 97 void Print() override; 98 void PrintToPDF(const CefString& path, 99 const CefPdfPrintSettings& settings, 100 CefRefPtr<CefPdfPrintCallback> callback) override; 101 void Find(int identifier, 102 const CefString& searchText, 103 bool forward, 104 bool matchCase, 105 bool findNext) override; 106 void StopFinding(bool clearSelection) override; 107 void ShowDevTools(const CefWindowInfo& windowInfo, 108 CefRefPtr<CefClient> client, 109 const CefBrowserSettings& settings, 110 const CefPoint& inspect_element_at) override; 111 void CloseDevTools() override; 112 bool HasDevTools() override; 113 bool IsWindowRenderingDisabled() override; 114 void WasResized() override; 115 void WasHidden(bool hidden) override; 116 void NotifyScreenInfoChanged() override; 117 void Invalidate(PaintElementType type) override; 118 void SendExternalBeginFrame() override; 119 void SendTouchEvent(const CefTouchEvent& event) override; 120 void SendFocusEvent(bool setFocus) override; 121 void SendCaptureLostEvent() override; 122 void NotifyMoveOrResizeStarted() override; 123 int GetWindowlessFrameRate() override; 124 void SetWindowlessFrameRate(int frame_rate) override; 125 void ImeSetComposition(const CefString& text, 126 const std::vector<CefCompositionUnderline>& underlines, 127 const CefRange& replacement_range, 128 const CefRange& selection_range) override; 129 void ImeCommitText(const CefString& text, 130 const CefRange& replacement_range, 131 int relative_cursor_pos) override; 132 void ImeFinishComposingText(bool keep_selection) override; 133 void ImeCancelComposition() override; 134 void DragTargetDragEnter(CefRefPtr<CefDragData> drag_data, 135 const CefMouseEvent& event, 136 DragOperationsMask allowed_ops) override; 137 void DragTargetDragOver(const CefMouseEvent& event, 138 DragOperationsMask allowed_ops) override; 139 void DragTargetDragLeave() override; 140 void DragTargetDrop(const CefMouseEvent& event) override; 141 void DragSourceSystemDragEnded() override; 142 void DragSourceEndedAt(int x, int y, DragOperationsMask op) override; 143 void SetAudioMuted(bool mute) override; 144 bool IsAudioMuted() override; 145 void SetAccessibilityState(cef_state_t accessibility_state) override; 146 void SetAutoResizeEnabled(bool enabled, 147 const CefSize& min_size, 148 const CefSize& max_size) override; 149 CefRefPtr<CefExtension> GetExtension() override; 150 bool IsBackgroundHost() override; 151 152 // Returns true if windowless rendering is enabled. 153 bool IsWindowless() const override; 154 155 // Returns true if this browser supports picture-in-picture. 156 bool IsPictureInPictureSupported() const; 157 158 // Called when the OS window hosting the browser is destroyed. 159 void WindowDestroyed() override; 160 161 // Destroy the browser members. This method should only be called after the 162 // native browser window is not longer processing messages. 163 void DestroyBrowser() override; 164 165 // Cancel display of the context menu, if any. 166 void CancelContextMenu(); 167 168 bool MaybeAllowNavigation(content::RenderFrameHost* opener, 169 bool is_guest_view, 170 const content::OpenURLParams& params) override; 171 172 // Convert from view coordinates to screen coordinates. Potential display 173 // scaling will be applied to the result. 174 gfx::Point GetScreenPoint(const gfx::Point& view) const; 175 176 void StartDragging(const content::DropData& drop_data, 177 blink::DragOperationsMask allowed_ops, 178 const gfx::ImageSkia& image, 179 const gfx::Vector2d& image_offset, 180 const blink::mojom::DragEventSourceInfo& event_info, 181 content::RenderWidgetHostImpl* source_rwh); 182 void UpdateDragCursor(ui::mojom::DragOperation operation); 183 184 // Accessors that must be called on the UI thread. 185 extensions::ExtensionHost* GetExtensionHost() const; 186 187 void OnSetFocus(cef_focus_source_t source) override; 188 189 // Run the file chooser dialog specified by |params|. Only a single dialog may 190 // be pending at any given time. |callback| will be executed asynchronously 191 // after the dialog is dismissed or if another dialog is already pending. 192 void RunFileChooser(const CefFileDialogRunner::FileChooserParams& params, 193 CefFileDialogRunner::RunFileChooserCallback callback); 194 195 bool HandleContextMenu(content::WebContents* web_contents, 196 const content::ContextMenuParams& params); 197 198 enum DestructionState { 199 DESTRUCTION_STATE_NONE = 0, 200 DESTRUCTION_STATE_PENDING, 201 DESTRUCTION_STATE_ACCEPTED, 202 DESTRUCTION_STATE_COMPLETED 203 }; destruction_state()204 DestructionState destruction_state() const { return destruction_state_; } 205 206 // content::WebContentsDelegate methods. 207 content::WebContents* OpenURLFromTab( 208 content::WebContents* source, 209 const content::OpenURLParams& params) override; 210 bool ShouldTransferNavigation(bool is_main_frame_navigation) override; 211 void AddNewContents(content::WebContents* source, 212 std::unique_ptr<content::WebContents> new_contents, 213 const GURL& target_url, 214 WindowOpenDisposition disposition, 215 const gfx::Rect& initial_rect, 216 bool user_gesture, 217 bool* was_blocked) override; 218 void LoadingStateChanged(content::WebContents* source, 219 bool to_different_document) override; 220 void CloseContents(content::WebContents* source) override; 221 void UpdateTargetURL(content::WebContents* source, const GURL& url) override; 222 bool DidAddMessageToConsole(content::WebContents* source, 223 blink::mojom::ConsoleMessageLevel log_level, 224 const std::u16string& message, 225 int32_t line_no, 226 const std::u16string& source_id) override; 227 void BeforeUnloadFired(content::WebContents* source, 228 bool proceed, 229 bool* proceed_to_fire_unload) override; 230 bool TakeFocus(content::WebContents* source, bool reverse) override; 231 bool HandleContextMenu(content::RenderFrameHost* render_frame_host, 232 const content::ContextMenuParams& params) override; 233 content::KeyboardEventProcessingResult PreHandleKeyboardEvent( 234 content::WebContents* source, 235 const content::NativeWebKeyboardEvent& event) override; 236 bool HandleKeyboardEvent( 237 content::WebContents* source, 238 const content::NativeWebKeyboardEvent& event) override; 239 bool PreHandleGestureEvent(content::WebContents* source, 240 const blink::WebGestureEvent& event) override; 241 bool CanDragEnter(content::WebContents* source, 242 const content::DropData& data, 243 blink::DragOperationsMask operations_allowed) override; 244 void GetCustomWebContentsView( 245 content::WebContents* web_contents, 246 const GURL& target_url, 247 int opener_render_process_id, 248 int opener_render_frame_id, 249 content::WebContentsView** view, 250 content::RenderViewHostDelegateView** delegate_view) override; 251 void WebContentsCreated(content::WebContents* source_contents, 252 int opener_render_process_id, 253 int opener_render_frame_id, 254 const std::string& frame_name, 255 const GURL& target_url, 256 content::WebContents* new_contents) override; 257 void DidNavigateMainFramePostCommit( 258 content::WebContents* web_contents) override; 259 content::JavaScriptDialogManager* GetJavaScriptDialogManager( 260 content::WebContents* source) override; 261 void RunFileChooser(content::RenderFrameHost* render_frame_host, 262 scoped_refptr<content::FileSelectListener> listener, 263 const blink::mojom::FileChooserParams& params) override; 264 void EnterFullscreenModeForTab( 265 content::RenderFrameHost* requesting_frame, 266 const blink::mojom::FullscreenOptions& options) override; 267 void ExitFullscreenModeForTab(content::WebContents* web_contents) override; 268 bool IsFullscreenForTabOrPending( 269 const content::WebContents* web_contents) override; 270 blink::mojom::DisplayMode GetDisplayMode( 271 const content::WebContents* web_contents) override; 272 void FindReply(content::WebContents* web_contents, 273 int request_id, 274 int number_of_matches, 275 const gfx::Rect& selection_rect, 276 int active_match_ordinal, 277 bool final_update) override; 278 void UpdatePreferredSize(content::WebContents* source, 279 const gfx::Size& pref_size) override; 280 void ResizeDueToAutoResize(content::WebContents* source, 281 const gfx::Size& new_size) override; 282 void RequestMediaAccessPermission( 283 content::WebContents* web_contents, 284 const content::MediaStreamRequest& request, 285 content::MediaResponseCallback callback) override; 286 bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host, 287 const GURL& security_origin, 288 blink::mojom::MediaStreamType type) override; 289 bool IsNeverComposited(content::WebContents* web_contents) override; 290 content::PictureInPictureResult EnterPictureInPicture( 291 content::WebContents* web_contents, 292 const viz::SurfaceId& surface_id, 293 const gfx::Size& natural_size) override; 294 void ExitPictureInPicture() override; 295 296 // content::WebContentsObserver methods. 297 using content::WebContentsObserver::BeforeUnloadFired; 298 void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; 299 void RenderViewReady() override; 300 void DidFinishNavigation( 301 content::NavigationHandle* navigation_handle) override; 302 void OnAudioStateChanged(bool audible) override; 303 void AccessibilityEventReceived( 304 const content::AXEventNotificationDetails& content_event_bundle) override; 305 void AccessibilityLocationChangesReceived( 306 const std::vector<content::AXLocationChangeNotificationDetails>& locData) 307 override; 308 void WebContentsDestroyed() override; 309 310 private: 311 friend class CefBrowserPlatformDelegateAlloy; 312 313 static CefRefPtr<AlloyBrowserHostImpl> CreateInternal( 314 const CefBrowserSettings& settings, 315 CefRefPtr<CefClient> client, 316 content::WebContents* web_contents, 317 bool own_web_contents, 318 scoped_refptr<CefBrowserInfo> browser_info, 319 CefRefPtr<AlloyBrowserHostImpl> opener, 320 bool is_devtools_popup, 321 CefRefPtr<CefRequestContextImpl> request_context, 322 std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate, 323 CefRefPtr<CefExtension> extension); 324 325 AlloyBrowserHostImpl( 326 const CefBrowserSettings& settings, 327 CefRefPtr<CefClient> client, 328 content::WebContents* web_contents, 329 scoped_refptr<CefBrowserInfo> browser_info, 330 CefRefPtr<AlloyBrowserHostImpl> opener, 331 CefRefPtr<CefRequestContextImpl> request_context, 332 std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate, 333 CefRefPtr<CefExtension> extension); 334 335 // Give the platform delegate an opportunity to create the host window. 336 bool CreateHostWindow(); 337 338 // Create the CefFileDialogManager if it doesn't already exist. 339 void EnsureFileDialogManager(); 340 341 void StartAudioCapturer(); 342 void OnRecentlyAudibleTimerFired(); 343 344 void SetFocusInternal(bool focus); 345 346 CefWindowHandle opener_; 347 const bool is_windowless_; 348 CefWindowHandle host_window_handle_ = kNullWindowHandle; 349 CefRefPtr<CefExtension> extension_; 350 bool is_background_host_ = false; 351 352 // Represents the current browser destruction state. Only accessed on the UI 353 // thread. 354 DestructionState destruction_state_ = DESTRUCTION_STATE_NONE; 355 356 // True if the OS window hosting the browser has been destroyed. Only accessed 357 // on the UI thread. 358 bool window_destroyed_ = false; 359 360 // Used for creating and managing file dialogs. 361 std::unique_ptr<CefFileDialogManager> file_dialog_manager_; 362 363 // Used for creating and managing JavaScript dialogs. 364 std::unique_ptr<CefJavaScriptDialogManager> javascript_dialog_manager_; 365 366 // Used for creating and managing context menus. 367 std::unique_ptr<CefMenuManager> menu_manager_; 368 369 // Used for capturing audio for CefAudioHandler. 370 std::unique_ptr<CefAudioCapturer> audio_capturer_; 371 372 // Timer for determining when "recently audible" transitions to false. This 373 // starts running when a tab stops being audible, and is canceled if it starts 374 // being audible again before it fires. 375 base::OneShotTimer recently_audible_timer_; 376 }; 377 378 #endif // CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_HOST_IMPL_H_ 379