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 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_COOKIE_CHANGED_SUBSCRIPTION_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_COOKIE_CHANGED_SUBSCRIPTION_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/threading/thread_checker.h"
10 #include "components/signin/core/browser/signin_client.h"
11 #include "net/url_request/url_request_context_getter.h"
13 // The subscription for a cookie changed events. This class lives on the
15 class SigninCookieChangedSubscription
16 : public SigninClient::CookieChangedSubscription
,
17 public base::SupportsWeakPtr
<SigninCookieChangedSubscription
> {
19 // Creates a cookie changed subscription and registers for cookie changed
21 SigninCookieChangedSubscription(
22 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
24 const std::string
& name
,
25 const net::CookieStore::CookieChangedCallback
& callback
);
26 ~SigninCookieChangedSubscription() override
;
29 // Holder of a cookie store cookie changed subscription.
30 struct SubscriptionHolder
{
31 scoped_ptr
<net::CookieStore::CookieChangedSubscription
> subscription
;
33 ~SubscriptionHolder();
36 // Adds a callback for cookie changed events. This method is called on the
37 // network thread, so it is safe to access the cookie store.
38 static void RegisterForCookieChangesOnIOThread(
39 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
41 const std::string name
,
42 const net::CookieStore::CookieChangedCallback callback
,
43 SigninCookieChangedSubscription::SubscriptionHolder
*
44 out_subscription_holder
);
46 void RegisterForCookieChangedNotifications(const GURL
& url
,
47 const std::string
& name
);
49 // Posts a task on the |proxy| task runner that calls |OnCookieChanged| on
51 // Note that this method is called on the network thread, so |subscription|
52 // must not be used here, it is only passed around.
53 static void RunAsyncOnCookieChanged(
54 scoped_refptr
<base::TaskRunner
> proxy
,
55 base::WeakPtr
<SigninCookieChangedSubscription
> subscription
,
56 const net::CanonicalCookie
& cookie
,
59 // Handler for cookie changed events.
60 void OnCookieChanged(const net::CanonicalCookie
& cookie
, bool removed
);
62 // The context getter.
63 scoped_refptr
<net::URLRequestContextGetter
> context_getter_
;
65 // The holder of a cookie changed subscription. Must be destroyed on the
67 scoped_ptr
<SubscriptionHolder
> subscription_holder_io_
;
69 // Callback to be run on cookie changed events.
70 net::CookieStore::CookieChangedCallback callback_
;
72 base::ThreadChecker thread_checker_
;
74 DISALLOW_COPY_AND_ASSIGN(SigninCookieChangedSubscription
);
77 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_COOKIE_CHANGED_SUBSCRIPTION_H_