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..07fcc7dde7a8f156dbc2574b0853c385a8a9fc0d |
| --- /dev/null |
| +++ b/chrome/browser/signin/signin_cookie_changed_subscription.cc |
| @@ -0,0 +1,99 @@ |
| +// 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. |
| +void RegisterForCookieChangesOnIOThread( |
| + scoped_refptr<net::URLRequestContextGetter> context_getter, |
| + const GURL url, |
| + const std::string name, |
| + const net::CookieStore::CookieChangedCallback callback, |
| + SigninCookieChangedSubscription::SubscriptionHolder* |
| + out_subscription_holder) { |
| + DCHECK(out_subscription_holder); |
| + net::CookieStore* cookie_store = |
| + context_getter->GetURLRequestContext()->cookie_store(); |
| + DCHECK(cookie_store); |
| + out_subscription_holder->subscription = |
| + 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::SubscriptionHolder::SubscriptionHolder() { |
| +} |
| + |
| +SigninCookieChangedSubscription::SubscriptionHolder::~SubscriptionHolder() { |
| +} |
| + |
| +SigninCookieChangedSubscription::SigninCookieChangedSubscription( |
| + const net::CookieStore::CookieChangedCallback& callback) |
| + : callback_(callback), subscription_holder_io_(new SubscriptionHolder()) { |
| +} |
| + |
| +SigninCookieChangedSubscription::~SigninCookieChangedSubscription() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + content::BrowserThread::DeleteOnIOThread::Destruct( |
|
droger
2014/11/04 10:31:36
Optional:
This assumes that the network thread is
msarda
2014/11/04 12:06:06
Done.
|
| + subscription_holder_io_.release()); |
| +} |
| + |
| +void SigninCookieChangedSubscription::OnCookieChanged( |
| + const net::CanonicalCookie& cookie, |
| + bool removed) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + 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()); |
| + |
| + // The cookie store can only be accessed from the context getter which lives |
| + // on the IO thread. As |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::PostTask( |
|
droger
2014/11/04 10:31:36
I think this would be better:
context_getter->GetN
msarda
2014/11/04 12:06:06
Done.
|
| + content::BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&RegisterForCookieChangesOnIOThread, |
| + context_getter, |
| + url, |
| + name, |
| + run_on_current_thread_callback, |
| + base::Unretained(subscription_holder_io_.get()))); |
| +} |