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