// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. #ifndef CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_ #define CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_ #pragma once #include #include #include "include/cef_client.h" #include "include/wrapper/cef_helpers.h" #include "include/wrapper/cef_message_router.h" #include "include/wrapper/cef_resource_manager.h" #include "tests/cefclient/browser/client_types.h" #include "tests/cefclient/browser/test_runner.h" #if defined(OS_LINUX) #include "tests/cefclient/browser/dialog_handler_gtk.h" #include "tests/cefclient/browser/print_handler_gtk.h" #endif namespace client { class ClientDownloadImageCallback; // Client handler abstract base class. Provides common functionality shared by // all concrete client handler implementations. class ClientHandler : public CefClient, public CefContextMenuHandler, public CefDisplayHandler, public CefDownloadHandler, public CefDragHandler, public CefFocusHandler, public CefKeyboardHandler, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler, public CefResourceRequestHandler { public: // Implement this interface to receive notification of ClientHandler // events. The methods of this class will be called on the main thread unless // otherwise indicated. class Delegate { public: // Called when the browser is created. virtual void OnBrowserCreated(CefRefPtr browser) = 0; // Called when the browser is closing. virtual void OnBrowserClosing(CefRefPtr browser) = 0; // Called when the browser has been closed. virtual void OnBrowserClosed(CefRefPtr browser) = 0; // Set the window URL address. virtual void OnSetAddress(const std::string& url) = 0; // Set the window title. virtual void OnSetTitle(const std::string& title) = 0; // Set the Favicon image. virtual void OnSetFavicon(CefRefPtr image) {} // Set fullscreen mode. virtual void OnSetFullscreen(bool fullscreen) = 0; // Auto-resize contents. virtual void OnAutoResize(const CefSize& new_size) = 0; // Set the loading state. virtual void OnSetLoadingState(bool isLoading, bool canGoBack, bool canGoForward) = 0; // Set the draggable regions. virtual void OnSetDraggableRegions( const std::vector& regions) = 0; // Set focus to the next/previous control. virtual void OnTakeFocus(bool next) {} // Called on the UI thread before a context menu is displayed. virtual void OnBeforeContextMenu(CefRefPtr model) {} protected: virtual ~Delegate() {} }; typedef std::set MessageHandlerSet; // Constructor may be called on any thread. // |delegate| must outlive this object or DetachDelegate() must be called. ClientHandler(Delegate* delegate, bool is_osr, const std::string& startup_url); // This object may outlive the Delegate object so it's necessary for the // Delegate to detach itself before destruction. void DetachDelegate(); // CefClient methods CefRefPtr GetContextMenuHandler() override { return this; } CefRefPtr GetDisplayHandler() override { return this; } CefRefPtr GetDownloadHandler() override { return this; } CefRefPtr GetDragHandler() override { return this; } CefRefPtr GetFocusHandler() override { return this; } CefRefPtr GetKeyboardHandler() override { return this; } CefRefPtr GetLifeSpanHandler() override { return this; } CefRefPtr GetLoadHandler() override { return this; } CefRefPtr GetRequestHandler() override { return this; } bool OnProcessMessageReceived(CefRefPtr browser, CefRefPtr frame, CefProcessId source_process, CefRefPtr message) override; #if defined(OS_LINUX) CefRefPtr GetDialogHandler() override { return dialog_handler_; } CefRefPtr GetJSDialogHandler() override { return dialog_handler_; } CefRefPtr GetPrintHandler() override { return print_handler_; } #endif // CefContextMenuHandler methods void OnBeforeContextMenu(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, CefRefPtr model) override; bool OnContextMenuCommand(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, int command_id, EventFlags event_flags) override; // CefDisplayHandler methods void OnAddressChange(CefRefPtr browser, CefRefPtr frame, const CefString& url) override; void OnTitleChange(CefRefPtr browser, const CefString& title) override; void OnFaviconURLChange(CefRefPtr browser, const std::vector& icon_urls) override; void OnFullscreenModeChange(CefRefPtr browser, bool fullscreen) override; bool OnConsoleMessage(CefRefPtr browser, cef_log_severity_t level, const CefString& message, const CefString& source, int line) override; bool OnAutoResize(CefRefPtr browser, const CefSize& new_size) override; bool OnCursorChange(CefRefPtr browser, CefCursorHandle cursor, cef_cursor_type_t type, const CefCursorInfo& custom_cursor_info) override; // CefDownloadHandler methods void OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item, const CefString& suggested_name, CefRefPtr callback) override; void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) override; // CefDragHandler methods bool OnDragEnter(CefRefPtr browser, CefRefPtr dragData, CefDragHandler::DragOperationsMask mask) override; void OnDraggableRegionsChanged( CefRefPtr browser, CefRefPtr frame, const std::vector& regions) override; // CefFocusHandler methods void OnTakeFocus(CefRefPtr browser, bool next) override; bool OnSetFocus(CefRefPtr browser, FocusSource source) override; // CefKeyboardHandler methods bool OnPreKeyEvent(CefRefPtr browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut) override; // CefLifeSpanHandler methods bool OnBeforePopup( CefRefPtr browser, CefRefPtr frame, const CefString& target_url, const CefString& target_frame_name, CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr& client, CefBrowserSettings& settings, CefRefPtr& extra_info, bool* no_javascript_access) override; void OnAfterCreated(CefRefPtr browser) override; bool DoClose(CefRefPtr browser) override; void OnBeforeClose(CefRefPtr browser) override; // CefLoadHandler methods void OnLoadingStateChange(CefRefPtr browser, bool isLoading, bool canGoBack, bool canGoForward) override; void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) override; // CefRequestHandler methods bool OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool user_gesture, bool is_redirect) override; bool OnOpenURLFromTab( CefRefPtr browser, CefRefPtr frame, const CefString& target_url, CefRequestHandler::WindowOpenDisposition target_disposition, bool user_gesture) override; CefRefPtr GetResourceRequestHandler( CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool is_navigation, bool is_download, const CefString& request_initiator, bool& disable_default_handling) override; bool GetAuthCredentials(CefRefPtr browser, const CefString& origin_url, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr callback) override; bool OnQuotaRequest(CefRefPtr browser, const CefString& origin_url, int64 new_size, CefRefPtr callback) override; bool OnCertificateError(CefRefPtr browser, ErrorCode cert_error, const CefString& request_url, CefRefPtr ssl_info, CefRefPtr callback) override; bool OnSelectClientCertificate( CefRefPtr browser, bool isProxy, const CefString& host, int port, const X509CertificateList& certificates, CefRefPtr callback) override; void OnRenderProcessTerminated(CefRefPtr browser, TerminationStatus status) override; void OnDocumentAvailableInMainFrame(CefRefPtr browser) override; // CefResourceRequestHandler methods cef_return_value_t OnBeforeResourceLoad( CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr callback) override; CefRefPtr GetResourceHandler( CefRefPtr browser, CefRefPtr frame, CefRefPtr request) override; CefRefPtr GetResourceResponseFilter( CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response) override; void OnProtocolExecution(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool& allow_os_execution) override; // Returns the number of browsers currently using this handler. Can only be // called on the CEF UI thread. int GetBrowserCount() const; // Show a new DevTools popup window. void ShowDevTools(CefRefPtr browser, const CefPoint& inspect_element_at); // Close the existing DevTools popup window, if any. void CloseDevTools(CefRefPtr browser); // Test if the current site has SSL information available. bool HasSSLInformation(CefRefPtr browser); // Show SSL information for the current site. void ShowSSLInformation(CefRefPtr browser); // Set a string resource for loading via StringResourceProvider. void SetStringResource(const std::string& page, const std::string& data); // Returns the Delegate. Delegate* delegate() const { return delegate_; } // Returns the startup URL. std::string startup_url() const { return startup_url_; } // Returns true if this handler uses off-screen rendering. bool is_osr() const { return is_osr_; } // Set/get whether the client should download favicon images. Only safe to // call immediately after client creation or on the browser process UI thread. bool download_favicon_images() const { return download_favicon_images_; } void set_download_favicon_images(bool allow) { download_favicon_images_ = allow; } private: friend class ClientDownloadImageCallback; // Create a new popup window using the specified information. |is_devtools| // will be true if the window will be used for DevTools. Return true to // proceed with popup browser creation or false to cancel the popup browser. // May be called on any thead. bool CreatePopupWindow(CefRefPtr browser, bool is_devtools, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr& client, CefBrowserSettings& settings); // Execute Delegate notifications on the main thread. void NotifyBrowserCreated(CefRefPtr browser); void NotifyBrowserClosing(CefRefPtr browser); void NotifyBrowserClosed(CefRefPtr browser); void NotifyAddress(const CefString& url); void NotifyTitle(const CefString& title); void NotifyFavicon(CefRefPtr image); void NotifyFullscreen(bool fullscreen); void NotifyAutoResize(const CefSize& new_size); void NotifyLoadingState(bool isLoading, bool canGoBack, bool canGoForward); void NotifyDraggableRegions(const std::vector& regions); void NotifyTakeFocus(bool next); // Test context menu creation. void BuildTestMenu(CefRefPtr model); bool ExecuteTestMenu(int command_id); void SetOfflineState(CefRefPtr browser, bool offline); // THREAD SAFE MEMBERS // The following members may be accessed from any thread. // True if this handler uses off-screen rendering. const bool is_osr_; // The startup URL. const std::string startup_url_; // True if mouse cursor change is disabled. bool mouse_cursor_change_disabled_; // True if the browser is currently offline. bool offline_; // True if Favicon images should be downloaded. bool download_favicon_images_; #if defined(OS_LINUX) // Custom dialog handler for GTK. CefRefPtr dialog_handler_; CefRefPtr print_handler_; #endif // Handles the browser side of query routing. The renderer side is handled // in client_renderer.cc. CefRefPtr message_router_; // Manages the registration and delivery of resources. CefRefPtr resource_manager_; // Used to manage string resources in combination with StringResourceProvider. // Only accessed on the IO thread. test_runner::StringResourceMap string_resource_map_; // MAIN THREAD MEMBERS // The following members will only be accessed on the main thread. This will // be the same as the CEF UI thread except when using multi-threaded message // loop mode on Windows. Delegate* delegate_; // UI THREAD MEMBERS // The following members will only be accessed on the CEF UI thread. // Track state information for the text context menu. struct TestMenuState { TestMenuState() : check_item(true), radio_item(0) {} bool check_item; int radio_item; } test_menu_state_; // The current number of browsers using this handler. int browser_count_; // Console logging state. const std::string console_log_file_; bool first_console_message_; // True if an editable field currently has focus. bool focus_on_editable_field_; // True for the initial navigation after browser creation. bool initial_navigation_; // Set of Handlers registered with the message router. MessageHandlerSet message_handler_set_; DISALLOW_COPY_AND_ASSIGN(ClientHandler); }; } // namespace client #endif // CEF_TESTS_CEFCLIENT_BROWSER_CLIENT_HANDLER_H_