// Copyright © 2010 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. #ifndef CEFSHARP_CORE_INTERNALS_CLIENTADAPTER_H_ #define CEFSHARP_CORE_INTERNALS_CLIENTADAPTER_H_ #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 CefDownloadHandler, public CefFindHandler, public CefAudioHandler { private: gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; gcroot _browser; 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), _browserHwnd(NULL) { } ~ClientAdapter() { CloseAllPopups(true); //this will dispose the repository and cancel all pending tasks delete _pendingTaskRepository; _browser = nullptr; _browserControl = nullptr; _browserHwnd = nullptr; _cefBrowser = NULL; _tooltip = nullptr; _browserAdapter = nullptr; _popupBrowsers = nullptr; } HWND GetBrowserHwnd() { return _browserHwnd; } 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 GetFindHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetAudioHandler() OVERRIDE { //Audio Mirroring in CEF is only enabled when we a handler is returned //We return NULL if no handler is specified for performance reasons if (_browserControl->AudioHandler == nullptr) { return NULL; } return this; } virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefRefPtr frame, 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, CefRefPtr& extraInfo, 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, TransitionType transitionType) 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 bool OnBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool userGesture, bool isRedirect) OVERRIDE; virtual DECL bool OnOpenURLFromTab(CefRefPtr browser, CefRefPtr frame, const CefString& targetUrl, CefRequestHandler::WindowOpenDisposition targetDisposition, bool userGesture) OVERRIDE; virtual DECL CefRefPtr GetResourceRequestHandler(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool isNavigation, bool isDownload, const CefString& requestInitiator, bool& disableDefaultHandling) 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 OnQuotaRequest(CefRefPtr browser, const CefString& originUrl, int64 newSize, CefRefPtr callback) 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 OnSelectClientCertificate(CefRefPtr browser, bool isProxy, const CefString& host, int port, const CefRequestHandler::X509CertificateList& certificates, CefRefPtr callback) 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 bool OnAutoResize(CefRefPtr< CefBrowser > browser, const CefSize& new_size) 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 void OnLoadingProgressChange(CefRefPtr browser, double progress) OVERRIDE; virtual DECL bool OnTooltip(CefRefPtr browser, CefString& text) OVERRIDE; virtual DECL bool OnConsoleMessage(CefRefPtr browser, cef_log_severity_t level, 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, 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, CefRefPtr frame, const std::vector& regions) OVERRIDE; //CefDownloadHandler virtual DECL void OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item, const CefString& suggested_name, CefRefPtr callback) OVERRIDE; virtual DECL void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE; //CefFindHandler virtual DECL void OnFindResult(CefRefPtr browser, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate) OVERRIDE; //CefAudioHandler virtual DECL void OnAudioStreamStarted(CefRefPtr browser, int audio_stream_id, int channels, ChannelLayout channel_layout, int sample_rate, int frames_per_buffer) OVERRIDE; virtual DECL void OnAudioStreamPacket(CefRefPtr browser, int audio_stream_id, const float** data, int frames, int64 pts) OVERRIDE; virtual DECL void OnAudioStreamStopped(CefRefPtr browser, int audio_stream_id) OVERRIDE; IMPLEMENT_REFCOUNTING(ClientAdapter); }; } } #endif // CEFSHARP_CORE_INTERNALS_CLIENTADAPTER_H_