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 "components/gcm_driver/gcm_connection_observer.h" |
14 #include "google_apis/gaia/account_tracker.h" | 14 #include "google_apis/gaia/account_tracker.h" |
15 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
16 | 16 |
| 17 namespace base { |
| 18 class Time; |
| 19 } |
| 20 |
17 namespace gcm { | 21 namespace gcm { |
18 | 22 |
19 class GCMDriver; | 23 class GCMDriver; |
20 | 24 |
21 // Class for reporting back which accounts are signed into. It is only meant to | 25 // Class for reporting back which accounts are signed into. It is only meant to |
22 // be used when the user is signed into sync. | 26 // be used when the user is signed into sync. |
| 27 // |
| 28 // This class makes a check for tokens periodically, to make sure the user is |
| 29 // still logged into the profile, so that in the case that the user is not, we |
| 30 // can immediately report that to the GCM and stop messages addressed to that |
| 31 // user from ever reaching Chrome. |
23 class GCMAccountTracker : public gaia::AccountTracker::Observer, | 32 class GCMAccountTracker : public gaia::AccountTracker::Observer, |
24 public OAuth2TokenService::Consumer, | 33 public OAuth2TokenService::Consumer, |
25 public GCMConnectionObserver { | 34 public GCMConnectionObserver { |
26 public: | 35 public: |
27 // State of the account. | 36 // State of the account. |
28 // Allowed transitions: | 37 // Allowed transitions: |
29 // TOKEN_NEEDED - account info was created. | 38 // TOKEN_NEEDED - account info was created. |
30 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. | 39 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. |
31 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. | 40 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. |
32 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. | 41 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 78 |
70 // Starts tracking accounts. | 79 // Starts tracking accounts. |
71 void Start(); | 80 void Start(); |
72 | 81 |
73 // Gets the number of pending token requests. Only used for testing. | 82 // Gets the number of pending token requests. Only used for testing. |
74 size_t get_pending_token_request_count() const { | 83 size_t get_pending_token_request_count() const { |
75 return pending_token_requests_.size(); | 84 return pending_token_requests_.size(); |
76 } | 85 } |
77 | 86 |
78 private: | 87 private: |
| 88 friend class GCMAccountTrackerTest; |
| 89 |
79 // Maps account keys to account states. Keyed by account_ids as used by | 90 // Maps account keys to account states. Keyed by account_ids as used by |
80 // OAuth2TokenService. | 91 // OAuth2TokenService. |
81 typedef std::map<std::string, AccountInfo> AccountInfos; | 92 typedef std::map<std::string, AccountInfo> AccountInfos; |
82 | 93 |
83 // AccountTracker::Observer overrides. | 94 // AccountTracker::Observer overrides. |
84 void OnAccountAdded(const gaia::AccountIds& ids) override; | 95 void OnAccountAdded(const gaia::AccountIds& ids) override; |
85 void OnAccountRemoved(const gaia::AccountIds& ids) override; | 96 void OnAccountRemoved(const gaia::AccountIds& ids) override; |
86 void OnAccountSignInChanged(const gaia::AccountIds& ids, | 97 void OnAccountSignInChanged(const gaia::AccountIds& ids, |
87 bool is_signed_in) override; | 98 bool is_signed_in) override; |
88 | 99 |
89 // OAuth2TokenService::Consumer overrides. | 100 // OAuth2TokenService::Consumer overrides. |
90 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 101 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
91 const std::string& access_token, | 102 const std::string& access_token, |
92 const base::Time& expiration_time) override; | 103 const base::Time& expiration_time) override; |
93 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 104 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
94 const GoogleServiceAuthError& error) override; | 105 const GoogleServiceAuthError& error) override; |
95 | 106 |
96 // GCMConnectionObserver overrides. | 107 // GCMConnectionObserver overrides. |
97 void OnConnected(const net::IPEndPoint& ip_endpoint) override; | 108 void OnConnected(const net::IPEndPoint& ip_endpoint) override; |
98 void OnDisconnected() override; | 109 void OnDisconnected() override; |
99 | 110 |
| 111 // Schedules token reporting. |
| 112 void ScheduleReportTokens(); |
100 // Report the list of accounts with OAuth2 tokens back using the |callback_| | 113 // Report the list of accounts with OAuth2 tokens back using the |callback_| |
101 // function. If there are token requests in progress, do nothing. | 114 // function. If there are token requests in progress, do nothing. |
102 void CompleteCollectingTokens(); | 115 void ReportTokens(); |
103 // Verify that all of the tokens are ready to be passed down to the GCM | 116 // 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 | 117 // 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. | 118 // all tokens are valid and a fetching yet more tokens is required. |
106 bool SanitizeTokens(); | 119 void SanitizeTokens(); |
| 120 // Indicates whether token reporting is required, either because it is due, or |
| 121 // some of the accounts were removed. |
| 122 bool IsTokenReportingRequired() const; |
| 123 // Indicates whether there are tokens that still need fetching. |
| 124 bool IsTokenFetchingRequired() const; |
| 125 // Gets the time until next token reporting. |
| 126 base::TimeDelta GetTimeToNextTokenReporting() const; |
107 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or | 127 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or |
108 // OnGetTokenFailure(..). | 128 // OnGetTokenFailure(..). |
109 void DeleteTokenRequest(const OAuth2TokenService::Request* request); | 129 void DeleteTokenRequest(const OAuth2TokenService::Request* request); |
110 // Checks on all known accounts, and calls GetToken(..) for those with | 130 // Checks on all known accounts, and calls GetToken(..) for those with |
111 // |state == TOKEN_NEEDED|. | 131 // |state == TOKEN_NEEDED|. |
112 void GetAllNeededTokens(); | 132 void GetAllNeededTokens(); |
113 // Starts fetching the OAuth2 token for the GCM group scope. | 133 // Starts fetching the OAuth2 token for the GCM group scope. |
114 void GetToken(AccountInfos::iterator& account_iter); | 134 void GetToken(AccountInfos::iterator& account_iter); |
115 | 135 |
116 // Handling of actual sign in and sign out for accounts. | 136 // Handling of actual sign in and sign out for accounts. |
117 void OnAccountSignedIn(const gaia::AccountIds& ids); | 137 void OnAccountSignedIn(const gaia::AccountIds& ids); |
118 void OnAccountSignedOut(const gaia::AccountIds& ids); | 138 void OnAccountSignedOut(const gaia::AccountIds& ids); |
119 | 139 |
120 OAuth2TokenService* GetTokenService(); | 140 OAuth2TokenService* GetTokenService(); |
121 | 141 |
122 // Account tracker. | 142 // Account tracker. |
123 scoped_ptr<gaia::AccountTracker> account_tracker_; | 143 scoped_ptr<gaia::AccountTracker> account_tracker_; |
124 | 144 |
125 // GCM Driver. Not owned. | 145 // GCM Driver. Not owned. |
126 GCMDriver* driver_; | 146 GCMDriver* driver_; |
127 | 147 |
128 // State of the account. | 148 // State of the account. |
129 AccountInfos account_infos_; | 149 AccountInfos account_infos_; |
130 | 150 |
131 // Indicates whether shutdown has been called. | 151 // Indicates whether shutdown has been called. |
132 bool shutdown_called_; | 152 bool shutdown_called_; |
133 | 153 |
134 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; | 154 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; |
135 | 155 |
| 156 // Creates weak pointers used to postpone reporting tokens. See |
| 157 // ScheduleReportTokens. |
| 158 base::WeakPtrFactory<GCMAccountTracker> reporting_weak_ptr_factory_; |
| 159 |
136 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); | 160 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); |
137 }; | 161 }; |
138 | 162 |
139 } // namespace gcm | 163 } // namespace gcm |
140 | 164 |
141 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ | 165 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ |
OLD | NEW |