Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_request.h
blob8d81ba4616d84290a43c78234388f79ee7112df1
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.
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_
8 #include <set>
9 #include <string>
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"
17 // OAuth2TokenServiceRequest represents an asynchronous request to an
18 // OAuth2TokenService that may live in another thread.
20 // An OAuth2TokenServiceRequest can be created and started from any thread.
21 class OAuth2TokenServiceRequest : public OAuth2TokenService::Request,
22 public base::NonThreadSafe {
23 public:
24 class Core;
26 // Interface for providing an OAuth2TokenService.
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();
38 // Returns the task runner on which the token service lives.
40 // This method may be called from any thread.
41 virtual scoped_refptr<base::SingleThreadTaskRunner>
42 GetTokenServiceTaskRunner() = 0;
44 // Returns a pointer to a token service.
46 // Caller does not own the token service and must not delete it. The token
47 // service must outlive all instances of OAuth2TokenServiceRequest.
49 // This method may only be called from the task runner returned by
50 // |GetTokenServiceTaskRunner|.
51 virtual OAuth2TokenService* GetTokenService() = 0;
53 protected:
54 friend class base::RefCountedThreadSafe<TokenServiceProvider>;
55 virtual ~TokenServiceProvider();
58 // Creates and starts an access token request for |account_id| and |scopes|.
60 // |provider| is used to get the OAuth2TokenService.
62 // |account_id| must not be empty.
64 // |scopes| must not be empty.
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);
77 // Invalidates |access_token| for |account_id| and |scopes|.
79 // |provider| is used to get the OAuth2TokenService.
81 // |account_id| must not be empty.
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);
90 ~OAuth2TokenServiceRequest() override;
92 // OAuth2TokenService::Request.
93 std::string GetAccountId() const override;
95 private:
96 OAuth2TokenServiceRequest(const std::string& account_id);
98 void StartWithCore(const scoped_refptr<Core>& core);
100 const std::string account_id_;
101 scoped_refptr<Core> core_;
103 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenServiceRequest);
106 #endif // GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_REQUEST_H_