• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #ifndef CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_
7 #pragma once
8 
9 #include <set>
10 #include <string>
11 
12 #include "include/cef_client.h"
13 #include "include/wrapper/cef_helpers.h"
14 #include "include/wrapper/cef_message_router.h"
15 #include "include/wrapper/cef_resource_manager.h"
16 #include "tests/cefclient/browser/client_types.h"
17 #include "tests/cefclient/browser/test_runner.h"
18 
19 #if defined(OS_LINUX)
20 #include "tests/cefclient/browser/dialog_handler_gtk.h"
21 #include "tests/cefclient/browser/print_handler_gtk.h"
22 #endif
23 
24 namespace client {
25 
26 class ClientDownloadImageCallback;
27 
28 // Client handler abstract base class. Provides common functionality shared by
29 // all concrete client handler implementations.
30 class ClientHandler : public CefClient,
31                       public CefContextMenuHandler,
32                       public CefDisplayHandler,
33                       public CefDownloadHandler,
34                       public CefDragHandler,
35                       public CefFocusHandler,
36                       public CefKeyboardHandler,
37                       public CefLifeSpanHandler,
38                       public CefLoadHandler,
39                       public CefRequestHandler,
40                       public CefResourceRequestHandler {
41  public:
42   // Implement this interface to receive notification of ClientHandler
43   // events. The methods of this class will be called on the main thread unless
44   // otherwise indicated.
45   class Delegate {
46    public:
47     // Called when the browser is created.
48     virtual void OnBrowserCreated(CefRefPtr<CefBrowser> browser) = 0;
49 
50     // Called when the browser is closing.
51     virtual void OnBrowserClosing(CefRefPtr<CefBrowser> browser) = 0;
52 
53     // Called when the browser has been closed.
54     virtual void OnBrowserClosed(CefRefPtr<CefBrowser> browser) = 0;
55 
56     // Set the window URL address.
57     virtual void OnSetAddress(const std::string& url) = 0;
58 
59     // Set the window title.
60     virtual void OnSetTitle(const std::string& title) = 0;
61 
62     // Set the Favicon image.
OnSetFavicon(CefRefPtr<CefImage> image)63     virtual void OnSetFavicon(CefRefPtr<CefImage> image) {}
64 
65     // Set fullscreen mode.
66     virtual void OnSetFullscreen(bool fullscreen) = 0;
67 
68     // Auto-resize contents.
69     virtual void OnAutoResize(const CefSize& new_size) = 0;
70 
71     // Set the loading state.
72     virtual void OnSetLoadingState(bool isLoading,
73                                    bool canGoBack,
74                                    bool canGoForward) = 0;
75 
76     // Set the draggable regions.
77     virtual void OnSetDraggableRegions(
78         const std::vector<CefDraggableRegion>& regions) = 0;
79 
80     // Set focus to the next/previous control.
OnTakeFocus(bool next)81     virtual void OnTakeFocus(bool next) {}
82 
83     // Called on the UI thread before a context menu is displayed.
OnBeforeContextMenu(CefRefPtr<CefMenuModel> model)84     virtual void OnBeforeContextMenu(CefRefPtr<CefMenuModel> model) {}
85 
86    protected:
~Delegate()87     virtual ~Delegate() {}
88   };
89 
90   typedef std::set<CefMessageRouterBrowserSide::Handler*> MessageHandlerSet;
91 
92   // Constructor may be called on any thread.
93   // |delegate| must outlive this object or DetachDelegate() must be called.
94   ClientHandler(Delegate* delegate,
95                 bool is_osr,
96                 const std::string& startup_url);
97 
98   // This object may outlive the Delegate object so it's necessary for the
99   // Delegate to detach itself before destruction.
100   void DetachDelegate();
101 
102   // CefClient methods
GetContextMenuHandler()103   CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() override {
104     return this;
105   }
GetDisplayHandler()106   CefRefPtr<CefDisplayHandler> GetDisplayHandler() override { return this; }
GetDownloadHandler()107   CefRefPtr<CefDownloadHandler> GetDownloadHandler() override { return this; }
GetDragHandler()108   CefRefPtr<CefDragHandler> GetDragHandler() override { return this; }
GetFocusHandler()109   CefRefPtr<CefFocusHandler> GetFocusHandler() override { return this; }
GetKeyboardHandler()110   CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() override { return this; }
GetLifeSpanHandler()111   CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }
GetLoadHandler()112   CefRefPtr<CefLoadHandler> GetLoadHandler() override { return this; }
GetRequestHandler()113   CefRefPtr<CefRequestHandler> GetRequestHandler() override { return this; }
114   bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
115                                 CefRefPtr<CefFrame> frame,
116                                 CefProcessId source_process,
117                                 CefRefPtr<CefProcessMessage> message) override;
118 
119 #if defined(OS_LINUX)
GetDialogHandler()120   CefRefPtr<CefDialogHandler> GetDialogHandler() override {
121     return dialog_handler_;
122   }
GetJSDialogHandler()123   CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override {
124     return dialog_handler_;
125   }
GetPrintHandler()126   CefRefPtr<CefPrintHandler> GetPrintHandler() override {
127     return print_handler_;
128   }
129 #endif
130 
131   // CefContextMenuHandler methods
132   void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
133                            CefRefPtr<CefFrame> frame,
134                            CefRefPtr<CefContextMenuParams> params,
135                            CefRefPtr<CefMenuModel> model) override;
136   bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
137                             CefRefPtr<CefFrame> frame,
138                             CefRefPtr<CefContextMenuParams> params,
139                             int command_id,
140                             EventFlags event_flags) override;
141 
142   // CefDisplayHandler methods
143   void OnAddressChange(CefRefPtr<CefBrowser> browser,
144                        CefRefPtr<CefFrame> frame,
145                        const CefString& url) override;
146   void OnTitleChange(CefRefPtr<CefBrowser> browser,
147                      const CefString& title) override;
148   void OnFaviconURLChange(CefRefPtr<CefBrowser> browser,
149                           const std::vector<CefString>& icon_urls) override;
150   void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
151                               bool fullscreen) override;
152   bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
153                         cef_log_severity_t level,
154                         const CefString& message,
155                         const CefString& source,
156                         int line) override;
157   bool OnAutoResize(CefRefPtr<CefBrowser> browser,
158                     const CefSize& new_size) override;
159   bool OnCursorChange(CefRefPtr<CefBrowser> browser,
160                       CefCursorHandle cursor,
161                       cef_cursor_type_t type,
162                       const CefCursorInfo& custom_cursor_info) override;
163 
164   // CefDownloadHandler methods
165   void OnBeforeDownload(CefRefPtr<CefBrowser> browser,
166                         CefRefPtr<CefDownloadItem> download_item,
167                         const CefString& suggested_name,
168                         CefRefPtr<CefBeforeDownloadCallback> callback) override;
169   void OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
170                          CefRefPtr<CefDownloadItem> download_item,
171                          CefRefPtr<CefDownloadItemCallback> callback) override;
172 
173   // CefDragHandler methods
174   bool OnDragEnter(CefRefPtr<CefBrowser> browser,
175                    CefRefPtr<CefDragData> dragData,
176                    CefDragHandler::DragOperationsMask mask) override;
177   void OnDraggableRegionsChanged(
178       CefRefPtr<CefBrowser> browser,
179       CefRefPtr<CefFrame> frame,
180       const std::vector<CefDraggableRegion>& regions) override;
181 
182   // CefFocusHandler methods
183   void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) override;
184   bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) override;
185 
186   // CefKeyboardHandler methods
187   bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
188                      const CefKeyEvent& event,
189                      CefEventHandle os_event,
190                      bool* is_keyboard_shortcut) override;
191 
192   // CefLifeSpanHandler methods
193   bool OnBeforePopup(
194       CefRefPtr<CefBrowser> browser,
195       CefRefPtr<CefFrame> frame,
196       const CefString& target_url,
197       const CefString& target_frame_name,
198       CefLifeSpanHandler::WindowOpenDisposition target_disposition,
199       bool user_gesture,
200       const CefPopupFeatures& popupFeatures,
201       CefWindowInfo& windowInfo,
202       CefRefPtr<CefClient>& client,
203       CefBrowserSettings& settings,
204       CefRefPtr<CefDictionaryValue>& extra_info,
205       bool* no_javascript_access) override;
206   void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
207   bool DoClose(CefRefPtr<CefBrowser> browser) override;
208   void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
209 
210   // CefLoadHandler methods
211   void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
212                             bool isLoading,
213                             bool canGoBack,
214                             bool canGoForward) override;
215   void OnLoadError(CefRefPtr<CefBrowser> browser,
216                    CefRefPtr<CefFrame> frame,
217                    ErrorCode errorCode,
218                    const CefString& errorText,
219                    const CefString& failedUrl) override;
220 
221   // CefRequestHandler methods
222   bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
223                       CefRefPtr<CefFrame> frame,
224                       CefRefPtr<CefRequest> request,
225                       bool user_gesture,
226                       bool is_redirect) override;
227   bool OnOpenURLFromTab(
228       CefRefPtr<CefBrowser> browser,
229       CefRefPtr<CefFrame> frame,
230       const CefString& target_url,
231       CefRequestHandler::WindowOpenDisposition target_disposition,
232       bool user_gesture) override;
233   CefRefPtr<CefResourceRequestHandler> GetResourceRequestHandler(
234       CefRefPtr<CefBrowser> browser,
235       CefRefPtr<CefFrame> frame,
236       CefRefPtr<CefRequest> request,
237       bool is_navigation,
238       bool is_download,
239       const CefString& request_initiator,
240       bool& disable_default_handling) override;
241   bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,
242                           const CefString& origin_url,
243                           bool isProxy,
244                           const CefString& host,
245                           int port,
246                           const CefString& realm,
247                           const CefString& scheme,
248                           CefRefPtr<CefAuthCallback> callback) override;
249   bool OnQuotaRequest(CefRefPtr<CefBrowser> browser,
250                       const CefString& origin_url,
251                       int64 new_size,
252                       CefRefPtr<CefCallback> callback) override;
253   bool OnCertificateError(CefRefPtr<CefBrowser> browser,
254                           ErrorCode cert_error,
255                           const CefString& request_url,
256                           CefRefPtr<CefSSLInfo> ssl_info,
257                           CefRefPtr<CefCallback> callback) override;
258   bool OnSelectClientCertificate(
259       CefRefPtr<CefBrowser> browser,
260       bool isProxy,
261       const CefString& host,
262       int port,
263       const X509CertificateList& certificates,
264       CefRefPtr<CefSelectClientCertificateCallback> callback) override;
265   void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
266                                  TerminationStatus status) override;
267   void OnDocumentAvailableInMainFrame(CefRefPtr<CefBrowser> browser) override;
268 
269   // CefResourceRequestHandler methods
270   cef_return_value_t OnBeforeResourceLoad(
271       CefRefPtr<CefBrowser> browser,
272       CefRefPtr<CefFrame> frame,
273       CefRefPtr<CefRequest> request,
274       CefRefPtr<CefCallback> callback) override;
275   CefRefPtr<CefResourceHandler> GetResourceHandler(
276       CefRefPtr<CefBrowser> browser,
277       CefRefPtr<CefFrame> frame,
278       CefRefPtr<CefRequest> request) override;
279   CefRefPtr<CefResponseFilter> GetResourceResponseFilter(
280       CefRefPtr<CefBrowser> browser,
281       CefRefPtr<CefFrame> frame,
282       CefRefPtr<CefRequest> request,
283       CefRefPtr<CefResponse> response) override;
284   void OnProtocolExecution(CefRefPtr<CefBrowser> browser,
285                            CefRefPtr<CefFrame> frame,
286                            CefRefPtr<CefRequest> request,
287                            bool& allow_os_execution) override;
288 
289   // Returns the number of browsers currently using this handler. Can only be
290   // called on the CEF UI thread.
291   int GetBrowserCount() const;
292 
293   // Show a new DevTools popup window.
294   void ShowDevTools(CefRefPtr<CefBrowser> browser,
295                     const CefPoint& inspect_element_at);
296 
297   // Close the existing DevTools popup window, if any.
298   void CloseDevTools(CefRefPtr<CefBrowser> browser);
299 
300   // Test if the current site has SSL information available.
301   bool HasSSLInformation(CefRefPtr<CefBrowser> browser);
302 
303   // Show SSL information for the current site.
304   void ShowSSLInformation(CefRefPtr<CefBrowser> browser);
305 
306   // Set a string resource for loading via StringResourceProvider.
307   void SetStringResource(const std::string& page, const std::string& data);
308 
309   // Returns the Delegate.
delegate()310   Delegate* delegate() const { return delegate_; }
311 
312   // Returns the startup URL.
startup_url()313   std::string startup_url() const { return startup_url_; }
314 
315   // Returns true if this handler uses off-screen rendering.
is_osr()316   bool is_osr() const { return is_osr_; }
317 
318   // Set/get whether the client should download favicon images. Only safe to
319   // call immediately after client creation or on the browser process UI thread.
download_favicon_images()320   bool download_favicon_images() const { return download_favicon_images_; }
set_download_favicon_images(bool allow)321   void set_download_favicon_images(bool allow) {
322     download_favicon_images_ = allow;
323   }
324 
325  private:
326   friend class ClientDownloadImageCallback;
327 
328   // Create a new popup window using the specified information. |is_devtools|
329   // will be true if the window will be used for DevTools. Return true to
330   // proceed with popup browser creation or false to cancel the popup browser.
331   // May be called on any thead.
332   bool CreatePopupWindow(CefRefPtr<CefBrowser> browser,
333                          bool is_devtools,
334                          const CefPopupFeatures& popupFeatures,
335                          CefWindowInfo& windowInfo,
336                          CefRefPtr<CefClient>& client,
337                          CefBrowserSettings& settings);
338 
339   // Execute Delegate notifications on the main thread.
340   void NotifyBrowserCreated(CefRefPtr<CefBrowser> browser);
341   void NotifyBrowserClosing(CefRefPtr<CefBrowser> browser);
342   void NotifyBrowserClosed(CefRefPtr<CefBrowser> browser);
343   void NotifyAddress(const CefString& url);
344   void NotifyTitle(const CefString& title);
345   void NotifyFavicon(CefRefPtr<CefImage> image);
346   void NotifyFullscreen(bool fullscreen);
347   void NotifyAutoResize(const CefSize& new_size);
348   void NotifyLoadingState(bool isLoading, bool canGoBack, bool canGoForward);
349   void NotifyDraggableRegions(const std::vector<CefDraggableRegion>& regions);
350   void NotifyTakeFocus(bool next);
351 
352   // Test context menu creation.
353   void BuildTestMenu(CefRefPtr<CefMenuModel> model);
354   bool ExecuteTestMenu(int command_id);
355 
356   void SetOfflineState(CefRefPtr<CefBrowser> browser, bool offline);
357 
358   // THREAD SAFE MEMBERS
359   // The following members may be accessed from any thread.
360 
361   // True if this handler uses off-screen rendering.
362   const bool is_osr_;
363 
364   // The startup URL.
365   const std::string startup_url_;
366 
367   // True if mouse cursor change is disabled.
368   bool mouse_cursor_change_disabled_;
369 
370   // True if the browser is currently offline.
371   bool offline_;
372 
373   // True if Favicon images should be downloaded.
374   bool download_favicon_images_;
375 
376 #if defined(OS_LINUX)
377   // Custom dialog handler for GTK.
378   CefRefPtr<ClientDialogHandlerGtk> dialog_handler_;
379   CefRefPtr<ClientPrintHandlerGtk> print_handler_;
380 #endif
381 
382   // Handles the browser side of query routing. The renderer side is handled
383   // in client_renderer.cc.
384   CefRefPtr<CefMessageRouterBrowserSide> message_router_;
385 
386   // Manages the registration and delivery of resources.
387   CefRefPtr<CefResourceManager> resource_manager_;
388 
389   // Used to manage string resources in combination with StringResourceProvider.
390   // Only accessed on the IO thread.
391   test_runner::StringResourceMap string_resource_map_;
392 
393   // MAIN THREAD MEMBERS
394   // The following members will only be accessed on the main thread. This will
395   // be the same as the CEF UI thread except when using multi-threaded message
396   // loop mode on Windows.
397 
398   Delegate* delegate_;
399 
400   // UI THREAD MEMBERS
401   // The following members will only be accessed on the CEF UI thread.
402 
403   // Track state information for the text context menu.
404   struct TestMenuState {
TestMenuStateTestMenuState405     TestMenuState() : check_item(true), radio_item(0) {}
406     bool check_item;
407     int radio_item;
408   } test_menu_state_;
409 
410   // The current number of browsers using this handler.
411   int browser_count_;
412 
413   // Console logging state.
414   const std::string console_log_file_;
415   bool first_console_message_;
416 
417   // True if an editable field currently has focus.
418   bool focus_on_editable_field_;
419 
420   // True for the initial navigation after browser creation.
421   bool initial_navigation_;
422 
423   // Set of Handlers registered with the message router.
424   MessageHandlerSet message_handler_set_;
425 
426   DISALLOW_COPY_AND_ASSIGN(ClientHandler);
427 };
428 
429 }  // namespace client
430 
431 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_
432