diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 96a8a266a..3fa0f6c20 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -19,6 +19,7 @@ #include "CefSettings.h" #include "SchemeHandlerWrapper.h" #include "Internals/CefTaskScheduler.h" +#include "CookieAsyncWrapper.h" using namespace System::Collections::Generic; using namespace System::Linq; @@ -29,32 +30,17 @@ namespace CefSharp public ref class Cef sealed { private: - static HANDLE _event; static Object^ _sync; - static bool _result; static bool _initialized = false; static HashSet^ _disposables; static Cef() { - _event = CreateEvent(NULL, FALSE, FALSE, NULL); _sync = gcnew Object(); _disposables = gcnew HashSet(); } - static void IOT_SetCookie(const CefString& url, const CefCookie& cookie) - { - _result = CefCookieManager::GetGlobalManager()->SetCookie(url, cookie); - SetEvent(_event); - } - - static void IOT_DeleteCookies(const CefString& url, const CefString& name) - { - _result = CefCookieManager::GetGlobalManager()->DeleteCookies(url, name); - SetEvent(_event); - } - static void ParentProcessExitHandler(Object^ sender, EventArgs^ e) { if (Cef::IsInitialized) @@ -337,38 +323,19 @@ namespace CefSharp /// false if the cookie cannot be set (e.g. if illegal charecters such as ';' are used); otherwise true. static bool SetCookie(String^ url, String^ name, String^ value, String^ domain, String^ path, bool secure, bool httponly, bool has_expires, DateTime expires) { - msclr::lock l(_sync); - _result = false; - - CefCookie cookie; - StringUtils::AssignNativeFromClr(cookie.name, name); - StringUtils::AssignNativeFromClr(cookie.value, value); - StringUtils::AssignNativeFromClr(cookie.domain, domain); - StringUtils::AssignNativeFromClr(cookie.path, path); - cookie.secure = secure; - cookie.httponly = httponly; - cookie.has_expires = has_expires; - cookie.expires.year = expires.Year; - cookie.expires.month = expires.Month; - cookie.expires.day_of_month = expires.Day; - cookie.expires.hour = expires.Hour; - cookie.expires.minute = expires.Minute; - cookie.expires.second = expires.Second; - cookie.expires.millisecond = expires.Millisecond; + auto cookieInvoker = gcnew CookieAsyncWrapper(url, name, value, domain, path, secure, httponly, has_expires, expires); if (CefCurrentlyOn(TID_IO)) { - IOT_SetCookie(StringUtils::ToNative(url), cookie); - } - else - { - CefPostTask(TID_IO, NewCefRunnableFunction(IOT_SetCookie, - StringUtils::ToNative(url), cookie)); + return cookieInvoker->SetCookie(); } - WaitForSingleObject(_event, INFINITE); - return _result; - } + auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::SetCookie)); + + task->Wait(); + + return task->Result; + } /// Sets a cookie using mostly default parameters. This function expects each attribute to be well-formed. It will check for disallowed /// characters (e.g. the ';' character is disallowed within the cookie value attribute) and will return false without setting @@ -390,21 +357,18 @@ namespace CefSharp /// false if a non-empty invalid URL is specified, or if cookies cannot be accessed; otherwise, true. static bool DeleteCookies(String^ url, String^ name) { - msclr::lock l(_sync); - _result = false; + auto cookieInvoker = gcnew CookieAsyncWrapper(url, name); if (CefCurrentlyOn(TID_IO)) { - IOT_DeleteCookies(StringUtils::ToNative(url), StringUtils::ToNative(name)); - } - else - { - CefPostTask(TID_IO, NewCefRunnableFunction(IOT_DeleteCookies, - StringUtils::ToNative(url), StringUtils::ToNative(name))); + return cookieInvoker->DeleteCookies(); } - WaitForSingleObject(_event, INFINITE); - return _result; + auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::DeleteCookies)); + + task->Wait(); + + return task->Result; } /// Sets the directory path that will be used for storing cookie data. If is empty data will be stored in diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index bc9a0c042..84ee3a09b 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -265,6 +265,7 @@ + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index c2c89e2c8..c30bcc266 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -148,6 +148,9 @@ Header Files + + Header Files + Header Files diff --git a/CefSharp.Core/CookieAsyncWrapper.h b/CefSharp.Core/CookieAsyncWrapper.h new file mode 100644 index 000000000..fb07bba62 --- /dev/null +++ b/CefSharp.Core/CookieAsyncWrapper.h @@ -0,0 +1,61 @@ +// 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" + +namespace CefSharp +{ + private ref class CookieAsyncWrapper + { + String^ _url; + String^ _name; + String^ _value; + String^ _domain; + String^ _path; + bool _secure; + bool _httponly; + bool _hasExpires; + DateTime _expires; + + public: + //Used for SetCookies + CookieAsyncWrapper(String^ url, String^ name, String^ value, String^ domain, String^ path, bool secure, bool httponly, bool hasExpires, DateTime expires) : + _url(url), _name(name), _value(value), _domain(domain), _path(path), _secure(secure), _httponly(httponly), _hasExpires(hasExpires), _expires(expires) + { + } + + //Used for DeleteCookies + CookieAsyncWrapper(String^ url, String^ name) : _url(url), _name(name) + { + } + + bool SetCookie() + { + CefCookie cookie; + StringUtils::AssignNativeFromClr(cookie.name, _name); + StringUtils::AssignNativeFromClr(cookie.value, _value); + StringUtils::AssignNativeFromClr(cookie.domain, _domain); + StringUtils::AssignNativeFromClr(cookie.path, _path); + cookie.secure = _secure; + cookie.httponly = _httponly; + cookie.has_expires = _hasExpires; + cookie.expires.year = _expires.Year; + cookie.expires.month = _expires.Month; + cookie.expires.day_of_month = _expires.Day; + cookie.expires.hour = _expires.Hour; + cookie.expires.minute = _expires.Minute; + cookie.expires.second = _expires.Second; + cookie.expires.millisecond = _expires.Millisecond; + + return CefCookieManager::GetGlobalManager()->SetCookie(StringUtils::ToNative(_url), cookie); + } + + bool DeleteCookies() + { + return CefCookieManager::GetGlobalManager()->DeleteCookies(StringUtils::ToNative(_url), StringUtils::ToNative(_name)); + } + }; +} \ No newline at end of file