Merge remote-tracking branch 'remotes/upstream/cef/2272' into cleanup/resourcehandler

This commit is contained in:
amaitland
2015-04-28 07:32:39 +10:00
8 changed files with 134 additions and 70 deletions
@@ -152,6 +152,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\CefSharp.Core\Internals\MCefRefPtr.h" />
<ClInclude Include="..\CefSharp.Core\Internals\ReportUnhandledExceptions.h" />
<ClInclude Include="..\CefSharp.Core\Internals\StringUtils.h" />
<ClInclude Include="CefAppUnmanagedWrapper.h" />
<ClInclude Include="CefAppWrapper.h" />
@@ -65,6 +65,9 @@
<ClInclude Include="JavascriptCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\CefSharp.Core\Internals\ReportUnhandledExceptions.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp">
+16 -52
View File
@@ -20,6 +20,7 @@
#include "ResourceHandlerWrapper.h"
#include "SchemeHandlerFactoryWrapper.h"
#include "Internals/CefTaskScheduler.h"
#include "CookieAsyncWrapper.h"
using namespace System::Collections::Generic;
using namespace System::Linq;
@@ -30,32 +31,17 @@ namespace CefSharp
public ref class Cef sealed
{
private:
static HANDLE _event;
static Object^ _sync;
static bool _result;
static bool _initialized = false;
static HashSet<IDisposable^>^ _disposables;
static Cef()
{
_event = CreateEvent(NULL, FALSE, FALSE, NULL);
_sync = gcnew Object();
_disposables = gcnew HashSet<IDisposable^>();
}
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)
@@ -347,38 +333,19 @@ namespace CefSharp
/// <return>false if the cookie cannot be set (e.g. if illegal charecters such as ';' are used); otherwise true.</return>
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<bool>(cookieInvoker, &CookieAsyncWrapper::SetCookie));
task->Wait();
return task->Result;
}
/// <summary>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
@@ -400,21 +367,18 @@ namespace CefSharp
/// <return>false if a non-empty invalid URL is specified, or if cookies cannot be accessed; otherwise, true.</return>
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<bool>(cookieInvoker, &CookieAsyncWrapper::DeleteCookies));
task->Wait();
return task->Result;
}
/// <summary> Sets the directory path that will be used for storing cookie data. If <paramref name="path"/> is empty data will be stored in
+2
View File
@@ -253,6 +253,7 @@
<ClInclude Include="Internals\MCefRefPtr.h" />
<ClInclude Include="Internals\PluginVisitor.h" />
<ClInclude Include="Internals\RenderClientAdapter.h" />
<ClInclude Include="Internals\ReportUnhandledExceptions.h" />
<ClInclude Include="Internals\StringVisitor.h" />
<ClInclude Include="Internals\TypeConversion.h" />
<ClInclude Include="NativeMethodWrapper.h" />
@@ -262,6 +263,7 @@
<ClInclude Include="SchemeHandlerFactoryWrapper.h" />
<ClInclude Include="SchemeHandlerResponse.h" />
<ClInclude Include="ResourceHandlerWrapper.h" />
<ClInclude Include="CookieAsyncWrapper.h" />
<ClInclude Include="Stdafx.h" />
<ClInclude Include="Internals\StringUtils.h" />
<ClInclude Include="Internals\StreamAdapter.h" />
@@ -142,6 +142,12 @@
<ClInclude Include="Internals\CefTaskWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CookieAsyncWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\ReportUnhandledExceptions.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
+61
View File
@@ -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));
}
};
}
+3 -18
View File
@@ -6,7 +6,7 @@
#include "Stdafx.h"
#include <include/base/cef_logging.h>
#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<Exception^>(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^> _task;
gcroot<ITaskScheduler^> _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<Object^>(e));
auto msg = gcnew String(L"CefTaskWrapper caught an unexpected exception. This should never happen, please contact CefSharp for assistance");
ReportUnhandledExceptions::Report(msg, e);
}
};
@@ -0,0 +1,42 @@
// 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"
#include <include/base/cef_logging.h>
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<Object^>(gcnew Tuple<String^, Exception^>(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<String^, Exception^>^ params = dynamic_cast<Tuple<String^, Exception^>^>(state);
throw gcnew InvalidOperationException(params->Item1, params->Item2);
}
};
}
}