Files
cefsharp/CefSharp.Core/Internals/CookieManager.cpp
T
2016-01-11 14:11:30 +10:00

106 lines
3.5 KiB
C++

// Copyright © 2010-2016 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.
#include "Stdafx.h"
#include "CookieManager.h"
#include "CookieAsyncWrapper.h"
#include "CookieVisitor.h"
#include "Internals\CefCompletionCallbackAdapter.h"
#include "Cef.h"
namespace CefSharp
{
namespace Internals
{
void CookieManager::ThrowIfDisposed()
{
if (_cookieManager.get() == nullptr)
{
throw gcnew ObjectDisposedException("CookieManager");
}
}
Task<bool>^ CookieManager::DeleteCookiesAsync(String^ url, String^ name)
{
ThrowIfDisposed();
auto cookieInvoker = gcnew CookieAsyncWrapper(_cookieManager.get(), url, name);
if (CefCurrentlyOn(TID_IO))
{
auto source = gcnew TaskCompletionSource<bool>();
source->TrySetResult(cookieInvoker->DeleteCookies());
return source->Task;
}
return Cef::IOThreadTaskFactory->StartNew(gcnew Func<bool>(cookieInvoker, &CookieAsyncWrapper::DeleteCookies));
}
Task<bool>^ CookieManager::SetCookieAsync(String^ url, Cookie^ cookie)
{
ThrowIfDisposed();
auto cookieInvoker = gcnew CookieAsyncWrapper(_cookieManager.get(), url, cookie->Name, cookie->Value, cookie->Domain, cookie->Path, cookie->Secure, cookie->HttpOnly, cookie->Expires.HasValue, cookie->Expires.HasValue ? cookie->Expires.Value : DateTime());
if (CefCurrentlyOn(TID_IO))
{
auto source = gcnew TaskCompletionSource<bool>();
source->TrySetResult(cookieInvoker->SetCookie());
return source->Task;
}
return Cef::IOThreadTaskFactory->StartNew(gcnew Func<bool>(cookieInvoker, &CookieAsyncWrapper::SetCookie));
}
bool CookieManager::SetStoragePath(String^ path, bool persistSessionCookies)
{
ThrowIfDisposed();
return _cookieManager->SetStoragePath(StringUtils::ToNative(path), persistSessionCookies, NULL);
}
void CookieManager::SetSupportedSchemes(... cli::array<String^>^ schemes)
{
ThrowIfDisposed();
_cookieManager->SetSupportedSchemes(StringUtils::ToNative(schemes), NULL);
}
bool CookieManager::VisitAllCookies(ICookieVisitor^ visitor)
{
ThrowIfDisposed();
CefRefPtr<CookieVisitor> cookieVisitor = new CookieVisitor(visitor);
return _cookieManager->VisitAllCookies(cookieVisitor);
}
bool CookieManager::VisitUrlCookies(String^ url, bool includeHttpOnly, ICookieVisitor^ visitor)
{
ThrowIfDisposed();
CefRefPtr<CookieVisitor> cookieVisitor = new CookieVisitor(visitor);
return _cookieManager->VisitUrlCookies(StringUtils::ToNative(url), includeHttpOnly, cookieVisitor);
}
Task<bool>^ CookieManager::FlushStoreAsync()
{
ThrowIfDisposed();
auto handler = gcnew TaskCompletionHandler();
CefRefPtr<CefCompletionCallback> wrapper = new CefCompletionCallbackAdapter(handler);
if (_cookieManager->FlushStore(wrapper))
{
return handler->Task;
}
//returns false if cookies cannot be accessed.
return TaskExtensions::FromResult(false);
}
}
}