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/gaia/oauth2_token_service.h" | 19 #include "google_apis/gaia/oauth2_token_service.h" |
| 20 #include "net/base/ip_endpoint.h" |
20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
21 | 22 |
22 namespace gcm { | 23 namespace gcm { |
23 | 24 |
| 25 namespace { |
| 26 |
| 27 // Empty string is reserved for the default app handler. |
| 28 const char kDefaultAppHandler[] = ""; |
| 29 |
| 30 } // namespace |
| 31 |
24 // Helper class to save tasks to run until we're ready to execute them. | 32 // Helper class to save tasks to run until we're ready to execute them. |
25 class GCMDriverDesktop::DelayedTaskController { | 33 class GCMDriverDesktop::DelayedTaskController { |
26 public: | 34 public: |
27 DelayedTaskController(); | 35 DelayedTaskController(); |
28 ~DelayedTaskController(); | 36 ~DelayedTaskController(); |
29 | 37 |
30 // Adds a task that will be invoked once we're ready. | 38 // Adds a task that will be invoked once we're ready. |
31 void AddTask(const base::Closure& task); | 39 void AddTask(const base::Closure& task); |
32 | 40 |
33 // Sets ready status. It is ready only when check-in is completed and | 41 // Sets ready status. It is ready only when check-in is completed and |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 GCMClient::Result result) OVERRIDE; | 104 GCMClient::Result result) OVERRIDE; |
97 virtual void OnMessageReceived( | 105 virtual void OnMessageReceived( |
98 const std::string& app_id, | 106 const std::string& app_id, |
99 const GCMClient::IncomingMessage& message) OVERRIDE; | 107 const GCMClient::IncomingMessage& message) OVERRIDE; |
100 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 108 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
101 virtual void OnMessageSendError( | 109 virtual void OnMessageSendError( |
102 const std::string& app_id, | 110 const std::string& app_id, |
103 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 111 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
104 virtual void OnGCMReady() OVERRIDE; | 112 virtual void OnGCMReady() OVERRIDE; |
105 virtual void OnActivityRecorded() OVERRIDE; | 113 virtual void OnActivityRecorded() OVERRIDE; |
| 114 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE; |
| 115 virtual void OnDisconnected() OVERRIDE; |
106 | 116 |
107 // Called on IO thread. | 117 // Called on IO thread. |
108 void Initialize( | 118 void Initialize( |
109 scoped_ptr<GCMClientFactory> gcm_client_factory, | 119 scoped_ptr<GCMClientFactory> gcm_client_factory, |
110 const GCMClient::ChromeBuildInfo& chrome_build_info, | 120 const GCMClient::ChromeBuildInfo& chrome_build_info, |
111 const base::FilePath& store_path, | 121 const base::FilePath& store_path, |
112 const std::vector<std::string>& account_ids, | 122 const std::vector<std::string>& account_ids, |
113 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 123 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
114 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); | 124 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
115 void Start(const base::WeakPtr<GCMDriverDesktop>& service); | 125 void Start(const base::WeakPtr<GCMDriverDesktop>& service); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 base::Bind(&GCMDriverDesktop::GCMClientReady, service_)); | 253 base::Bind(&GCMDriverDesktop::GCMClientReady, service_)); |
244 } | 254 } |
245 | 255 |
246 void GCMDriverDesktop::IOWorker::OnActivityRecorded() { | 256 void GCMDriverDesktop::IOWorker::OnActivityRecorded() { |
247 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 257 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
248 // When an activity is recorded, get all the stats and refresh the UI of | 258 // When an activity is recorded, get all the stats and refresh the UI of |
249 // gcm-internals page. | 259 // gcm-internals page. |
250 GetGCMStatistics(false); | 260 GetGCMStatistics(false); |
251 } | 261 } |
252 | 262 |
| 263 void GCMDriverDesktop::IOWorker::OnConnected( |
| 264 const net::IPEndPoint& ip_endpoint) { |
| 265 ui_thread_->PostTask(FROM_HERE, |
| 266 base::Bind(&GCMDriverDesktop::OnConnected, |
| 267 service_, |
| 268 ip_endpoint)); |
| 269 } |
| 270 |
| 271 void GCMDriverDesktop::IOWorker::OnDisconnected() { |
| 272 ui_thread_->PostTask(FROM_HERE, |
| 273 base::Bind(&GCMDriverDesktop::OnDisconnected, service_)); |
| 274 } |
| 275 |
253 void GCMDriverDesktop::IOWorker::Start( | 276 void GCMDriverDesktop::IOWorker::Start( |
254 const base::WeakPtr<GCMDriverDesktop>& service) { | 277 const base::WeakPtr<GCMDriverDesktop>& service) { |
255 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 278 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
256 | 279 |
257 service_ = service; | 280 service_ = service; |
258 gcm_client_->Start(); | 281 gcm_client_->Start(); |
259 } | 282 } |
260 | 283 |
261 void GCMDriverDesktop::IOWorker::Stop() { | 284 void GCMDriverDesktop::IOWorker::Stop() { |
262 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 285 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 scoped_ptr<GCMClientFactory> gcm_client_factory, | 353 scoped_ptr<GCMClientFactory> gcm_client_factory, |
331 scoped_ptr<IdentityProvider> identity_provider, | 354 scoped_ptr<IdentityProvider> identity_provider, |
332 const GCMClient::ChromeBuildInfo& chrome_build_info, | 355 const GCMClient::ChromeBuildInfo& chrome_build_info, |
333 const base::FilePath& store_path, | 356 const base::FilePath& store_path, |
334 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 357 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
335 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 358 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
336 const scoped_refptr<base::SequencedTaskRunner>& io_thread, | 359 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
337 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) | 360 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
338 : gcm_enabled_(true), | 361 : gcm_enabled_(true), |
339 gcm_client_ready_(false), | 362 gcm_client_ready_(false), |
| 363 connected_(false), |
340 identity_provider_(identity_provider.Pass()), | 364 identity_provider_(identity_provider.Pass()), |
341 ui_thread_(ui_thread), | 365 ui_thread_(ui_thread), |
342 io_thread_(io_thread), | 366 io_thread_(io_thread), |
343 weak_ptr_factory_(this) { | 367 weak_ptr_factory_(this) { |
344 // Get the list of available accounts. | 368 // Get the list of available accounts. |
345 std::vector<std::string> account_ids; | 369 std::vector<std::string> account_ids; |
346 account_ids = identity_provider_->GetTokenService()->GetAccounts(); | 370 account_ids = identity_provider_->GetTokenService()->GetAccounts(); |
347 | 371 |
348 // Create and initialize the GCMClient. Note that this does not initiate the | 372 // Create and initialize the GCMClient. Note that this does not initiate the |
349 // GCM check-in. | 373 // GCM check-in. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 bool GCMDriverDesktop::IsStarted() const { | 551 bool GCMDriverDesktop::IsStarted() const { |
528 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 552 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
529 return !account_id_.empty(); | 553 return !account_id_.empty(); |
530 } | 554 } |
531 | 555 |
532 bool GCMDriverDesktop::IsGCMClientReady() const { | 556 bool GCMDriverDesktop::IsGCMClientReady() const { |
533 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 557 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
534 return gcm_client_ready_; | 558 return gcm_client_ready_; |
535 } | 559 } |
536 | 560 |
| 561 bool GCMDriverDesktop::IsConnected() const { |
| 562 return connected_; |
| 563 } |
| 564 |
537 void GCMDriverDesktop::GetGCMStatistics( | 565 void GCMDriverDesktop::GetGCMStatistics( |
538 const GetGCMStatisticsCallback& callback, | 566 const GetGCMStatisticsCallback& callback, |
539 bool clear_logs) { | 567 bool clear_logs) { |
540 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 568 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
541 DCHECK(!callback.is_null()); | 569 DCHECK(!callback.is_null()); |
542 | 570 |
543 request_gcm_statistics_callback_ = callback; | 571 request_gcm_statistics_callback_ = callback; |
544 io_thread_->PostTask( | 572 io_thread_->PostTask( |
545 FROM_HERE, | 573 FROM_HERE, |
546 base::Bind(&GCMDriverDesktop::IOWorker::GetGCMStatistics, | 574 base::Bind(&GCMDriverDesktop::IOWorker::GetGCMStatistics, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 void GCMDriverDesktop::GCMClientReady() { | 688 void GCMDriverDesktop::GCMClientReady() { |
661 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 689 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
662 | 690 |
663 if (gcm_client_ready_) | 691 if (gcm_client_ready_) |
664 return; | 692 return; |
665 gcm_client_ready_ = true; | 693 gcm_client_ready_ = true; |
666 | 694 |
667 delayed_task_controller_->SetReady(); | 695 delayed_task_controller_->SetReady(); |
668 } | 696 } |
669 | 697 |
| 698 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { |
| 699 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 700 |
| 701 connected_ = true; |
| 702 |
| 703 // Drop the event if signed out. |
| 704 if (account_id_.empty()) |
| 705 return; |
| 706 |
| 707 const GCMAppHandlerMap& app_handler_map = app_handlers(); |
| 708 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin(); |
| 709 iter != app_handler_map.end(); ++iter) { |
| 710 iter->second->OnConnected(ip_endpoint); |
| 711 } |
| 712 |
| 713 GetAppHandler(kDefaultAppHandler)->OnConnected(ip_endpoint); |
| 714 } |
| 715 |
| 716 void GCMDriverDesktop::OnDisconnected() { |
| 717 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 718 |
| 719 connected_ = false; |
| 720 |
| 721 // Drop the event if signed out. |
| 722 if (account_id_.empty()) |
| 723 return; |
| 724 |
| 725 const GCMAppHandlerMap& app_handler_map = app_handlers(); |
| 726 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin(); |
| 727 iter != app_handler_map.end(); ++iter) { |
| 728 iter->second->OnDisconnected(); |
| 729 } |
| 730 |
| 731 GetAppHandler(kDefaultAppHandler)->OnDisconnected(); |
| 732 } |
| 733 |
670 void GCMDriverDesktop::GetGCMStatisticsFinished( | 734 void GCMDriverDesktop::GetGCMStatisticsFinished( |
671 const GCMClient::GCMStatistics& stats) { | 735 const GCMClient::GCMStatistics& stats) { |
672 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 736 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
673 | 737 |
674 // Normally request_gcm_statistics_callback_ would not be null. | 738 // Normally request_gcm_statistics_callback_ would not be null. |
675 if (!request_gcm_statistics_callback_.is_null()) | 739 if (!request_gcm_statistics_callback_.is_null()) |
676 request_gcm_statistics_callback_.Run(stats); | 740 request_gcm_statistics_callback_.Run(stats); |
677 else | 741 else |
678 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 742 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
679 } | 743 } |
680 | 744 |
681 std::string GCMDriverDesktop::SignedInUserName() const { | 745 std::string GCMDriverDesktop::SignedInUserName() const { |
682 if (IsStarted()) | 746 if (IsStarted()) |
683 return identity_provider_->GetActiveUsername(); | 747 return identity_provider_->GetActiveUsername(); |
684 return std::string(); | 748 return std::string(); |
685 } | 749 } |
686 | 750 |
687 } // namespace gcm | 751 } // namespace gcm |
| 752 |
OLD | NEW |