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.
4
5#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_COOKIE_HELPER_H_
6#define COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_COOKIE_HELPER_H_
7
8#include "base/callback_helpers.h"
9#include "base/memory/ref_counted.h"
10#include "net/cookies/canonical_cookie.h"
11#include "net/url_request/url_request_context_getter.h"
12
13namespace base {
14class MessageLoopProxy;
15}
16
17namespace net {
18class URLRequestContextGetter;
19}
20
21// This class fetches GAIA cookie on IO thread on behalf of SigninManager which
22// only lives on the UI thread.
23class SigninManagerCookieHelper
24    : public base::RefCountedThreadSafe<SigninManagerCookieHelper> {
25 public:
26  explicit SigninManagerCookieHelper(
27      net::URLRequestContextGetter* request_context_getter,
28      scoped_refptr<base::MessageLoopProxy> ui_thread,
29      scoped_refptr<base::MessageLoopProxy> io_thread);
30
31  // Starts fetching gaia cookies, which will notify its completion via
32  // callback.
33  void StartFetchingGaiaCookiesOnUIThread(
34      const base::Callback<void(const net::CookieList& cookies)>& callback);
35
36  // Starts fetching cookies for the given URL. This must be called on
37  // |ui_thread_|.
38  void StartFetchingCookiesOnUIThread(
39      const GURL& url,
40      const base::Callback<void(const net::CookieList& cookies)>& callback);
41
42 private:
43  friend class base::RefCountedThreadSafe<SigninManagerCookieHelper>;
44  ~SigninManagerCookieHelper();
45
46  // Fetch cookies for the given URL. This must be called on |io_thread_|.
47  void FetchCookiesOnIOThread(const GURL& url);
48
49  // Callback for fetching cookies. This must be called on |io_thread_|.
50  void OnCookiesFetched(const net::CookieList& cookies);
51
52  // Notifies the completion callback. This must be called on |ui_thread_|.
53  void NotifyOnUIThread(const net::CookieList& cookies);
54
55  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
56  // This only mutates on |ui_thread_|.
57  base::Callback<void(const net::CookieList& cookies)> completion_callback_;
58
59  // The MessageLoopProxy that this class uses as its UI thread.
60  scoped_refptr<base::MessageLoopProxy> ui_thread_;
61  // The MessageLoopProxy that this class uses as its IO thread.
62  scoped_refptr<base::MessageLoopProxy> io_thread_;
63  DISALLOW_COPY_AND_ASSIGN(SigninManagerCookieHelper);
64};
65
66#endif  // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_COOKIE_HELPER_H_
67