| Index: content/common/cookie_service_impl.cc
|
| diff --git a/content/common/cookie_service_impl.cc b/content/common/cookie_service_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6b40fb62dc75b0796b2490fd97bca588a7e063e1
|
| --- /dev/null
|
| +++ b/content/common/cookie_service_impl.cc
|
| @@ -0,0 +1,217 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/common/cookie_service_impl.h"
|
| +
|
| +#include "net/cookies/canonical_cookie.h"
|
| +#include "net/cookies/cookie_constants.h"
|
| +#include "net/cookies/cookie_options.h"
|
| +#include "url/gurl.h"
|
| +
|
| +namespace {
|
| +
|
| +// Class to wrap a CookieDeletionFilterPtr and provide a predicate for
|
| +// use by DeleteAllCreatedBetweenWithPredicateAsync.
|
| +class PredicateWrapper {
|
| + public:
|
| + explicit PredicateWrapper(content::mojom::CookieDeletionFilterPtr filter)
|
| + : use_blacklist_(filter->domain_blacklist.has_value()),
|
| + domain_blacklist_(filter->domain_blacklist.has_value()
|
| + ? std::set<std::string>(
|
| + filter->domain_blacklist.value().begin(),
|
| + filter->domain_blacklist.value().end())
|
| + : std::set<std::string>()),
|
| + use_whitelist_(filter->domain_whitelist.has_value()),
|
| + domain_whitelist_(filter->domain_whitelist.has_value()
|
| + ? std::set<std::string>(
|
| + filter->domain_whitelist.value().begin(),
|
| + filter->domain_whitelist.value().end())
|
| + : std::set<std::string>()),
|
| + session_control_(filter->session_control) {}
|
| +
|
| + bool Predicate(const net::CanonicalCookie& cookie) {
|
| + // Ignore begin/end times; they're handled by method args.
|
| + if (use_blacklist_ && domain_blacklist_.count(cookie.Domain()) != 0)
|
| + return true;
|
| +
|
| + if (use_whitelist_ && domain_whitelist_.count(cookie.Domain()) == 0)
|
| + return true;
|
| +
|
| + if (session_control_ ==
|
| + content::mojom::CookieDeletionSessionControl::SESSION &&
|
| + !cookie.IsPersistent()) {
|
| + return true;
|
| + }
|
| +
|
| + if (session_control_ ==
|
| + content::mojom::CookieDeletionSessionControl::PERSISTENT &&
|
| + cookie.IsPersistent()) {
|
| + return true;
|
| + }
|
| +
|
| + return false;
|
| + }
|
| +
|
| + private:
|
| + bool use_blacklist_;
|
| + std::set<std::string> domain_blacklist_;
|
| +
|
| + bool use_whitelist_;
|
| + std::set<std::string> domain_whitelist_;
|
| +
|
| + content::mojom::CookieDeletionSessionControl session_control_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PredicateWrapper);
|
| +};
|
| +
|
| +content::mojom::CookieChangeCause ChangeCauseTranslation(
|
| + net::CookieStore::ChangeCause net_cause) {
|
| + switch (net_cause) {
|
| + case net::CookieStore::ChangeCause::INSERTED:
|
| + return content::mojom::CookieChangeCause::INSERTED;
|
| + case net::CookieStore::ChangeCause::EXPLICIT:
|
| + case net::CookieStore::ChangeCause::EXPLICIT_DELETE_BETWEEN:
|
| + case net::CookieStore::ChangeCause::EXPLICIT_DELETE_PREDICATE:
|
| + case net::CookieStore::ChangeCause::EXPLICIT_DELETE_SINGLE:
|
| + case net::CookieStore::ChangeCause::EXPLICIT_DELETE_CANONICAL:
|
| + return content::mojom::CookieChangeCause::EXPLICIT;
|
| + case net::CookieStore::ChangeCause::UNKNOWN_DELETION:
|
| + return content::mojom::CookieChangeCause::UNKNOWN_DELETION;
|
| + case net::CookieStore::ChangeCause::OVERWRITE:
|
| + return content::mojom::CookieChangeCause::OVERWRITE;
|
| + case net::CookieStore::ChangeCause::EXPIRED:
|
| + return content::mojom::CookieChangeCause::EXPIRED;
|
| + case net::CookieStore::ChangeCause::EVICTED:
|
| + return content::mojom::CookieChangeCause::EVICTED;
|
| + case net::CookieStore::ChangeCause::EXPIRED_OVERWRITE:
|
| + return content::mojom::CookieChangeCause::EXPIRED_OVERWRITE;
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace content {
|
| +
|
| +CookieServiceImpl::NotificationRegistration::~NotificationRegistration() {}
|
| +
|
| +CookieServiceImpl::CookieServiceImpl(net::CookieStore* cookie_store)
|
| + : cookie_store_(cookie_store) {}
|
| +
|
| +CookieServiceImpl::~CookieServiceImpl() {}
|
| +
|
| +void CookieServiceImpl::AddRequest(
|
| + content::mojom::CookieServiceRequest request) {
|
| + bindings_.AddBinding(this, std::move(request));
|
| +}
|
| +
|
| +void CookieServiceImpl::GetAllCookies(GetAllCookiesCallback callback) {
|
| + cookie_store_->GetAllCookiesAsync(std::move(callback));
|
| +}
|
| +
|
| +void CookieServiceImpl::GetCookieList(const GURL& url,
|
| + const net::CookieOptions& cookie_options,
|
| + GetCookieListCallback callback) {
|
| + cookie_store_->GetCookieListWithOptionsAsync(url, cookie_options,
|
| + std::move(callback));
|
| +}
|
| +
|
| +void CookieServiceImpl::SetCanonicalCookie(
|
| + const net::CanonicalCookie& cookie,
|
| + bool secure_source,
|
| + bool modify_http_only,
|
| + SetCanonicalCookieCallback callback) {
|
| + cookie_store_->SetCanonicalCookieAsync(
|
| + base::MakeUnique<net::CanonicalCookie>(cookie), secure_source,
|
| + modify_http_only, std::move(callback));
|
| +}
|
| +
|
| +void CookieServiceImpl::DeleteCookies(
|
| + content::mojom::CookieDeletionFilterPtr filter,
|
| + DeleteCookiesCallback callback) {
|
| + base::Time start_time;
|
| + base::Time end_time;
|
| +
|
| + if (filter->created_after_time.has_value())
|
| + end_time = filter->created_after_time.value();
|
| +
|
| + if (filter->created_before_time.has_value())
|
| + start_time = filter->created_before_time.value();
|
| +
|
| + cookie_store_->DeleteAllCreatedBetweenWithPredicateAsync(
|
| + start_time, end_time,
|
| + base::Bind(&PredicateWrapper::Predicate,
|
| + base::MakeUnique<PredicateWrapper>(std::move(filter))),
|
| + std::move(callback));
|
| +}
|
| +
|
| +void CookieServiceImpl::RequestNotification(
|
| + const GURL& url,
|
| + const std::string& name,
|
| + RequestNotificationCallback callback) {
|
| + mojom::CookieChangeNotificationPtr notification_pointer;
|
| + mojom::CookieChangeNotificationRequest notification_request =
|
| + mojo::MakeRequest(¬ification_pointer);
|
| +
|
| + std::unique_ptr<NotificationRegistration> notification_registration;
|
| + notification_registration->notification_pointer =
|
| + std::move(notification_pointer);
|
| +
|
| + notification_registration->subscription = cookie_store_->AddCallbackForCookie(
|
| + url, name,
|
| + base::Bind(&CookieServiceImpl::CookieChanged,
|
| + // base::Unretained is safe as destruction of the
|
| + // CookieServiceImpl will also destroy the
|
| + // notifications_registered list (which this object will be
|
| + // inserted into, below), which will destroy the
|
| + // CookieChangedSubscription, unregistering the callback.
|
| + base::Unretained(this),
|
| + // base::Unretained is safe as destruction of the
|
| + // NotificationRegistration will also destroy the
|
| + // CookieChangedSubscription, unregistering the callback.
|
| + base::Unretained(notification_registration.get())));
|
| +
|
| + notification_registration->notification_pointer.set_connection_error_handler(
|
| + base::Bind(&CookieServiceImpl::NotificationPipeBroken,
|
| + // base::Unretained is safe as destruction of the
|
| + // CookieServiceImpl will also destroy the
|
| + // notifications_registered list (which this object will be
|
| + // inserted into, below), which will destroy the
|
| + // notification_pointer, rendering this callback moot.
|
| + base::Unretained(this),
|
| + // base::Unretained is safe as destruction of the
|
| + // NotificationRegistration will also destroy the
|
| + // CookieChangedSubscription, unregistering the callback.
|
| + base::Unretained(notification_registration.get())));
|
| +
|
| + notifications_registered_.push_back(std::move(notification_registration));
|
| +}
|
| +
|
| +void CookieServiceImpl::CookieChanged(NotificationRegistration* registration,
|
| + const net::CanonicalCookie& cookie,
|
| + net::CookieStore::ChangeCause cause) {
|
| + registration->notification_pointer->OnCookieChanged(
|
| + cookie, ChangeCauseTranslation(cause));
|
| +}
|
| +
|
| +void CookieServiceImpl::NotificationPipeBroken(
|
| + NotificationRegistration* registration) {
|
| + for (auto it = notifications_registered_.begin();
|
| + it != notifications_registered_.end(); ++it) {
|
| + if (it->get() == registration) {
|
| + // It isn't expected this will be a common enough operation for
|
| + // the performance of std::vector::erase() to matter.
|
| + notifications_registered_.erase(it);
|
| + return;
|
| + }
|
| + }
|
| + // A broken connection error should never be raised for an unknown pipe.
|
| + NOTREACHED();
|
| +}
|
| +
|
| +void CookieServiceImpl::CloneInterface(
|
| + mojom::CookieServiceRequest new_interface) {
|
| + AddRequest(std::move(new_interface));
|
| +}
|
| +
|
| +} // namespace content
|
|
|