// 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. #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^ CookieManager::DeleteCookiesAsync(String^ url, String^ name) { ThrowIfDisposed(); auto cookieInvoker = gcnew CookieAsyncWrapper(_cookieManager.get(), url, name); if (CefCurrentlyOn(TID_IO)) { auto source = gcnew TaskCompletionSource(); source->TrySetResult(cookieInvoker->DeleteCookies()); return source->Task; } return Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::DeleteCookies)); } Task^ 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(); source->TrySetResult(cookieInvoker->SetCookie()); return source->Task; } return Cef::IOThreadTaskFactory->StartNew(gcnew Func(cookieInvoker, &CookieAsyncWrapper::SetCookie)); } bool CookieManager::SetStoragePath(String^ path, bool persistSessionCookies) { ThrowIfDisposed(); return _cookieManager->SetStoragePath(StringUtils::ToNative(path), persistSessionCookies, NULL); } void CookieManager::SetSupportedSchemes(... cli::array^ schemes) { ThrowIfDisposed(); _cookieManager->SetSupportedSchemes(StringUtils::ToNative(schemes), NULL); } bool CookieManager::VisitAllCookies(ICookieVisitor^ visitor) { ThrowIfDisposed(); CefRefPtr cookieVisitor = new CookieVisitor(visitor); return _cookieManager->VisitAllCookies(cookieVisitor); } bool CookieManager::VisitUrlCookies(String^ url, bool includeHttpOnly, ICookieVisitor^ visitor) { ThrowIfDisposed(); CefRefPtr cookieVisitor = new CookieVisitor(visitor); return _cookieManager->VisitUrlCookies(StringUtils::ToNative(url), includeHttpOnly, cookieVisitor); } Task^ CookieManager::FlushStoreAsync() { ThrowIfDisposed(); auto handler = gcnew TaskCompletionHandler(); CefRefPtr wrapper = new CefCompletionCallbackAdapter(handler); if (_cookieManager->FlushStore(wrapper)) { return handler->Task; } //returns false if cookies cannot be accessed. return TaskExtensions::FromResult(false); } } }