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
,
20 const std::string
& name
,
21 const net::CookieStore::CookieChangedCallback
& callback
)
22 : context_getter_(context_getter
),
23 subscription_holder_io_(new SubscriptionHolder
),
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();
35 network_task_runner
->DeleteSoon(FROM_HERE
,
36 subscription_holder_io_
.release());
40 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications(
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
49 net::CookieStore::CookieChangedCallback run_on_current_thread_callback
=
50 base::Bind(&SigninCookieChangedSubscription::RunAsyncOnCookieChanged
,
51 base::MessageLoopProxy::current(),
53 base::Closure register_closure
=
54 base::Bind(&RegisterForCookieChangesOnIOThread
,
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();
65 network_task_runner
->PostTask(FROM_HERE
, register_closure
);
70 void SigninCookieChangedSubscription::RegisterForCookieChangesOnIOThread(
71 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
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();
81 out_subscription_holder
->subscription
=
82 cookie_store
->AddCallbackForCookie(url
, name
, callback
);
86 void SigninCookieChangedSubscription::RunAsyncOnCookieChanged(
87 scoped_refptr
<base::TaskRunner
> proxy
,
88 base::WeakPtr
<SigninCookieChangedSubscription
> subscription
,
89 const net::CanonicalCookie
& cookie
,
91 proxy
->PostTask(FROM_HERE
,
92 base::Bind(&SigninCookieChangedSubscription::OnCookieChanged
,
98 void SigninCookieChangedSubscription::OnCookieChanged(
99 const net::CanonicalCookie
& cookie
,
101 DCHECK(thread_checker_
.CalledOnValidThread());
102 if (!callback_
.is_null()) {
103 callback_
.Run(cookie
, removed
);