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 CHROME_BROWSER_SUPERVISED_USER_PERMISSION_REQUEST_CREATOR_APIARY_H_
6#define CHROME_BROWSER_SUPERVISED_USER_PERMISSION_REQUEST_CREATOR_APIARY_H_
7
8#include <string>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/scoped_vector.h"
12#include "chrome/browser/supervised_user/permission_request_creator.h"
13#include "google_apis/gaia/oauth2_token_service.h"
14#include "net/url_request/url_fetcher_delegate.h"
15#include "url/gurl.h"
16
17class Profile;
18class SupervisedUserSigninManagerWrapper;
19
20namespace base {
21class Time;
22}
23
24namespace net {
25class URLFetcher;
26class URLRequestContextGetter;
27}
28
29class PermissionRequestCreatorApiary : public PermissionRequestCreator,
30                                       public OAuth2TokenService::Consumer,
31                                       public net::URLFetcherDelegate {
32 public:
33  PermissionRequestCreatorApiary(
34      OAuth2TokenService* oauth2_token_service,
35      scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper,
36      net::URLRequestContextGetter* context);
37  virtual ~PermissionRequestCreatorApiary();
38
39  static scoped_ptr<PermissionRequestCreator> CreateWithProfile(
40      Profile* profile);
41
42  // PermissionRequestCreator implementation:
43  virtual void CreatePermissionRequest(const GURL& url_requested,
44                                       const base::Closure& callback) OVERRIDE;
45
46 private:
47  struct Request;
48  typedef ScopedVector<Request>::iterator RequestIterator;
49
50  // OAuth2TokenService::Consumer implementation:
51  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
52                                 const std::string& access_token,
53                                 const base::Time& expiration_time) OVERRIDE;
54  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
55                                 const GoogleServiceAuthError& error) OVERRIDE;
56
57  // net::URLFetcherDelegate implementation.
58  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
59
60  std::string GetApiScopeToUse() const;
61
62  // Requests an access token, which is the first thing we need. This is where
63  // we restart when the returned access token has expired.
64  void StartFetching(Request* request);
65
66  void DispatchNetworkError(RequestIterator it, int error_code);
67  void DispatchGoogleServiceAuthError(RequestIterator it,
68                                      const GoogleServiceAuthError& error);
69
70  OAuth2TokenService* oauth2_token_service_;
71  scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper_;
72  net::URLRequestContextGetter* context_;
73
74  ScopedVector<Request> requests_;
75};
76
77#endif  // CHROME_BROWSER_SUPERVISED_USER_PERMISSION_REQUEST_CREATOR_APIARY_H_
78