Files
cefsharp/CefSharp.Core/Internals/ClientAdapter.h
T
amaitland e20209b82d ManagedCefBrowserAdapter - _browserAdapter was never set so the Close method wasn't being called
Remove GetCefBrowser method as ManagedCefBrowserAdapter now totally relies on the managed wrappers for calls like SendKeyEvent
2016-01-12 15:16:31 +10:00

198 lines
13 KiB
C++

// Copyright © 2010-2016 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<IWebBrowserInternal^> _browserControl;
HWND _browserHwnd;
CefRefPtr<CefBrowser> _cefBrowser;
gcroot<IBrowser^> _browser;
gcroot<Dictionary<int, IBrowser^>^> _popupBrowsers;
gcroot<String^> _tooltip;
gcroot<IBrowserAdapter^> _browserAdapter;
//contains in-progress eval script tasks
gcroot<PendingTaskRepository<JavascriptResponse^>^> _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<int, IBrowser^>()),
_pendingTaskRepository(gcnew PendingTaskRepository<JavascriptResponse^>()),
_browserAdapter(browserAdapter)
{
}
~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<JavascriptResponse^>^ GetPendingTaskRepository();
void CloseAllPopups(bool forceClose);
void MethodInvocationComplete(MethodInvocationResult^ result);
IBrowser^ GetBrowserWrapper(int browserId);
// CefClient
virtual DECL CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefDownloadHandler> GetDownloadHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefFocusHandler> GetFocusHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefDialogHandler> GetDialogHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefDragHandler> GetDragHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefGeolocationHandler> GetGeolocationHandler() OVERRIDE { return this; }
virtual DECL bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) OVERRIDE;
// CefLifeSpanHandler
virtual DECL bool OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
const CefString& target_url, const CefString& target_frame_name,
CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo, CefRefPtr<CefClient>& client, CefBrowserSettings& settings, bool* no_javascript_access) OVERRIDE;
virtual DECL void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler
virtual DECL void OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame) OVERRIDE;
virtual DECL void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) OVERRIDE;
virtual DECL void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE;
// CefRequestHandler
virtual DECL CefRefPtr<CefResourceHandler> GetResourceHandler(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request) OVERRIDE;
virtual DECL cef_return_value_t OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefRequestCallback> callback) OVERRIDE;
virtual DECL bool GetAuthCredentials(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, bool isProxy,
const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr<CefAuthCallback> callback) OVERRIDE;
virtual DECL bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool isRedirect) OVERRIDE;
virtual DECL bool OnOpenURLFromTab(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& targetUrl,
CefRequestHandler::WindowOpenDisposition targetDisposition, bool userGesture) OVERRIDE;
virtual DECL bool OnCertificateError(CefRefPtr<CefBrowser> browser, cef_errorcode_t cert_error, const CefString& request_url, CefRefPtr<CefSSLInfo> ssl_info, CefRefPtr<CefRequestCallback> callback) OVERRIDE;
virtual DECL bool OnQuotaRequest(CefRefPtr<CefBrowser> browser, const CefString& originUrl, int64 newSize, CefRefPtr<CefRequestCallback> callback) OVERRIDE;
virtual DECL void OnResourceRedirect(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefString& newUrl) OVERRIDE;
virtual DECL bool OnResourceResponse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response) OVERRIDE;
virtual DECL void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefResponse> response, URLRequestStatus status, int64 receivedContentLength) OVERRIDE;
virtual DECL void OnProtocolExecution(CefRefPtr<CefBrowser> browser, const CefString& url, bool& allowOSExecution) OVERRIDE;
virtual DECL void OnPluginCrashed(CefRefPtr<CefBrowser> browser, const CefString& plugin_path) OVERRIDE;
virtual DECL void OnRenderViewReady(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser, TerminationStatus status) OVERRIDE;
// CefDisplayHandler
virtual DECL void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE;
virtual DECL void OnAddressChange(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& url) OVERRIDE;
virtual DECL void OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) OVERRIDE;
virtual DECL void OnFaviconURLChange(CefRefPtr<CefBrowser> browser, const std::vector<CefString>& iconUrls) OVERRIDE;
virtual DECL void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser, bool fullscreen) OVERRIDE;
virtual DECL bool OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text) OVERRIDE;
virtual DECL bool OnConsoleMessage(CefRefPtr<CefBrowser> browser, const CefString& message, const CefString& source, int line) OVERRIDE;
virtual DECL void OnStatusMessage(CefRefPtr<CefBrowser> browser, const CefString& message) OVERRIDE;
// CefContextMenuHandler
virtual DECL void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) OVERRIDE;
virtual DECL bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params, int commandId, EventFlags eventFlags) OVERRIDE;
virtual DECL void OnContextMenuDismissed(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame) OVERRIDE;
virtual DECL bool RunContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model, CefRefPtr<CefRunContextMenuCallback> callback) OVERRIDE;
// CefFocusHandler
virtual DECL void OnGotFocus(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) OVERRIDE;
virtual DECL void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) OVERRIDE;
// CefKeyboardHandler
virtual DECL bool OnKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event) OVERRIDE;
virtual DECL bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut) OVERRIDE;
// CefJSDialogHandler
virtual DECL bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,
JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text,
CefRefPtr<CefJSDialogCallback> callback, bool& suppress_message) OVERRIDE;
virtual DECL bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser, const CefString& message_text, bool is_reload,
CefRefPtr<CefJSDialogCallback> callback) OVERRIDE;
virtual DECL void OnResetDialogState(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL void OnDialogClosed(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefDialogHandler
virtual DECL bool OnFileDialog(CefRefPtr<CefBrowser> browser, FileDialogMode mode, const CefString& title,
const CefString& default_file_path, const std::vector<CefString>& accept_filters, int selected_accept_filter,
CefRefPtr<CefFileDialogCallback> callback) OVERRIDE;
//CefDragHandler
virtual DECL bool OnDragEnter(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDragData> dragData, DragOperationsMask mask) OVERRIDE;
virtual DECL void OnDraggableRegionsChanged(CefRefPtr<CefBrowser> browser, const std::vector<CefDraggableRegion>& regions) OVERRIDE;
//CefGeolocationHandler
virtual DECL bool OnRequestGeolocationPermission(CefRefPtr<CefBrowser> browser, const CefString& requesting_url, int request_id,
CefRefPtr<CefGeolocationCallback> callback) OVERRIDE;
virtual DECL void OnCancelGeolocationPermission(CefRefPtr<CefBrowser> browser, const CefString& requesting_url, int request_id) OVERRIDE;
//CefDownloadHandler
virtual void OnBeforeDownload(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback) OVERRIDE;
virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item,
CefRefPtr<CefDownloadItemCallback> callback) OVERRIDE;
//sends out an eval script request to the render process
Task<JavascriptResponse^>^ EvaluateScriptAsync(int browserId, bool isBrowserPopup, int64 frameId, String^ script, Nullable<TimeSpan> timeout);
IMPLEMENT_REFCOUNTING(ClientAdapter);
};
}
}