Re-land: C++ readability review
[chromium-blink-merge.git] / chrome / browser / signin / signin_cookie_changed_subscription.cc
blobd4d8012e5f2487a11dbb27f5102ca0bf3fda00be
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.
5 #include "chrome/browser/signin/signin_cookie_changed_subscription.h"
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"
11 SigninCookieChangedSubscription::SubscriptionHolder::SubscriptionHolder() {
14 SigninCookieChangedSubscription::SubscriptionHolder::~SubscriptionHolder() {
17 SigninCookieChangedSubscription::SigninCookieChangedSubscription(
18 scoped_refptr<net::URLRequestContextGetter> context_getter,
19 const GURL& url,
20 const std::string& name,
21 const net::CookieStore::CookieChangedCallback& callback)
22 : context_getter_(context_getter),
23 subscription_holder_io_(new SubscriptionHolder),
24 callback_(callback) {
25 RegisterForCookieChangedNotifications(url, name);
28 SigninCookieChangedSubscription::~SigninCookieChangedSubscription() {
29 DCHECK(thread_checker_.CalledOnValidThread());
30 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner =
31 context_getter_->GetNetworkTaskRunner();
32 if (network_task_runner->BelongsToCurrentThread()) {
33 subscription_holder_io_.reset();
34 } else {
35 network_task_runner->DeleteSoon(FROM_HERE,
36 subscription_holder_io_.release());
40 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications(
41 const GURL& url,
42 const std::string& name) {
43 DCHECK(thread_checker_.CalledOnValidThread());
45 // The cookie store can only be accessed from the context getter which lives
46 // on the network thread. As |AddCookieChangedCallback| is called from the
47 // main thread, a thread jump is needed to register for cookie changed
48 // notifications.
49 net::CookieStore::CookieChangedCallback run_on_current_thread_callback =
50 base::Bind(&SigninCookieChangedSubscription::RunAsyncOnCookieChanged,
51 base::MessageLoopProxy::current(),
52 this->AsWeakPtr());
53 base::Closure register_closure =
54 base::Bind(&RegisterForCookieChangesOnIOThread,
55 context_getter_,
56 url,
57 name,
58 run_on_current_thread_callback,
59 base::Unretained(subscription_holder_io_.get()));
60 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner =
61 context_getter_->GetNetworkTaskRunner();
62 if (network_task_runner->BelongsToCurrentThread()) {
63 register_closure.Run();
64 } else {
65 network_task_runner->PostTask(FROM_HERE, register_closure);
69 // static
70 void SigninCookieChangedSubscription::RegisterForCookieChangesOnIOThread(
71 scoped_refptr<net::URLRequestContextGetter> context_getter,
72 const GURL url,
73 const std::string name,
74 const net::CookieStore::CookieChangedCallback callback,
75 SigninCookieChangedSubscription::SubscriptionHolder*
76 out_subscription_holder) {
77 DCHECK(out_subscription_holder);
78 net::CookieStore* cookie_store =
79 context_getter->GetURLRequestContext()->cookie_store();
80 DCHECK(cookie_store);
81 out_subscription_holder->subscription =
82 cookie_store->AddCallbackForCookie(url, name, callback);
85 // static
86 void SigninCookieChangedSubscription::RunAsyncOnCookieChanged(
87 scoped_refptr<base::TaskRunner> proxy,
88 base::WeakPtr<SigninCookieChangedSubscription> subscription,
89 const net::CanonicalCookie& cookie,
90 bool removed) {
91 proxy->PostTask(FROM_HERE,
92 base::Bind(&SigninCookieChangedSubscription::OnCookieChanged,
93 subscription,
94 cookie,
95 removed));
98 void SigninCookieChangedSubscription::OnCookieChanged(
99 const net::CanonicalCookie& cookie,
100 bool removed) {
101 DCHECK(thread_checker_.CalledOnValidThread());
102 if (!callback_.is_null()) {
103 callback_.Run(cookie, removed);