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 "components/signin/core/browser/signin_cookie_changed_subscription.h"
7 #include "base/thread_task_runner_handle.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"
12 SigninCookieChangedSubscription::SubscriptionHolder::SubscriptionHolder() {
15 SigninCookieChangedSubscription::SubscriptionHolder::~SubscriptionHolder() {
18 SigninCookieChangedSubscription::SigninCookieChangedSubscription(
19 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
21 const std::string
& name
,
22 const net::CookieStore::CookieChangedCallback
& callback
)
23 : context_getter_(context_getter
),
24 subscription_holder_io_(new SubscriptionHolder
),
26 RegisterForCookieChangedNotifications(url
, name
);
29 SigninCookieChangedSubscription::~SigninCookieChangedSubscription() {
30 DCHECK(thread_checker_
.CalledOnValidThread());
31 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner
=
32 context_getter_
->GetNetworkTaskRunner();
33 if (network_task_runner
->BelongsToCurrentThread()) {
34 subscription_holder_io_
.reset();
36 network_task_runner
->DeleteSoon(FROM_HERE
,
37 subscription_holder_io_
.release());
41 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications(
43 const std::string
& name
) {
44 DCHECK(thread_checker_
.CalledOnValidThread());
46 // The cookie store can only be accessed from the context getter which lives
47 // on the network thread. As |AddCookieChangedCallback| is called from the
48 // main thread, a thread jump is needed to register for cookie changed
50 net::CookieStore::CookieChangedCallback run_on_current_thread_callback
=
51 base::Bind(&SigninCookieChangedSubscription::RunAsyncOnCookieChanged
,
52 base::ThreadTaskRunnerHandle::Get(), this->AsWeakPtr());
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
);