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

Side by Side Diff: components/gcm_driver/gcm_driver_desktop.cc

Issue 745123002: Link GCM heartbeat with wake on wifi preference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make GCM wake from suspend aware Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/gcm_driver/gcm_driver_desktop.h" 5 #include "components/gcm_driver/gcm_driver_desktop.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 #include "base/threading/sequenced_worker_pool.h" 16 #include "base/threading/sequenced_worker_pool.h"
17 #include "components/gcm_driver/gcm_account_mapper.h" 17 #include "components/gcm_driver/gcm_account_mapper.h"
18 #include "components/gcm_driver/gcm_app_handler.h" 18 #include "components/gcm_driver/gcm_app_handler.h"
19 #include "components/gcm_driver/gcm_channel_status_syncer.h" 19 #include "components/gcm_driver/gcm_channel_status_syncer.h"
20 #include "components/gcm_driver/gcm_client_factory.h" 20 #include "components/gcm_driver/gcm_client_factory.h"
21 #include "components/gcm_driver/gcm_delayed_task_controller.h" 21 #include "components/gcm_driver/gcm_delayed_task_controller.h"
22 #include "components/gcm_driver/system_encryptor.h" 22 #include "components/gcm_driver/system_encryptor.h"
23 #include "google_apis/gcm/engine/account_mapping.h" 23 #include "google_apis/gcm/engine/account_mapping.h"
24 #include "net/base/ip_endpoint.h" 24 #include "net/base/ip_endpoint.h"
25 #include "net/url_request/url_request_context_getter.h" 25 #include "net/url_request/url_request_context_getter.h"
26 26
27 #if defined(OS_CHROMEOS)
28 #include "components/timers/alarm_timer.h"
29 #include "google_apis/gcm/engine/heartbeat_manager.h"
30 #include "google_apis/gcm/engine/mcs_client.h"
31 #endif
32
27 namespace gcm { 33 namespace gcm {
28 34
29 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { 35 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate {
30 public: 36 public:
31 // Called on UI thread. 37 // Called on UI thread.
32 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 38 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
33 const scoped_refptr<base::SequencedTaskRunner>& io_thread); 39 const scoped_refptr<base::SequencedTaskRunner>& io_thread);
34 virtual ~IOWorker(); 40 virtual ~IOWorker();
35 41
36 // Overridden from GCMClient::Delegate: 42 // Overridden from GCMClient::Delegate:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 const std::string& receiver_id, 80 const std::string& receiver_id,
75 const GCMClient::OutgoingMessage& message); 81 const GCMClient::OutgoingMessage& message);
76 void GetGCMStatistics(bool clear_logs); 82 void GetGCMStatistics(bool clear_logs);
77 void SetGCMRecording(bool recording); 83 void SetGCMRecording(bool recording);
78 84
79 void SetAccountTokens( 85 void SetAccountTokens(
80 const std::vector<GCMClient::AccountTokenInfo>& account_tokens); 86 const std::vector<GCMClient::AccountTokenInfo>& account_tokens);
81 void UpdateAccountMapping(const AccountMapping& account_mapping); 87 void UpdateAccountMapping(const AccountMapping& account_mapping);
82 void RemoveAccountMapping(const std::string& account_id); 88 void RemoveAccountMapping(const std::string& account_id);
83 void SetLastTokenFetchTime(const base::Time& time); 89 void SetLastTokenFetchTime(const base::Time& time);
90 void WakeFromSuspendForHeartbeat(bool wake);
84 91
85 // For testing purpose. Can be called from UI thread. Use with care. 92 // For testing purpose. Can be called from UI thread. Use with care.
86 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } 93 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); }
87 94
88 private: 95 private:
89 scoped_refptr<base::SequencedTaskRunner> ui_thread_; 96 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
90 scoped_refptr<base::SequencedTaskRunner> io_thread_; 97 scoped_refptr<base::SequencedTaskRunner> io_thread_;
91 98
92 base::WeakPtr<GCMDriverDesktop> service_; 99 base::WeakPtr<GCMDriverDesktop> service_;
93 100
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 gcm_client_->RemoveAccountMapping(account_id); 341 gcm_client_->RemoveAccountMapping(account_id);
335 } 342 }
336 343
337 void GCMDriverDesktop::IOWorker::SetLastTokenFetchTime(const base::Time& time) { 344 void GCMDriverDesktop::IOWorker::SetLastTokenFetchTime(const base::Time& time) {
338 DCHECK(io_thread_->RunsTasksOnCurrentThread()); 345 DCHECK(io_thread_->RunsTasksOnCurrentThread());
339 346
340 if (gcm_client_.get()) 347 if (gcm_client_.get())
341 gcm_client_->SetLastTokenFetchTime(time); 348 gcm_client_->SetLastTokenFetchTime(time);
342 } 349 }
343 350
351 void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) {
352 #if defined(OS_CHROMEOS)
353 DCHECK(io_thread_->RunsTasksOnCurrentThread());
354 DCHECK(gcm_client_->GetMCSClient());
355
356 scoped_ptr<base::Timer> timer;
357 if (wake)
358 timer.reset(new timers::AlarmTimer(true, false));
359 else
360 timer.reset(new base::Timer(true, false));
361
362 gcm_client_->GetMCSClient()->heartbeat_manager()->UpdateHeartbeatTimer(
363 timer.Pass());
364 #endif
365 }
366
344 GCMDriverDesktop::GCMDriverDesktop( 367 GCMDriverDesktop::GCMDriverDesktop(
345 scoped_ptr<GCMClientFactory> gcm_client_factory, 368 scoped_ptr<GCMClientFactory> gcm_client_factory,
346 const GCMClient::ChromeBuildInfo& chrome_build_info, 369 const GCMClient::ChromeBuildInfo& chrome_build_info,
347 const std::string& channel_status_request_url, 370 const std::string& channel_status_request_url,
348 const std::string& user_agent, 371 const std::string& user_agent,
349 PrefService* prefs, 372 PrefService* prefs,
350 const base::FilePath& store_path, 373 const base::FilePath& store_path,
351 const scoped_refptr<net::URLRequestContextGetter>& request_context, 374 const scoped_refptr<net::URLRequestContextGetter>& request_context,
352 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 375 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
353 const scoped_refptr<base::SequencedTaskRunner>& io_thread, 376 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 675
653 last_token_fetch_time_ = time; 676 last_token_fetch_time_ = time;
654 677
655 io_thread_->PostTask( 678 io_thread_->PostTask(
656 FROM_HERE, 679 FROM_HERE,
657 base::Bind(&GCMDriverDesktop::IOWorker::SetLastTokenFetchTime, 680 base::Bind(&GCMDriverDesktop::IOWorker::SetLastTokenFetchTime,
658 base::Unretained(io_worker_.get()), 681 base::Unretained(io_worker_.get()),
659 time)); 682 time));
660 } 683 }
661 684
685 void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) {
686 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
687
688 if (!IsStarted()) {
689 // The GCM service has not been started yet, either because the user has not
690 // signed in or because no apps/extensions are using GCM. Queue this task
691 // to run whenever GCM does start up.
692 gcm_started_callback_ =
Nicolas Zea 2014/12/05 00:53:05 I don't think we need a generic callback here unti
Chirantan Ekbote 2014/12/05 21:49:05 Done.
693 base::Bind(&GCMDriverDesktop::WakeFromSuspendForHeartbeat,
694 weak_ptr_factory_.GetWeakPtr(),
695 wake);
696 return;
697 }
698
699 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) {
700 // The GCM service has started but the client is not ready yet.
701 delayed_task_controller_->AddTask(
702 base::Bind(&GCMDriverDesktop::WakeFromSuspendForHeartbeat,
703 weak_ptr_factory_.GetWeakPtr(),
704 wake));
705 return;
706 }
707
708 // The GCMClient is ready so we can go ahead and post this task to the
709 // IOWorker.
710 io_thread_->PostTask(
711 FROM_HERE,
712 base::Bind(&GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat,
713 base::Unretained(io_worker_.get()),
714 wake));
715 }
716
662 void GCMDriverDesktop::SetAccountTokens( 717 void GCMDriverDesktop::SetAccountTokens(
663 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { 718 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
664 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 719 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
665 720
666 account_mapper_->SetAccountTokens(account_tokens); 721 account_mapper_->SetAccountTokens(account_tokens);
667 722
668 io_thread_->PostTask( 723 io_thread_->PostTask(
669 FROM_HERE, 724 FROM_HERE,
670 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, 725 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens,
671 base::Unretained(io_worker_.get()), 726 base::Unretained(io_worker_.get()),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 762
708 // Note that we need to pass weak pointer again since the existing weak 763 // Note that we need to pass weak pointer again since the existing weak
709 // pointer in IOWorker might have been invalidated when check-out occurs. 764 // pointer in IOWorker might have been invalidated when check-out occurs.
710 io_thread_->PostTask( 765 io_thread_->PostTask(
711 FROM_HERE, 766 FROM_HERE,
712 base::Bind(&GCMDriverDesktop::IOWorker::Start, 767 base::Bind(&GCMDriverDesktop::IOWorker::Start,
713 base::Unretained(io_worker_.get()), 768 base::Unretained(io_worker_.get()),
714 weak_ptr_factory_.GetWeakPtr())); 769 weak_ptr_factory_.GetWeakPtr()));
715 770
716 gcm_started_ = true; 771 gcm_started_ = true;
772 if (!gcm_started_callback_.is_null()) {
773 gcm_started_callback_.Run();
774 gcm_started_callback_.Reset();
775 }
776
717 return GCMClient::SUCCESS; 777 return GCMClient::SUCCESS;
718 } 778 }
719 779
720 void GCMDriverDesktop::RemoveCachedData() { 780 void GCMDriverDesktop::RemoveCachedData() {
721 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 781 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
722 // Remove all the queued tasks since they no longer make sense after 782 // Remove all the queued tasks since they no longer make sense after
723 // GCM service is stopped. 783 // GCM service is stopped.
724 weak_ptr_factory_.InvalidateWeakPtrs(); 784 weak_ptr_factory_.InvalidateWeakPtrs();
725 785
726 gcm_started_ = false; 786 gcm_started_ = false;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 878 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
819 879
820 // Normally request_gcm_statistics_callback_ would not be null. 880 // Normally request_gcm_statistics_callback_ would not be null.
821 if (!request_gcm_statistics_callback_.is_null()) 881 if (!request_gcm_statistics_callback_.is_null())
822 request_gcm_statistics_callback_.Run(stats); 882 request_gcm_statistics_callback_.Run(stats);
823 else 883 else
824 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; 884 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL.";
825 } 885 }
826 886
827 } // namespace gcm 887 } // namespace gcm
828
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698