// 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/cef_client.h" #include "cefclient/util.h" // 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 CefDisplayHandler, public CefDownloadHandler, public CefGeolocationHandler, public CefKeyboardHandler, public CefLifeSpanHandler, public CefLoadHandler, public CefRenderHandler, public CefRequestHandler { public: // Interface for process message delegates. Do not perform work in the // RenderDelegate constructor. class ProcessMessageDelegate : public virtual CefBase { public: // Called when a process message is received. Return true if the message was // handled and should not be passed on to other handlers. // ProcessMessageDelegates should check for unique message names to avoid // interfering with each other. virtual bool OnProcessMessageReceived( CefRefPtr handler, CefRefPtr browser, CefProcessId source_process, CefRefPtr message) { return false; } }; typedef std::set > ProcessMessageDelegateSet; // Interface implemented to handle off-screen rendering. class RenderHandler : public CefRenderHandler { public: virtual void OnBeforeClose(CefRefPtr browser) =0; }; ClientHandler(); virtual ~ClientHandler(); // CefClient methods virtual CefRefPtr GetContextMenuHandler() OVERRIDE { return this; } virtual CefRefPtr GetDisplayHandler() OVERRIDE { return this; } virtual CefRefPtr GetDownloadHandler() OVERRIDE { return this; } virtual CefRefPtr GetGeolocationHandler() 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; // CefDisplayHandler methods virtual void OnLoadingStateChange(CefRefPtr browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE; 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; // CefGeolocationHandler methods virtual void OnRequestGeolocationPermission( CefRefPtr browser, const CefString& requesting_url, int request_id, CefRefPtr callback) 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 OnLoadStart(CefRefPtr browser, CefRefPtr frame) OVERRIDE; virtual void OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) OVERRIDE; virtual void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE; virtual void OnRenderProcessTerminated(CefRefPtr browser, TerminationStatus status) OVERRIDE; // CefRequestHandler methods 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; // 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; void SetMainHwnd(CefWindowHandle hwnd); CefWindowHandle GetMainHwnd() { return m_MainHwnd; } void SetEditHwnd(CefWindowHandle hwnd); void SetOSRHandler(CefRefPtr handler) { m_OSRHandler = handler; } CefRefPtr GetOSRHandler() { return m_OSRHandler; } void SetButtonHwnds(CefWindowHandle backHwnd, CefWindowHandle forwardHwnd, CefWindowHandle reloadHwnd, CefWindowHandle stopHwnd); CefRefPtr GetBrowser() { return m_Browser; } int GetBrowserId() { return m_BrowserId; } // 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() { return m_bIsClosing; } std::string GetLogFile(); void SetLastDownloadFile(const std::string& fileName); std::string GetLastDownloadFile(); // 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); // Returns the startup URL. std::string GetStartupURL() { return m_StartupURL; } // Create an external browser window that loads the specified URL. static void LaunchExternalBrowser(const std::string& url); void BeginTracing(); void EndTracing(); bool Save(const std::string& path, const std::string& data); protected: void SetLoading(bool isLoading); void SetNavState(bool canGoBack, bool canGoForward); // Create all of ProcessMessageDelegate objects. static void CreateProcessMessageDelegates( ProcessMessageDelegateSet& delegates); // 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; } m_TestMenuState; // 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); // The child browser window CefRefPtr m_Browser; // List of any popup browser windows. Only accessed on the CEF UI thread. typedef std::list > BrowserList; BrowserList m_PopupBrowsers; // The main frame window handle CefWindowHandle m_MainHwnd; // The child browser id int m_BrowserId; // True if the main browser window is currently closing. bool m_bIsClosing; // The edit window handle CefWindowHandle m_EditHwnd; // The button window handles CefWindowHandle m_BackHwnd; CefWindowHandle m_ForwardHwnd; CefWindowHandle m_StopHwnd; CefWindowHandle m_ReloadHwnd; CefRefPtr m_OSRHandler; // Support for logging. std::string m_LogFile; // Support for downloading files. std::string m_LastDownloadFile; // True if an editable field currently has focus. bool m_bFocusOnEditableField; // Registered delegates. ProcessMessageDelegateSet process_message_delegates_; // If true DevTools will be opened in an external browser window. bool m_bExternalDevTools; // List of open DevTools URLs if not using an external browser window. std::set m_OpenDevToolsURLs; // The startup URL. std::string m_StartupURL; // True if mouse cursor change is disabled. bool m_bMouseCursorChangeDisabled; // Number of currently existing browser windows. The application will exit // when the number of windows reaches 0. static int m_BrowserCount; // Include the default reference counting implementation. IMPLEMENT_REFCOUNTING(ClientHandler); // Include the default locking implementation. IMPLEMENT_LOCKING(ClientHandler); }; #endif // CEF_TESTS_CEFCLIENT_CLIENT_HANDLER_H_