Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: chrome/common/net/gaia/oauth2_api_call_flow.h

Issue 9937004: Create an abstract flow class that abstracts the following logical steps of calling an OAuth2 enabl… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 CHROME_COMMON_NET_GAIA_OAUTH2_API_CALL_FLOW_H_
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_API_CALL_FLOW_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h"
12 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h"
13 #include "chrome/common/net/gaia/oauth2_mint_token_consumer.h"
14 #include "chrome/common/net/gaia/oauth2_mint_token_fetcher.h"
15 #include "content/public/common/url_fetcher.h"
16 #include "content/public/common/url_fetcher_delegate.h"
17
18 class GoogleServiceAuthError;
19 class OAuth2MintTokenFlowTest;
20
21 namespace net {
22 class URLRequestContextGetter;
23 }
24
25 // Base calss for all classes that implement a flow to call OAuth2
msw 2012/03/30 23:56:32 nit spelling: "class".
Munjal (Google) 2012/04/04 19:24:38 Done.
26 // enabled APIs.
27 //
28 // Given a refresh token, an access token, and a list of scopes an OAuth2
29 // enabled API is called in the following way:
30 // 1. Try the given access token to call the API.
31 // 2. If that does not work, use the refresh token and scopes to generate
32 // a new access token.
33 // 3. Try the new access token to call the API.
34 //
35 // This class abstracts the basic steps and exposes template methods
36 // for sub-classes to implement for API specific details.
37 class OAuth2ApiCallFlow
38 : public content::URLFetcherDelegate,
39 public OAuth2AccessTokenConsumer {
40 public:
41 // Creates an instance that works with the given data.
42 // Note that access_token can be empty. In that case, the flow will skip
msw 2012/03/30 23:56:32 optional nit: vertical bars around |access_token|.
Munjal (Google) 2012/04/04 19:24:38 Done.
43 // the first step (of trying an existing acces token).
44 OAuth2ApiCallFlow(
45 net::URLRequestContextGetter* context,
46 const std::string& refresh_token,
47 const std::string& access_token,
48 const std::vector<std::string>& scopes);
49
50 virtual ~OAuth2ApiCallFlow();
51
52 // Start the flow.
53 void Start();
54
55 // OAuth2AccessTokenFetcher implementation.
56 virtual void OnGetTokenSuccess(const std::string& access_token) OVERRIDE;
57 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
58
59 // content::URLFetcherDelegate implementation.
60 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
61
62 protected:
63 // Template methods for sub-classes.
64
65 // Methods to help create HTTP request.
66 virtual GURL CreateApiCallUrl() = 0;
67 virtual std::string CreateApiCallBody() = 0;
68
69 // Sub-classes can expose appropriate observer interface by implementing
msw 2012/03/30 23:56:32 nit grammar: "can expose *an* appropriate".
Munjal (Google) 2012/04/04 19:24:38 Done.
70 // these template methods.
71 // Called when the API call finished successfully.
72 virtual void ProcessApiCallSuccess(const content::URLFetcher* source) = 0;
73 // Called when the API call failed.
74 virtual void ProcessApiCallFailure(const content::URLFetcher* source) = 0;
75 // Called when a new access token is generated.
76 virtual void ProcessNewAccessToken(const std::string& access_token) = 0;
77 virtual void ProcessMintAccessTokenFailure(
78 const GoogleServiceAuthError& error) = 0;
79
80 private:
81 // The steps this class performs are:
msw 2012/03/30 23:56:32 This is a duplicated but less informative version
Munjal (Google) 2012/04/04 19:24:38 Done.
82 // 1. Try existing access token.
83 // 2. If that works, flow is done. If not, generate a new access token.
84 // 3. Try using new access token.
85 enum State {
86 INITIAL,
87 API_CALL_STARTED,
88 API_CALL_DONE,
89 MINT_ACCESS_TOKEN_STARTED,
90 MINT_ACCESS_TOKEN_DONE,
91 ERROR_STATE
92 };
93
94 friend class OAuth2ApiCallFlowTest;
95
96 // Helper to create an instnace of access token fetcher.
Ryan Sleevi 2012/03/30 22:54:17 nit typo: instnace -> instance
Munjal (Google) 2012/04/04 19:24:38 Done.
97 // Caller owns the returned instance.
98 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher();
msw 2012/03/30 23:56:32 nit: Mention that this is virtual just for unit te
Munjal (Google) 2012/04/04 19:24:38 Done.
99
100 // Creates an instance of URLFetcher that does not send or save cookies.
101 // The URLFether's method will be GET if body is empty, POST otherwise.
msw 2012/03/30 23:56:32 nit: perhaps rephrase or mention that "body" indic
Munjal (Google) 2012/04/04 19:24:38 Done.
102 // Caller owns the returned instance.
103 virtual content::URLFetcher* CreateURLFetcher();
msw 2012/03/30 23:56:32 nit: Mention that this is virtual just for unit te
Munjal (Google) 2012/04/04 19:24:38 Done.
104
105 // Helper methods to implement the state machien for the flow.
Ryan Sleevi 2012/03/30 22:54:17 nit typo: machien -> machine
Munjal (Google) 2012/04/04 19:24:38 Done.
106 void BeginApiCall();
107 void EndApiCall(const content::URLFetcher* source);
108 void BeginMintAccessToken();
109 void EndMintAccessToken(const GoogleServiceAuthError* error);
110
111 net::URLRequestContextGetter* context_;
112 std::string refresh_token_;
113 std::string access_token_;
114 std::vector<std::string> scopes_;
115
116 State state_;
117 // Whether we have already tried minting access token once.
Ryan Sleevi 2012/03/30 22:54:17 nit grammar: minting access token once. -> minting
Munjal (Google) 2012/04/04 19:24:38 Done.
118 bool tried_mint_access_token_;
119
120 scoped_ptr<content::URLFetcher> url_fetcher_;
121 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
122
123 DISALLOW_COPY_AND_ASSIGN(OAuth2ApiCallFlow);
124 };
125
126 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_API_CALL_FLOW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698