1 // Copyright (c) 2015 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_BROWSER_PLATFORM_DELEGATE_H_ 7 #define CEF_LIBCEF_BROWSER_BROWSER_PLATFORM_DELEGATE_H_ 8 9 #include <string> 10 #include <vector> 11 12 #include "include/cef_client.h" 13 #include "include/cef_drag_data.h" 14 #include "include/internal/cef_types.h" 15 #include "include/views/cef_browser_view.h" 16 17 #include "base/callback_forward.h" 18 #include "extensions/common/mojom/view_type.mojom-forward.h" 19 #include "third_party/blink/public/common/page/drag_operation.h" 20 #include "third_party/blink/public/mojom/drag/drag.mojom-forward.h" 21 #include "third_party/skia/include/core/SkColor.h" 22 #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-forward.h" 23 #include "ui/base/window_open_disposition.h" 24 25 class GURL; 26 27 namespace blink { 28 class WebGestureEvent; 29 class WebMouseEvent; 30 class WebMouseWheelEvent; 31 class WebInputEvent; 32 class WebTouchEvent; 33 } // namespace blink 34 35 namespace content { 36 struct AXEventNotificationDetails; 37 struct AXLocationChangeNotificationDetails; 38 struct DropData; 39 struct NativeWebKeyboardEvent; 40 class RenderViewHost; 41 class RenderViewHostDelegateView; 42 class RenderWidgetHostImpl; 43 class WebContents; 44 class WebContentsView; 45 } // namespace content 46 47 namespace extensions { 48 class Extension; 49 class ExtensionHost; 50 } // namespace extensions 51 52 namespace gfx { 53 class ImageSkia; 54 class Point; 55 class Rect; 56 class Size; 57 class Vector2d; 58 } // namespace gfx 59 60 #if defined(TOOLKIT_VIEWS) 61 namespace views { 62 class Widget; 63 } 64 #endif 65 66 struct CefBrowserCreateParams; 67 class CefBrowserHostBase; 68 class CefFileDialogRunner; 69 class CefJavaScriptDialogRunner; 70 class CefMenuRunner; 71 72 // Provides platform-specific implementations of browser functionality. All 73 // methods are called on the browser process UI thread unless otherwise 74 // indicated. 75 class CefBrowserPlatformDelegate { 76 public: 77 CefBrowserPlatformDelegate(const CefBrowserPlatformDelegate&) = delete; 78 CefBrowserPlatformDelegate& operator=(const CefBrowserPlatformDelegate&) = 79 delete; 80 81 // Create a new CefBrowserPlatformDelegate instance. May be called on multiple 82 // threads. 83 static std::unique_ptr<CefBrowserPlatformDelegate> Create( 84 const CefBrowserCreateParams& create_params); 85 86 // Called from AlloyBrowserHostImpl::Create. 87 // Wait for the call to WebContentsCreated(owned=true) before taking ownership 88 // of the resulting WebContents object. 89 virtual content::WebContents* CreateWebContents( 90 CefBrowserCreateParams& create_params, 91 bool& own_web_contents); 92 93 // Called to create the view objects for a new WebContents. Will only be 94 // called a single time per instance. May be called on multiple threads. Only 95 // used with windowless rendering. 96 virtual void CreateViewForWebContents( 97 content::WebContentsView** view, 98 content::RenderViewHostDelegateView** delegate_view); 99 100 // Called after the WebContents for a browser has been created. |owned| will 101 // be true if |web_contents| was created via CreateWebContents() and we should 102 // take ownership. This will also be called for popup WebContents created 103 // indirectly by Chromium. Will only be called a single time per instance. 104 virtual void WebContentsCreated(content::WebContents* web_contents, 105 bool owned); 106 107 // Called when Chromium is ready to hand over ownership of a popup 108 // WebContents. WebContentsCreated(owned=false) will be called first for 109 // |new_contents|. Will only be called a single time per instance. See also 110 // the WebContentsDelegate documentation. 111 virtual void AddNewContents( 112 content::WebContents* source, 113 std::unique_ptr<content::WebContents> new_contents, 114 const GURL& target_url, 115 WindowOpenDisposition disposition, 116 const gfx::Rect& initial_rect, 117 bool user_gesture, 118 bool* was_blocked); 119 120 // Called when the WebContents is destroyed. This will be called before 121 // BrowserDestroyed(). Will only be called a single time per instance. 122 virtual void WebContentsDestroyed(content::WebContents* web_contents); 123 124 // See WebContentsDelegate documentation. 125 virtual bool ShouldAllowRendererInitiatedCrossProcessNavigation( 126 bool is_main_frame_navigation); 127 128 // Called after the RenderViewHost is created. 129 virtual void RenderViewCreated(content::RenderViewHost* render_view_host); 130 131 // See WebContentsObserver documentation. 132 virtual void RenderViewReady(); 133 134 // Called after the owning AlloyBrowserHostImpl is created. Will only be 135 // called a single time per instance. Do not send any client notifications 136 // from this method. 137 virtual void BrowserCreated(CefBrowserHostBase* browser); 138 139 // Called from AlloyBrowserHostImpl::Create. 140 virtual void CreateExtensionHost(const extensions::Extension* extension, 141 const GURL& url, 142 extensions::mojom::ViewType host_type); 143 144 // Returns the current extension host. 145 virtual extensions::ExtensionHost* GetExtensionHost() const; 146 147 // Send any notifications related to browser creation. Called after 148 // BrowserCreated(). 149 virtual void NotifyBrowserCreated(); 150 151 // Send any notifications related to browser destruction. Called before 152 // BrowserDestroyed(). 153 virtual void NotifyBrowserDestroyed(); 154 155 // Called before the owning AlloyBrowserHostImpl is destroyed. Will only be 156 // called a single time per instance. All references to the 157 // AlloyBrowserHostImpl and WebContents should be cleared when this method is 158 // called. Do not send any client notifications from this method. 159 virtual void BrowserDestroyed(CefBrowserHostBase* browser); 160 161 // Create the window that hosts the browser. Will only be called a single time 162 // per instance. Only used with windowed rendering. 163 virtual bool CreateHostWindow(); 164 165 // Sends a message to close the window that hosts the browser. On native 166 // platforms this will be done via the OS. DestroyBrowser will be called after 167 // the native window has closed. Only used with windowed rendering. 168 virtual void CloseHostWindow(); 169 170 // Return the OS handle for the window that hosts the browser. For windowed 171 // rendering this will return the most immediate parent window handle. For 172 // windowless rendering this will return the parent window handle specified by 173 // the client, which may be NULL. May be called on multiple threads. 174 virtual CefWindowHandle GetHostWindowHandle() const; 175 176 #if defined(TOOLKIT_VIEWS) 177 // Returns the Widget owner for the browser window. Only used with windowed 178 // rendering. 179 virtual views::Widget* GetWindowWidget() const; 180 181 // Returns the BrowserView associated with this browser. Only used with views- 182 // based browsers. 183 virtual CefRefPtr<CefBrowserView> GetBrowserView() const; 184 #endif 185 186 // Called after the WebContents have been created for a new popup browser 187 // parented to this browser but before the AlloyBrowserHostImpl is created for 188 // the popup. |is_devtools| will be true if the popup will host DevTools. This 189 // method will be called before WebContentsCreated() is called on 190 // |new_platform_delegate|. Do not make the new browser visible in this 191 // callback. 192 virtual void PopupWebContentsCreated( 193 const CefBrowserSettings& settings, 194 CefRefPtr<CefClient> client, 195 content::WebContents* new_web_contents, 196 CefBrowserPlatformDelegate* new_platform_delegate, 197 bool is_devtools); 198 199 // Called after the AlloyBrowserHostImpl is created for a new popup browser 200 // parented to this browser. |is_devtools| will be true if the popup will host 201 // DevTools. This method will be called immediately after 202 // CefLifeSpanHandler::OnAfterCreated() for the popup browser. It is safe to 203 // make the new browser visible in this callback (for example, add the browser 204 // to a window and show it). 205 virtual void PopupBrowserCreated(CefBrowserHostBase* new_browser, 206 bool is_devtools); 207 208 // Returns the background color for the browser. The alpha component will be 209 // either SK_AlphaTRANSPARENT or SK_AlphaOPAQUE (e.g. fully transparent or 210 // fully opaque). SK_AlphaOPAQUE will always be returned for windowed 211 // browsers. SK_ColorTRANSPARENT may be returned for windowless browsers to 212 // enable transparency. 213 virtual SkColor GetBackgroundColor() const; 214 215 // Notify the window that it was resized. 216 virtual void WasResized(); 217 218 // Send input events. 219 virtual void SendKeyEvent(const CefKeyEvent& event); 220 virtual void SendMouseClickEvent(const CefMouseEvent& event, 221 CefBrowserHost::MouseButtonType type, 222 bool mouseUp, 223 int clickCount); 224 virtual void SendMouseMoveEvent(const CefMouseEvent& event, bool mouseLeave); 225 virtual void SendMouseWheelEvent(const CefMouseEvent& event, 226 int deltaX, 227 int deltaY); 228 virtual void SendTouchEvent(const CefTouchEvent& event); 229 230 // Send focus event. The browser's WebContents may be NULL when this method is 231 // called. 232 virtual void SetFocus(bool setFocus); 233 234 // Send capture lost event. 235 virtual void SendCaptureLostEvent(); 236 237 #if BUILDFLAG(IS_WIN) || (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)) 238 // The window hosting the browser is about to be moved or resized. Only used 239 // on Windows and Linux. 240 virtual void NotifyMoveOrResizeStarted(); 241 242 // Resize the host window to the given dimensions. Only used with windowed 243 // rendering on Windows and Linux. 244 virtual void SizeTo(int width, int height); 245 #endif 246 247 // Convert from view coordinates to screen coordinates. Potential display 248 // scaling will be applied to the result. 249 virtual gfx::Point GetScreenPoint(const gfx::Point& view) const; 250 251 // Open the specified text in the default text editor. 252 virtual void ViewText(const std::string& text); 253 254 // Forward the keyboard event to the application or frame window to allow 255 // processing of shortcut keys. 256 virtual bool HandleKeyboardEvent( 257 const content::NativeWebKeyboardEvent& event); 258 259 // See WebContentsDelegate documentation. 260 virtual bool PreHandleGestureEvent(content::WebContents* source, 261 const blink::WebGestureEvent& event); 262 263 // See WebContentsDelegate documentation. 264 virtual bool IsNeverComposited(content::WebContents* web_contents); 265 266 // Invoke platform specific handling for the external protocol. 267 static void HandleExternalProtocol(const GURL& url); 268 269 // Returns the OS event handle, if any, associated with |event|. 270 virtual CefEventHandle GetEventHandle( 271 const content::NativeWebKeyboardEvent& event) const; 272 273 // Create the platform-specific file dialog runner. 274 virtual std::unique_ptr<CefFileDialogRunner> CreateFileDialogRunner(); 275 276 // Create the platform-specific JavaScript dialog runner. 277 virtual std::unique_ptr<CefJavaScriptDialogRunner> 278 CreateJavaScriptDialogRunner(); 279 280 // Create the platform-specific menu runner. 281 virtual std::unique_ptr<CefMenuRunner> CreateMenuRunner(); 282 283 // Returns true if this delegate implements windowless rendering. May be 284 // called on multiple threads. 285 virtual bool IsWindowless() const; 286 287 // Returns true if this delegate implements views-hosted browser handling. May 288 // be called on multiple threads. 289 virtual bool IsViewsHosted() const; 290 291 // Notify the browser that it was hidden. Only used with windowless rendering. 292 virtual void WasHidden(bool hidden); 293 294 // Notify the browser that screen information has changed. Only used with 295 // windowless rendering. 296 virtual void NotifyScreenInfoChanged(); 297 298 // Invalidate the view. Only used with windowless rendering. 299 virtual void Invalidate(cef_paint_element_type_t type); 300 301 // Send the external begin frame message. Only used with windowless rendering. 302 virtual void SendExternalBeginFrame(); 303 304 // Set the windowless frame rate. Only used with windowless rendering. 305 virtual void SetWindowlessFrameRate(int frame_rate); 306 307 // IME-related callbacks. See documentation in CefBrowser and 308 // CefRenderHandler. Only used with windowless rendering. 309 virtual void ImeSetComposition( 310 const CefString& text, 311 const std::vector<CefCompositionUnderline>& underlines, 312 const CefRange& replacement_range, 313 const CefRange& selection_range); 314 virtual void ImeCommitText(const CefString& text, 315 const CefRange& replacement_range, 316 int relative_cursor_pos); 317 virtual void ImeFinishComposingText(bool keep_selection); 318 virtual void ImeCancelComposition(); 319 320 // Drag/drop-related callbacks. See documentation in CefRenderHandler. Only 321 // used with windowless rendering. 322 virtual void DragTargetDragEnter(CefRefPtr<CefDragData> drag_data, 323 const CefMouseEvent& event, 324 cef_drag_operations_mask_t allowed_ops); 325 virtual void DragTargetDragOver(const CefMouseEvent& event, 326 cef_drag_operations_mask_t allowed_ops); 327 virtual void DragTargetDragLeave(); 328 virtual void DragTargetDrop(const CefMouseEvent& event); 329 virtual void StartDragging( 330 const content::DropData& drop_data, 331 blink::DragOperationsMask allowed_ops, 332 const gfx::ImageSkia& image, 333 const gfx::Vector2d& image_offset, 334 const blink::mojom::DragEventSourceInfo& event_info, 335 content::RenderWidgetHostImpl* source_rwh); 336 virtual void UpdateDragCursor(ui::mojom::DragOperation operation); 337 virtual void DragSourceEndedAt(int x, int y, cef_drag_operations_mask_t op); 338 virtual void DragSourceSystemDragEnded(); 339 virtual void AccessibilityEventReceived( 340 const content::AXEventNotificationDetails& eventData); 341 virtual void AccessibilityLocationChangesReceived( 342 const std::vector<content::AXLocationChangeNotificationDetails>& locData); 343 virtual gfx::Point GetDialogPosition(const gfx::Size& size); 344 virtual gfx::Size GetMaximumDialogSize(); 345 346 // See CefBrowserHost documentation. 347 virtual void SetAutoResizeEnabled(bool enabled, 348 const CefSize& min_size, 349 const CefSize& max_size); 350 virtual void SetAccessibilityState(cef_state_t accessibility_state); 351 virtual bool IsPrintPreviewSupported() const; 352 virtual void Print(); 353 virtual void PrintToPDF(const CefString& path, 354 const CefPdfPrintSettings& settings, 355 CefRefPtr<CefPdfPrintCallback> callback); 356 virtual void Find(const CefString& searchText, 357 bool forward, 358 bool matchCase, 359 bool findNext); 360 virtual void StopFinding(bool clearSelection); 361 362 protected: 363 // Allow deletion via std::unique_ptr only. 364 friend std::default_delete<CefBrowserPlatformDelegate>; 365 366 CefBrowserPlatformDelegate(); 367 virtual ~CefBrowserPlatformDelegate(); 368 369 static int TranslateWebEventModifiers(uint32 cef_modifiers); 370 371 // Not owned by this object. 372 content::WebContents* web_contents_ = nullptr; 373 CefBrowserHostBase* browser_ = nullptr; 374 }; 375 376 #endif // CEF_LIBCEF_BROWSER_BROWSER_PLATFORM_DELEGATE_H_ 377