oauth2_token_service_request.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
6#define GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
7
8#include <set>
9#include <string>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/single_thread_task_runner.h"
14#include "base/threading/non_thread_safe.h"
15#include "google_apis/gaia/oauth2_token_service.h"
16
17// OAuth2TokenServiceRequest represents an asynchronous request to an
18// OAuth2TokenService that may live in another thread.
19//
20// An OAuth2TokenServiceRequest can be created and started from any thread.
21class OAuth2TokenServiceRequest : public OAuth2TokenService::Request,
22                                  public base::NonThreadSafe {
23 public:
24  class Core;
25
26  // Interface for providing an OAuth2TokenService.
27  //
28  // Ref-counted so that OAuth2TokenServiceRequest can ensure this object isn't
29  // destroyed out from under the token service task runner thread.  Because
30  // OAuth2TokenServiceRequest has a reference, implementations of
31  // TokenServiceProvider must be capable of being destroyed on the same thread
32  // on which the OAuth2TokenServiceRequest was created.
33  class TokenServiceProvider
34      : public base::RefCountedThreadSafe<TokenServiceProvider> {
35   public:
36    TokenServiceProvider();
37
38    // Returns the task runner on which the token service lives.
39    //
40    // This method may be called from any thread.
41    virtual scoped_refptr<base::SingleThreadTaskRunner>
42        GetTokenServiceTaskRunner() = 0;
43
44    // Returns a pointer to a token service.
45    //
46    // Caller does not own the token service and must not delete it.  The token
47    // service must outlive all instances of OAuth2TokenServiceRequest.
48    //
49    // This method may only be called from the task runner returned by
50    // |GetTokenServiceTaskRunner|.
51    virtual OAuth2TokenService* GetTokenService() = 0;
52
53   protected:
54    friend class base::RefCountedThreadSafe<TokenServiceProvider>;
55    virtual ~TokenServiceProvider();
56  };
57
58  // Creates and starts an access token request for |account_id| and |scopes|.
59  //
60  // |provider| is used to get the OAuth2TokenService.
61  //
62  // |account_id| must not be empty.
63  //
64  // |scopes| must not be empty.
65  //
66  // |consumer| will be invoked in the same thread that invoked CreateAndStart
67  // and must outlive the returned request object.  Destroying the request
68  // object ensure that |consumer| will not be called.  However, the actual
69  // network activities may not be canceled and the cache in OAuth2TokenService
70  // may be populated with the fetched results.
71  static scoped_ptr<OAuth2TokenServiceRequest> CreateAndStart(
72      const scoped_refptr<TokenServiceProvider>& provider,
73      const std::string& account_id,
74      const OAuth2TokenService::ScopeSet& scopes,
75      OAuth2TokenService::Consumer* consumer);
76
77  // Invalidates |access_token| for |account_id| and |scopes|.
78  //
79  // |provider| is used to get the OAuth2TokenService.
80  //
81  // |account_id| must not be empty.
82  //
83  // |scopes| must not be empty.
84  static void InvalidateToken(
85      const scoped_refptr<TokenServiceProvider>& provider,
86      const std::string& account_id,
87      const OAuth2TokenService::ScopeSet& scopes,
88      const std::string& access_token);
89
90  virtual ~OAuth2TokenServiceRequest();
91
92  // OAuth2TokenService::Request.
93  virtual std::string GetAccountId() const OVERRIDE;
94
95 private:
96  OAuth2TokenServiceRequest(const std::string& account_id);
97
98  void StartWithCore(const scoped_refptr<Core>& core);
99
100  const std::string account_id_;
101  scoped_refptr<Core> core_;
102
103  DISALLOW_COPY_AND_ASSIGN(OAuth2TokenServiceRequest);
104};
105
106#endif  // GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
107