Chromium Code Reviews| Index: chrome/browser/signin/signin_cookie_changed_subscription.cc |
| diff --git a/chrome/browser/signin/signin_cookie_changed_subscription.cc b/chrome/browser/signin/signin_cookie_changed_subscription.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..65880fa121c41eb5bed361535ae1daf775ac5963 |
| --- /dev/null |
| +++ b/chrome/browser/signin/signin_cookie_changed_subscription.cc |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2014 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 "chrome/browser/signin/signin_cookie_changed_subscription.h" |
| + |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/cookies/cookie_store.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +namespace { |
| + |
| +// Adds a callback for cookie changed events. This method is called on the IO |
| +// thread, so it is safe to access the cookie store. |
| +scoped_ptr<net::CookieStore::CookieChangedSubscription> |
| +RegisterForCookieChangesOnIOThread( |
| + scoped_refptr<net::URLRequestContextGetter> context_getter, |
| + const GURL url, |
| + const std::string name, |
| + const net::CookieStore::CookieChangedCallback callback) { |
| + net::CookieStore* cookie_store = |
| + context_getter->GetURLRequestContext()->cookie_store(); |
| + DCHECK(cookie_store); |
| + return cookie_store->AddCallbackForCookie(url, name, callback); |
| +} |
| + |
| +// Posts a task on the |proxy| task runner that calls |OnCookieChanged| on |
| +// |subscription|. |
| +// Note that this method is called on the IO thread, so |subscription| must |
| +// not be used here, it is only passed around. |
| +void RunAsyncOnCookieChanged( |
| + scoped_refptr<base::TaskRunner> proxy, |
| + base::WeakPtr<SigninCookieChangedSubscription> subscription, |
| + const net::CanonicalCookie& cookie, |
| + bool removed) { |
| + proxy->PostTask(FROM_HERE, |
| + base::Bind(&SigninCookieChangedSubscription::OnCookieChanged, |
| + subscription, |
| + cookie, |
| + removed)); |
| +} |
| + |
| +} // namespace |
| + |
| + |
| +SigninCookieChangedSubscription::SigninCookieChangedSubscription( |
| + const net::CookieStore::CookieChangedCallback& callback) |
| + : callback_(callback) { |
| +} |
| + |
| +SigninCookieChangedSubscription::~SigninCookieChangedSubscription() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (cookie_changed_subscription_) { |
| + content::BrowserThread::DeleteOnIOThread::Destruct( |
| + cookie_changed_subscription_.release()); |
| + } |
| +} |
| + |
| +void SigninCookieChangedSubscription::SetCookieChangedSubscription( |
| + scoped_ptr<net::CookieStore::CookieChangedSubscription> subscription) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(subscription); |
| + DCHECK(!cookie_changed_subscription_); |
| + cookie_changed_subscription_ = subscription.Pass(); |
| +} |
| + |
| +void SigninCookieChangedSubscription::OnCookieChanged( |
| + const net::CanonicalCookie& cookie, |
| + bool removed) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(cookie_changed_subscription_); |
| + if (!callback_.is_null()) { |
| + callback_.Run(cookie, removed); |
| + } |
| +} |
| + |
| +void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications( |
| + scoped_refptr<net::URLRequestContextGetter> context_getter, |
| + const GURL& url, |
| + const std::string& name) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + // On iOS, the cookie store can only be accessed from the IO thread. As |
|
droger
2014/10/31 12:42:01
This comment may need to be updated.
msarda
2014/10/31 13:18:39
Done.
|
| + // |AddCookieChangedCallback| is called from the main thread, a thread |
| + // jump is needed to register for cookie changed notifications. |
| + net::CookieStore::CookieChangedCallback run_on_current_thread_callback = |
| + base::Bind(&RunAsyncOnCookieChanged, |
| + base::MessageLoopProxy::current(), |
| + this->AsWeakPtr()); |
| + content::BrowserThread::PostTaskAndReplyWithResult( |
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&RegisterForCookieChangesOnIOThread, |
| + context_getter, |
| + url, |
| + name, |
| + run_on_current_thread_callback), |
| + base::Bind(&SigninCookieChangedSubscription::SetCookieChangedSubscription, |
| + this->AsWeakPtr())); |
| +} |