Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/signin/signin_cookie_changed_subscription.h" | |
| 6 | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 #include "net/cookies/cookie_store.h" | |
| 9 #include "net/url_request/url_request_context.h" | |
| 10 #include "net/url_request/url_request_context_getter.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Adds a callback for cookie changed events. This method is called on the IO | |
| 15 // thread, so it is safe to access the cookie store. | |
| 16 scoped_ptr<net::CookieStore::CookieChangedSubscription> | |
| 17 RegisterForCookieChangesOnIOThread( | |
| 18 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 19 const GURL url, | |
| 20 const std::string name, | |
| 21 const net::CookieStore::CookieChangedCallback callback) { | |
| 22 net::CookieStore* cookie_store = | |
| 23 context_getter->GetURLRequestContext()->cookie_store(); | |
| 24 DCHECK(cookie_store); | |
| 25 return cookie_store->AddCallbackForCookie(url, name, callback); | |
| 26 } | |
| 27 | |
| 28 // Posts a task on the |proxy| task runner that calls |OnCookieChanged| on | |
| 29 // |subscription|. | |
| 30 // Note that this method is called on the IO thread, so |subscription| must | |
| 31 // not be used here, it is only passed around. | |
| 32 void RunAsyncOnCookieChanged( | |
| 33 scoped_refptr<base::TaskRunner> proxy, | |
| 34 base::WeakPtr<SigninCookieChangedSubscription> subscription, | |
| 35 const net::CanonicalCookie& cookie, | |
| 36 bool removed) { | |
| 37 proxy->PostTask(FROM_HERE, | |
| 38 base::Bind(&SigninCookieChangedSubscription::OnCookieChanged, | |
| 39 subscription, | |
| 40 cookie, | |
| 41 removed)); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 | |
| 47 SigninCookieChangedSubscription::SigninCookieChangedSubscription( | |
| 48 const net::CookieStore::CookieChangedCallback& callback) | |
| 49 : callback_(callback) { | |
| 50 } | |
| 51 | |
| 52 SigninCookieChangedSubscription::~SigninCookieChangedSubscription() { | |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 54 if (cookie_changed_subscription_) { | |
| 55 content::BrowserThread::DeleteOnIOThread::Destruct( | |
| 56 cookie_changed_subscription_.release()); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 void SigninCookieChangedSubscription::SetCookieChangedSubscription( | |
| 61 scoped_ptr<net::CookieStore::CookieChangedSubscription> subscription) { | |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 63 DCHECK(subscription); | |
| 64 DCHECK(!cookie_changed_subscription_); | |
| 65 cookie_changed_subscription_ = subscription.Pass(); | |
| 66 } | |
| 67 | |
| 68 void SigninCookieChangedSubscription::OnCookieChanged( | |
| 69 const net::CanonicalCookie& cookie, | |
| 70 bool removed) { | |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 72 DCHECK(cookie_changed_subscription_); | |
| 73 if (!callback_.is_null()) { | |
| 74 callback_.Run(cookie, removed); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications( | |
| 79 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 80 const GURL& url, | |
| 81 const std::string& name) { | |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 83 | |
| 84 // 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.
| |
| 85 // |AddCookieChangedCallback| is called from the main thread, a thread | |
| 86 // jump is needed to register for cookie changed notifications. | |
| 87 net::CookieStore::CookieChangedCallback run_on_current_thread_callback = | |
| 88 base::Bind(&RunAsyncOnCookieChanged, | |
| 89 base::MessageLoopProxy::current(), | |
| 90 this->AsWeakPtr()); | |
| 91 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 92 content::BrowserThread::IO, | |
| 93 FROM_HERE, | |
| 94 base::Bind(&RegisterForCookieChangesOnIOThread, | |
| 95 context_getter, | |
| 96 url, | |
| 97 name, | |
| 98 run_on_current_thread_callback), | |
| 99 base::Bind(&SigninCookieChangedSubscription::SetCookieChangedSubscription, | |
| 100 this->AsWeakPtr())); | |
| 101 } | |
| OLD | NEW |