| 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" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
| 14 #include "chrome/browser/extensions/state_store.h" | 14 #include "chrome/browser/extensions/state_store.h" |
| 15 #include "chrome/browser/services/gcm/gcm_client_factory.h" |
| 15 #include "chrome/browser/services/gcm/gcm_event_router.h" | 16 #include "chrome/browser/services/gcm/gcm_event_router.h" |
| 16 #include "chrome/browser/signin/signin_manager.h" | 17 #include "chrome/browser/signin/signin_manager.h" |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
| 18 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "components/user_prefs/pref_registry_syncable.h" | 21 #include "components/user_prefs/pref_registry_syncable.h" |
| 21 #include "components/webdata/encryptor/encryptor.h" | 22 #include "components/webdata/encryptor/encryptor.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
| 24 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 173 |
| 173 for (size_t i = 0; i < task_queue->tasks.size(); ++i) | 174 for (size_t i = 0; i < task_queue->tasks.size(); ++i) |
| 174 task_queue->tasks[i].Run(); | 175 task_queue->tasks[i].Run(); |
| 175 task_queue->tasks.clear(); | 176 task_queue->tasks.clear(); |
| 176 } | 177 } |
| 177 | 178 |
| 178 class GCMProfileService::IOWorker | 179 class GCMProfileService::IOWorker |
| 179 : public GCMClient::Delegate, | 180 : public GCMClient::Delegate, |
| 180 public base::RefCountedThreadSafe<GCMProfileService::IOWorker>{ | 181 public base::RefCountedThreadSafe<GCMProfileService::IOWorker>{ |
| 181 public: | 182 public: |
| 183 // Called on UI thread. |
| 182 explicit IOWorker(const base::WeakPtr<GCMProfileService>& service); | 184 explicit IOWorker(const base::WeakPtr<GCMProfileService>& service); |
| 183 | 185 |
| 184 // Overridden from GCMClient::Delegate: | 186 // Overridden from GCMClient::Delegate: |
| 185 // Called from IO thread. | 187 // Called on IO thread. |
| 186 virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info, | 188 virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info, |
| 187 GCMClient::Result result) OVERRIDE; | 189 GCMClient::Result result) OVERRIDE; |
| 188 virtual void OnRegisterFinished(const std::string& app_id, | 190 virtual void OnRegisterFinished(const std::string& app_id, |
| 189 const std::string& registration_id, | 191 const std::string& registration_id, |
| 190 GCMClient::Result result) OVERRIDE; | 192 GCMClient::Result result) OVERRIDE; |
| 191 virtual void OnSendFinished(const std::string& app_id, | 193 virtual void OnSendFinished(const std::string& app_id, |
| 192 const std::string& message_id, | 194 const std::string& message_id, |
| 193 GCMClient::Result result) OVERRIDE; | 195 GCMClient::Result result) OVERRIDE; |
| 194 virtual void OnMessageReceived( | 196 virtual void OnMessageReceived( |
| 195 const std::string& app_id, | 197 const std::string& app_id, |
| 196 const GCMClient::IncomingMessage& message) OVERRIDE; | 198 const GCMClient::IncomingMessage& message) OVERRIDE; |
| 197 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 199 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 198 virtual void OnMessageSendError(const std::string& app_id, | 200 virtual void OnMessageSendError(const std::string& app_id, |
| 199 const std::string& message_id, | 201 const std::string& message_id, |
| 200 GCMClient::Result result) OVERRIDE; | 202 GCMClient::Result result) OVERRIDE; |
| 201 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; | 203 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; |
| 202 virtual void OnLoadingCompleted() OVERRIDE; | 204 virtual void OnLoadingCompleted() OVERRIDE; |
| 203 virtual base::TaskRunner* GetFileTaskRunner() OVERRIDE; | |
| 204 | 205 |
| 205 void CheckGCMClientLoading(); | 206 // Called on IO thread. |
| 207 void Initialize(); |
| 206 void SetUser(const std::string& username); | 208 void SetUser(const std::string& username); |
| 207 void RemoveUser(const std::string& username); | 209 void RemoveUser(const std::string& username); |
| 208 void CheckIn(); | 210 void CheckIn(); |
| 209 // TODO(fgorski): Update to pass by const ref. | 211 // TODO(fgorski): Update to pass by const ref. |
| 210 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); | 212 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); |
| 211 void CheckOut(); | 213 void CheckOut(); |
| 212 void Register(const std::string& app_id, | 214 void Register(const std::string& app_id, |
| 213 const std::vector<std::string>& sender_ids, | 215 const std::vector<std::string>& sender_ids, |
| 214 const std::string& cert); | 216 const std::string& cert); |
| 215 void Unregister(const std::string& app_id); | 217 void Unregister(const std::string& app_id); |
| 216 void Send(const std::string& app_id, | 218 void Send(const std::string& app_id, |
| 217 const std::string& receiver_id, | 219 const std::string& receiver_id, |
| 218 const GCMClient::OutgoingMessage& message); | 220 const GCMClient::OutgoingMessage& message); |
| 219 | 221 |
| 220 private: | 222 private: |
| 221 friend class base::RefCountedThreadSafe<IOWorker>; | 223 friend class base::RefCountedThreadSafe<IOWorker>; |
| 222 virtual ~IOWorker(); | 224 virtual ~IOWorker(); |
| 223 | 225 |
| 224 const base::WeakPtr<GCMProfileService> service_; | 226 const base::WeakPtr<GCMProfileService> service_; |
| 225 | 227 |
| 228 // Not owned. |
| 229 GCMClient* gcm_client_; |
| 230 |
| 226 // The username (email address) of the signed-in user. | 231 // The username (email address) of the signed-in user. |
| 227 std::string username_; | 232 std::string username_; |
| 228 | 233 |
| 229 // The checkin info obtained from the server for the signed in user associated | 234 // The checkin info obtained from the server for the signed in user associated |
| 230 // with the profile. | 235 // with the profile. |
| 231 GCMClient::CheckinInfo checkin_info_; | 236 GCMClient::CheckinInfo checkin_info_; |
| 232 }; | 237 }; |
| 233 | 238 |
| 234 GCMProfileService::IOWorker::IOWorker( | 239 GCMProfileService::IOWorker::IOWorker( |
| 235 const base::WeakPtr<GCMProfileService>& service) | 240 const base::WeakPtr<GCMProfileService>& service) |
| 236 : service_(service) { | 241 : service_(service), |
| 242 gcm_client_(NULL) { |
| 243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 237 } | 244 } |
| 238 | 245 |
| 239 GCMProfileService::IOWorker::~IOWorker() { | 246 GCMProfileService::IOWorker::~IOWorker() { |
| 240 } | 247 } |
| 241 | 248 |
| 249 void GCMProfileService::IOWorker::Initialize() { |
| 250 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 251 |
| 252 gcm_client_ = GCMClientFactory::GetClient(); |
| 253 |
| 254 content::BrowserThread::PostTask( |
| 255 content::BrowserThread::UI, |
| 256 FROM_HERE, |
| 257 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, |
| 258 service_, |
| 259 gcm_client_->IsLoading())); |
| 260 } |
| 261 |
| 242 void GCMProfileService::IOWorker::OnCheckInFinished( | 262 void GCMProfileService::IOWorker::OnCheckInFinished( |
| 243 const GCMClient::CheckinInfo& checkin_info, | 263 const GCMClient::CheckinInfo& checkin_info, |
| 244 GCMClient::Result result) { | 264 GCMClient::Result result) { |
| 245 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 265 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 246 | 266 |
| 247 checkin_info_ = checkin_info; | 267 checkin_info_ = checkin_info; |
| 248 | 268 |
| 249 content::BrowserThread::PostTask( | 269 content::BrowserThread::PostTask( |
| 250 content::BrowserThread::UI, | 270 content::BrowserThread::UI, |
| 251 FROM_HERE, | 271 FROM_HERE, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 } | 353 } |
| 334 | 354 |
| 335 void GCMProfileService::IOWorker::OnLoadingCompleted() { | 355 void GCMProfileService::IOWorker::OnLoadingCompleted() { |
| 336 content::BrowserThread::PostTask( | 356 content::BrowserThread::PostTask( |
| 337 content::BrowserThread::UI, | 357 content::BrowserThread::UI, |
| 338 FROM_HERE, | 358 FROM_HERE, |
| 339 base::Bind(&GCMProfileService::GCMClientLoadingFinished, | 359 base::Bind(&GCMProfileService::GCMClientLoadingFinished, |
| 340 service_)); | 360 service_)); |
| 341 } | 361 } |
| 342 | 362 |
| 343 base::TaskRunner* GCMProfileService::IOWorker::GetFileTaskRunner() { | |
| 344 // TODO(jianli): to be implemented. | |
| 345 return NULL; | |
| 346 } | |
| 347 | |
| 348 void GCMProfileService::IOWorker::CheckGCMClientLoading() { | |
| 349 content::BrowserThread::PostTask( | |
| 350 content::BrowserThread::UI, | |
| 351 FROM_HERE, | |
| 352 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, | |
| 353 service_, | |
| 354 GCMClient::Get()->IsLoading())); | |
| 355 } | |
| 356 | |
| 357 void GCMProfileService::IOWorker::SetUser(const std::string& username) { | 363 void GCMProfileService::IOWorker::SetUser(const std::string& username) { |
| 358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 364 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 359 DCHECK(username_.empty() && !username.empty()); | 365 DCHECK(username_.empty() && !username.empty()); |
| 360 | 366 |
| 361 username_ = username; | 367 username_ = username; |
| 362 GCMClient::Get()->SetUserDelegate(username_, this); | 368 gcm_client_->SetUserDelegate(username_, this); |
| 363 } | 369 } |
| 364 | 370 |
| 365 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { | 371 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { |
| 366 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 367 DCHECK(!username.empty()); | 373 DCHECK(!username.empty()); |
| 368 | 374 |
| 369 if (username_.empty()) | 375 if (username_.empty()) |
| 370 return; | 376 return; |
| 371 username_.clear(); | 377 username_.clear(); |
| 372 GCMClient::Get()->SetUserDelegate(username_, NULL); | 378 gcm_client_->SetUserDelegate(username_, NULL); |
| 373 } | 379 } |
| 374 | 380 |
| 375 void GCMProfileService::IOWorker::CheckIn() { | 381 void GCMProfileService::IOWorker::CheckIn() { |
| 376 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 377 | 383 |
| 378 GCMClient::Get()->CheckIn(username_); | 384 gcm_client_->CheckIn(username_); |
| 379 } | 385 } |
| 380 | 386 |
| 381 void GCMProfileService::IOWorker::SetCheckinInfo( | 387 void GCMProfileService::IOWorker::SetCheckinInfo( |
| 382 GCMClient::CheckinInfo checkin_info) { | 388 GCMClient::CheckinInfo checkin_info) { |
| 383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 389 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 384 | 390 |
| 385 checkin_info_ = checkin_info; | 391 checkin_info_ = checkin_info; |
| 386 } | 392 } |
| 387 | 393 |
| 388 void GCMProfileService::IOWorker::CheckOut() { | 394 void GCMProfileService::IOWorker::CheckOut() { |
| 389 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 395 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 390 | 396 |
| 391 username_.clear(); | 397 username_.clear(); |
| 392 checkin_info_.Reset(); | 398 checkin_info_.Reset(); |
| 393 } | 399 } |
| 394 | 400 |
| 395 void GCMProfileService::IOWorker::Register( | 401 void GCMProfileService::IOWorker::Register( |
| 396 const std::string& app_id, | 402 const std::string& app_id, |
| 397 const std::vector<std::string>& sender_ids, | 403 const std::vector<std::string>& sender_ids, |
| 398 const std::string& cert) { | 404 const std::string& cert) { |
| 399 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 405 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 400 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 406 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 401 | 407 |
| 402 GCMClient::Get()->Register(username_, app_id, cert, sender_ids); | 408 gcm_client_->Register(username_, app_id, cert, sender_ids); |
| 403 } | 409 } |
| 404 | 410 |
| 405 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { | 411 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { |
| 406 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 412 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 407 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 413 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 408 | 414 |
| 409 GCMClient::Get()->Unregister(username_, app_id); | 415 gcm_client_->Unregister(username_, app_id); |
| 410 } | 416 } |
| 411 | 417 |
| 412 void GCMProfileService::IOWorker::Send( | 418 void GCMProfileService::IOWorker::Send( |
| 413 const std::string& app_id, | 419 const std::string& app_id, |
| 414 const std::string& receiver_id, | 420 const std::string& receiver_id, |
| 415 const GCMClient::OutgoingMessage& message) { | 421 const GCMClient::OutgoingMessage& message) { |
| 416 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 422 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 417 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 423 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 418 | 424 |
| 419 GCMClient::Get()->Send(username_, app_id, receiver_id, message); | 425 gcm_client_->Send(username_, app_id, receiver_id, message); |
| 420 } | 426 } |
| 421 | 427 |
| 422 GCMProfileService::RegistrationInfo::RegistrationInfo() { | 428 GCMProfileService::RegistrationInfo::RegistrationInfo() { |
| 423 } | 429 } |
| 424 | 430 |
| 425 GCMProfileService::RegistrationInfo::~RegistrationInfo() { | 431 GCMProfileService::RegistrationInfo::~RegistrationInfo() { |
| 426 } | 432 } |
| 427 | 433 |
| 428 bool GCMProfileService::RegistrationInfo::IsValid() const { | 434 bool GCMProfileService::RegistrationInfo::IsValid() const { |
| 429 return !sender_ids.empty() && !registration_id.empty(); | 435 return !sender_ids.empty() && !registration_id.empty(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 458 prefs::kGCMUserToken, | 464 prefs::kGCMUserToken, |
| 459 "", | 465 "", |
| 460 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 466 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 461 } | 467 } |
| 462 | 468 |
| 463 GCMProfileService::GCMProfileService(Profile* profile) | 469 GCMProfileService::GCMProfileService(Profile* profile) |
| 464 : profile_(profile), | 470 : profile_(profile), |
| 465 testing_delegate_(NULL), | 471 testing_delegate_(NULL), |
| 466 weak_ptr_factory_(this) { | 472 weak_ptr_factory_(this) { |
| 467 DCHECK(!profile->IsOffTheRecord()); | 473 DCHECK(!profile->IsOffTheRecord()); |
| 474 |
| 468 Init(); | 475 Init(); |
| 469 } | 476 } |
| 470 | 477 |
| 471 GCMProfileService::GCMProfileService(Profile* profile, | 478 GCMProfileService::GCMProfileService(Profile* profile, |
| 472 TestingDelegate* testing_delegate) | 479 TestingDelegate* testing_delegate) |
| 473 : profile_(profile), | 480 : profile_(profile), |
| 474 testing_delegate_(testing_delegate), | 481 testing_delegate_(testing_delegate), |
| 475 weak_ptr_factory_(this) { | 482 weak_ptr_factory_(this) { |
| 476 Init(); | 483 Init(); |
| 477 } | 484 } |
| 478 | 485 |
| 479 GCMProfileService::~GCMProfileService() { | 486 GCMProfileService::~GCMProfileService() { |
| 480 if (username_.empty()) | 487 if (username_.empty()) |
| 481 return; | 488 return; |
| 482 content::BrowserThread::PostTask( | 489 content::BrowserThread::PostTask( |
| 483 content::BrowserThread::IO, | 490 content::BrowserThread::IO, |
| 484 FROM_HERE, | 491 FROM_HERE, |
| 485 base::Bind(&GCMProfileService::IOWorker::RemoveUser, | 492 base::Bind(&GCMProfileService::IOWorker::RemoveUser, |
| 486 io_worker_, | 493 io_worker_, |
| 487 username_)); | 494 username_)); |
| 488 } | 495 } |
| 489 | 496 |
| 490 void GCMProfileService::Init() { | 497 void GCMProfileService::Init() { |
| 491 delayed_task_controller_.reset(new DelayedTaskController); | 498 delayed_task_controller_.reset(new DelayedTaskController); |
| 492 | 499 |
| 493 // This has to be done first since CheckIn depends on it. | 500 // This has to be done first since CheckIn depends on it. |
| 494 io_worker_ = new IOWorker(weak_ptr_factory_.GetWeakPtr()); | 501 io_worker_ = new IOWorker(weak_ptr_factory_.GetWeakPtr()); |
| 495 | 502 |
| 496 // Checks if GCMClient has finished the loading. | 503 // This initializes GCMClient and also does the check to find out if GCMClient |
| 504 // has finished the loading. |
| 497 content::BrowserThread::PostTask( | 505 content::BrowserThread::PostTask( |
| 498 content::BrowserThread::IO, | 506 content::BrowserThread::IO, |
| 499 FROM_HERE, | 507 FROM_HERE, |
| 500 base::Bind(&GCMProfileService::IOWorker::CheckGCMClientLoading, | 508 base::Bind(&GCMProfileService::IOWorker::Initialize, io_worker_)); |
| 501 io_worker_)); | |
| 502 | 509 |
| 503 // In case that the profile has been signed in before GCMProfileService is | 510 // In case that the profile has been signed in before GCMProfileService is |
| 504 // created. | 511 // created. |
| 505 SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile_); | 512 SigninManagerBase* manager = SigninManagerFactory::GetForProfile(profile_); |
| 506 if (manager) | 513 if (manager) |
| 507 AddUser(manager->GetAuthenticatedUsername()); | 514 AddUser(manager->GetAuthenticatedUsername()); |
| 508 | 515 |
| 509 registrar_.Add(this, | 516 registrar_.Add(this, |
| 510 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 517 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 511 content::Source<Profile>(profile_)); | 518 content::Source<Profile>(profile_)); |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 | 976 |
| 970 return true; | 977 return true; |
| 971 } | 978 } |
| 972 | 979 |
| 973 // static | 980 // static |
| 974 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 981 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
| 975 return kRegistrationKey; | 982 return kRegistrationKey; |
| 976 } | 983 } |
| 977 | 984 |
| 978 } // namespace gcm | 985 } // namespace gcm |
| OLD | NEW |