summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/content/browser/wallet/wallet_signin_helper.h
blob: d7cf5ff42a928409407e2a5b2c1c5b2d986dddf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_SIGNIN_HELPER_H_
#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_SIGNIN_HELPER_H_

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"

namespace net {
class URLFetcher;
class URLRequestContextGetter;
class URLRequestStatus;
}

class GoogleServiceAuthError;

namespace autofill {
namespace wallet {

class WalletSigninHelperDelegate;

// Authenticates the user against the Online Wallet service.
// This class is not thread-safe.  An instance may be used on any thread, but
// should not be accessed from multiple threads.
class WalletSigninHelper : public net::URLFetcherDelegate {
 public:
  // Constructs a helper that works with a given |delegate| and uses a given
  // |getter| to obtain a context for URL. Both |delegate| and |getter| shall
  // remain valid over the entire lifetime of the created instance.
  WalletSigninHelper(WalletSigninHelperDelegate* delegate,
                     net::URLRequestContextGetter* getter);

  virtual ~WalletSigninHelper();

  // Initiates an attempt to passively sign the user into the Online Wallet.
  // A passive sign-in is a non-interactive refresh of content area cookies,
  // and it succeeds as long as the Online Wallet service could safely accept
  // or refresh the existing area cookies, and the user doesn't need to be
  // fully reauthenticated with the service.
  // Either OnPassiveSigninSuccess or OnPassiveSigninFailure will be called
  // on the original thread.
  void StartPassiveSignin();

  // Initiates a fetch of the user name of a signed-in user.
  // Either OnUserNameFetchSuccess or OnUserNameFetchFailure will
  // be called on the original thread.
  void StartUserNameFetch();

  // Initiates the fetch of the user's Google Wallet cookie.
  void StartWalletCookieValueFetch();

 protected:
  // Sign-in helper states (for tests).
  enum State {
    IDLE,
    PASSIVE_EXECUTING_SIGNIN,
    PASSIVE_FETCHING_USERINFO,
    USERNAME_FETCHING_USERINFO,
  };

  // (For tests) Current state of the sign-in helper.
  State state() const { return state_; }

  // (For tests) URL used to fetch the currently signed-in user info.
  std::string GetGetAccountInfoUrlForTesting() const;

 private:
  // Called if a service authentication error occurs.
  void OnServiceError(const GoogleServiceAuthError& error);

  // Called if any other error occurs.
  void OnOtherError();

  // URLFetcherDelegate implementation.
  virtual void OnURLFetchComplete(const net::URLFetcher* fetcher) OVERRIDE;

  // Initiates fetching of the currently signed-in user information.
  void StartFetchingUserNameFromSession();

  // Processes the user information received from the server by url_fetcher_
  // and calls the delegate callbacks on success/failure.
  void ProcessGetAccountInfoResponseAndFinish();

  // Attempts to parse a response from the Online Wallet sign-in.
  // Returns true if the response is correct and the sign-in has succeeded.
  // Otherwise, it calls OnServiceError() and returns false.
  bool ParseSignInResponse();

  // Attempts to parse the GetAccountInfo response from the server.
  // Returns true on success; the obtained email address is stored into |email|.
  bool ParseGetAccountInfoResponse(const net::URLFetcher* fetcher,
                                   std::string* email);

  // Callback for when the Google Wallet cookie has been retrieved.
  void ReturnWalletCookieValue(const std::string& cookie_value);

  // Should be valid throughout the lifetime of the instance.
  WalletSigninHelperDelegate* const delegate_;

  // URLRequestContextGetter to be used for URLFetchers.
  net::URLRequestContextGetter* const getter_;

  // While passive login/merge session URL fetches are going on:
  scoped_ptr<net::URLFetcher> url_fetcher_;

  // User account name (email) fetched from OnGetUserInfoSuccess().
  std::string username_;

  // Current internal state of the helper.
  State state_;

  base::WeakPtrFactory<WalletSigninHelper> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(WalletSigninHelper);
};

}  // namespace wallet
}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_SIGNIN_HELPER_H_