Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / signin / core / browser / signin_cookie_changed_subscription.cc
blob212e3ea18851c6baff07a012e25f45793ac528b7
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,
20 const GURL& url,
21 const std::string& name,
22 const net::CookieStore::CookieChangedCallback& callback)
23 : context_getter_(context_getter),
24 subscription_holder_io_(new SubscriptionHolder),
25 callback_(callback) {
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();
35 } else {
36 network_task_runner->DeleteSoon(FROM_HERE,
37 subscription_holder_io_.release());
41 void SigninCookieChangedSubscription::RegisterForCookieChangedNotifications(
42 const GURL& url,
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
49 // notifications.
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,
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);