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 | 59 // |account_tracker| is used to deliver information about the accounts present |
54 // email addresses, account ids and OAuth2 access tokens. | 60 // in the browser context to |driver|. |
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 // deliver information about the account, while |callback| will be called | |
60 // once all of the accounts have been fetched a necessary OAuth2 token, as | |
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, | 61 GCMAccountTracker(scoped_ptr<gaia::AccountTracker> account_tracker, |
65 const UpdateAccountsCallback& callback); | 62 GCMDriver* driver); |
66 virtual ~GCMAccountTracker(); | 63 virtual ~GCMAccountTracker(); |
67 | 64 |
68 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is | 65 // 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 | 66 // called Start() and Stop() should no longer be used. Must be called before |
70 // destruction. | 67 // destruction. |
71 void Shutdown(); | 68 void Shutdown(); |
72 | 69 |
73 // Starts tracking accounts. | 70 // Starts tracking accounts. |
74 void Start(); | 71 void Start(); |
75 // Stops tracking accounts. Cancels all of the pending token requests. | 72 |
76 void Stop(); | 73 // Gets the number of pending token requests. Only used for testing. |
| 74 size_t get_pending_token_request_count() const { |
| 75 return pending_token_requests_.size(); |
| 76 } |
77 | 77 |
78 private: | 78 private: |
79 // Maps account keys to account states. Keyed by account_ids as used by | 79 // Maps account keys to account states. Keyed by account_ids as used by |
80 // OAuth2TokenService. | 80 // OAuth2TokenService. |
81 typedef std::map<std::string, AccountInfo> AccountInfos; | 81 typedef std::map<std::string, AccountInfo> AccountInfos; |
82 | 82 |
83 // AccountTracker::Observer overrides. | 83 // AccountTracker::Observer overrides. |
84 virtual void OnAccountAdded(const gaia::AccountIds& ids) override; | 84 virtual void OnAccountAdded(const gaia::AccountIds& ids) override; |
85 virtual void OnAccountRemoved(const gaia::AccountIds& ids) override; | 85 virtual void OnAccountRemoved(const gaia::AccountIds& ids) override; |
86 virtual void OnAccountSignInChanged(const gaia::AccountIds& ids, | 86 virtual void OnAccountSignInChanged(const gaia::AccountIds& ids, |
87 bool is_signed_in) override; | 87 bool is_signed_in) override; |
88 | 88 |
89 // OAuth2TokenService::Consumer overrides. | 89 // OAuth2TokenService::Consumer overrides. |
90 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 90 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
91 const std::string& access_token, | 91 const std::string& access_token, |
92 const base::Time& expiration_time) override; | 92 const base::Time& expiration_time) override; |
93 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 93 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
94 const GoogleServiceAuthError& error) override; | 94 const GoogleServiceAuthError& error) override; |
95 | 95 |
| 96 // GCMConnectionObserver overrides. |
| 97 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) override; |
| 98 virtual void OnDisconnected() override; |
| 99 |
96 // Report the list of accounts with OAuth2 tokens back using the |callback_| | 100 // Report the list of accounts with OAuth2 tokens back using the |callback_| |
97 // function. If there are token requests in progress, do nothing. | 101 // function. If there are token requests in progress, do nothing. |
98 void CompleteCollectingTokens(); | 102 void CompleteCollectingTokens(); |
| 103 // Verify that all of the tokens are ready to be passed down to the GCM |
| 104 // Driver, e.g. none of them has expired or is missing. Returns true if not |
| 105 // all tokens are valid and a fetching yet more tokens is required. |
| 106 bool SanitizeTokens(); |
99 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or | 107 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or |
100 // OnGetTokenFailure(..). | 108 // OnGetTokenFailure(..). |
101 void DeleteTokenRequest(const OAuth2TokenService::Request* request); | 109 void DeleteTokenRequest(const OAuth2TokenService::Request* request); |
102 // Checks on all known accounts, and calls GetToken(..) for those with | 110 // Checks on all known accounts, and calls GetToken(..) for those with |
103 // |state == TOKEN_NEEDED|. | 111 // |state == TOKEN_NEEDED|. |
104 void GetAllNeededTokens(); | 112 void GetAllNeededTokens(); |
105 // Starts fetching the OAuth2 token for the GCM group scope. | 113 // Starts fetching the OAuth2 token for the GCM group scope. |
106 void GetToken(AccountInfos::iterator& account_iter); | 114 void GetToken(AccountInfos::iterator& account_iter); |
107 | 115 |
108 // Handling of actual sign in and sign out for accounts. | 116 // Handling of actual sign in and sign out for accounts. |
109 void OnAccountSignedIn(const gaia::AccountIds& ids); | 117 void OnAccountSignedIn(const gaia::AccountIds& ids); |
110 void OnAccountSignedOut(const gaia::AccountIds& ids); | 118 void OnAccountSignedOut(const gaia::AccountIds& ids); |
111 | 119 |
112 OAuth2TokenService* GetTokenService(); | 120 OAuth2TokenService* GetTokenService(); |
113 | 121 |
114 // Account tracker. | 122 // Account tracker. |
115 scoped_ptr<gaia::AccountTracker> account_tracker_; | 123 scoped_ptr<gaia::AccountTracker> account_tracker_; |
116 | 124 |
117 // Callback to be called after all of the account and OAuth2 tokens are | 125 // GCM Driver. Not owned. |
118 // collected. | 126 GCMDriver* driver_; |
119 UpdateAccountsCallback callback_; | |
120 | 127 |
121 // State of the account. | 128 // State of the account. |
122 AccountInfos account_infos_; | 129 AccountInfos account_infos_; |
123 | 130 |
124 // Indicates whether shutdown has been called. | 131 // Indicates whether shutdown has been called. |
125 bool shutdown_called_; | 132 bool shutdown_called_; |
126 | 133 |
127 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | 134 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; |
128 | 135 |
129 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); | 136 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); |
130 }; | 137 }; |
131 | 138 |
132 } // namespace gcm | 139 } // namespace gcm |
133 | 140 |
134 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 141 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
OLD | NEW |