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 COMPONENTS_POLICY_CORE_COMMON_CLOUD_USER_INFO_FETCHER_H_
6#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_USER_INFO_FETCHER_H_
7
8#include <string>
9#include "base/memory/scoped_ptr.h"
10#include "components/policy/policy_export.h"
11#include "net/url_request/url_fetcher_delegate.h"
12
13class GoogleServiceAuthError;
14
15namespace base {
16class DictionaryValue;
17}
18
19namespace net {
20class URLFetcher;
21class URLRequestContextGetter;
22}
23
24namespace policy {
25
26// Class that makes a UserInfo request, parses the response, and notifies
27// a provided Delegate when the request is complete.
28class POLICY_EXPORT UserInfoFetcher : public net::URLFetcherDelegate {
29 public:
30  class POLICY_EXPORT Delegate {
31   public:
32    // Invoked when the UserInfo request has succeeded, passing the parsed
33    // response in |response|. Delegate may free the UserInfoFetcher in this
34    // callback.
35    virtual void OnGetUserInfoSuccess(
36        const base::DictionaryValue* response) = 0;
37
38    // Invoked when the UserInfo request has failed, passing the associated
39    // error in |error|. Delegate may free the UserInfoFetcher in this
40    // callback.
41    virtual void OnGetUserInfoFailure(const GoogleServiceAuthError& error) = 0;
42  };
43
44  // Create a new UserInfoFetcher. |context| can be NULL for unit tests.
45  UserInfoFetcher(Delegate* delegate, net::URLRequestContextGetter* context);
46  virtual ~UserInfoFetcher();
47
48  // Starts the UserInfo request, using the passed OAuth2 |access_token|.
49  void Start(const std::string& access_token);
50
51  // net::URLFetcherDelegate implementation.
52  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
53
54 private:
55  Delegate* delegate_;
56  net::URLRequestContextGetter* context_;
57  scoped_ptr<net::URLFetcher> url_fetcher_;
58
59  DISALLOW_COPY_AND_ASSIGN(UserInfoFetcher);
60};
61
62}  // namespace policy
63
64#endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_USER_INFO_FETCHER_H_
65