From fb1c70a44c73a0dc96ca729968ca15778cb3d58c Mon Sep 17 00:00:00 2001 From: amaitland Date: Sun, 25 Sep 2016 20:35:04 +1000 Subject: [PATCH] Rename IPluginHandler to IRequestContextHandler Add IRequestContextHandler.GetCookieManager() - Can now have unique CookieManager for each RequestContext **Breaking Change** CookieManager should be moved from the CefSharp.Internals namespace - will do that at some point later hopefully --- CefSharp.Core/Internals/CookieManager.h | 27 +++++++++++++++++ CefSharp.Core/RequestContext.h | 8 ++--- CefSharp.Core/RequestContextHandler.h | 30 +++++++++++++++---- .../CefSharp.Wpf.Example.csproj | 2 +- .../Handlers/PluginHandler.cs | 17 ----------- .../Handlers/RequestContextHandler.cs | 23 ++++++++++++++ CefSharp/CefSharp.csproj | 2 +- ...inHandler.cs => IRequestContextHandler.cs} | 16 ++++++++-- 8 files changed, 94 insertions(+), 31 deletions(-) delete mode 100644 CefSharp.Wpf.Example/Handlers/PluginHandler.cs create mode 100644 CefSharp.Wpf.Example/Handlers/RequestContextHandler.cs rename CefSharp/{IPluginHandler.cs => IRequestContextHandler.cs} (61%) diff --git a/CefSharp.Core/Internals/CookieManager.h b/CefSharp.Core/Internals/CookieManager.h index 7c64d3758d..ed904cd263 100644 --- a/CefSharp.Core/Internals/CookieManager.h +++ b/CefSharp.Core/Internals/CookieManager.h @@ -7,6 +7,7 @@ #include "Stdafx.h" #include "include/cef_cookie.h" +#include "CefCompletionCallbackAdapter.h" using namespace System::Threading::Tasks; @@ -21,6 +22,23 @@ namespace CefSharp void ThrowIfDisposed(); public: + /// + // Creates a new cookie manager. If |path| is empty data will be stored in + // memory only. Otherwise, data will be stored at the specified |path|. To + // persist session cookies (cookies without an expiry date or validity + // interval) set |persist_session_cookies| to true. Session cookies are + // generally intended to be transient and most Web browsers do not persist + // them. If |callback| is non-NULL it will be executed asnychronously on the + // IO thread after the manager's storage has been initialized. + /// + /*--cef(optional_param=path,optional_param=callback)--*/ + CookieManager(String^ path, bool persistSessionCookies, ICompletionCallback^ callback) + { + CefRefPtr wrapper = callback == nullptr ? NULL : new CefCompletionCallbackAdapter(callback); + + _cookieManager = CefCookieManager::CreateManager(StringUtils::ToNative(path), persistSessionCookies, wrapper); + } + CookieManager(const CefRefPtr &cookieManager) :_cookieManager(cookieManager.get()) { @@ -46,6 +64,15 @@ namespace CefSharp virtual Task^>^ VisitUrlCookiesAsync(String^ url, bool includeHttpOnly); virtual bool VisitUrlCookies(String^ url, bool includeHttpOnly, ICookieVisitor^ visitor); virtual Task^ FlushStoreAsync(); + + operator CefRefPtr() + { + if (this == nullptr) + { + return NULL; + } + return _cookieManager.get(); + } }; } } \ No newline at end of file diff --git a/CefSharp.Core/RequestContext.h b/CefSharp.Core/RequestContext.h index 7725d8255d..95f93d1348 100644 --- a/CefSharp.Core/RequestContext.h +++ b/CefSharp.Core/RequestContext.h @@ -56,15 +56,15 @@ namespace CefSharp _requestContext = CefRequestContext::CreateContext(settings, NULL); } - RequestContext(IPluginHandler^ pluginHandler) + RequestContext(IRequestContextHandler^ requestContextHandler) { CefRequestContextSettings settings; - _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(pluginHandler)); + _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(requestContextHandler)); } - RequestContext(RequestContextSettings^ settings, IPluginHandler^ pluginHandler) : _settings(settings) + RequestContext(RequestContextSettings^ settings, IRequestContextHandler^ requestContextHandler) : _settings(settings) { - _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(pluginHandler)); + _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(requestContextHandler)); } !RequestContext() diff --git a/CefSharp.Core/RequestContextHandler.h b/CefSharp.Core/RequestContextHandler.h index 7f53e6f57e..e65c9e485c 100644 --- a/CefSharp.Core/RequestContextHandler.h +++ b/CefSharp.Core/RequestContextHandler.h @@ -7,22 +7,40 @@ #include "Stdafx.h" #include "Internals\TypeConversion.h" +#include "Internals\CookieManager.h" namespace CefSharp { private class RequestContextHandler : public CefRequestContextHandler { - gcroot _pluginHandler; + gcroot _requestContextHandler; public: - RequestContextHandler(IPluginHandler^ pluginHandler) - : _pluginHandler(pluginHandler) + RequestContextHandler(IRequestContextHandler^ requestContextHandler) + : _requestContextHandler(requestContextHandler) { } ~RequestContextHandler() { - _pluginHandler = nullptr; + _requestContextHandler = nullptr; + } + + virtual CefRefPtr GetCookieManager() OVERRIDE + { + if (Object::ReferenceEquals(_requestContextHandler, nullptr)) + { + return NULL; + } + + auto cookieManager = _requestContextHandler->GetCookieManager(); + + if (cookieManager == nullptr) + { + return NULL; + } + + return (CookieManager^)cookieManager; } virtual bool OnBeforePluginLoad(const CefString& mime_type, @@ -31,12 +49,12 @@ namespace CefSharp CefRefPtr plugin_info, CefRequestContextHandler::PluginPolicy* plugin_policy) OVERRIDE { - if (!Object::ReferenceEquals(_pluginHandler, nullptr)) + if (!Object::ReferenceEquals(_requestContextHandler, nullptr)) { auto pluginInfo = TypeConversion::FromNative(plugin_info); auto pluginPolicy = (CefSharp::PluginPolicy)*plugin_policy; - auto result = _pluginHandler->OnBeforePluginLoad(StringUtils::ToClr(mime_type), + auto result = _requestContextHandler->OnBeforePluginLoad(StringUtils::ToClr(mime_type), StringUtils::ToClr(plugin_url), StringUtils::ToClr(top_origin_url), pluginInfo, diff --git a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj index 05ecb5a912..fa72dbac73 100644 --- a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj +++ b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj @@ -105,7 +105,7 @@ - + SimpleMainWindow.xaml diff --git a/CefSharp.Wpf.Example/Handlers/PluginHandler.cs b/CefSharp.Wpf.Example/Handlers/PluginHandler.cs deleted file mode 100644 index 03825dba8e..0000000000 --- a/CefSharp.Wpf.Example/Handlers/PluginHandler.cs +++ /dev/null @@ -1,17 +0,0 @@ -// 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. - -namespace CefSharp.Wpf.Example.Handlers -{ - public class PluginHandler : IPluginHandler - { - bool IPluginHandler.OnBeforePluginLoad(string mimeType, string url, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy) - { - //pluginPolicy = PluginPolicy.Disable; - //return true; - - return false; - } - } -} diff --git a/CefSharp.Wpf.Example/Handlers/RequestContextHandler.cs b/CefSharp.Wpf.Example/Handlers/RequestContextHandler.cs new file mode 100644 index 0000000000..53b901764c --- /dev/null +++ b/CefSharp.Wpf.Example/Handlers/RequestContextHandler.cs @@ -0,0 +1,23 @@ +// 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. + +namespace CefSharp.Wpf.Example.Handlers +{ + public class RequestContextHandler : IRequestContextHandler + { + bool IRequestContextHandler.OnBeforePluginLoad(string mimeType, string url, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy) + { + //pluginPolicy = PluginPolicy.Disable; + //return true; + + return false; + } + + ICookieManager IRequestContextHandler.GetCookieManager() + { + //Provide your own cookie manager + return null; + } + } +} diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 8801db691d..85b7be7153 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -95,7 +95,7 @@ - + diff --git a/CefSharp/IPluginHandler.cs b/CefSharp/IRequestContextHandler.cs similarity index 61% rename from CefSharp/IPluginHandler.cs rename to CefSharp/IRequestContextHandler.cs index 8930b60778..9931423f91 100644 --- a/CefSharp/IPluginHandler.cs +++ b/CefSharp/IRequestContextHandler.cs @@ -5,10 +5,22 @@ namespace CefSharp { /// - /// Implement this interface to cancel loading of specific plugins + /// Implement this interface to provide handler implementations. The handler + /// instance will not be released until all objects related to the context have + /// been destroyed. Implement this interface to cancel loading of specific plugins /// - public interface IPluginHandler + public interface IRequestContextHandler { + /// + /// Called on the browser process IO thread to retrieve the cookie manager. If + /// this method returns NULL the default cookie manager retrievable via + /// IRequestContext.GetDefaultCookieManager() will be used. + /// + /// If + /// this method returns null the default cookie manager retrievable via + /// IRequestContext.GetDefaultCookieManager() will be used.. + ICookieManager GetCookieManager(); + /// /// Called on the CEF IO thread before a plugin instance is loaded. /// The default plugin policy can be set at runtime using the `--plugin-policy=[allow|detect|block]` command-line flag.