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