1// Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_OBFUSCATED_GAIA_ID_FETCHER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_OBFUSCATED_GAIA_ID_FETCHER_H_
7
8#include <string>
9
10#include "google_apis/gaia/oauth2_api_call_flow.h"
11
12class GoogleServiceAuthError;
13
14namespace content {
15class URLFetcher;
16}
17
18namespace net {
19class URLRequestContextGetter;
20}
21
22namespace extensions {
23
24// Fetches obfuscated Gaia ID of the Google Account that is logged in to Chrome.
25// This starts an asynchronous operation which reports success to the delegate.
26// Call "Start()" to start the async fetch.
27class ObfuscatedGaiaIdFetcher : public OAuth2ApiCallFlow {
28 public:
29  // Delegate interface that is called when obfuscated Gaia id fetch
30  // succeeds or fails.
31  class Delegate {
32   public:
33    virtual void OnObfuscatedGaiaIdFetchSuccess(
34        const std::string& obfuscated_id) {}
35    virtual void OnObfuscatedGaiaIdFetchFailure(
36        const GoogleServiceAuthError& error) {}
37    virtual ~Delegate() {}
38  };
39
40  // TODO(petewil): Someday let's make a profile keyed service to cache
41  // the Gaia ID.
42
43  ObfuscatedGaiaIdFetcher(net::URLRequestContextGetter* context,
44                          Delegate* delegate,
45                          const std::string& refresh_token);
46  virtual ~ObfuscatedGaiaIdFetcher();
47
48  static std::vector<std::string> GetScopes();
49
50 protected:
51  // OAuth2ApiCallFlow implementation
52  virtual GURL CreateApiCallUrl() OVERRIDE;
53  virtual std::string CreateApiCallBody() OVERRIDE;
54  virtual void ProcessApiCallSuccess(
55      const net::URLFetcher* source) OVERRIDE;
56  virtual void ProcessApiCallFailure(
57      const net::URLFetcher* source) OVERRIDE;
58  virtual void ProcessNewAccessToken(const std::string& access_token) OVERRIDE;
59  virtual void ProcessMintAccessTokenFailure(
60      const GoogleServiceAuthError& error) OVERRIDE;
61
62 private:
63  FRIEND_TEST_ALL_PREFIXES(ObfuscatedGaiaIdFetcherTest, SetUp);
64  FRIEND_TEST_ALL_PREFIXES(ObfuscatedGaiaIdFetcherTest, ParseResponse);
65  FRIEND_TEST_ALL_PREFIXES(ObfuscatedGaiaIdFetcherTest, ProcessApiCallSuccess);
66  FRIEND_TEST_ALL_PREFIXES(ObfuscatedGaiaIdFetcherTest, ProcessApiCallFailure);
67
68  void ReportSuccess(const std::string& obfuscated_id);
69  void ReportFailure(const GoogleServiceAuthError& error);
70
71  // Get the obfuscated Gaia ID out of the response body.
72  static bool ParseResponse(
73      const std::string& data, std::string* obfuscated_id);
74
75  // Unowned pointer to the delegate.  Normally the delegate owns
76  // this fetcher class.
77  Delegate* delegate_;
78
79  DISALLOW_COPY_AND_ASSIGN(ObfuscatedGaiaIdFetcher);
80};
81
82}  // namespace extensions
83
84#endif  // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_OBFUSCATED_GAIA_ID_FETCHER_H_
85