Chromium Code Reviews| 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); |
| 185 void Initialize(); | |
| 183 | 186 |
| 184 // Overridden from GCMClient::Delegate: | 187 // Overridden from GCMClient::Delegate: |
| 185 // Called from IO thread. | 188 // Called on IO thread. |
| 186 virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info, | 189 virtual void OnCheckInFinished(const GCMClient::CheckinInfo& checkin_info, |
| 187 GCMClient::Result result) OVERRIDE; | 190 GCMClient::Result result) OVERRIDE; |
| 188 virtual void OnRegisterFinished(const std::string& app_id, | 191 virtual void OnRegisterFinished(const std::string& app_id, |
| 189 const std::string& registration_id, | 192 const std::string& registration_id, |
| 190 GCMClient::Result result) OVERRIDE; | 193 GCMClient::Result result) OVERRIDE; |
| 191 virtual void OnSendFinished(const std::string& app_id, | 194 virtual void OnSendFinished(const std::string& app_id, |
| 192 const std::string& message_id, | 195 const std::string& message_id, |
| 193 GCMClient::Result result) OVERRIDE; | 196 GCMClient::Result result) OVERRIDE; |
| 194 virtual void OnMessageReceived( | 197 virtual void OnMessageReceived( |
| 195 const std::string& app_id, | 198 const std::string& app_id, |
| 196 const GCMClient::IncomingMessage& message) OVERRIDE; | 199 const GCMClient::IncomingMessage& message) OVERRIDE; |
| 197 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 200 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 198 virtual void OnMessageSendError(const std::string& app_id, | 201 virtual void OnMessageSendError(const std::string& app_id, |
| 199 const std::string& message_id, | 202 const std::string& message_id, |
| 200 GCMClient::Result result) OVERRIDE; | 203 GCMClient::Result result) OVERRIDE; |
| 201 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; | 204 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; |
| 202 virtual void OnLoadingCompleted() OVERRIDE; | 205 virtual void OnLoadingCompleted() OVERRIDE; |
| 203 virtual base::TaskRunner* GetFileTaskRunner() OVERRIDE; | |
| 204 | 206 |
| 205 void CheckGCMClientLoading(); | 207 void CheckGCMClientLoading(); |
| 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 |
| 226 void InitializeGCMClient(); | |
| 227 | |
| 224 const base::WeakPtr<GCMProfileService> service_; | 228 const base::WeakPtr<GCMProfileService> service_; |
| 225 | 229 |
| 230 // Not owned. | |
| 231 GCMClient* gcm_client_; | |
| 232 | |
| 226 // The username (email address) of the signed-in user. | 233 // The username (email address) of the signed-in user. |
| 227 std::string username_; | 234 std::string username_; |
| 228 | 235 |
| 229 // The checkin info obtained from the server for the signed in user associated | 236 // The checkin info obtained from the server for the signed in user associated |
| 230 // with the profile. | 237 // with the profile. |
| 231 GCMClient::CheckinInfo checkin_info_; | 238 GCMClient::CheckinInfo checkin_info_; |
| 232 }; | 239 }; |
| 233 | 240 |
| 234 GCMProfileService::IOWorker::IOWorker( | 241 GCMProfileService::IOWorker::IOWorker( |
| 235 const base::WeakPtr<GCMProfileService>& service) | 242 const base::WeakPtr<GCMProfileService>& service) |
| 236 : service_(service) { | 243 : service_(service), |
| 244 gcm_client_(NULL) { | |
| 245 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 237 } | 246 } |
| 238 | 247 |
| 239 GCMProfileService::IOWorker::~IOWorker() { | 248 GCMProfileService::IOWorker::~IOWorker() { |
| 240 } | 249 } |
| 241 | 250 |
| 251 void GCMProfileService::IOWorker::Initialize() { | |
| 252 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 253 | |
| 254 content::BrowserThread::PostTask( | |
| 255 content::BrowserThread::IO, | |
| 256 FROM_HERE, | |
| 257 base::Bind(&GCMProfileService::IOWorker::InitializeGCMClient, this)); | |
| 258 } | |
| 259 | |
| 260 void GCMProfileService::IOWorker::InitializeGCMClient() { | |
| 261 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 262 | |
| 263 gcm_client_ = GCMClientFactory::GetClient(); | |
| 264 } | |
| 265 | |
| 242 void GCMProfileService::IOWorker::OnCheckInFinished( | 266 void GCMProfileService::IOWorker::OnCheckInFinished( |
| 243 const GCMClient::CheckinInfo& checkin_info, | 267 const GCMClient::CheckinInfo& checkin_info, |
| 244 GCMClient::Result result) { | 268 GCMClient::Result result) { |
| 245 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 269 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 246 | 270 |
| 247 checkin_info_ = checkin_info; | 271 checkin_info_ = checkin_info; |
| 248 | 272 |
| 249 content::BrowserThread::PostTask( | 273 content::BrowserThread::PostTask( |
| 250 content::BrowserThread::UI, | 274 content::BrowserThread::UI, |
| 251 FROM_HERE, | 275 FROM_HERE, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 357 } |
| 334 | 358 |
| 335 void GCMProfileService::IOWorker::OnLoadingCompleted() { | 359 void GCMProfileService::IOWorker::OnLoadingCompleted() { |
| 336 content::BrowserThread::PostTask( | 360 content::BrowserThread::PostTask( |
| 337 content::BrowserThread::UI, | 361 content::BrowserThread::UI, |
| 338 FROM_HERE, | 362 FROM_HERE, |
| 339 base::Bind(&GCMProfileService::GCMClientLoadingFinished, | 363 base::Bind(&GCMProfileService::GCMClientLoadingFinished, |
| 340 service_)); | 364 service_)); |
| 341 } | 365 } |
| 342 | 366 |
| 343 base::TaskRunner* GCMProfileService::IOWorker::GetFileTaskRunner() { | |
| 344 // TODO(jianli): to be implemented. | |
| 345 return NULL; | |
| 346 } | |
| 347 | |
| 348 void GCMProfileService::IOWorker::CheckGCMClientLoading() { | 367 void GCMProfileService::IOWorker::CheckGCMClientLoading() { |
| 349 content::BrowserThread::PostTask( | 368 content::BrowserThread::PostTask( |
| 350 content::BrowserThread::UI, | 369 content::BrowserThread::UI, |
| 351 FROM_HERE, | 370 FROM_HERE, |
| 352 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, | 371 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, |
| 353 service_, | 372 service_, |
| 354 GCMClient::Get()->IsLoading())); | 373 gcm_client_->IsLoading())); |
| 355 } | 374 } |
| 356 | 375 |
| 357 void GCMProfileService::IOWorker::SetUser(const std::string& username) { | 376 void GCMProfileService::IOWorker::SetUser(const std::string& username) { |
| 358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 377 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 359 DCHECK(username_.empty() && !username.empty()); | 378 DCHECK(username_.empty() && !username.empty()); |
| 360 | 379 |
| 361 username_ = username; | 380 username_ = username; |
| 362 GCMClient::Get()->SetUserDelegate(username_, this); | 381 gcm_client_->SetUserDelegate(username_, this); |
| 363 } | 382 } |
| 364 | 383 |
| 365 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { | 384 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { |
| 366 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 385 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 367 DCHECK(!username.empty()); | 386 DCHECK(!username.empty()); |
| 368 | 387 |
| 369 if (username_.empty()) | 388 if (username_.empty()) |
| 370 return; | 389 return; |
| 371 username_.clear(); | 390 username_.clear(); |
| 372 GCMClient::Get()->SetUserDelegate(username_, NULL); | 391 gcm_client_->SetUserDelegate(username_, NULL); |
| 373 } | 392 } |
| 374 | 393 |
| 375 void GCMProfileService::IOWorker::CheckIn() { | 394 void GCMProfileService::IOWorker::CheckIn() { |
| 376 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 395 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 377 | 396 |
| 378 GCMClient::Get()->CheckIn(username_); | 397 gcm_client_->CheckIn(username_); |
| 379 } | 398 } |
| 380 | 399 |
| 381 void GCMProfileService::IOWorker::SetCheckinInfo( | 400 void GCMProfileService::IOWorker::SetCheckinInfo( |
| 382 GCMClient::CheckinInfo checkin_info) { | 401 GCMClient::CheckinInfo checkin_info) { |
| 383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 402 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 384 | 403 |
| 385 checkin_info_ = checkin_info; | 404 checkin_info_ = checkin_info; |
| 386 } | 405 } |
| 387 | 406 |
| 388 void GCMProfileService::IOWorker::CheckOut() { | 407 void GCMProfileService::IOWorker::CheckOut() { |
| 389 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 408 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 390 | 409 |
| 391 username_.clear(); | 410 username_.clear(); |
| 392 checkin_info_.Reset(); | 411 checkin_info_.Reset(); |
| 393 } | 412 } |
| 394 | 413 |
| 395 void GCMProfileService::IOWorker::Register( | 414 void GCMProfileService::IOWorker::Register( |
| 396 const std::string& app_id, | 415 const std::string& app_id, |
| 397 const std::vector<std::string>& sender_ids, | 416 const std::vector<std::string>& sender_ids, |
| 398 const std::string& cert) { | 417 const std::string& cert) { |
| 399 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 418 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 400 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 419 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 401 | 420 |
| 402 GCMClient::Get()->Register(username_, app_id, cert, sender_ids); | 421 gcm_client_->Register(username_, app_id, cert, sender_ids); |
| 403 } | 422 } |
| 404 | 423 |
| 405 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { | 424 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { |
| 406 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 425 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 407 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 426 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 408 | 427 |
| 409 GCMClient::Get()->Unregister(username_, app_id); | 428 gcm_client_->Unregister(username_, app_id); |
| 410 } | 429 } |
| 411 | 430 |
| 412 void GCMProfileService::IOWorker::Send( | 431 void GCMProfileService::IOWorker::Send( |
| 413 const std::string& app_id, | 432 const std::string& app_id, |
| 414 const std::string& receiver_id, | 433 const std::string& receiver_id, |
| 415 const GCMClient::OutgoingMessage& message) { | 434 const GCMClient::OutgoingMessage& message) { |
| 416 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 435 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 417 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 436 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
| 418 | 437 |
| 419 GCMClient::Get()->Send(username_, app_id, receiver_id, message); | 438 gcm_client_->Send(username_, app_id, receiver_id, message); |
| 420 } | 439 } |
| 421 | 440 |
| 422 GCMProfileService::RegistrationInfo::RegistrationInfo() { | 441 GCMProfileService::RegistrationInfo::RegistrationInfo() { |
| 423 } | 442 } |
| 424 | 443 |
| 425 GCMProfileService::RegistrationInfo::~RegistrationInfo() { | 444 GCMProfileService::RegistrationInfo::~RegistrationInfo() { |
| 426 } | 445 } |
| 427 | 446 |
| 428 bool GCMProfileService::RegistrationInfo::IsValid() const { | 447 bool GCMProfileService::RegistrationInfo::IsValid() const { |
| 429 return !sender_ids.empty() && !registration_id.empty(); | 448 return !sender_ids.empty() && !registration_id.empty(); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 458 prefs::kGCMUserToken, | 477 prefs::kGCMUserToken, |
| 459 "", | 478 "", |
| 460 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 479 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 461 } | 480 } |
| 462 | 481 |
| 463 GCMProfileService::GCMProfileService(Profile* profile) | 482 GCMProfileService::GCMProfileService(Profile* profile) |
| 464 : profile_(profile), | 483 : profile_(profile), |
| 465 testing_delegate_(NULL), | 484 testing_delegate_(NULL), |
| 466 weak_ptr_factory_(this) { | 485 weak_ptr_factory_(this) { |
| 467 DCHECK(!profile->IsOffTheRecord()); | 486 DCHECK(!profile->IsOffTheRecord()); |
| 487 | |
| 468 Init(); | 488 Init(); |
| 469 } | 489 } |
| 470 | 490 |
| 471 GCMProfileService::GCMProfileService(Profile* profile, | 491 GCMProfileService::GCMProfileService(Profile* profile, |
| 472 TestingDelegate* testing_delegate) | 492 TestingDelegate* testing_delegate) |
| 473 : profile_(profile), | 493 : profile_(profile), |
| 474 testing_delegate_(testing_delegate), | 494 testing_delegate_(testing_delegate), |
| 475 weak_ptr_factory_(this) { | 495 weak_ptr_factory_(this) { |
| 476 Init(); | 496 Init(); |
| 477 } | 497 } |
| 478 | 498 |
| 479 GCMProfileService::~GCMProfileService() { | 499 GCMProfileService::~GCMProfileService() { |
| 480 if (username_.empty()) | 500 if (username_.empty()) |
| 481 return; | 501 return; |
| 482 content::BrowserThread::PostTask( | 502 content::BrowserThread::PostTask( |
| 483 content::BrowserThread::IO, | 503 content::BrowserThread::IO, |
| 484 FROM_HERE, | 504 FROM_HERE, |
| 485 base::Bind(&GCMProfileService::IOWorker::RemoveUser, | 505 base::Bind(&GCMProfileService::IOWorker::RemoveUser, |
| 486 io_worker_, | 506 io_worker_, |
| 487 username_)); | 507 username_)); |
| 488 } | 508 } |
| 489 | 509 |
| 490 void GCMProfileService::Init() { | 510 void GCMProfileService::Init() { |
| 491 delayed_task_controller_.reset(new DelayedTaskController); | 511 delayed_task_controller_.reset(new DelayedTaskController); |
| 492 | 512 |
| 493 // This has to be done first since CheckIn depends on it. | 513 // This has to be done first since CheckIn depends on it. |
| 494 io_worker_ = new IOWorker(weak_ptr_factory_.GetWeakPtr()); | 514 io_worker_ = new IOWorker(weak_ptr_factory_.GetWeakPtr()); |
| 515 io_worker_->Initialize(); | |
|
Nicolas Zea
2014/01/21 23:04:45
I think it's cleaner to have the GCMProfileService
jianli
2014/01/21 23:16:16
Good idea. Also moved the logic defined in CheckGC
| |
| 495 | 516 |
| 496 // Checks if GCMClient has finished the loading. | 517 // Checks if GCMClient has finished the loading. |
| 497 content::BrowserThread::PostTask( | 518 content::BrowserThread::PostTask( |
| 498 content::BrowserThread::IO, | 519 content::BrowserThread::IO, |
| 499 FROM_HERE, | 520 FROM_HERE, |
| 500 base::Bind(&GCMProfileService::IOWorker::CheckGCMClientLoading, | 521 base::Bind(&GCMProfileService::IOWorker::CheckGCMClientLoading, |
| 501 io_worker_)); | 522 io_worker_)); |
| 502 | 523 |
| 503 // In case that the profile has been signed in before GCMProfileService is | 524 // In case that the profile has been signed in before GCMProfileService is |
| 504 // created. | 525 // created. |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 969 | 990 |
| 970 return true; | 991 return true; |
| 971 } | 992 } |
| 972 | 993 |
| 973 // static | 994 // static |
| 974 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 995 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
| 975 return kRegistrationKey; | 996 return kRegistrationKey; |
| 976 } | 997 } |
| 977 | 998 |
| 978 } // namespace gcm | 999 } // namespace gcm |
| OLD | NEW |