OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 public gaia::AccountTracker::Observer { | 86 public gaia::AccountTracker::Observer { |
87 public: | 87 public: |
88 typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens; | 88 typedef std::map<ExtensionTokenKey, IdentityTokenCacheValue> CachedTokens; |
89 | 89 |
90 class ShutdownObserver { | 90 class ShutdownObserver { |
91 public: | 91 public: |
92 virtual void OnShutdown() = 0; | 92 virtual void OnShutdown() = 0; |
93 }; | 93 }; |
94 | 94 |
95 explicit IdentityAPI(content::BrowserContext* context); | 95 explicit IdentityAPI(content::BrowserContext* context); |
96 virtual ~IdentityAPI(); | 96 ~IdentityAPI() override; |
97 | 97 |
98 // Request serialization queue for getAuthToken. | 98 // Request serialization queue for getAuthToken. |
99 IdentityMintRequestQueue* mint_queue(); | 99 IdentityMintRequestQueue* mint_queue(); |
100 | 100 |
101 // Token cache | 101 // Token cache |
102 void SetCachedToken(const ExtensionTokenKey& key, | 102 void SetCachedToken(const ExtensionTokenKey& key, |
103 const IdentityTokenCacheValue& token_data); | 103 const IdentityTokenCacheValue& token_data); |
104 void EraseCachedToken(const std::string& extension_id, | 104 void EraseCachedToken(const std::string& extension_id, |
105 const std::string& token); | 105 const std::string& token); |
106 void EraseAllCachedTokens(); | 106 void EraseAllCachedTokens(); |
107 const IdentityTokenCacheValue& GetCachedToken(const ExtensionTokenKey& key); | 107 const IdentityTokenCacheValue& GetCachedToken(const ExtensionTokenKey& key); |
108 | 108 |
109 const CachedTokens& GetAllCachedTokens(); | 109 const CachedTokens& GetAllCachedTokens(); |
110 | 110 |
111 // Account queries. | 111 // Account queries. |
112 std::vector<std::string> GetAccounts() const; | 112 std::vector<std::string> GetAccounts() const; |
113 std::string FindAccountKeyByGaiaId(const std::string& gaia_id); | 113 std::string FindAccountKeyByGaiaId(const std::string& gaia_id); |
114 | 114 |
115 // BrowserContextKeyedAPI implementation. | 115 // BrowserContextKeyedAPI implementation. |
116 virtual void Shutdown() override; | 116 void Shutdown() override; |
117 static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); | 117 static BrowserContextKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
118 | 118 |
119 // gaia::AccountTracker::Observer implementation: | 119 // gaia::AccountTracker::Observer implementation: |
120 virtual void OnAccountAdded(const gaia::AccountIds& ids) override; | 120 void OnAccountAdded(const gaia::AccountIds& ids) override; |
121 virtual void OnAccountRemoved(const gaia::AccountIds& ids) override; | 121 void OnAccountRemoved(const gaia::AccountIds& ids) override; |
122 virtual void OnAccountSignInChanged(const gaia::AccountIds& ids, | 122 void OnAccountSignInChanged(const gaia::AccountIds& ids, |
123 bool is_signed_in) override; | 123 bool is_signed_in) override; |
124 | 124 |
125 void AddShutdownObserver(ShutdownObserver* observer); | 125 void AddShutdownObserver(ShutdownObserver* observer); |
126 void RemoveShutdownObserver(ShutdownObserver* observer); | 126 void RemoveShutdownObserver(ShutdownObserver* observer); |
127 | 127 |
128 void SetAccountStateForTest(gaia::AccountIds ids, bool is_signed_in); | 128 void SetAccountStateForTest(gaia::AccountIds ids, bool is_signed_in); |
129 | 129 |
130 private: | 130 private: |
131 friend class BrowserContextKeyedAPIFactory<IdentityAPI>; | 131 friend class BrowserContextKeyedAPIFactory<IdentityAPI>; |
132 | 132 |
133 // BrowserContextKeyedAPI implementation. | 133 // BrowserContextKeyedAPI implementation. |
(...skipping 12 matching lines...) Expand all Loading... |
146 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | 146 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
147 | 147 |
148 class IdentityGetAccountsFunction : public ChromeUIThreadExtensionFunction { | 148 class IdentityGetAccountsFunction : public ChromeUIThreadExtensionFunction { |
149 public: | 149 public: |
150 DECLARE_EXTENSION_FUNCTION("identity.getAccounts", | 150 DECLARE_EXTENSION_FUNCTION("identity.getAccounts", |
151 IDENTITY_GETACCOUNTS); | 151 IDENTITY_GETACCOUNTS); |
152 | 152 |
153 IdentityGetAccountsFunction(); | 153 IdentityGetAccountsFunction(); |
154 | 154 |
155 private: | 155 private: |
156 virtual ~IdentityGetAccountsFunction(); | 156 ~IdentityGetAccountsFunction() override; |
157 | 157 |
158 // UIThreadExtensionFunction implementation. | 158 // UIThreadExtensionFunction implementation. |
159 virtual ExtensionFunction::ResponseAction Run() override; | 159 ExtensionFunction::ResponseAction Run() override; |
160 }; | 160 }; |
161 | 161 |
162 // identity.getAuthToken fetches an OAuth 2 function for the | 162 // identity.getAuthToken fetches an OAuth 2 function for the |
163 // caller. The request has three sub-flows: non-interactive, | 163 // caller. The request has three sub-flows: non-interactive, |
164 // interactive, and sign-in. | 164 // interactive, and sign-in. |
165 // | 165 // |
166 // In the non-interactive flow, getAuthToken requests a token from | 166 // In the non-interactive flow, getAuthToken requests a token from |
167 // GAIA. GAIA may respond with a token, an error, or "consent | 167 // GAIA. GAIA may respond with a token, an error, or "consent |
168 // required". In the consent required cases, getAuthToken proceeds to | 168 // required". In the consent required cases, getAuthToken proceeds to |
169 // the second, interactive phase. | 169 // the second, interactive phase. |
(...skipping 17 matching lines...) Expand all Loading... |
187 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", | 187 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", |
188 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN); | 188 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN); |
189 | 189 |
190 IdentityGetAuthTokenFunction(); | 190 IdentityGetAuthTokenFunction(); |
191 | 191 |
192 const ExtensionTokenKey* GetExtensionTokenKeyForTest() { | 192 const ExtensionTokenKey* GetExtensionTokenKeyForTest() { |
193 return token_key_.get(); | 193 return token_key_.get(); |
194 } | 194 } |
195 | 195 |
196 protected: | 196 protected: |
197 virtual ~IdentityGetAuthTokenFunction(); | 197 ~IdentityGetAuthTokenFunction() override; |
198 | 198 |
199 // IdentitySigninFlow::Delegate implementation: | 199 // IdentitySigninFlow::Delegate implementation: |
200 virtual void SigninSuccess() override; | 200 void SigninSuccess() override; |
201 virtual void SigninFailed() override; | 201 void SigninFailed() override; |
202 | 202 |
203 // GaiaWebAuthFlow::Delegate implementation: | 203 // GaiaWebAuthFlow::Delegate implementation: |
204 virtual void OnGaiaFlowFailure(GaiaWebAuthFlow::Failure failure, | 204 void OnGaiaFlowFailure(GaiaWebAuthFlow::Failure failure, |
205 GoogleServiceAuthError service_error, | 205 GoogleServiceAuthError service_error, |
206 const std::string& oauth_error) override; | 206 const std::string& oauth_error) override; |
207 virtual void OnGaiaFlowCompleted(const std::string& access_token, | 207 void OnGaiaFlowCompleted(const std::string& access_token, |
208 const std::string& expiration) override; | 208 const std::string& expiration) override; |
209 | 209 |
210 // Starts a login access token request. | 210 // Starts a login access token request. |
211 virtual void StartLoginAccessTokenRequest(); | 211 virtual void StartLoginAccessTokenRequest(); |
212 | 212 |
213 // OAuth2TokenService::Consumer implementation: | 213 // OAuth2TokenService::Consumer implementation: |
214 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 214 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
215 const std::string& access_token, | 215 const std::string& access_token, |
216 const base::Time& expiration_time) override; | 216 const base::Time& expiration_time) override; |
217 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 217 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
218 const GoogleServiceAuthError& error) override; | 218 const GoogleServiceAuthError& error) override; |
219 | 219 |
220 // Starts a mint token request to GAIA. | 220 // Starts a mint token request to GAIA. |
221 // Exposed for testing. | 221 // Exposed for testing. |
222 virtual void StartGaiaRequest(const std::string& login_access_token); | 222 virtual void StartGaiaRequest(const std::string& login_access_token); |
223 | 223 |
224 // Caller owns the returned instance. | 224 // Caller owns the returned instance. |
225 // Exposed for testing. | 225 // Exposed for testing. |
226 virtual OAuth2MintTokenFlow* CreateMintTokenFlow(); | 226 virtual OAuth2MintTokenFlow* CreateMintTokenFlow(); |
227 | 227 |
228 scoped_ptr<OAuth2TokenService::Request> login_token_request_; | 228 scoped_ptr<OAuth2TokenService::Request> login_token_request_; |
229 | 229 |
230 private: | 230 private: |
231 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, | 231 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, |
232 ComponentWithChromeClientId); | 232 ComponentWithChromeClientId); |
233 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, | 233 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, |
234 ComponentWithNormalClientId); | 234 ComponentWithNormalClientId); |
235 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, InteractiveQueueShutdown); | 235 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, InteractiveQueueShutdown); |
236 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, NoninteractiveShutdown); | 236 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, NoninteractiveShutdown); |
237 | 237 |
238 // ExtensionFunction: | 238 // ExtensionFunction: |
239 virtual bool RunAsync() override; | 239 bool RunAsync() override; |
240 | 240 |
241 // Helpers to report async function results to the caller. | 241 // Helpers to report async function results to the caller. |
242 void StartAsyncRun(); | 242 void StartAsyncRun(); |
243 void CompleteAsyncRun(bool success); | 243 void CompleteAsyncRun(bool success); |
244 void CompleteFunctionWithResult(const std::string& access_token); | 244 void CompleteFunctionWithResult(const std::string& access_token); |
245 void CompleteFunctionWithError(const std::string& error); | 245 void CompleteFunctionWithError(const std::string& error); |
246 | 246 |
247 // Initiate/complete the sub-flows. | 247 // Initiate/complete the sub-flows. |
248 void StartSigninFlow(); | 248 void StartSigninFlow(); |
249 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); | 249 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); |
250 void CompleteMintTokenFlow(); | 250 void CompleteMintTokenFlow(); |
251 | 251 |
252 // IdentityMintRequestQueue::Request implementation: | 252 // IdentityMintRequestQueue::Request implementation: |
253 virtual void StartMintToken(IdentityMintRequestQueue::MintType type) override; | 253 void StartMintToken(IdentityMintRequestQueue::MintType type) override; |
254 | 254 |
255 // OAuth2MintTokenFlow::Delegate implementation: | 255 // OAuth2MintTokenFlow::Delegate implementation: |
256 virtual void OnMintTokenSuccess(const std::string& access_token, | 256 void OnMintTokenSuccess(const std::string& access_token, |
257 int time_to_live) override; | 257 int time_to_live) override; |
258 virtual void OnMintTokenFailure( | 258 void OnMintTokenFailure(const GoogleServiceAuthError& error) override; |
259 const GoogleServiceAuthError& error) override; | 259 void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice) override; |
260 virtual void OnIssueAdviceSuccess( | |
261 const IssueAdviceInfo& issue_advice) override; | |
262 | 260 |
263 // IdentityAPI::ShutdownObserver implementation: | 261 // IdentityAPI::ShutdownObserver implementation: |
264 virtual void OnShutdown() override; | 262 void OnShutdown() override; |
265 | 263 |
266 #if defined(OS_CHROMEOS) | 264 #if defined(OS_CHROMEOS) |
267 // Starts a login access token request for device robot account. This method | 265 // Starts a login access token request for device robot account. This method |
268 // will be called only in enterprise kiosk mode in ChromeOS. | 266 // will be called only in enterprise kiosk mode in ChromeOS. |
269 virtual void StartDeviceLoginAccessTokenRequest(); | 267 virtual void StartDeviceLoginAccessTokenRequest(); |
270 #endif | 268 #endif |
271 | 269 |
272 // Methods for invoking UI. Overridable for testing. | 270 // Methods for invoking UI. Overridable for testing. |
273 virtual void ShowLoginPopup(); | 271 virtual void ShowLoginPopup(); |
274 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); | 272 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); |
(...skipping 24 matching lines...) Expand all Loading... |
299 | 297 |
300 class IdentityGetProfileUserInfoFunction | 298 class IdentityGetProfileUserInfoFunction |
301 : public ChromeUIThreadExtensionFunction { | 299 : public ChromeUIThreadExtensionFunction { |
302 public: | 300 public: |
303 DECLARE_EXTENSION_FUNCTION("identity.getProfileUserInfo", | 301 DECLARE_EXTENSION_FUNCTION("identity.getProfileUserInfo", |
304 IDENTITY_GETPROFILEUSERINFO); | 302 IDENTITY_GETPROFILEUSERINFO); |
305 | 303 |
306 IdentityGetProfileUserInfoFunction(); | 304 IdentityGetProfileUserInfoFunction(); |
307 | 305 |
308 private: | 306 private: |
309 virtual ~IdentityGetProfileUserInfoFunction(); | 307 ~IdentityGetProfileUserInfoFunction() override; |
310 | 308 |
311 // UIThreadExtensionFunction implementation. | 309 // UIThreadExtensionFunction implementation. |
312 virtual ExtensionFunction::ResponseAction Run() override; | 310 ExtensionFunction::ResponseAction Run() override; |
313 }; | 311 }; |
314 | 312 |
315 class IdentityRemoveCachedAuthTokenFunction | 313 class IdentityRemoveCachedAuthTokenFunction |
316 : public ChromeSyncExtensionFunction { | 314 : public ChromeSyncExtensionFunction { |
317 public: | 315 public: |
318 DECLARE_EXTENSION_FUNCTION("identity.removeCachedAuthToken", | 316 DECLARE_EXTENSION_FUNCTION("identity.removeCachedAuthToken", |
319 EXPERIMENTAL_IDENTITY_REMOVECACHEDAUTHTOKEN) | 317 EXPERIMENTAL_IDENTITY_REMOVECACHEDAUTHTOKEN) |
320 IdentityRemoveCachedAuthTokenFunction(); | 318 IdentityRemoveCachedAuthTokenFunction(); |
321 | 319 |
322 protected: | 320 protected: |
323 virtual ~IdentityRemoveCachedAuthTokenFunction(); | 321 ~IdentityRemoveCachedAuthTokenFunction() override; |
324 | 322 |
325 // SyncExtensionFunction implementation: | 323 // SyncExtensionFunction implementation: |
326 virtual bool RunSync() override; | 324 bool RunSync() override; |
327 }; | 325 }; |
328 | 326 |
329 class IdentityLaunchWebAuthFlowFunction : public ChromeAsyncExtensionFunction, | 327 class IdentityLaunchWebAuthFlowFunction : public ChromeAsyncExtensionFunction, |
330 public WebAuthFlow::Delegate { | 328 public WebAuthFlow::Delegate { |
331 public: | 329 public: |
332 DECLARE_EXTENSION_FUNCTION("identity.launchWebAuthFlow", | 330 DECLARE_EXTENSION_FUNCTION("identity.launchWebAuthFlow", |
333 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW); | 331 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW); |
334 | 332 |
335 IdentityLaunchWebAuthFlowFunction(); | 333 IdentityLaunchWebAuthFlowFunction(); |
336 | 334 |
337 // Tests may override extension_id. | 335 // Tests may override extension_id. |
338 void InitFinalRedirectURLPrefixForTest(const std::string& extension_id); | 336 void InitFinalRedirectURLPrefixForTest(const std::string& extension_id); |
339 | 337 |
340 private: | 338 private: |
341 virtual ~IdentityLaunchWebAuthFlowFunction(); | 339 ~IdentityLaunchWebAuthFlowFunction() override; |
342 virtual bool RunAsync() override; | 340 bool RunAsync() override; |
343 | 341 |
344 // WebAuthFlow::Delegate implementation. | 342 // WebAuthFlow::Delegate implementation. |
345 virtual void OnAuthFlowFailure(WebAuthFlow::Failure failure) override; | 343 void OnAuthFlowFailure(WebAuthFlow::Failure failure) override; |
346 virtual void OnAuthFlowURLChange(const GURL& redirect_url) override; | 344 void OnAuthFlowURLChange(const GURL& redirect_url) override; |
347 virtual void OnAuthFlowTitleChange(const std::string& title) override {} | 345 void OnAuthFlowTitleChange(const std::string& title) override {} |
348 | 346 |
349 // Helper to initialize final URL prefix. | 347 // Helper to initialize final URL prefix. |
350 void InitFinalRedirectURLPrefix(const std::string& extension_id); | 348 void InitFinalRedirectURLPrefix(const std::string& extension_id); |
351 | 349 |
352 scoped_ptr<WebAuthFlow> auth_flow_; | 350 scoped_ptr<WebAuthFlow> auth_flow_; |
353 GURL final_url_prefix_; | 351 GURL final_url_prefix_; |
354 }; | 352 }; |
355 | 353 |
356 } // namespace extensions | 354 } // namespace extensions |
357 | 355 |
358 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 356 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
OLD | NEW |