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/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
16 #include "components/gcm_driver/gcm_app_handler.h" | 16 #include "components/gcm_driver/gcm_app_handler.h" |
17 #include "components/gcm_driver/gcm_client_factory.h" | 17 #include "components/gcm_driver/gcm_client_factory.h" |
18 #include "components/gcm_driver/system_encryptor.h" | 18 #include "components/gcm_driver/system_encryptor.h" |
19 #include "google_apis/gcm/engine/account_mapping.h" | 19 #include "google_apis/gcm/engine/account_mapping.h" |
20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
22 | 22 |
23 namespace gcm { | 23 namespace gcm { |
24 | 24 |
25 namespace { | |
26 | |
27 // Empty string is reserved for the default app handler. | |
28 const char kDefaultAppHandler[] = ""; | |
29 | |
30 } // namespace | |
31 | |
32 // Helper class to save tasks to run until we're ready to execute them. | 25 // Helper class to save tasks to run until we're ready to execute them. |
33 class GCMDriverDesktop::DelayedTaskController { | 26 class GCMDriverDesktop::DelayedTaskController { |
34 public: | 27 public: |
35 DelayedTaskController(); | 28 DelayedTaskController(); |
36 ~DelayedTaskController(); | 29 ~DelayedTaskController(); |
37 | 30 |
38 // Adds a task that will be invoked once we're ready. | 31 // Adds a task that will be invoked once we're ready. |
39 void AddTask(const base::Closure& task); | 32 void AddTask(const base::Closure& task); |
40 | 33 |
41 // Sets ready status. It is ready only when check-in is completed and | 34 // Sets ready status. It is ready only when check-in is completed and |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 | 449 |
457 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { | 450 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { |
458 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 451 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
459 GCMDriver::RemoveAppHandler(app_id); | 452 GCMDriver::RemoveAppHandler(app_id); |
460 | 453 |
461 // Stops the GCM service when no app intends to consume it. | 454 // Stops the GCM service when no app intends to consume it. |
462 if (app_handlers().empty()) | 455 if (app_handlers().empty()) |
463 Stop(); | 456 Stop(); |
464 } | 457 } |
465 | 458 |
| 459 void GCMDriverDesktop::AddConnectionObserver(GCMConnectionObserver* observer) { |
| 460 connection_observer_list_.AddObserver(observer); |
| 461 } |
| 462 |
| 463 void GCMDriverDesktop::RemoveConnectionObserver( |
| 464 GCMConnectionObserver* observer) { |
| 465 connection_observer_list_.RemoveObserver(observer); |
| 466 } |
| 467 |
466 void GCMDriverDesktop::Enable() { | 468 void GCMDriverDesktop::Enable() { |
467 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 469 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
468 | 470 |
469 if (gcm_enabled_) | 471 if (gcm_enabled_) |
470 return; | 472 return; |
471 gcm_enabled_ = true; | 473 gcm_enabled_ = true; |
472 | 474 |
473 EnsureStarted(); | 475 EnsureStarted(); |
474 } | 476 } |
475 | 477 |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 | 754 |
753 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { | 755 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { |
754 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 756 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
755 | 757 |
756 connected_ = true; | 758 connected_ = true; |
757 | 759 |
758 // Drop the event if the service has been stopped. | 760 // Drop the event if the service has been stopped. |
759 if (!gcm_started_) | 761 if (!gcm_started_) |
760 return; | 762 return; |
761 | 763 |
762 const GCMAppHandlerMap& app_handler_map = app_handlers(); | 764 FOR_EACH_OBSERVER(GCMConnectionObserver, |
763 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin(); | 765 connection_observer_list_, |
764 iter != app_handler_map.end(); ++iter) { | 766 OnConnected(ip_endpoint)); |
765 iter->second->OnConnected(ip_endpoint); | |
766 } | |
767 | |
768 GetAppHandler(kDefaultAppHandler)->OnConnected(ip_endpoint); | |
769 } | 767 } |
770 | 768 |
771 void GCMDriverDesktop::OnDisconnected() { | 769 void GCMDriverDesktop::OnDisconnected() { |
772 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 770 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
773 | 771 |
774 connected_ = false; | 772 connected_ = false; |
775 | 773 |
776 // Drop the event if the service has been stopped. | 774 // Drop the event if the service has been stopped. |
777 if (!gcm_started_) | 775 if (!gcm_started_) |
778 return; | 776 return; |
779 | 777 |
780 const GCMAppHandlerMap& app_handler_map = app_handlers(); | 778 FOR_EACH_OBSERVER( |
781 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin(); | 779 GCMConnectionObserver, connection_observer_list_, OnDisconnected()); |
782 iter != app_handler_map.end(); ++iter) { | |
783 iter->second->OnDisconnected(); | |
784 } | |
785 | |
786 GetAppHandler(kDefaultAppHandler)->OnDisconnected(); | |
787 } | 780 } |
788 | 781 |
789 void GCMDriverDesktop::GetGCMStatisticsFinished( | 782 void GCMDriverDesktop::GetGCMStatisticsFinished( |
790 const GCMClient::GCMStatistics& stats) { | 783 const GCMClient::GCMStatistics& stats) { |
791 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 784 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
792 | 785 |
793 // Normally request_gcm_statistics_callback_ would not be null. | 786 // Normally request_gcm_statistics_callback_ would not be null. |
794 if (!request_gcm_statistics_callback_.is_null()) | 787 if (!request_gcm_statistics_callback_.is_null()) |
795 request_gcm_statistics_callback_.Run(stats); | 788 request_gcm_statistics_callback_.Run(stats); |
796 else | 789 else |
797 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 790 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
798 } | 791 } |
799 | 792 |
800 } // namespace gcm | 793 } // namespace gcm |
801 | 794 |
OLD | NEW |