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