| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "components/gcm_driver/gcm_client.h" | 12 #include "components/gcm_driver/gcm_client.h" |
| 13 #include "components/gcm_driver/gcm_connection_observer.h" |
| 13 #include "google_apis/gaia/account_tracker.h" | 14 #include "google_apis/gaia/account_tracker.h" |
| 14 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 15 | 16 |
| 16 namespace gcm { | 17 namespace gcm { |
| 17 | 18 |
| 19 class GCMDriver; |
| 20 |
| 18 // Class for reporting back which accounts are signed into. It is only meant to | 21 // Class for reporting back which accounts are signed into. It is only meant to |
| 19 // be used when the user is signed into sync. | 22 // be used when the user is signed into sync. |
| 20 class GCMAccountTracker : public gaia::AccountTracker::Observer, | 23 class GCMAccountTracker : public gaia::AccountTracker::Observer, |
| 21 public OAuth2TokenService::Consumer { | 24 public OAuth2TokenService::Consumer, |
| 25 public GCMConnectionObserver { |
| 22 public: | 26 public: |
| 23 // State of the account. | 27 // State of the account. |
| 24 // Allowed transitions: | 28 // Allowed transitions: |
| 25 // TOKEN_NEEDED - account info was created. | 29 // TOKEN_NEEDED - account info was created. |
| 26 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. | 30 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. |
| 27 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. | 31 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. |
| 28 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. | 32 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. |
| 29 // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed. | 33 // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed. |
| 30 // TOKEN_NEEDED -> ACCOUNT_REMOVED - account was removed. | 34 // TOKEN_NEEDED -> ACCOUNT_REMOVED - account was removed. |
| 31 // TOKEN_PRESENT -> ACCOUNT_REMOVED - account was removed. | 35 // TOKEN_PRESENT -> ACCOUNT_REMOVED - account was removed. |
| 32 enum AccountState { | 36 enum AccountState { |
| 33 TOKEN_NEEDED, // Needs a token (AccountInfo was recently created or | 37 TOKEN_NEEDED, // Needs a token (AccountInfo was recently created or |
| 34 // token request failed). | 38 // token request failed). |
| 35 GETTING_TOKEN, // There is a pending token request. | 39 GETTING_TOKEN, // There is a pending token request. |
| 36 TOKEN_PRESENT, // We have a token for the account. | 40 TOKEN_PRESENT, // We have a token for the account. |
| 37 ACCOUNT_REMOVED, // Account was removed, and we didn't report it yet. | 41 ACCOUNT_REMOVED, // Account was removed, and we didn't report it yet. |
| 38 }; | 42 }; |
| 39 | 43 |
| 40 // Stores necessary account information and state of token fetching. | 44 // Stores necessary account information and state of token fetching. |
| 41 struct AccountInfo { | 45 struct AccountInfo { |
| 42 AccountInfo(const std::string& email, AccountState state); | 46 AccountInfo(const std::string& email, AccountState state); |
| 43 ~AccountInfo(); | 47 ~AccountInfo(); |
| 44 | 48 |
| 45 // Email address of the tracked account. | 49 // Email address of the tracked account. |
| 46 std::string email; | 50 std::string email; |
| 47 // OAuth2 access token, when |state| is TOKEN_PRESENT. | 51 // OAuth2 access token, when |state| is TOKEN_PRESENT. |
| 48 std::string access_token; | 52 std::string access_token; |
| 53 // Expiration time of the access tokens. |
| 54 base::Time expiration_time; |
| 49 // Status of the token fetching. | 55 // Status of the token fetching. |
| 50 AccountState state; | 56 AccountState state; |
| 51 }; | 57 }; |
| 52 | 58 |
| 53 // Callback for the GetAccountsForCheckin call. |account_tokens|: list of | |
| 54 // email addresses, account ids and OAuth2 access tokens. | |
| 55 typedef base::Callback<void(const std::vector<GCMClient::AccountTokenInfo>& | |
| 56 account_tokens)> UpdateAccountsCallback; | |
| 57 | |
| 58 // Creates an instance of GCMAccountTracker. |account_tracker| is used to | 59 // Creates an instance of GCMAccountTracker. |account_tracker| is used to |
| 59 // deliver information about the account, while |callback| will be called | 60 // deliver information about the accounts present in the browser context to |
| 60 // once all of the accounts have been fetched a necessary OAuth2 token, as | 61 // |driver|. |
| 61 // many times as the list of accounts is stable, meaning that all accounts | |
| 62 // are known and there is no related activity in progress for them, like | |
| 63 // fetching OAuth2 tokens. | |
| 64 GCMAccountTracker(scoped_ptr<gaia::AccountTracker> account_tracker, | 62 GCMAccountTracker(scoped_ptr<gaia::AccountTracker> account_tracker, |
| 65 const UpdateAccountsCallback& callback); | 63 GCMDriver* driver); |
| 66 virtual ~GCMAccountTracker(); | 64 virtual ~GCMAccountTracker(); |
| 67 | 65 |
| 68 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is | 66 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is |
| 69 // called Start() and Stop() should no longer be used. Must be called before | 67 // called Start() and Stop() should no longer be used. Must be called before |
| 70 // destruction. | 68 // destruction. |
| 71 void Shutdown(); | 69 void Shutdown(); |
| 72 | 70 |
| 73 // Starts tracking accounts. | 71 // Starts tracking accounts. |
| 74 void Start(); | 72 void Start(); |
| 75 // Stops tracking accounts. Cancels all of the pending token requests. | 73 |
| 76 void Stop(); | 74 // Gets the number of pending token requests. Only used for testing. |
| 75 size_t get_pending_token_request_count() const { |
| 76 return pending_token_requests_.size(); |
| 77 } |
| 77 | 78 |
| 78 private: | 79 private: |
| 79 // Maps account keys to account states. Keyed by account_ids as used by | 80 // Maps account keys to account states. Keyed by account_ids as used by |
| 80 // OAuth2TokenService. | 81 // OAuth2TokenService. |
| 81 typedef std::map<std::string, AccountInfo> AccountInfos; | 82 typedef std::map<std::string, AccountInfo> AccountInfos; |
| 82 | 83 |
| 83 // AccountTracker::Observer overrides. | 84 // AccountTracker::Observer overrides. |
| 84 virtual void OnAccountAdded(const gaia::AccountIds& ids) OVERRIDE; | 85 virtual void OnAccountAdded(const gaia::AccountIds& ids) OVERRIDE; |
| 85 virtual void OnAccountRemoved(const gaia::AccountIds& ids) OVERRIDE; | 86 virtual void OnAccountRemoved(const gaia::AccountIds& ids) OVERRIDE; |
| 86 virtual void OnAccountSignInChanged(const gaia::AccountIds& ids, | 87 virtual void OnAccountSignInChanged(const gaia::AccountIds& ids, |
| 87 bool is_signed_in) OVERRIDE; | 88 bool is_signed_in) OVERRIDE; |
| 88 | 89 |
| 89 // OAuth2TokenService::Consumer overrides. | 90 // OAuth2TokenService::Consumer overrides. |
| 90 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 91 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 91 const std::string& access_token, | 92 const std::string& access_token, |
| 92 const base::Time& expiration_time) OVERRIDE; | 93 const base::Time& expiration_time) OVERRIDE; |
| 93 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 94 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 94 const GoogleServiceAuthError& error) OVERRIDE; | 95 const GoogleServiceAuthError& error) OVERRIDE; |
| 95 | 96 |
| 97 // GCMConnectionObserver overrides. |
| 98 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE; |
| 99 virtual void OnDisconnected() OVERRIDE; |
| 100 |
| 96 // Report the list of accounts with OAuth2 tokens back using the |callback_| | 101 // Report the list of accounts with OAuth2 tokens back using the |callback_| |
| 97 // function. If there are token requests in progress, do nothing. | 102 // function. If there are token requests in progress, do nothing. |
| 98 void CompleteCollectingTokens(); | 103 void CompleteCollectingTokens(); |
| 104 // Verify that all of the tokens are ready to be passed down to the GCM |
| 105 // Driver, e.g. none of them has expired or is missing. Returns true if not |
| 106 // all tokens are valid and a fetching yet more tokens is required. |
| 107 bool SanitizeTokens(); |
| 99 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or | 108 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or |
| 100 // OnGetTokenFailure(..). | 109 // OnGetTokenFailure(..). |
| 101 void DeleteTokenRequest(const OAuth2TokenService::Request* request); | 110 void DeleteTokenRequest(const OAuth2TokenService::Request* request); |
| 102 // Checks on all known accounts, and calls GetToken(..) for those with | 111 // Checks on all known accounts, and calls GetToken(..) for those with |
| 103 // |state == TOKEN_NEEDED|. | 112 // |state == TOKEN_NEEDED|. |
| 104 void GetAllNeededTokens(); | 113 void GetAllNeededTokens(); |
| 105 // Starts fetching the OAuth2 token for the GCM group scope. | 114 // Starts fetching the OAuth2 token for the GCM group scope. |
| 106 void GetToken(AccountInfos::iterator& account_iter); | 115 void GetToken(AccountInfos::iterator& account_iter); |
| 107 | 116 |
| 108 // Handling of actual sign in and sign out for accounts. | 117 // Handling of actual sign in and sign out for accounts. |
| 109 void OnAccountSignedIn(const gaia::AccountIds& ids); | 118 void OnAccountSignedIn(const gaia::AccountIds& ids); |
| 110 void OnAccountSignedOut(const gaia::AccountIds& ids); | 119 void OnAccountSignedOut(const gaia::AccountIds& ids); |
| 111 | 120 |
| 112 OAuth2TokenService* GetTokenService(); | 121 OAuth2TokenService* GetTokenService(); |
| 113 | 122 |
| 114 // Account tracker. | 123 // Account tracker. |
| 115 scoped_ptr<gaia::AccountTracker> account_tracker_; | 124 scoped_ptr<gaia::AccountTracker> account_tracker_; |
| 116 | 125 |
| 117 // Callback to be called after all of the account and OAuth2 tokens are | 126 // GCM Driver. Not owned. |
| 118 // collected. | 127 GCMDriver* driver_; |
| 119 UpdateAccountsCallback callback_; | |
| 120 | 128 |
| 121 // State of the account. | 129 // State of the account. |
| 122 AccountInfos account_infos_; | 130 AccountInfos account_infos_; |
| 123 | 131 |
| 124 // Indicates whether shutdown has been called. | 132 // Indicates whether shutdown has been called. |
| 125 bool shutdown_called_; | 133 bool shutdown_called_; |
| 126 | 134 |
| 127 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | 135 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; |
| 128 | 136 |
| 129 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); | 137 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); |
| 130 }; | 138 }; |
| 131 | 139 |
| 132 } // namespace gcm | 140 } // namespace gcm |
| 133 | 141 |
| 134 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 142 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
| OLD | NEW |