| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 BLIMP_CLIENT_CORE_SESSION_IDENTITY_SOURCE_H_ | |
| 6 #define BLIMP_CLIENT_CORE_SESSION_IDENTITY_SOURCE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "blimp/client/public/blimp_client_context_delegate.h" | |
| 14 #include "google_apis/gaia/identity_provider.h" | |
| 15 #include "google_apis/gaia/oauth2_token_service.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace client { | |
| 19 | |
| 20 // IdentitySource handles OAuth2 token request and forward user sign in state | |
| 21 // change to Blimp with ProfileIdentityProvider. | |
| 22 class IdentitySource : public OAuth2TokenService::Consumer, | |
| 23 public OAuth2TokenService::Observer, | |
| 24 public IdentityProvider::Observer { | |
| 25 public: | |
| 26 using TokenCallback = base::Callback<void(const std::string&)>; | |
| 27 using TokenErrorCallback = | |
| 28 base::Callback<void(const GoogleServiceAuthError&)>; | |
| 29 | |
| 30 explicit IdentitySource(std::unique_ptr<IdentityProvider> identity_provider, | |
| 31 const TokenErrorCallback& error_callback, | |
| 32 const TokenCallback& callback); | |
| 33 ~IdentitySource() override; | |
| 34 | |
| 35 // Start Blimp authentication by requesting OAuth2 token from Google. | |
| 36 // Duplicate connect calls during token fetching will be ignored. | |
| 37 void Connect(); | |
| 38 | |
| 39 // Returns the account name for the current user. | |
| 40 std::string GetActiveUsername(); | |
| 41 | |
| 42 // Add sign in state observer. | |
| 43 void AddObserver(IdentityProvider::Observer* observer); | |
| 44 | |
| 45 // Remove sign in state observer. | |
| 46 void RemoveObserver(IdentityProvider::Observer* observer); | |
| 47 | |
| 48 // OAuth2TokenService::Consumer implementation. | |
| 49 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 50 const std::string& access_token, | |
| 51 const base::Time& expiration_time) override; | |
| 52 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 53 const GoogleServiceAuthError& error) override; | |
| 54 | |
| 55 // OAuth2TokenService::Observer implementation. | |
| 56 void OnRefreshTokenAvailable(const std::string& account_id) override; | |
| 57 | |
| 58 protected: | |
| 59 // Provide OAuth2 token service and listen to Google account sign in state, | |
| 60 // protected for testing. | |
| 61 std::unique_ptr<IdentityProvider> identity_provider_; | |
| 62 | |
| 63 private: | |
| 64 // Fetch OAuth token. | |
| 65 void FetchAuthToken(); | |
| 66 | |
| 67 // OAuth2 token request. The request is created when we start to fetch the | |
| 68 // access token in OAuth2TokenService::StartRequest, and destroyed when | |
| 69 // OAuth2TokenService::Consumer callback get called. | |
| 70 std::unique_ptr<OAuth2TokenService::Request> token_request_; | |
| 71 | |
| 72 TokenErrorCallback error_callback_; | |
| 73 | |
| 74 // Callback after OAuth2 token is fetched. | |
| 75 TokenCallback token_callback_; | |
| 76 | |
| 77 // If we are fetching OAuth2 token. Connect call during token fetching will | |
| 78 // be ignored. | |
| 79 bool is_fetching_token_; | |
| 80 | |
| 81 // Account id of current active user, used during token request. | |
| 82 std::string account_id_; | |
| 83 | |
| 84 // Current retry count due to request cancellation. | |
| 85 int retry_times_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(IdentitySource); | |
| 88 }; | |
| 89 | |
| 90 } // namespace client | |
| 91 } // namespace blimp | |
| 92 | |
| 93 #endif // BLIMP_CLIENT_CORE_SESSION_IDENTITY_SOURCE_H_ | |
| OLD | NEW |