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

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: Patch to land 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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
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 started yet.
579 if (username_.empty())
580 return;
581
582 RemoveCachedData();
583
584 content::BrowserThread::PostTask(
585 content::BrowserThread::IO,
586 FROM_HERE,
587 base::Bind(&GCMProfileService::IOWorker::Stop, io_worker_));
588 }
589
562 void GCMProfileService::Register(const std::string& app_id, 590 void GCMProfileService::Register(const std::string& app_id,
563 const std::vector<std::string>& sender_ids, 591 const std::vector<std::string>& sender_ids,
564 const std::string& cert, 592 const std::string& cert,
565 RegisterCallback callback) { 593 RegisterCallback callback) {
566 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 594 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
567 DCHECK(!app_id.empty() && !sender_ids.empty() && !callback.is_null()); 595 DCHECK(!app_id.empty() && !sender_ids.empty() && !callback.is_null());
568 596
569 // Ensure that check-in has been done. 597 // Ensure that check-in has been done.
570 EnsureLoaded(); 598 EnsureLoaded();
571 599
(...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 793 // 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. 794 // pointer in IOWorker might have been invalidated when check-out occurs.
767 content::BrowserThread::PostTask( 795 content::BrowserThread::PostTask(
768 content::BrowserThread::IO, 796 content::BrowserThread::IO,
769 FROM_HERE, 797 FROM_HERE,
770 base::Bind(&GCMProfileService::IOWorker::Load, 798 base::Bind(&GCMProfileService::IOWorker::Load,
771 io_worker_, 799 io_worker_,
772 weak_ptr_factory_.GetWeakPtr())); 800 weak_ptr_factory_.GetWeakPtr()));
773 } 801 }
774 802
775 void GCMProfileService::CheckOut() { 803 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 804 // Remove all the queued tasks since they no longer make sense after
784 // check-out. 805 // GCM service is stopped.
785 weak_ptr_factory_.InvalidateWeakPtrs(); 806 weak_ptr_factory_.InvalidateWeakPtrs();
786 807
808 username_.clear();
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
835 // This has to be done before removing the cached data since we need to do
836 // the lookup based on the cached data.
837 RemovePersistedData();
838
839 RemoveCachedData();
802 840
803 content::BrowserThread::PostTask( 841 content::BrowserThread::PostTask(
804 content::BrowserThread::IO, 842 content::BrowserThread::IO,
805 FROM_HERE, 843 FROM_HERE,
806 base::Bind(&GCMProfileService::IOWorker::CheckOut, io_worker_)); 844 base::Bind(&GCMProfileService::IOWorker::CheckOut, io_worker_));
807 } 845 }
808 846
809 void GCMProfileService::ResetGCMClient() { 847 void GCMProfileService::ResetGCMClient() {
810 content::BrowserThread::PostTask( 848 content::BrowserThread::PostTask(
811 content::BrowserThread::IO, 849 content::BrowserThread::IO,
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 1107
1070 return true; 1108 return true;
1071 } 1109 }
1072 1110
1073 // static 1111 // static
1074 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { 1112 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() {
1075 return kRegistrationKey; 1113 return kRegistrationKey;
1076 } 1114 }
1077 1115
1078 } // namespace gcm 1116 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/gcm_profile_service.h ('k') | chrome/browser/services/gcm/gcm_profile_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698