Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: chrome/browser/services/gcm/gcm_profile_service.cc

Issue 183013007: [GCM] Stop/restart GCM when the state is forced off/on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 GCMClient::Result result) OVERRIDE; 257 GCMClient::Result result) OVERRIDE;
258 virtual void OnGCMReady() OVERRIDE; 258 virtual void OnGCMReady() OVERRIDE;
259 259
260 // Called on IO thread. 260 // Called on IO thread.
261 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory, 261 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory,
262 const base::FilePath& store_path, 262 const base::FilePath& store_path,
263 const std::vector<std::string>& account_ids, 263 const std::vector<std::string>& account_ids,
264 const scoped_refptr<net::URLRequestContextGetter>& 264 const scoped_refptr<net::URLRequestContextGetter>&
265 url_request_context_getter); 265 url_request_context_getter);
266 void Reset(); 266 void Reset();
267 void Load(const base::WeakPtr<GCMProfileService>& service); 267 void Load(const base::WeakPtr<GCMProfileService>& service);
Dmitry Titov 2014/03/04 02:47:51 Note: If you rename GCMClient::Load -> Start, you
jianli 2014/03/04 05:14:26 Yes, will rename all these if we proceed with the
268 void Stop();
268 void CheckOut(); 269 void CheckOut();
269 void Register(const std::string& app_id, 270 void Register(const std::string& app_id,
270 const std::vector<std::string>& sender_ids, 271 const std::vector<std::string>& sender_ids,
271 const std::string& cert); 272 const std::string& cert);
272 void Unregister(const std::string& app_id); 273 void Unregister(const std::string& app_id);
273 void Send(const std::string& app_id, 274 void Send(const std::string& app_id,
274 const std::string& receiver_id, 275 const std::string& receiver_id,
275 const GCMClient::OutgoingMessage& message); 276 const GCMClient::OutgoingMessage& message);
276 277
277 // For testing purpose. Can be called from UI thread. Use with care. 278 // For testing purpose. Can be called from UI thread. Use with care.
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 } 420 }
420 421
421 void GCMProfileService::IOWorker::Load( 422 void GCMProfileService::IOWorker::Load(
422 const base::WeakPtr<GCMProfileService>& service) { 423 const base::WeakPtr<GCMProfileService>& service) {
423 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 424 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
424 425
425 service_ = service; 426 service_ = service;
426 gcm_client_->Load(); 427 gcm_client_->Load();
427 } 428 }
428 429
430 void GCMProfileService::IOWorker::Stop() {
431 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
432
433 gcm_client_->Stop();
434 }
435
429 void GCMProfileService::IOWorker::CheckOut() { 436 void GCMProfileService::IOWorker::CheckOut() {
430 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 437 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
431 438
432 gcm_client_->CheckOut(); 439 gcm_client_->CheckOut();
433 440
434 // Note that we still need to keep GCMClient instance alive since the profile 441 // Note that we still need to keep GCMClient instance alive since the profile
435 // might be signed in again. 442 // might be signed in again.
436 } 443 }
437 444
438 void GCMProfileService::IOWorker::Register( 445 void GCMProfileService::IOWorker::Register(
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 profile_->GetPath().Append(chrome::kGCMStoreDirname), 559 profile_->GetPath().Append(chrome::kGCMStoreDirname),
553 account_ids, 560 account_ids,
554 url_request_context_getter)); 561 url_request_context_getter));
555 562
556 // Load from the GCM store and initiate the GCM check-in if the rollout signal 563 // Load from the GCM store and initiate the GCM check-in if the rollout signal
557 // indicates yes. 564 // indicates yes.
558 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) 565 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED)
559 EnsureLoaded(); 566 EnsureLoaded();
560 } 567 }
561 568
569 void GCMProfileService::Start() {
570 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
571
572 EnsureLoaded();
573 }
574
575 void GCMProfileService::Stop() {
576 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
577
578 // No need to stop GCM service if not signed in yet.
579 if (username_.empty())
580 return;
581 username_.clear();
fgorski 2014/03/04 20:07:49 Seems that clearing username could be a part of Re
jianli 2014/03/04 20:52:26 Done.
582
583 RemoveCachedData();
584
585 content::BrowserThread::PostTask(
586 content::BrowserThread::IO,
587 FROM_HERE,
588 base::Bind(&GCMProfileService::IOWorker::Stop, io_worker_));
589 }
590
562 void GCMProfileService::Register(const std::string& app_id, 591 void GCMProfileService::Register(const std::string& app_id,
563 const std::vector<std::string>& sender_ids, 592 const std::vector<std::string>& sender_ids,
564 const std::string& cert, 593 const std::string& cert,
565 RegisterCallback callback) { 594 RegisterCallback callback) {
566 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 595 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
567 DCHECK(!app_id.empty() && !sender_ids.empty() && !callback.is_null()); 596 DCHECK(!app_id.empty() && !sender_ids.empty() && !callback.is_null());
568 597
569 // Ensure that check-in has been done. 598 // Ensure that check-in has been done.
570 EnsureLoaded(); 599 EnsureLoaded();
571 600
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 // Note that we need to pass weak pointer again since the existing weak 794 // Note that we need to pass weak pointer again since the existing weak
766 // pointer in IOWorker might have been invalidated when check-out occurs. 795 // pointer in IOWorker might have been invalidated when check-out occurs.
767 content::BrowserThread::PostTask( 796 content::BrowserThread::PostTask(
768 content::BrowserThread::IO, 797 content::BrowserThread::IO,
769 FROM_HERE, 798 FROM_HERE,
770 base::Bind(&GCMProfileService::IOWorker::Load, 799 base::Bind(&GCMProfileService::IOWorker::Load,
771 io_worker_, 800 io_worker_,
772 weak_ptr_factory_.GetWeakPtr())); 801 weak_ptr_factory_.GetWeakPtr()));
773 } 802 }
774 803
775 void GCMProfileService::CheckOut() { 804 void GCMProfileService::RemoveCachedData() {
776 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
777
778 // We still proceed with the check-out logic even if the check-in is not
779 // initiated in the current session. This will make sure that all the
780 // persisted data written previously will get purged.
781 username_.clear();
782
783 // Remove all the queued tasks since they no longer make sense after 805 // Remove all the queued tasks since they no longer make sense after
784 // check-out. 806 // GCM service is stopped.
785 weak_ptr_factory_.InvalidateWeakPtrs(); 807 weak_ptr_factory_.InvalidateWeakPtrs();
786 808
809 gcm_client_ready_ = false;
810 delayed_task_controller_.reset();
811 register_callbacks_.clear();
812 send_callbacks_.clear();
813 registration_info_map_.clear();
814 }
815
816 void GCMProfileService::RemovePersistedData() {
787 // Remove persisted data from app's state store. 817 // Remove persisted data from app's state store.
788 for (RegistrationInfoMap::const_iterator iter = 818 for (RegistrationInfoMap::const_iterator iter =
789 registration_info_map_.begin(); 819 registration_info_map_.begin();
790 iter != registration_info_map_.end(); ++iter) { 820 iter != registration_info_map_.end(); ++iter) {
791 DeleteRegistrationInfo(iter->first); 821 DeleteRegistrationInfo(iter->first);
792 } 822 }
793 823
794 // Remove persisted data from prefs store. 824 // Remove persisted data from prefs store.
795 profile_->GetPrefs()->ClearPref(prefs::kGCMRegisteredAppIDs); 825 profile_->GetPrefs()->ClearPref(prefs::kGCMRegisteredAppIDs);
826 }
796 827
797 gcm_client_ready_ = false; 828 void GCMProfileService::CheckOut() {
798 delayed_task_controller_.reset(); 829 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
799 register_callbacks_.clear(); 830
800 send_callbacks_.clear(); 831 // We still proceed with the check-out logic even if the check-in is not
801 registration_info_map_.clear(); 832 // initiated in the current session. This will make sure that all the
833 // persisted data written previously will get purged.
834 username_.clear();
835
836 // This has to be done before removing the cached data since we need to do
837 // the lookup based on the cached data.
838 RemovePersistedData();
839
840 RemoveCachedData();
802 841
803 content::BrowserThread::PostTask( 842 content::BrowserThread::PostTask(
804 content::BrowserThread::IO, 843 content::BrowserThread::IO,
805 FROM_HERE, 844 FROM_HERE,
806 base::Bind(&GCMProfileService::IOWorker::CheckOut, io_worker_)); 845 base::Bind(&GCMProfileService::IOWorker::CheckOut, io_worker_));
807 } 846 }
808 847
809 void GCMProfileService::ResetGCMClient() { 848 void GCMProfileService::ResetGCMClient() {
810 content::BrowserThread::PostTask( 849 content::BrowserThread::PostTask(
811 content::BrowserThread::IO, 850 content::BrowserThread::IO,
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 1108
1070 return true; 1109 return true;
1071 } 1110 }
1072 1111
1073 // static 1112 // static
1074 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { 1113 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() {
1075 return kRegistrationKey; 1114 return kRegistrationKey;
1076 } 1115 }
1077 1116
1078 } // namespace gcm 1117 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698