// Copyright © 2010-2015 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. #pragma once #include "Stdafx.h" #include "include/cef_app.h" #include "include/cef_client.h" #include "include/cef_render_process_handler.h" #include "include/internal/cef_types.h" using namespace System::Threading::Tasks; namespace CefSharp { namespace Internals { private class ClientAdapter : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler, public CefDisplayHandler, public CefContextMenuHandler, public CefFocusHandler, public CefKeyboardHandler, public CefJSDialogHandler, public CefDialogHandler, public CefDragHandler, public CefGeolocationHandler, public CefDownloadHandler { private: gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; gcroot^> _popupBrowsers; gcroot _tooltip; gcroot _browserAdapter; //contains in-progress eval script tasks gcroot^> _pendingTaskRepository; //contains js callback factories for each browser void ThrowUnknownPopupBrowser(String^ context) { throw gcnew ApplicationException(String::Format("{0} couldn't find IBrowser entry! Please contact CefSharp development.", context)); } IBrowser^ GetBrowserWrapper(int browserId, bool isPopup); public: ClientAdapter(IWebBrowserInternal^ browserControl, IBrowserAdapter^ browserAdapter) : _browserControl(browserControl), _popupBrowsers(gcnew Dictionary()), _pendingTaskRepository(gcnew PendingTaskRepository()), _browserAdapter(browserAdapter) { } ~ClientAdapter() { CloseAllPopups(true); //this will dispose the repository and cancel all pending tasks delete _pendingTaskRepository; _browserControl = nullptr; _browserHwnd = nullptr; _cefBrowser = NULL; _tooltip = nullptr; _browserAdapter = nullptr; _popupBrowsers = nullptr; } HWND GetBrowserHwnd() { return _browserHwnd; } CefRefPtr GetCefBrowser() { return _cefBrowser; } PendingTaskRepository^ GetPendingTaskRepository(); void CloseAllPopups(bool forceClose); void MethodInvocationComplete(MethodInvocationResult^ result); IBrowser^ GetBrowserWrapper(int browserId); // CefClient virtual DECL CefRefPtr GetLifeSpanHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetLoadHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetRequestHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDisplayHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDownloadHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetContextMenuHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetFocusHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetKeyboardHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetJSDialogHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDialogHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDragHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetGeolocationHandler() OVERRIDE { return this; } virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) OVERRIDE; // CefLifeSpanHandler virtual DECL 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, bool* no_javascript_access) OVERRIDE; virtual DECL void OnAfterCreated(CefRefPtr browser) OVERRIDE; virtual DECL bool DoClose(CefRefPtr browser) OVERRIDE; virtual DECL void OnBeforeClose(CefRefPtr browser) OVERRIDE; // CefLoadHandler virtual DECL void OnLoadStart(CefRefPtr browser, CefRefPtr frame) OVERRIDE; virtual DECL void OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) OVERRIDE; virtual DECL void OnLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE; // CefRequestHandler virtual DECL CefRefPtr GetResourceHandler(CefRefPtr browser, CefRefPtr frame, CefRefPtr request) OVERRIDE; virtual DECL cef_return_value_t OnBeforeResourceLoad(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr callback) OVERRIDE; virtual DECL bool GetAuthCredentials(CefRefPtr browser, CefRefPtr frame, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr callback) OVERRIDE; virtual DECL bool OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool isRedirect) OVERRIDE; virtual DECL bool OnOpenURLFromTab(CefRefPtr browser, CefRefPtr frame, const CefString& targetUrl, CefRequestHandler::WindowOpenDisposition targetDisposition, bool userGesture) OVERRIDE; virtual DECL bool OnCertificateError(CefRefPtr browser, cef_errorcode_t cert_error, const CefString& request_url, CefRefPtr ssl_info, CefRefPtr callback) OVERRIDE; virtual DECL bool OnQuotaRequest(CefRefPtr browser, const CefString& originUrl, int64 newSize, CefRefPtr callback) OVERRIDE; virtual DECL void OnResourceRedirect(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefString& newUrl) OVERRIDE; virtual DECL bool OnResourceResponse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response) OVERRIDE; virtual DECL void OnProtocolExecution(CefRefPtr browser, const CefString& url, bool& allowOSExecution) OVERRIDE; virtual DECL void OnPluginCrashed(CefRefPtr browser, const CefString& plugin_path) OVERRIDE; virtual DECL void OnRenderViewReady(CefRefPtr browser) OVERRIDE; virtual DECL void OnRenderProcessTerminated(CefRefPtr browser, TerminationStatus status) OVERRIDE; // CefDisplayHandler virtual DECL void OnLoadingStateChange(CefRefPtr browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE; virtual DECL void OnAddressChange(CefRefPtr browser, CefRefPtr frame, const CefString& url) OVERRIDE; virtual DECL void OnTitleChange(CefRefPtr browser, const CefString& title) OVERRIDE; virtual DECL void OnFaviconURLChange(CefRefPtr browser, const std::vector& iconUrls) OVERRIDE; virtual DECL void OnFullscreenModeChange(CefRefPtr browser, bool fullscreen) OVERRIDE; virtual DECL bool OnTooltip(CefRefPtr browser, CefString& text) OVERRIDE; virtual DECL bool OnConsoleMessage(CefRefPtr browser, const CefString& message, const CefString& source, int line) OVERRIDE; virtual DECL void OnStatusMessage(CefRefPtr browser, const CefString& message) OVERRIDE; // CefContextMenuHandler virtual DECL void OnBeforeContextMenu(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, CefRefPtr model) OVERRIDE; virtual DECL bool OnContextMenuCommand(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, int commandId, EventFlags eventFlags) OVERRIDE; virtual DECL void OnContextMenuDismissed(CefRefPtr browser, CefRefPtr frame) OVERRIDE; virtual DECL bool RunContextMenu(CefRefPtr browser, CefRefPtr frame, CefRefPtr params, CefRefPtr model, CefRefPtr callback) OVERRIDE; // CefFocusHandler virtual DECL void OnGotFocus(CefRefPtr browser) OVERRIDE; virtual DECL bool OnSetFocus(CefRefPtr browser, FocusSource source) OVERRIDE; virtual DECL void OnTakeFocus(CefRefPtr browser, bool next) OVERRIDE; // CefKeyboardHandler virtual DECL bool OnKeyEvent(CefRefPtr browser, const CefKeyEvent& event, CefEventHandle os_event) OVERRIDE; virtual DECL bool OnPreKeyEvent(CefRefPtr browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut) OVERRIDE; // CefJSDialogHandler virtual DECL 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 DECL bool OnBeforeUnloadDialog(CefRefPtr browser, const CefString& message_text, bool is_reload, CefRefPtr callback) OVERRIDE; virtual DECL void OnResetDialogState(CefRefPtr browser) OVERRIDE; virtual DECL void OnDialogClosed(CefRefPtr browser) OVERRIDE; // CefDialogHandler virtual DECL bool OnFileDialog(CefRefPtr browser, FileDialogMode mode, const CefString& title, const CefString& default_file_path, const std::vector& accept_filters, int selected_accept_filter, CefRefPtr callback) OVERRIDE; //CefDragHandler virtual DECL bool OnDragEnter(CefRefPtr browser, CefRefPtr dragData, DragOperationsMask mask) OVERRIDE; virtual DECL void OnDraggableRegionsChanged(CefRefPtr browser, const std::vector& regions) OVERRIDE; //CefGeolocationHandler virtual DECL bool OnRequestGeolocationPermission(CefRefPtr browser, const CefString& requesting_url, int request_id, CefRefPtr callback) OVERRIDE; virtual DECL void OnCancelGeolocationPermission(CefRefPtr browser, const CefString& requesting_url, int request_id) OVERRIDE; //CefDownloadHandler 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; //sends out an eval script request to the render process Task^ EvaluateScriptAsync(int browserId, bool isBrowserPopup, int64 frameId, String^ script, Nullable timeout); IMPLEMENT_REFCOUNTING(ClientAdapter); }; } }