summaryrefslogtreecommitdiffstats
path: root/google_apis/gaia/oauth2_token_service_request.h
blob: 8d81ba4616d84290a43c78234388f79ee7112df1 (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
// Copyright 2014 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 GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
#define GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_

#include <set>
#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/non_thread_safe.h"
#include "google_apis/gaia/oauth2_token_service.h"

// OAuth2TokenServiceRequest represents an asynchronous request to an
// OAuth2TokenService that may live in another thread.
//
// An OAuth2TokenServiceRequest can be created and started from any thread.
class OAuth2TokenServiceRequest : public OAuth2TokenService::Request,
                                  public base::NonThreadSafe {
 public:
  class Core;

  // Interface for providing an OAuth2TokenService.
  //
  // Ref-counted so that OAuth2TokenServiceRequest can ensure this object isn't
  // destroyed out from under the token service task runner thread.  Because
  // OAuth2TokenServiceRequest has a reference, implementations of
  // TokenServiceProvider must be capable of being destroyed on the same thread
  // on which the OAuth2TokenServiceRequest was created.
  class TokenServiceProvider
      : public base::RefCountedThreadSafe<TokenServiceProvider> {
   public:
    TokenServiceProvider();

    // Returns the task runner on which the token service lives.
    //
    // This method may be called from any thread.
    virtual scoped_refptr<base::SingleThreadTaskRunner>
        GetTokenServiceTaskRunner() = 0;

    // Returns a pointer to a token service.
    //
    // Caller does not own the token service and must not delete it.  The token
    // service must outlive all instances of OAuth2TokenServiceRequest.
    //
    // This method may only be called from the task runner returned by
    // |GetTokenServiceTaskRunner|.
    virtual OAuth2TokenService* GetTokenService() = 0;

   protected:
    friend class base::RefCountedThreadSafe<TokenServiceProvider>;
    virtual ~TokenServiceProvider();
  };

  // Creates and starts an access token request for |account_id| and |scopes|.
  //
  // |provider| is used to get the OAuth2TokenService.
  //
  // |account_id| must not be empty.
  //
  // |scopes| must not be empty.
  //
  // |consumer| will be invoked in the same thread that invoked CreateAndStart
  // and must outlive the returned request object.  Destroying the request
  // object ensure that |consumer| will not be called.  However, the actual
  // network activities may not be canceled and the cache in OAuth2TokenService
  // may be populated with the fetched results.
  static scoped_ptr<OAuth2TokenServiceRequest> CreateAndStart(
      const scoped_refptr<TokenServiceProvider>& provider,
      const std::string& account_id,
      const OAuth2TokenService::ScopeSet& scopes,
      OAuth2TokenService::Consumer* consumer);

  // Invalidates |access_token| for |account_id| and |scopes|.
  //
  // |provider| is used to get the OAuth2TokenService.
  //
  // |account_id| must not be empty.
  //
  // |scopes| must not be empty.
  static void InvalidateToken(
      const scoped_refptr<TokenServiceProvider>& provider,
      const std::string& account_id,
      const OAuth2TokenService::ScopeSet& scopes,
      const std::string& access_token);

  ~OAuth2TokenServiceRequest() override;

  // OAuth2TokenService::Request.
  std::string GetAccountId() const override;

 private:
  OAuth2TokenServiceRequest(const std::string& account_id);

  void StartWithCore(const scoped_refptr<Core>& core);

  const std::string account_id_;
  scoped_refptr<Core> core_;

  DISALLOW_COPY_AND_ASSIGN(OAuth2TokenServiceRequest);
};

#endif  // GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_