OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 159 } |
160 | 160 |
161 class GCMProfileService::IOWorker | 161 class GCMProfileService::IOWorker |
162 : public GCMClient::Delegate, | 162 : public GCMClient::Delegate, |
163 public base::RefCountedThreadSafe<GCMProfileService::IOWorker>{ | 163 public base::RefCountedThreadSafe<GCMProfileService::IOWorker>{ |
164 public: | 164 public: |
165 explicit IOWorker(const base::WeakPtr<GCMProfileService>& service); | 165 explicit IOWorker(const base::WeakPtr<GCMProfileService>& service); |
166 | 166 |
167 // Overridden from GCMClient::Delegate: | 167 // Overridden from GCMClient::Delegate: |
168 // Called from IO thread. | 168 // Called from IO thread. |
169 virtual void OnCheckInFinished(const GCMClient::CheckInInfo& checkin_info, | 169 virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info, |
170 GCMClient::Result result) OVERRIDE; | 170 GCMClient::Result result) OVERRIDE; |
171 virtual void OnRegisterFinished(const std::string& app_id, | 171 virtual void OnRegisterFinished(const std::string& app_id, |
172 const std::string& registration_id, | 172 const std::string& registration_id, |
173 GCMClient::Result result) OVERRIDE; | 173 GCMClient::Result result) OVERRIDE; |
174 virtual void OnSendFinished(const std::string& app_id, | 174 virtual void OnSendFinished(const std::string& app_id, |
175 const std::string& message_id, | 175 const std::string& message_id, |
176 GCMClient::Result result) OVERRIDE; | 176 GCMClient::Result result) OVERRIDE; |
177 virtual void OnMessageReceived( | 177 virtual void OnMessageReceived( |
178 const std::string& app_id, | 178 const std::string& app_id, |
179 const GCMClient::IncomingMessage& message) OVERRIDE; | 179 const GCMClient::IncomingMessage& message) OVERRIDE; |
180 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 180 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
181 virtual void OnMessageSendError(const std::string& app_id, | 181 virtual void OnMessageSendError(const std::string& app_id, |
182 const std::string& message_id, | 182 const std::string& message_id, |
183 GCMClient::Result result) OVERRIDE; | 183 GCMClient::Result result) OVERRIDE; |
184 virtual GCMClient::CheckInInfo GetCheckInInfo() const OVERRIDE; | 184 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; |
185 virtual void OnLoadingCompleted() OVERRIDE; | 185 virtual void OnLoadingCompleted() OVERRIDE; |
186 virtual base::TaskRunner* GetFileTaskRunner() OVERRIDE; | 186 virtual base::TaskRunner* GetFileTaskRunner() OVERRIDE; |
187 | 187 |
188 void CheckGCMClientLoading(); | 188 void CheckGCMClientLoading(); |
189 void SetUser(const std::string& username); | 189 void SetUser(const std::string& username); |
190 void RemoveUser(const std::string& username); | 190 void RemoveUser(const std::string& username); |
191 void CheckIn(); | 191 void CheckIn(); |
192 void SetCheckInInfo(GCMClient::CheckInInfo checkin_info); | 192 // TODO(fgorski): Update to pass by const ref. |
| 193 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); |
193 void CheckOut(); | 194 void CheckOut(); |
194 void Register(const std::string& app_id, | 195 void Register(const std::string& app_id, |
195 const std::vector<std::string>& sender_ids, | 196 const std::vector<std::string>& sender_ids, |
196 const std::string& cert); | 197 const std::string& cert); |
197 void Unregister(const std::string& app_id); | 198 void Unregister(const std::string& app_id); |
198 void Send(const std::string& app_id, | 199 void Send(const std::string& app_id, |
199 const std::string& receiver_id, | 200 const std::string& receiver_id, |
200 const GCMClient::OutgoingMessage& message); | 201 const GCMClient::OutgoingMessage& message); |
201 | 202 |
202 private: | 203 private: |
203 friend class base::RefCountedThreadSafe<IOWorker>; | 204 friend class base::RefCountedThreadSafe<IOWorker>; |
204 virtual ~IOWorker(); | 205 virtual ~IOWorker(); |
205 | 206 |
206 const base::WeakPtr<GCMProfileService> service_; | 207 const base::WeakPtr<GCMProfileService> service_; |
207 | 208 |
208 // The username (email address) of the signed-in user. | 209 // The username (email address) of the signed-in user. |
209 std::string username_; | 210 std::string username_; |
210 | 211 |
211 // The checkin info obtained from the server for the signed in user associated | 212 // The checkin info obtained from the server for the signed in user associated |
212 // with the profile. | 213 // with the profile. |
213 GCMClient::CheckInInfo checkin_info_; | 214 GCMClient::CheckinInfo checkin_info_; |
214 }; | 215 }; |
215 | 216 |
216 GCMProfileService::IOWorker::IOWorker( | 217 GCMProfileService::IOWorker::IOWorker( |
217 const base::WeakPtr<GCMProfileService>& service) | 218 const base::WeakPtr<GCMProfileService>& service) |
218 : service_(service) { | 219 : service_(service) { |
219 } | 220 } |
220 | 221 |
221 GCMProfileService::IOWorker::~IOWorker() { | 222 GCMProfileService::IOWorker::~IOWorker() { |
222 } | 223 } |
223 | 224 |
224 void GCMProfileService::IOWorker::OnCheckInFinished( | 225 void GCMProfileService::IOWorker::OnCheckInFinished( |
225 const GCMClient::CheckInInfo& checkin_info, | 226 const GCMClient::CheckinInfo& checkin_info, |
226 GCMClient::Result result) { | 227 GCMClient::Result result) { |
227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 228 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
228 | 229 |
229 checkin_info_ = checkin_info; | 230 checkin_info_ = checkin_info; |
230 | 231 |
231 content::BrowserThread::PostTask( | 232 content::BrowserThread::PostTask( |
232 content::BrowserThread::UI, | 233 content::BrowserThread::UI, |
233 FROM_HERE, | 234 FROM_HERE, |
234 base::Bind(&GCMProfileService::CheckInFinished, | 235 base::Bind(&GCMProfileService::CheckInFinished, |
235 service_, | 236 service_, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 content::BrowserThread::PostTask( | 304 content::BrowserThread::PostTask( |
304 content::BrowserThread::UI, | 305 content::BrowserThread::UI, |
305 FROM_HERE, | 306 FROM_HERE, |
306 base::Bind(&GCMProfileService::MessageSendError, | 307 base::Bind(&GCMProfileService::MessageSendError, |
307 service_, | 308 service_, |
308 app_id, | 309 app_id, |
309 message_id, | 310 message_id, |
310 result)); | 311 result)); |
311 } | 312 } |
312 | 313 |
313 GCMClient::CheckInInfo GCMProfileService::IOWorker::GetCheckInInfo() const { | 314 GCMClient::CheckinInfo GCMProfileService::IOWorker::GetCheckinInfo() const { |
314 return checkin_info_; | 315 return checkin_info_; |
315 } | 316 } |
316 | 317 |
317 void GCMProfileService::IOWorker::OnLoadingCompleted() { | 318 void GCMProfileService::IOWorker::OnLoadingCompleted() { |
318 content::BrowserThread::PostTask( | 319 content::BrowserThread::PostTask( |
319 content::BrowserThread::UI, | 320 content::BrowserThread::UI, |
320 FROM_HERE, | 321 FROM_HERE, |
321 base::Bind(&GCMProfileService::GCMClientLoadingFinished, | 322 base::Bind(&GCMProfileService::GCMClientLoadingFinished, |
322 service_)); | 323 service_)); |
323 } | 324 } |
(...skipping 29 matching lines...) Expand all Loading... |
353 username_.clear(); | 354 username_.clear(); |
354 GCMClient::Get()->SetUserDelegate(username_, NULL); | 355 GCMClient::Get()->SetUserDelegate(username_, NULL); |
355 } | 356 } |
356 | 357 |
357 void GCMProfileService::IOWorker::CheckIn() { | 358 void GCMProfileService::IOWorker::CheckIn() { |
358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 359 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
359 | 360 |
360 GCMClient::Get()->CheckIn(username_); | 361 GCMClient::Get()->CheckIn(username_); |
361 } | 362 } |
362 | 363 |
363 void GCMProfileService::IOWorker::SetCheckInInfo( | 364 void GCMProfileService::IOWorker::SetCheckinInfo( |
364 GCMClient::CheckInInfo checkin_info) { | 365 GCMClient::CheckinInfo checkin_info) { |
365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 366 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
366 | 367 |
367 checkin_info_ = checkin_info; | 368 checkin_info_ = checkin_info; |
368 } | 369 } |
369 | 370 |
370 void GCMProfileService::IOWorker::CheckOut() { | 371 void GCMProfileService::IOWorker::CheckOut() { |
371 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
372 | 373 |
373 username_.clear(); | 374 username_.clear(); |
374 checkin_info_.Reset(); | 375 checkin_info_.Reset(); |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 PrefService* pref_service = profile_->GetPrefs(); | 664 PrefService* pref_service = profile_->GetPrefs(); |
664 uint64 android_id = pref_service->GetUint64(prefs::kGCMUserAccountID); | 665 uint64 android_id = pref_service->GetUint64(prefs::kGCMUserAccountID); |
665 std::string base64_token = pref_service->GetString(prefs::kGCMUserToken); | 666 std::string base64_token = pref_service->GetString(prefs::kGCMUserToken); |
666 std::string encrypted_secret; | 667 std::string encrypted_secret; |
667 base::Base64Decode(base::StringPiece(base64_token), &encrypted_secret); | 668 base::Base64Decode(base::StringPiece(base64_token), &encrypted_secret); |
668 if (android_id && !encrypted_secret.empty()) { | 669 if (android_id && !encrypted_secret.empty()) { |
669 std::string decrypted_secret; | 670 std::string decrypted_secret; |
670 Encryptor::DecryptString(encrypted_secret, &decrypted_secret); | 671 Encryptor::DecryptString(encrypted_secret, &decrypted_secret); |
671 uint64 secret = 0; | 672 uint64 secret = 0; |
672 if (base::StringToUint64(decrypted_secret, &secret) && secret) { | 673 if (base::StringToUint64(decrypted_secret, &secret) && secret) { |
673 GCMClient::CheckInInfo checkin_info; | 674 GCMClient::CheckinInfo checkin_info; |
674 checkin_info.android_id = android_id; | 675 checkin_info.android_id = android_id; |
675 checkin_info.secret = secret; | 676 checkin_info.secret = secret; |
676 content::BrowserThread::PostTask( | 677 content::BrowserThread::PostTask( |
677 content::BrowserThread::IO, | 678 content::BrowserThread::IO, |
678 FROM_HERE, | 679 FROM_HERE, |
679 base::Bind(&GCMProfileService::IOWorker::SetCheckInInfo, | 680 base::Bind(&GCMProfileService::IOWorker::SetCheckinInfo, |
680 io_worker_, | 681 io_worker_, |
681 checkin_info)); | 682 checkin_info)); |
682 | 683 |
683 if (testing_delegate_) | 684 if (testing_delegate_) |
684 testing_delegate_->CheckInFinished(checkin_info, GCMClient::SUCCESS); | 685 testing_delegate_->CheckInFinished(checkin_info, GCMClient::SUCCESS); |
685 | 686 |
686 return; | 687 return; |
687 } | 688 } |
688 } | 689 } |
689 | 690 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 // unregister request fails. If this occurs, it does not bring any harm since | 729 // unregister request fails. If this occurs, it does not bring any harm since |
729 // we simply reject the messages/events received from the server. | 730 // we simply reject the messages/events received from the server. |
730 content::BrowserThread::PostTask( | 731 content::BrowserThread::PostTask( |
731 content::BrowserThread::IO, | 732 content::BrowserThread::IO, |
732 FROM_HERE, | 733 FROM_HERE, |
733 base::Bind(&GCMProfileService::IOWorker::Unregister, | 734 base::Bind(&GCMProfileService::IOWorker::Unregister, |
734 io_worker_, | 735 io_worker_, |
735 app_id)); | 736 app_id)); |
736 } | 737 } |
737 | 738 |
738 void GCMProfileService::CheckInFinished(GCMClient::CheckInInfo checkin_info, | 739 void GCMProfileService::CheckInFinished(GCMClient::CheckinInfo checkin_info, |
739 GCMClient::Result result) { | 740 GCMClient::Result result) { |
740 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 741 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
741 | 742 |
742 // Save the check-in info into the profile's prefs store. | 743 // Save the check-in info into the profile's prefs store. |
743 PrefService* pref_service = profile_->GetPrefs(); | 744 PrefService* pref_service = profile_->GetPrefs(); |
744 pref_service->SetUint64(prefs::kGCMUserAccountID, checkin_info.android_id); | 745 pref_service->SetUint64(prefs::kGCMUserAccountID, checkin_info.android_id); |
745 | 746 |
746 // Encrypt the secret for persisting purpose. | 747 // Encrypt the secret for persisting purpose. |
747 std::string encrypted_secret; | 748 std::string encrypted_secret; |
748 Encryptor::EncryptString(base::Uint64ToString(checkin_info.secret), | 749 Encryptor::EncryptString(base::Uint64ToString(checkin_info.secret), |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 | 940 |
940 return true; | 941 return true; |
941 } | 942 } |
942 | 943 |
943 // static | 944 // static |
944 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 945 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
945 return kRegistrationKey; | 946 return kRegistrationKey; |
946 } | 947 } |
947 | 948 |
948 } // namespace gcm | 949 } // namespace gcm |
OLD | NEW |