summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_apis/operation_runner.h
blob: 4f5fe3009f97378a14fa518e72d871610b284d97 (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
// Copyright (c) 2012 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 CHROME_BROWSER_GOOGLE_APIS_OPERATION_RUNNER_H_
#define CHROME_BROWSER_GOOGLE_APIS_OPERATION_RUNNER_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/google_apis/gdata_errorcode.h"

class Profile;

namespace net {
class URLRequestContextGetter;
}

namespace google_apis {

class AuthenticatedOperationInterface;
class AuthService;
class OperationRegistry;

// Helper class that runs AuthenticatedOperationInterface objects, handling
// retries and authentication.
class OperationRunner {
 public:
  // |url_request_context_getter| is used to perform authentication with
  // AuthService.
  //
  // |scopes| specifies OAuth2 scopes.
  //
  // |custom_user_agent| will be used for the User-Agent header in HTTP
  // requests issued through the operation runner if the value is not empty.
  OperationRunner(Profile* profile,
                  net::URLRequestContextGetter* url_request_context_getter,
                  const std::vector<std::string>& scopes,
                  const std::string& custom_user_agent);
  virtual ~OperationRunner();

  AuthService* auth_service() { return auth_service_.get(); }
  OperationRegistry* operation_registry() {
    return operation_registry_.get();
  }

  // Prepares the object for use.
  virtual void Initialize();

  // Cancels all in-flight operations.
  void CancelAll();

  // Starts an operation implementing the AuthenticatedOperationInterface
  // interface, and makes the operation retry upon authentication failures by
  // calling back to RetryOperation.
  void StartOperationWithRetry(AuthenticatedOperationInterface* operation);

 private:
  // Called when the access token is fetched.
  void OnAccessTokenFetched(
      const base::WeakPtr<AuthenticatedOperationInterface>& operation,
      GDataErrorCode error,
      const std::string& access_token);

  // Clears any authentication token and retries the operation, which forces
  // an authentication token refresh.
  void RetryOperation(AuthenticatedOperationInterface* operation);

  Profile* profile_;  // Not owned.

  scoped_ptr<AuthService> auth_service_;
  scoped_ptr<OperationRegistry> operation_registry_;
  const std::string custom_user_agent_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<OperationRunner> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(OperationRunner);
};

}  // namespace google_apis

#endif  // CHROME_BROWSER_GOOGLE_APIS_OPERATION_RUNNER_H_