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 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); |
Nicolas Zea
2014/01/07 00:41:52
const ref
fgorski
2014/01/07 23:46:44
Added TODO.
| |
193 void CheckOut(); | 193 void CheckOut(); |
194 void Register(const std::string& app_id, | 194 void Register(const std::string& app_id, |
195 const std::vector<std::string>& sender_ids, | 195 const std::vector<std::string>& sender_ids, |
196 const std::string& cert); | 196 const std::string& cert); |
197 void Unregister(const std::string& app_id); | 197 void Unregister(const std::string& app_id); |
198 void Send(const std::string& app_id, | 198 void Send(const std::string& app_id, |
199 const std::string& receiver_id, | 199 const std::string& receiver_id, |
200 const GCMClient::OutgoingMessage& message); | 200 const GCMClient::OutgoingMessage& message); |
201 | 201 |
202 private: | 202 private: |
203 friend class base::RefCountedThreadSafe<IOWorker>; | 203 friend class base::RefCountedThreadSafe<IOWorker>; |
204 virtual ~IOWorker(); | 204 virtual ~IOWorker(); |
205 | 205 |
206 const base::WeakPtr<GCMProfileService> service_; | 206 const base::WeakPtr<GCMProfileService> service_; |
207 | 207 |
208 // The username (email address) of the signed-in user. | 208 // The username (email address) of the signed-in user. |
209 std::string username_; | 209 std::string username_; |
210 | 210 |
211 // The checkin info obtained from the server for the signed in user associated | 211 // The checkin info obtained from the server for the signed in user associated |
212 // with the profile. | 212 // with the profile. |
213 GCMClient::CheckInInfo checkin_info_; | 213 GCMClient::CheckinInfo checkin_info_; |
214 }; | 214 }; |
215 | 215 |
216 GCMProfileService::IOWorker::IOWorker( | 216 GCMProfileService::IOWorker::IOWorker( |
217 const base::WeakPtr<GCMProfileService>& service) | 217 const base::WeakPtr<GCMProfileService>& service) |
218 : service_(service) { | 218 : service_(service) { |
219 } | 219 } |
220 | 220 |
221 GCMProfileService::IOWorker::~IOWorker() { | 221 GCMProfileService::IOWorker::~IOWorker() { |
222 } | 222 } |
223 | 223 |
224 void GCMProfileService::IOWorker::OnCheckInFinished( | 224 void GCMProfileService::IOWorker::OnCheckInFinished( |
225 const GCMClient::CheckInInfo& checkin_info, | 225 const GCMClient::CheckinInfo& checkin_info, |
226 GCMClient::Result result) { | 226 GCMClient::Result result) { |
227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
228 | 228 |
229 checkin_info_ = checkin_info; | 229 checkin_info_ = checkin_info; |
230 | 230 |
231 content::BrowserThread::PostTask( | 231 content::BrowserThread::PostTask( |
232 content::BrowserThread::UI, | 232 content::BrowserThread::UI, |
233 FROM_HERE, | 233 FROM_HERE, |
234 base::Bind(&GCMProfileService::CheckInFinished, | 234 base::Bind(&GCMProfileService::CheckInFinished, |
235 service_, | 235 service_, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 content::BrowserThread::PostTask( | 303 content::BrowserThread::PostTask( |
304 content::BrowserThread::UI, | 304 content::BrowserThread::UI, |
305 FROM_HERE, | 305 FROM_HERE, |
306 base::Bind(&GCMProfileService::MessageSendError, | 306 base::Bind(&GCMProfileService::MessageSendError, |
307 service_, | 307 service_, |
308 app_id, | 308 app_id, |
309 message_id, | 309 message_id, |
310 result)); | 310 result)); |
311 } | 311 } |
312 | 312 |
313 GCMClient::CheckInInfo GCMProfileService::IOWorker::GetCheckInInfo() const { | 313 GCMClient::CheckinInfo GCMProfileService::IOWorker::GetCheckinInfo() const { |
314 return checkin_info_; | 314 return checkin_info_; |
315 } | 315 } |
316 | 316 |
317 void GCMProfileService::IOWorker::OnLoadingCompleted() { | 317 void GCMProfileService::IOWorker::OnLoadingCompleted() { |
318 content::BrowserThread::PostTask( | 318 content::BrowserThread::PostTask( |
319 content::BrowserThread::UI, | 319 content::BrowserThread::UI, |
320 FROM_HERE, | 320 FROM_HERE, |
321 base::Bind(&GCMProfileService::GCMClientLoadingFinished, | 321 base::Bind(&GCMProfileService::GCMClientLoadingFinished, |
322 service_)); | 322 service_)); |
323 } | 323 } |
(...skipping 29 matching lines...) Expand all Loading... | |
353 username_.clear(); | 353 username_.clear(); |
354 GCMClient::Get()->SetUserDelegate(username_, NULL); | 354 GCMClient::Get()->SetUserDelegate(username_, NULL); |
355 } | 355 } |
356 | 356 |
357 void GCMProfileService::IOWorker::CheckIn() { | 357 void GCMProfileService::IOWorker::CheckIn() { |
358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
359 | 359 |
360 GCMClient::Get()->CheckIn(username_); | 360 GCMClient::Get()->CheckIn(username_); |
361 } | 361 } |
362 | 362 |
363 void GCMProfileService::IOWorker::SetCheckInInfo( | 363 void GCMProfileService::IOWorker::SetCheckinInfo( |
364 GCMClient::CheckInInfo checkin_info) { | 364 GCMClient::CheckinInfo checkin_info) { |
365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
366 | 366 |
367 checkin_info_ = checkin_info; | 367 checkin_info_ = checkin_info; |
368 } | 368 } |
369 | 369 |
370 void GCMProfileService::IOWorker::CheckOut() { | 370 void GCMProfileService::IOWorker::CheckOut() { |
371 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 371 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
372 | 372 |
373 username_.clear(); | 373 username_.clear(); |
374 checkin_info_.Reset(); | 374 checkin_info_.Reset(); |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
663 PrefService* pref_service = profile_->GetPrefs(); | 663 PrefService* pref_service = profile_->GetPrefs(); |
664 uint64 android_id = pref_service->GetUint64(prefs::kGCMUserAccountID); | 664 uint64 android_id = pref_service->GetUint64(prefs::kGCMUserAccountID); |
665 std::string base64_token = pref_service->GetString(prefs::kGCMUserToken); | 665 std::string base64_token = pref_service->GetString(prefs::kGCMUserToken); |
666 std::string encrypted_secret; | 666 std::string encrypted_secret; |
667 base::Base64Decode(base::StringPiece(base64_token), &encrypted_secret); | 667 base::Base64Decode(base::StringPiece(base64_token), &encrypted_secret); |
668 if (android_id && !encrypted_secret.empty()) { | 668 if (android_id && !encrypted_secret.empty()) { |
669 std::string decrypted_secret; | 669 std::string decrypted_secret; |
670 Encryptor::DecryptString(encrypted_secret, &decrypted_secret); | 670 Encryptor::DecryptString(encrypted_secret, &decrypted_secret); |
671 uint64 secret = 0; | 671 uint64 secret = 0; |
672 if (base::StringToUint64(decrypted_secret, &secret) && secret) { | 672 if (base::StringToUint64(decrypted_secret, &secret) && secret) { |
673 GCMClient::CheckInInfo checkin_info; | 673 GCMClient::CheckinInfo checkin_info; |
674 checkin_info.android_id = android_id; | 674 checkin_info.android_id = android_id; |
675 checkin_info.secret = secret; | 675 checkin_info.secret = secret; |
676 content::BrowserThread::PostTask( | 676 content::BrowserThread::PostTask( |
677 content::BrowserThread::IO, | 677 content::BrowserThread::IO, |
678 FROM_HERE, | 678 FROM_HERE, |
679 base::Bind(&GCMProfileService::IOWorker::SetCheckInInfo, | 679 base::Bind(&GCMProfileService::IOWorker::SetCheckinInfo, |
680 io_worker_, | 680 io_worker_, |
681 checkin_info)); | 681 checkin_info)); |
682 | 682 |
683 if (testing_delegate_) | 683 if (testing_delegate_) |
684 testing_delegate_->CheckInFinished(checkin_info, GCMClient::SUCCESS); | 684 testing_delegate_->CheckInFinished(checkin_info, GCMClient::SUCCESS); |
685 | 685 |
686 return; | 686 return; |
687 } | 687 } |
688 } | 688 } |
689 | 689 |
(...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 | 728 // unregister request fails. If this occurs, it does not bring any harm since |
729 // we simply reject the messages/events received from the server. | 729 // we simply reject the messages/events received from the server. |
730 content::BrowserThread::PostTask( | 730 content::BrowserThread::PostTask( |
731 content::BrowserThread::IO, | 731 content::BrowserThread::IO, |
732 FROM_HERE, | 732 FROM_HERE, |
733 base::Bind(&GCMProfileService::IOWorker::Unregister, | 733 base::Bind(&GCMProfileService::IOWorker::Unregister, |
734 io_worker_, | 734 io_worker_, |
735 app_id)); | 735 app_id)); |
736 } | 736 } |
737 | 737 |
738 void GCMProfileService::CheckInFinished(GCMClient::CheckInInfo checkin_info, | 738 void GCMProfileService::CheckInFinished(GCMClient::CheckinInfo checkin_info, |
739 GCMClient::Result result) { | 739 GCMClient::Result result) { |
740 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 740 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
741 | 741 |
742 // Save the check-in info into the profile's prefs store. | 742 // Save the check-in info into the profile's prefs store. |
743 PrefService* pref_service = profile_->GetPrefs(); | 743 PrefService* pref_service = profile_->GetPrefs(); |
744 pref_service->SetUint64(prefs::kGCMUserAccountID, checkin_info.android_id); | 744 pref_service->SetUint64(prefs::kGCMUserAccountID, checkin_info.android_id); |
745 | 745 |
746 // Encrypt the secret for persisting purpose. | 746 // Encrypt the secret for persisting purpose. |
747 std::string encrypted_secret; | 747 std::string encrypted_secret; |
748 Encryptor::EncryptString(base::Uint64ToString(checkin_info.secret), | 748 Encryptor::EncryptString(base::Uint64ToString(checkin_info.secret), |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
939 | 939 |
940 return true; | 940 return true; |
941 } | 941 } |
942 | 942 |
943 // static | 943 // static |
944 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 944 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
945 return kRegistrationKey; | 945 return kRegistrationKey; |
946 } | 946 } |
947 | 947 |
948 } // namespace gcm | 948 } // namespace gcm |
OLD | NEW |