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 void RegisterForCookieChangesOnIOThread( | |
| 17 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 18 const GURL url, | |
| 19 const std::string name, | |
| 20 const net::CookieStore::CookieChangedCallback callback, | |
| 21 SigninCookieChangedSubscription::SubscriptionHolder* | |
| 22 out_subscription_holder) { | |
| 23 DCHECK(out_subscription_holder); | |
| 24 net::CookieStore* cookie_store = | |
| 25 context_getter->GetURLRequestContext()->cookie_store(); | |
| 26 DCHECK(cookie_store); | |
| 27 out_subscription_holder->subscription = | |
| 28 cookie_store->AddCallbackForCookie(url, name, callback); | |
| 29 } | |
| 30 | |
| 31 // Posts a task on the |proxy| task runner that calls |OnCookieChanged| on | |
| 32 // |subscription|. | |
| 33 // Note that this method is called on the IO thread, so |subscription| must | |
| 34 // not be used here, it is only passed around. | |
| 35 void RunAsyncOnCookieChanged( | |
| 36 scoped_refptr<base::TaskRunner> proxy, | |
| 37 base::WeakPtr<SigninCookieChangedSubscription> subscription, | |
| 38 const net::CanonicalCookie& cookie, | |
| 39 bool removed) { | |
| 40 proxy->PostTask(FROM_HERE, | |
| 41 base::Bind(&SigninCookieChangedSubscription::OnCookieChanged, | |
| 42 subscription, | |
| 43 cookie, | |
| 44 removed)); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 SigninCookieChangedSubscription::SubscriptionHolder::SubscriptionHolder() { | |
| 50 } | |
| 51 | |
| 52 SigninCookieChangedSubscription::SubscriptionHolder::~SubscriptionHolder() { | |
| 53 } | |
| 54 | |
| 55 SigninCookieChangedSubscription::SigninCookieChangedSubscription( | |
| 56 const net::CookieStore::CookieChangedCallback& callback) | |
| 57 : callback_(callback), subscription_holder_io_(new SubscriptionHolder()) { | |
| 58 } | |
| 59 | |
| 60 SigninCookieChangedSubscription::~SigninCookieChangedSubscription() { | |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 62 | |
| 63 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.
| |
| 64 subscription_holder_io_.release()); | |
| 65 } | |
| 66 | |
| 67 void SigninCookieChangedSubscription::OnCookieChanged( | |
| 68 const net::CanonicalCookie& cookie, | |
| 69 bool removed) { | |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 71 if (!callback_.is_null()) { | |
| 72 callback_.Run(cookie, removed); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications( | |
| 77 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 78 const GURL& url, | |
| 79 const std::string& name) { | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 | |
| 82 // The cookie store can only be accessed from the context getter which lives | |
| 83 // on the IO thread. As |AddCookieChangedCallback| is called from the main | |
| 84 // thread, a thread jump is needed to register for cookie changed | |
| 85 // notifications. | |
| 86 net::CookieStore::CookieChangedCallback run_on_current_thread_callback = | |
| 87 base::Bind(&RunAsyncOnCookieChanged, | |
| 88 base::MessageLoopProxy::current(), | |
| 89 this->AsWeakPtr()); | |
| 90 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.
| |
| 91 content::BrowserThread::IO, | |
| 92 FROM_HERE, | |
| 93 base::Bind(&RegisterForCookieChangesOnIOThread, | |
| 94 context_getter, | |
| 95 url, | |
| 96 name, | |
| 97 run_on_current_thread_callback, | |
| 98 base::Unretained(subscription_holder_io_.get()))); | |
| 99 } | |
| OLD | NEW |