OLD | NEW |
---|---|
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 #endif | |
30 | |
27 namespace gcm { | 31 namespace gcm { |
28 | 32 |
29 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { | 33 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
30 public: | 34 public: |
31 // Called on UI thread. | 35 // Called on UI thread. |
32 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 36 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
33 const scoped_refptr<base::SequencedTaskRunner>& io_thread); | 37 const scoped_refptr<base::SequencedTaskRunner>& io_thread); |
34 virtual ~IOWorker(); | 38 virtual ~IOWorker(); |
35 | 39 |
36 // Overridden from GCMClient::Delegate: | 40 // Overridden from GCMClient::Delegate: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 const std::string& receiver_id, | 78 const std::string& receiver_id, |
75 const GCMClient::OutgoingMessage& message); | 79 const GCMClient::OutgoingMessage& message); |
76 void GetGCMStatistics(bool clear_logs); | 80 void GetGCMStatistics(bool clear_logs); |
77 void SetGCMRecording(bool recording); | 81 void SetGCMRecording(bool recording); |
78 | 82 |
79 void SetAccountTokens( | 83 void SetAccountTokens( |
80 const std::vector<GCMClient::AccountTokenInfo>& account_tokens); | 84 const std::vector<GCMClient::AccountTokenInfo>& account_tokens); |
81 void UpdateAccountMapping(const AccountMapping& account_mapping); | 85 void UpdateAccountMapping(const AccountMapping& account_mapping); |
82 void RemoveAccountMapping(const std::string& account_id); | 86 void RemoveAccountMapping(const std::string& account_id); |
83 void SetLastTokenFetchTime(const base::Time& time); | 87 void SetLastTokenFetchTime(const base::Time& time); |
88 void WakeFromSuspendForHeartbeat(bool wake); | |
84 | 89 |
85 // For testing purpose. Can be called from UI thread. Use with care. | 90 // For testing purpose. Can be called from UI thread. Use with care. |
86 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } | 91 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
87 | 92 |
88 private: | 93 private: |
89 scoped_refptr<base::SequencedTaskRunner> ui_thread_; | 94 scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
90 scoped_refptr<base::SequencedTaskRunner> io_thread_; | 95 scoped_refptr<base::SequencedTaskRunner> io_thread_; |
91 | 96 |
92 base::WeakPtr<GCMDriverDesktop> service_; | 97 base::WeakPtr<GCMDriverDesktop> service_; |
93 | 98 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 gcm_client_->RemoveAccountMapping(account_id); | 339 gcm_client_->RemoveAccountMapping(account_id); |
335 } | 340 } |
336 | 341 |
337 void GCMDriverDesktop::IOWorker::SetLastTokenFetchTime(const base::Time& time) { | 342 void GCMDriverDesktop::IOWorker::SetLastTokenFetchTime(const base::Time& time) { |
338 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 343 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
339 | 344 |
340 if (gcm_client_.get()) | 345 if (gcm_client_.get()) |
341 gcm_client_->SetLastTokenFetchTime(time); | 346 gcm_client_->SetLastTokenFetchTime(time); |
342 } | 347 } |
343 | 348 |
349 void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) { | |
350 #if defined(OS_CHROMEOS) | |
351 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | |
352 | |
353 scoped_ptr<base::Timer> timer; | |
354 if (wake) | |
355 timer.reset(new timers::AlarmTimer(true, false)); | |
356 else | |
357 timer.reset(new base::Timer(true, false)); | |
358 | |
359 gcm_client_->UpdateHeartbeatTimer(timer.Pass()); | |
360 #endif | |
361 } | |
362 | |
344 GCMDriverDesktop::GCMDriverDesktop( | 363 GCMDriverDesktop::GCMDriverDesktop( |
345 scoped_ptr<GCMClientFactory> gcm_client_factory, | 364 scoped_ptr<GCMClientFactory> gcm_client_factory, |
346 const GCMClient::ChromeBuildInfo& chrome_build_info, | 365 const GCMClient::ChromeBuildInfo& chrome_build_info, |
347 const std::string& channel_status_request_url, | 366 const std::string& channel_status_request_url, |
348 const std::string& user_agent, | 367 const std::string& user_agent, |
349 PrefService* prefs, | 368 PrefService* prefs, |
350 const base::FilePath& store_path, | 369 const base::FilePath& store_path, |
351 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 370 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
352 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 371 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
353 const scoped_refptr<base::SequencedTaskRunner>& io_thread, | 372 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
354 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) | 373 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
355 : gcm_channel_status_syncer_( | 374 : gcm_channel_status_syncer_( |
356 new GCMChannelStatusSyncer(this, | 375 new GCMChannelStatusSyncer(this, |
357 prefs, | 376 prefs, |
358 channel_status_request_url, | 377 channel_status_request_url, |
359 user_agent, | 378 user_agent, |
360 request_context)), | 379 request_context)), |
361 signed_in_(false), | 380 signed_in_(false), |
362 gcm_started_(false), | 381 gcm_started_(false), |
363 gcm_enabled_(true), | 382 gcm_enabled_(true), |
364 connected_(false), | 383 connected_(false), |
365 account_mapper_(new GCMAccountMapper(this)), | 384 account_mapper_(new GCMAccountMapper(this)), |
366 // Setting to max, to make sure it does not prompt for token reporting | 385 // Setting to max, to make sure it does not prompt for token reporting |
367 // Before reading a reasonable value from the DB, which might be never, | 386 // Before reading a reasonable value from the DB, which might be never, |
368 // in which case the fetching will be triggered. | 387 // in which case the fetching will be triggered. |
369 last_token_fetch_time_(base::Time::Max()), | 388 last_token_fetch_time_(base::Time::Max()), |
370 ui_thread_(ui_thread), | 389 ui_thread_(ui_thread), |
371 io_thread_(io_thread), | 390 io_thread_(io_thread), |
391 wake_from_suspend_enabled_(false), | |
372 weak_ptr_factory_(this) { | 392 weak_ptr_factory_(this) { |
373 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); | 393 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); |
374 | 394 |
375 // Create and initialize the GCMClient. Note that this does not initiate the | 395 // Create and initialize the GCMClient. Note that this does not initiate the |
376 // GCM check-in. | 396 // GCM check-in. |
377 io_worker_.reset(new IOWorker(ui_thread, io_thread)); | 397 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
378 io_thread_->PostTask( | 398 io_thread_->PostTask( |
379 FROM_HERE, | 399 FROM_HERE, |
380 base::Bind(&GCMDriverDesktop::IOWorker::Initialize, | 400 base::Bind(&GCMDriverDesktop::IOWorker::Initialize, |
381 base::Unretained(io_worker_.get()), | 401 base::Unretained(io_worker_.get()), |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
652 | 672 |
653 last_token_fetch_time_ = time; | 673 last_token_fetch_time_ = time; |
654 | 674 |
655 io_thread_->PostTask( | 675 io_thread_->PostTask( |
656 FROM_HERE, | 676 FROM_HERE, |
657 base::Bind(&GCMDriverDesktop::IOWorker::SetLastTokenFetchTime, | 677 base::Bind(&GCMDriverDesktop::IOWorker::SetLastTokenFetchTime, |
658 base::Unretained(io_worker_.get()), | 678 base::Unretained(io_worker_.get()), |
659 time)); | 679 time)); |
660 } | 680 } |
661 | 681 |
682 void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) { | |
683 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | |
684 | |
685 wake_from_suspend_enabled_ = wake; | |
686 if (!IsStarted()) | |
687 return; | |
688 | |
689 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { | |
690 // The GCM service has started but the client is not ready yet. | |
691 delayed_task_controller_->AddTask( | |
692 base::Bind(&GCMDriverDesktop::WakeFromSuspendForHeartbeat, | |
693 weak_ptr_factory_.GetWeakPtr(), | |
694 wake_from_suspend_enabled_)); | |
695 return; | |
696 } | |
697 | |
698 // The GCMClient is ready so we can go ahead and post this task to the | |
699 // IOWorker. | |
700 io_thread_->PostTask( | |
701 FROM_HERE, | |
702 base::Bind(&GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat, | |
703 base::Unretained(io_worker_.get()), | |
704 wake_from_suspend_enabled_)); | |
705 } | |
706 | |
662 void GCMDriverDesktop::SetAccountTokens( | 707 void GCMDriverDesktop::SetAccountTokens( |
663 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 708 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
664 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 709 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
665 | 710 |
666 account_mapper_->SetAccountTokens(account_tokens); | 711 account_mapper_->SetAccountTokens(account_tokens); |
667 | 712 |
668 io_thread_->PostTask( | 713 io_thread_->PostTask( |
669 FROM_HERE, | 714 FROM_HERE, |
670 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, | 715 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, |
671 base::Unretained(io_worker_.get()), | 716 base::Unretained(io_worker_.get()), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
707 | 752 |
708 // Note that we need to pass weak pointer again since the existing weak | 753 // 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. | 754 // pointer in IOWorker might have been invalidated when check-out occurs. |
710 io_thread_->PostTask( | 755 io_thread_->PostTask( |
711 FROM_HERE, | 756 FROM_HERE, |
712 base::Bind(&GCMDriverDesktop::IOWorker::Start, | 757 base::Bind(&GCMDriverDesktop::IOWorker::Start, |
713 base::Unretained(io_worker_.get()), | 758 base::Unretained(io_worker_.get()), |
714 weak_ptr_factory_.GetWeakPtr())); | 759 weak_ptr_factory_.GetWeakPtr())); |
715 | 760 |
716 gcm_started_ = true; | 761 gcm_started_ = true; |
762 WakeFromSuspendForHeartbeat(wake_from_suspend_enabled_); | |
Nicolas Zea
2014/12/05 22:28:11
nit: should we only call this if wake_from_suspend
Chirantan Ekbote
2014/12/05 22:38:46
Not necessarily. On chrome os, the HeartbeatManag
| |
763 | |
717 return GCMClient::SUCCESS; | 764 return GCMClient::SUCCESS; |
718 } | 765 } |
719 | 766 |
720 void GCMDriverDesktop::RemoveCachedData() { | 767 void GCMDriverDesktop::RemoveCachedData() { |
721 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 768 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
722 // Remove all the queued tasks since they no longer make sense after | 769 // Remove all the queued tasks since they no longer make sense after |
723 // GCM service is stopped. | 770 // GCM service is stopped. |
724 weak_ptr_factory_.InvalidateWeakPtrs(); | 771 weak_ptr_factory_.InvalidateWeakPtrs(); |
725 | 772 |
726 gcm_started_ = false; | 773 gcm_started_ = false; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
818 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 865 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
819 | 866 |
820 // Normally request_gcm_statistics_callback_ would not be null. | 867 // Normally request_gcm_statistics_callback_ would not be null. |
821 if (!request_gcm_statistics_callback_.is_null()) | 868 if (!request_gcm_statistics_callback_.is_null()) |
822 request_gcm_statistics_callback_.Run(stats); | 869 request_gcm_statistics_callback_.Run(stats); |
823 else | 870 else |
824 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 871 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
825 } | 872 } |
826 | 873 |
827 } // namespace gcm | 874 } // namespace gcm |
828 | |
OLD | NEW |