From 9f3832d06c30e878ef8a5784b4a41ccb22cf8fa7 Mon Sep 17 00:00:00 2001 From: rassilon Date: Sat, 25 Apr 2015 17:54:31 -0400 Subject: [PATCH 1/6] Make utility class for reporting unhandled exceptions. --- .../CefSharp.BrowserSubprocess.Core.vcxproj | 1 + ...arp.BrowserSubprocess.Core.vcxproj.filters | 3 +++ CefSharp.Core/CefSharp.Core.vcxproj | 1 + CefSharp.Core/CefSharp.Core.vcxproj.filters | 3 +++ CefSharp.Core/Internals/CefTaskWrapper.h | 21 +++---------------- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index f742974ac..9230d4db6 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -152,6 +152,7 @@ + diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index 7a3d2f4d8..5550541d5 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -65,6 +65,9 @@ Header Files + + Header Files + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index ec17cbd0b..bc9a0c042 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -255,6 +255,7 @@ + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 9977a5a1c..c2c89e2c8 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -148,6 +148,9 @@ Header Files + + Header Files + diff --git a/CefSharp.Core/Internals/CefTaskWrapper.h b/CefSharp.Core/Internals/CefTaskWrapper.h index b83f89610..070c79101 100644 --- a/CefSharp.Core/Internals/CefTaskWrapper.h +++ b/CefSharp.Core/Internals/CefTaskWrapper.h @@ -6,7 +6,7 @@ #include "Stdafx.h" -#include +#include "ReportUnhandledExceptions.h" using namespace System; using namespace System::Threading::Tasks; @@ -20,15 +20,6 @@ namespace CefSharp { private class CefTaskWrapper { - private: - // Throw the exception, this method executes on the thread pool - // in order to report a .net exception from a CEF thread. - static void ReportUnhandledException(Object ^state) - { - Exception ^e = dynamic_cast(state); - throw gcnew InvalidOperationException(gcnew String(L"CefTaskWrapper caught an unexpected exception. This should never happen, please contact CefSharp for assistance."), e); - } - public: gcroot _task; gcroot _scheduler; @@ -56,14 +47,8 @@ namespace CefSharp // If this occurs then someone has broken a .Net ThreadScheduler/Task invariant // i.e. trying to run a task on the wrong scheduler, or some // weird exception during task completion. - auto msg = String::Format(gcnew String(L"CefTaskWrapper caught an unexpected exception. This should never happen, please contact CefSharp for assistance: {0}"), e->ToString()); - LOG(ERROR) << StringUtils::ToNative(msg).ToString(); - // Throw the exception from a threadpool thread in hopes that - // AppDomain::UnhandledException event handler will get called - // with the exception. - ThreadPool::UnsafeQueueUserWorkItem( - gcnew WaitCallback(&CefTaskWrapper::ReportUnhandledException), - static_cast(e)); + auto msg = gcnew String(L"CefTaskWrapper caught an unexpected exception. This should never happen, please contact CefSharp for assistance"); + ReportUnhandledExceptions::Report(msg, e); } }; From 3a6f9d0d80ff126227faafe2151930afbe6770a0 Mon Sep 17 00:00:00 2001 From: rassilon Date: Sat, 25 Apr 2015 18:07:21 -0400 Subject: [PATCH 2/6] Helps to add the new file. --- .../Internals/ReportUnhandledExceptions.h | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 CefSharp.Core/Internals/ReportUnhandledExceptions.h diff --git a/CefSharp.Core/Internals/ReportUnhandledExceptions.h b/CefSharp.Core/Internals/ReportUnhandledExceptions.h new file mode 100644 index 000000000..fd4d7147b --- /dev/null +++ b/CefSharp.Core/Internals/ReportUnhandledExceptions.h @@ -0,0 +1,38 @@ +#pragma once + +#include "Stdafx.h" + +#include + +namespace CefSharp +{ + namespace Internals + { + + private class ReportUnhandledExceptions + { + public: + static void Report(String^ str, Exception^ exception) + { + auto msg = String::Format(gcnew String(L"{0}: {1}"), str, exception->ToString()); + LOG(ERROR) << StringUtils::ToNative(str).ToString(); + // Throw the exception from a threadpool thread so that + // AppDomain::UnhandledException event handler will get called + // with the exception. + ThreadPool::UnsafeQueueUserWorkItem( + gcnew WaitCallback(&ReportUnhandledExceptions::ThrowUnhandledException), + static_cast(gcnew Tuple(str, exception))); + } + private: + // Throw the exception, this method executes on the thread pool + // in order to report a .net exception from a CEF thread. + static void ThrowUnhandledException(Object ^state) + { + Tuple^ params = dynamic_cast^>(state); + throw gcnew InvalidOperationException(params->Item1, params->Item2); + } + }; + } +} + + From 3c17379c46ff4c024ab28f2c4bd84e31d5da2064 Mon Sep 17 00:00:00 2001 From: rassilon Date: Sat, 25 Apr 2015 18:14:47 -0400 Subject: [PATCH 3/6] Add license. --- CefSharp.Core/Internals/ReportUnhandledExceptions.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CefSharp.Core/Internals/ReportUnhandledExceptions.h b/CefSharp.Core/Internals/ReportUnhandledExceptions.h index fd4d7147b..83b33a34b 100644 --- a/CefSharp.Core/Internals/ReportUnhandledExceptions.h +++ b/CefSharp.Core/Internals/ReportUnhandledExceptions.h @@ -1,3 +1,7 @@ +// Copyright © 2010-2015 The CefSharp Project. 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" From 73cd506f762f753d7a7e4142d2ea967602a47c9f Mon Sep 17 00:00:00 2001 From: amaitland Date: Thu, 23 Apr 2015 09:57:03 +1000 Subject: [PATCH 4/6] Create wrapper class to call `SetCookie` async using new Task factories --- CefSharp.Core/Cef.h | 42 ++++------------- CefSharp.Core/CefSharp.Core.vcxproj | 1 + CefSharp.Core/CefSharp.Core.vcxproj.filters | 3 ++ CefSharp.Core/SetCookieInvoker.h | 50 +++++++++++++++++++++ 4 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 CefSharp.Core/SetCookieInvoker.h diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 96a8a266a..1cf9acb28 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 "SetCookieInvoker.h" using namespace System::Collections::Generic; using namespace System::Linq; @@ -43,12 +44,6 @@ namespace CefSharp _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); @@ -337,38 +332,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 SetCookieInvoker(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, &SetCookieInvoker::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 diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index bc9a0c042..c7a825815 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..74871b852 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -145,6 +145,9 @@ Header Files + + Header Files + Header Files diff --git a/CefSharp.Core/SetCookieInvoker.h b/CefSharp.Core/SetCookieInvoker.h new file mode 100644 index 000000000..82c8f4dd9 --- /dev/null +++ b/CefSharp.Core/SetCookieInvoker.h @@ -0,0 +1,50 @@ +// 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 SetCookieInvoker + { + String^ _url; + String^ _name; + String^ _value; + String^ _domain; + String^ _path; + bool _secure; + bool _httponly; + bool _hasExpires; + DateTime _expires; + + public: + SetCookieInvoker(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) + { + } + + 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); + } + }; +} \ No newline at end of file From fe5543c6af153fd6196f62377ceecab4c7f43d8e Mon Sep 17 00:00:00 2001 From: amaitland Date: Sat, 25 Apr 2015 10:46:18 +1000 Subject: [PATCH 5/6] Move DeleteCookies into SetCookieInvoker (will rename class shortly) --- CefSharp.Core/Cef.h | 26 +++++++------------------- CefSharp.Core/SetCookieInvoker.h | 11 +++++++++++ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 1cf9acb28..7a8966f72 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -30,26 +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_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) @@ -366,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 SetCookieInvoker(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, &SetCookieInvoker::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/SetCookieInvoker.h b/CefSharp.Core/SetCookieInvoker.h index 82c8f4dd9..4589f8d8f 100644 --- a/CefSharp.Core/SetCookieInvoker.h +++ b/CefSharp.Core/SetCookieInvoker.h @@ -21,11 +21,17 @@ namespace CefSharp DateTime _expires; public: + //Used for SetCookies SetCookieInvoker(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 + SetCookieInvoker(String^ url, String^ name) : _url(url), _name(name) + { + } + bool SetCookie() { CefCookie cookie; @@ -46,5 +52,10 @@ namespace CefSharp 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 From ac15d23af7218b723ff96e03d59018d9b7bb0906 Mon Sep 17 00:00:00 2001 From: amaitland Date: Sat, 25 Apr 2015 10:47:58 +1000 Subject: [PATCH 6/6] Rename SetCookieInvoker to CookieAsyncWrapper --- CefSharp.Core/Cef.h | 10 +++++----- CefSharp.Core/CefSharp.Core.vcxproj | 2 +- CefSharp.Core/CefSharp.Core.vcxproj.filters | 4 ++-- .../{SetCookieInvoker.h => CookieAsyncWrapper.h} | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) rename CefSharp.Core/{SetCookieInvoker.h => CookieAsyncWrapper.h} (86%) diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 7a8966f72..3fa0f6c20 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -19,7 +19,7 @@ #include "CefSettings.h" #include "SchemeHandlerWrapper.h" #include "Internals/CefTaskScheduler.h" -#include "SetCookieInvoker.h" +#include "CookieAsyncWrapper.h" using namespace System::Collections::Generic; using namespace System::Linq; @@ -323,14 +323,14 @@ 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) { - auto cookieInvoker = gcnew SetCookieInvoker(url, name, value, domain, path, secure, httponly, has_expires, expires); + auto cookieInvoker = gcnew CookieAsyncWrapper(url, name, value, domain, path, secure, httponly, has_expires, expires); if (CefCurrentlyOn(TID_IO)) { return cookieInvoker->SetCookie(); } - auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &SetCookieInvoker::SetCookie)); + auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::SetCookie)); task->Wait(); @@ -357,14 +357,14 @@ 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) { - auto cookieInvoker = gcnew SetCookieInvoker(url, name); + auto cookieInvoker = gcnew CookieAsyncWrapper(url, name); if (CefCurrentlyOn(TID_IO)) { return cookieInvoker->DeleteCookies(); } - auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &SetCookieInvoker::DeleteCookies)); + auto task = Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::DeleteCookies)); task->Wait(); diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index c7a825815..84ee3a09b 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -265,7 +265,7 @@ - + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 74871b852..c30bcc266 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -145,10 +145,10 @@ Header Files - + Header Files - + Header Files diff --git a/CefSharp.Core/SetCookieInvoker.h b/CefSharp.Core/CookieAsyncWrapper.h similarity index 86% rename from CefSharp.Core/SetCookieInvoker.h rename to CefSharp.Core/CookieAsyncWrapper.h index 4589f8d8f..fb07bba62 100644 --- a/CefSharp.Core/SetCookieInvoker.h +++ b/CefSharp.Core/CookieAsyncWrapper.h @@ -8,7 +8,7 @@ namespace CefSharp { - private ref class SetCookieInvoker + private ref class CookieAsyncWrapper { String^ _url; String^ _name; @@ -22,13 +22,13 @@ namespace CefSharp public: //Used for SetCookies - SetCookieInvoker(String^ url, String^ name, String^ value, String^ domain, String^ path, bool secure, bool httponly, bool hasExpires, DateTime expires) : + 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 - SetCookieInvoker(String^ url, String^ name) : _url(url), _name(name) + CookieAsyncWrapper(String^ url, String^ name) : _url(url), _name(name) { }