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

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

Issue 320993003: [GCM] Add app handler support for connection events (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add basic tests Created 6 years, 6 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 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
24 // 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.
25 class GCMDriverDesktop::DelayedTaskController { 26 class GCMDriverDesktop::DelayedTaskController {
26 public: 27 public:
27 DelayedTaskController(); 28 DelayedTaskController();
28 ~DelayedTaskController(); 29 ~DelayedTaskController();
29 30
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 GCMClient::Result result) OVERRIDE; 97 GCMClient::Result result) OVERRIDE;
97 virtual void OnMessageReceived( 98 virtual void OnMessageReceived(
98 const std::string& app_id, 99 const std::string& app_id,
99 const GCMClient::IncomingMessage& message) OVERRIDE; 100 const GCMClient::IncomingMessage& message) OVERRIDE;
100 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; 101 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
101 virtual void OnMessageSendError( 102 virtual void OnMessageSendError(
102 const std::string& app_id, 103 const std::string& app_id,
103 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; 104 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
104 virtual void OnGCMReady() OVERRIDE; 105 virtual void OnGCMReady() OVERRIDE;
105 virtual void OnActivityRecorded() OVERRIDE; 106 virtual void OnActivityRecorded() OVERRIDE;
107 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE;
108 virtual void OnDisconnected() OVERRIDE;
106 109
107 // Called on IO thread. 110 // Called on IO thread.
108 void Initialize( 111 void Initialize(
109 scoped_ptr<GCMClientFactory> gcm_client_factory, 112 scoped_ptr<GCMClientFactory> gcm_client_factory,
110 const GCMClient::ChromeBuildInfo& chrome_build_info, 113 const GCMClient::ChromeBuildInfo& chrome_build_info,
111 const base::FilePath& store_path, 114 const base::FilePath& store_path,
112 const std::vector<std::string>& account_ids, 115 const std::vector<std::string>& account_ids,
113 const scoped_refptr<net::URLRequestContextGetter>& request_context, 116 const scoped_refptr<net::URLRequestContextGetter>& request_context,
114 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); 117 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
115 void Start(const base::WeakPtr<GCMDriverDesktop>& service); 118 void Start(const base::WeakPtr<GCMDriverDesktop>& service);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 base::Bind(&GCMDriverDesktop::GCMClientReady, service_)); 246 base::Bind(&GCMDriverDesktop::GCMClientReady, service_));
244 } 247 }
245 248
246 void GCMDriverDesktop::IOWorker::OnActivityRecorded() { 249 void GCMDriverDesktop::IOWorker::OnActivityRecorded() {
247 DCHECK(io_thread_->RunsTasksOnCurrentThread()); 250 DCHECK(io_thread_->RunsTasksOnCurrentThread());
248 // When an activity is recorded, get all the stats and refresh the UI of 251 // When an activity is recorded, get all the stats and refresh the UI of
249 // gcm-internals page. 252 // gcm-internals page.
250 GetGCMStatistics(false); 253 GetGCMStatistics(false);
251 } 254 }
252 255
256 void GCMDriverDesktop::IOWorker::OnConnected(
257 const net::IPEndPoint& ip_endpoint) {
258 ui_thread_->PostTask(FROM_HERE,
259 base::Bind(&GCMDriverDesktop::OnConnected,
260 service_,
261 ip_endpoint));
262 }
263
264 void GCMDriverDesktop::IOWorker::OnDisconnected() {
265 ui_thread_->PostTask(FROM_HERE,
266 base::Bind(&GCMDriverDesktop::OnDisconnected, service_));
267 }
268
253 void GCMDriverDesktop::IOWorker::Start( 269 void GCMDriverDesktop::IOWorker::Start(
254 const base::WeakPtr<GCMDriverDesktop>& service) { 270 const base::WeakPtr<GCMDriverDesktop>& service) {
255 DCHECK(io_thread_->RunsTasksOnCurrentThread()); 271 DCHECK(io_thread_->RunsTasksOnCurrentThread());
256 272
257 service_ = service; 273 service_ = service;
258 gcm_client_->Start(); 274 gcm_client_->Start();
259 } 275 }
260 276
261 void GCMDriverDesktop::IOWorker::Stop() { 277 void GCMDriverDesktop::IOWorker::Stop() {
262 DCHECK(io_thread_->RunsTasksOnCurrentThread()); 278 DCHECK(io_thread_->RunsTasksOnCurrentThread());
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 scoped_ptr<GCMClientFactory> gcm_client_factory, 346 scoped_ptr<GCMClientFactory> gcm_client_factory,
331 scoped_ptr<IdentityProvider> identity_provider, 347 scoped_ptr<IdentityProvider> identity_provider,
332 const GCMClient::ChromeBuildInfo& chrome_build_info, 348 const GCMClient::ChromeBuildInfo& chrome_build_info,
333 const base::FilePath& store_path, 349 const base::FilePath& store_path,
334 const scoped_refptr<net::URLRequestContextGetter>& request_context, 350 const scoped_refptr<net::URLRequestContextGetter>& request_context,
335 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 351 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
336 const scoped_refptr<base::SequencedTaskRunner>& io_thread, 352 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
337 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) 353 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner)
338 : gcm_enabled_(true), 354 : gcm_enabled_(true),
339 gcm_client_ready_(false), 355 gcm_client_ready_(false),
356 connected_(false),
340 identity_provider_(identity_provider.Pass()), 357 identity_provider_(identity_provider.Pass()),
341 ui_thread_(ui_thread), 358 ui_thread_(ui_thread),
342 io_thread_(io_thread), 359 io_thread_(io_thread),
343 weak_ptr_factory_(this) { 360 weak_ptr_factory_(this) {
344 // Get the list of available accounts. 361 // Get the list of available accounts.
345 std::vector<std::string> account_ids; 362 std::vector<std::string> account_ids;
346 account_ids = identity_provider_->GetTokenService()->GetAccounts(); 363 account_ids = identity_provider_->GetTokenService()->GetAccounts();
347 364
348 // Create and initialize the GCMClient. Note that this does not initiate the 365 // Create and initialize the GCMClient. Note that this does not initiate the
349 // GCM check-in. 366 // GCM check-in.
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 bool GCMDriverDesktop::IsStarted() const { 544 bool GCMDriverDesktop::IsStarted() const {
528 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 545 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
529 return !account_id_.empty(); 546 return !account_id_.empty();
530 } 547 }
531 548
532 bool GCMDriverDesktop::IsGCMClientReady() const { 549 bool GCMDriverDesktop::IsGCMClientReady() const {
533 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 550 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
534 return gcm_client_ready_; 551 return gcm_client_ready_;
535 } 552 }
536 553
554 bool GCMDriverDesktop::IsConnected() const {
555 return connected_;
556 }
557
537 void GCMDriverDesktop::GetGCMStatistics( 558 void GCMDriverDesktop::GetGCMStatistics(
538 const GetGCMStatisticsCallback& callback, 559 const GetGCMStatisticsCallback& callback,
539 bool clear_logs) { 560 bool clear_logs) {
540 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 561 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
541 DCHECK(!callback.is_null()); 562 DCHECK(!callback.is_null());
542 563
543 request_gcm_statistics_callback_ = callback; 564 request_gcm_statistics_callback_ = callback;
544 io_thread_->PostTask( 565 io_thread_->PostTask(
545 FROM_HERE, 566 FROM_HERE,
546 base::Bind(&GCMDriverDesktop::IOWorker::GetGCMStatistics, 567 base::Bind(&GCMDriverDesktop::IOWorker::GetGCMStatistics,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 void GCMDriverDesktop::GCMClientReady() { 681 void GCMDriverDesktop::GCMClientReady() {
661 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 682 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
662 683
663 if (gcm_client_ready_) 684 if (gcm_client_ready_)
664 return; 685 return;
665 gcm_client_ready_ = true; 686 gcm_client_ready_ = true;
666 687
667 delayed_task_controller_->SetReady(); 688 delayed_task_controller_->SetReady();
668 } 689 }
669 690
691 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) {
692 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
693
694 connected_ = true;
695
696 // Drop the event if signed out.
697 if (account_id_.empty())
698 return;
699
700 const GCMAppHandlerMap& app_handler_map = app_handlers();
701 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin();
702 iter != app_handler_map.end(); ++iter) {
703 iter->second->OnConnected(ip_endpoint);
704 }
705
706 // Inform the default app handler.
707 GetAppHandler("")->OnConnected(ip_endpoint);
fgorski 2014/06/09 23:19:43 nit: could we add a constant for a default app han
Nicolas Zea 2014/06/09 23:28:25 Done.
708 }
709
710 void GCMDriverDesktop::OnDisconnected() {
711 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
712
713 connected_ = false;
714
715 // Drop the event if signed out.
716 if (account_id_.empty())
717 return;
718
719 const GCMAppHandlerMap& app_handler_map = app_handlers();
720 for (GCMAppHandlerMap::const_iterator iter = app_handler_map.begin();
721 iter != app_handler_map.end(); ++iter) {
722 iter->second->OnDisconnected();
723 }
724
725 // Inform the default app handler.
726 GetAppHandler("")->OnDisconnected();
727 }
728
670 void GCMDriverDesktop::GetGCMStatisticsFinished( 729 void GCMDriverDesktop::GetGCMStatisticsFinished(
671 const GCMClient::GCMStatistics& stats) { 730 const GCMClient::GCMStatistics& stats) {
672 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); 731 DCHECK(ui_thread_->RunsTasksOnCurrentThread());
673 732
674 // Normally request_gcm_statistics_callback_ would not be null. 733 // Normally request_gcm_statistics_callback_ would not be null.
675 if (!request_gcm_statistics_callback_.is_null()) 734 if (!request_gcm_statistics_callback_.is_null())
676 request_gcm_statistics_callback_.Run(stats); 735 request_gcm_statistics_callback_.Run(stats);
677 else 736 else
678 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; 737 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL.";
679 } 738 }
680 739
681 std::string GCMDriverDesktop::SignedInUserName() const { 740 std::string GCMDriverDesktop::SignedInUserName() const {
682 if (IsStarted()) 741 if (IsStarted())
683 return identity_provider_->GetActiveUsername(); 742 return identity_provider_->GetActiveUsername();
684 return std::string(); 743 return std::string();
685 } 744 }
686 745
687 } // namespace gcm 746 } // namespace gcm
747
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698