// 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_CLIENT_HANDLER_H_ #define CEF_TESTS_CEFCLIENT_CLIENT_HANDLER_H_ #pragma once #include #include #include #include #include "include/base/cef_lock.h" #include "include/cef_client.h" #include "include/wrapper/cef_helpers.h" #include "include/wrapper/cef_message_router.h" #if defined(OS_LINUX) // The Linux client uses GTK instead of the underlying platform type (X11). #include #define ClientWindowHandle GtkWidget* #else #define ClientWindowHandle CefWindowHandle #endif // Define this value to redirect all popup URLs to the main application browser // window. // #define TEST_REDIRECT_POPUP_URLS // ClientHandler implementation. class ClientHandler : public CefClient, public CefContextMenuHandler, public CefDialogHandler, public CefDisplayHandler, public CefDownloadHandler, public CefDragHandler, public CefGeolocationHandler, public CefJSDialogHandler, public CefKeyboardHandler, public CefLifeSpanHandler, public CefLoadHandler, public CefRenderHandler, public CefRequestHandler { public: // Interface implemented to handle off-screen rendering. class RenderHandler : public CefRenderHandler { public: virtual void OnBeforeClose(CefRefPtr browser) =0; }; typedef std::set MessageHandlerSet; ClientHandler(); virtual ~ClientHandler(); // CefClient methods virtual CefRefPtr GetContextMenuHandler() OVERRIDE { return this; } virtual CefRefPtr GetDialogHandler() OVERRIDE { return this; } virtual CefRefPtr GetDisplayHandler() OVERRIDE { return this; } virtual CefRefPtr GetDownloadHandler() OVERRIDE { return this; } virtual CefRefPtr GetDragHandler() OVERRIDE { return this; } virtual CefRefPtr GetGeolocationHandler() OVERRIDE { return this; } virtual CefRefPtr GetJSDialogHandler() OVERRIDE { return this; } virtual CefRefPtr GetKeyboardHandler() OVERRIDE { return this; } virtual CefRefPtr GetLifeSpanHandler() OVERRIDE { return this; } virtual CefRefPtr GetLoadHandler() OVERRIDE { return this; } virtual CefRefPtr GetRenderHandler() OVERRIDE { return this; } virtual CefRefPtr GetRequestHandler() OVERRIDE { return this; } virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) OVERRIDE; // CefContextMenuHandler methods virtual void OnBeforeContextMenu(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, CefRefPtr model) OVERRIDE; virtual bool OnContextMenuCommand(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, int command_id, EventFlags event_flags) OVERRIDE; // CefDialogHandler methods virtual bool OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, const CefString& default_file_name, const std::vector& accept_types, CefRefPtr callback) OVERRIDE; // CefDisplayHandler methods virtual void OnAddressChange(CefRefPtr browser, CefRefPtr frame, const CefString& url) OVERRIDE; virtual void OnTitleChange(CefRefPtr browser, const CefString& title) OVERRIDE; virtual bool OnConsoleMessage(CefRefPtr browser, const CefString& message, const CefString& source, int line) OVERRIDE; // CefDownloadHandler methods virtual void OnBeforeDownload( CefRefPtr browser, CefRefPtr download_item, const CefString& suggested_name, CefRefPtr callback) OVERRIDE; virtual void OnDownloadUpdated( CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE; // CefDragHandler methods virtual bool OnDragEnter(CefRefPtr browser, CefRefPtr dragData, CefDragHandler::DragOperationsMask mask) OVERRIDE; // CefGeolocationHandler methods virtual bool OnRequestGeolocationPermission( CefRefPtr browser, const CefString& requesting_url, int request_id, CefRefPtr callback) OVERRIDE; // CefJSDialogHandler methods virtual bool OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text, CefRefPtr callback, bool& suppress_message) OVERRIDE; virtual bool OnBeforeUnloadDialog( CefRefPtr browser, const CefString& message_text, bool is_reload, CefRefPtr callback) OVERRIDE; virtual void OnResetDialogState(CefRefPtr browser) OVERRIDE; // CefKeyboardHandler methods virtual bool OnPreKeyEvent(CefRefPtr browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut) OVERRIDE; // CefLifeSpanHandler methods virtual bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, const CefString& target_url, const CefString& target_frame_name, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr& client, CefBrowserSettings& settings, bool* no_javascript_access) OVERRIDE; virtual void OnAfterCreated(CefRefPtr browser) OVERRIDE; virtual bool DoClose(CefRefPtr browser) OVERRIDE; virtual void OnBeforeClose(CefRefPtr browser) OVERRIDE; // CefLoadHandler methods virtual void OnLoadingStateChange(CefRefPtr browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE; virtual void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE; // CefRequestHandler methods virtual bool OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool is_redirect) OVERRIDE; virtual CefRefPtr GetResourceHandler( CefRefPtr browser, CefRefPtr frame, CefRefPtr request) OVERRIDE; virtual bool OnQuotaRequest(CefRefPtr browser, const CefString& origin_url, int64 new_size, CefRefPtr callback) OVERRIDE; virtual void OnProtocolExecution(CefRefPtr browser, const CefString& url, bool& allow_os_execution) OVERRIDE; virtual void OnRenderProcessTerminated(CefRefPtr browser, TerminationStatus status) OVERRIDE; // CefRenderHandler methods virtual bool GetRootScreenRect(CefRefPtr browser, CefRect& rect) OVERRIDE; virtual bool GetViewRect(CefRefPtr browser, CefRect& rect) OVERRIDE; virtual bool GetScreenPoint(CefRefPtr browser, int viewX, int viewY, int& screenX, int& screenY) OVERRIDE; virtual bool GetScreenInfo(CefRefPtr browser, CefScreenInfo& screen_info) OVERRIDE; virtual void OnPopupShow(CefRefPtr browser, bool show) OVERRIDE; virtual void OnPopupSize(CefRefPtr browser, const CefRect& rect) OVERRIDE; virtual void OnPaint(CefRefPtr browser, PaintElementType type, const RectList& dirtyRects, const void* buffer, int width, int height) OVERRIDE; virtual void OnCursorChange(CefRefPtr browser, CefCursorHandle cursor) OVERRIDE; virtual bool StartDragging(CefRefPtr browser, CefRefPtr drag_data, CefRenderHandler::DragOperationsMask allowed_ops, int x, int y) OVERRIDE; virtual void UpdateDragCursor(CefRefPtr browser, CefRenderHandler::DragOperation operation) OVERRIDE; void SetMainWindowHandle(ClientWindowHandle handle); ClientWindowHandle GetMainWindowHandle() const; void SetEditWindowHandle(ClientWindowHandle handle); void SetButtonWindowHandles(ClientWindowHandle backHandle, ClientWindowHandle forwardHandle, ClientWindowHandle reloadHandle, ClientWindowHandle stopHandle); void SetOSRHandler(CefRefPtr handler); CefRefPtr GetOSRHandler() const; CefRefPtr GetBrowser() const; int GetBrowserId() const; // Request that all existing browser windows close. void CloseAllBrowsers(bool force_close); // Returns true if the main browser window is currently closing. Used in // combination with DoClose() and the OS close notification to properly handle // 'onbeforeunload' JavaScript events during window close. bool IsClosing() const; std::string GetLogFile() const; void SetLastDownloadFile(const std::string& fileName); std::string GetLastDownloadFile() const; // Send a notification to the application. Notifications should not block the // caller. enum NotificationType { NOTIFY_CONSOLE_MESSAGE, NOTIFY_DOWNLOAD_COMPLETE, NOTIFY_DOWNLOAD_ERROR, }; void SendNotification(NotificationType type); void ShowDevTools(CefRefPtr browser); void CloseDevTools(CefRefPtr browser); // Returns the startup URL. std::string GetStartupURL() const; void BeginTracing(); void EndTracing(); bool Save(const std::string& path, const std::string& data); private: void SetLoading(bool isLoading); void SetNavState(bool canGoBack, bool canGoForward); // Create all CefMessageRouterBrowserSide::Handler objects. They will be // deleted when the ClientHandler is destroyed. static void CreateMessageHandlers(MessageHandlerSet& handlers); // Test context menu creation. void BuildTestMenu(CefRefPtr model); bool ExecuteTestMenu(int command_id); struct TestMenuState { TestMenuState() : check_item(true), radio_item(0) {} bool check_item; int radio_item; } test_menu_state_; // Returns the full download path for the specified file, or an empty path to // use the default temp directory. std::string GetDownloadPath(const std::string& file_name); // START THREAD SAFE MEMBERS // The following members are thread-safe because they're initialized during // object construction and not changed thereafter. // The startup URL. std::string startup_url_; // True if mouse cursor change is disabled. bool mouse_cursor_change_disabled_; // END THREAD SAFE MEMBERS // Lock used to protect members accessed on multiple threads. Make it mutable // so that it can be used from const methods. mutable base::Lock lock_; // START LOCK PROTECTED MEMBERS // The following members are accessed on multiple threads and must be // protected by |lock_|. // The child browser window. CefRefPtr browser_; // The child browser id. int browser_id_; // True if the main browser window is currently closing. bool is_closing_; // END LOCK PROTECTED MEMBERS // START UI THREAD ACCESS ONLY MEMBERS // The following members will only be accessed on the CEF UI thread. // List of any popup browser windows. typedef std::list > BrowserList; BrowserList popup_browsers_; // The main frame window handle. ClientWindowHandle main_handle_; // The edit window handle. ClientWindowHandle edit_handle_; // The button window handles. ClientWindowHandle back_handle_; ClientWindowHandle forward_handle_; ClientWindowHandle stop_handle_; ClientWindowHandle reload_handle_; // The handler for off-screen rendering, if any. CefRefPtr osr_handler_; // Support for logging. std::string log_file_; // Support for downloading files. std::string last_download_file_; // True if an editable field currently has focus. bool focus_on_editable_field_; // Handles the browser side of query routing. The renderer side is handled // in client_renderer.cpp. CefRefPtr message_router_; // Set of Handlers registered with the message router. MessageHandlerSet message_handler_set_; // Number of currently existing browser windows. The application will exit // when the number of windows reaches 0. static int browser_count_; #if defined(OS_LINUX) // Linux-only implementation of GTK-based dialog boxes. static void OnDialogResponse(GtkDialog *dialog, gint response_id, ClientHandler* handler); GtkWidget* gtk_dialog_; CefRefPtr js_dialog_callback_; #endif // END UI THREAD ACCESS ONLY MEMBERS // Include the default reference counting implementation. IMPLEMENT_REFCOUNTING(ClientHandler); }; #endif // CEF_TESTS_CEFCLIENT_CLIENT_HANDLER_H_