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

Side by Side Diff: chrome/browser/services/gcm/push_messaging_service_impl.cc

Issue 905713002: Push API: Fix registration failing because app handler is missing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@countgrace
Patch Set: Avoid decreasing too often Created 5 years, 10 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
« no previous file with comments | « chrome/browser/services/gcm/push_messaging_service_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/services/gcm/push_messaging_service_impl.h" 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h"
6 6
7 #include <bitset> 7 #include <bitset>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 if (count <= 0) 102 if (count <= 0)
103 return; 103 return;
104 104
105 // Create the GCMProfileService, and hence instantiate this class. 105 // Create the GCMProfileService, and hence instantiate this class.
106 GCMProfileService* gcm_service = 106 GCMProfileService* gcm_service =
107 GCMProfileServiceFactory::GetForProfile(profile); 107 GCMProfileServiceFactory::GetForProfile(profile);
108 PushMessagingServiceImpl* push_service = 108 PushMessagingServiceImpl* push_service =
109 static_cast<PushMessagingServiceImpl*>( 109 static_cast<PushMessagingServiceImpl*>(
110 gcm_service->push_messaging_service()); 110 gcm_service->push_messaging_service());
111 111
112 push_service->IncreasePushRegistrationCount(count); 112 push_service->IncreasePushRegistrationCount(count, false /* is_pending */);
113 } 113 }
114 114
115 PushMessagingServiceImpl::PushMessagingServiceImpl( 115 PushMessagingServiceImpl::PushMessagingServiceImpl(
116 GCMProfileService* gcm_profile_service, 116 GCMProfileService* gcm_profile_service,
117 Profile* profile) 117 Profile* profile)
118 : gcm_profile_service_(gcm_profile_service), 118 : gcm_profile_service_(gcm_profile_service),
119 profile_(profile), 119 profile_(profile),
120 push_registration_count_(0), 120 push_registration_count_(0),
121 pending_push_registration_count_(0),
121 weak_factory_(this) { 122 weak_factory_(this) {
122 } 123 }
123 124
124 PushMessagingServiceImpl::~PushMessagingServiceImpl() { 125 PushMessagingServiceImpl::~PushMessagingServiceImpl() {
125 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, 126 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver,
126 // then we should call RemoveAppHandler. 127 // then we should call RemoveAppHandler.
127 } 128 }
128 129
129 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add) { 130 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add,
131 bool is_pending) {
130 DCHECK(add > 0); 132 DCHECK(add > 0);
131 if (push_registration_count_ == 0) { 133 if (push_registration_count_ + pending_push_registration_count_ == 0) {
132 gcm_profile_service_->driver()->AddAppHandler( 134 gcm_profile_service_->driver()->AddAppHandler(
133 kPushMessagingApplicationIdPrefix, this); 135 kPushMessagingApplicationIdPrefix, this);
134 } 136 }
135 push_registration_count_ += add; 137 if (is_pending) {
136 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, 138 pending_push_registration_count_ += add;
137 push_registration_count_); 139 } else {
140 push_registration_count_ += add;
141 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount,
142 push_registration_count_);
143 }
138 } 144 }
139 145
140 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract) { 146 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract,
147 bool was_pending) {
141 DCHECK(subtract > 0); 148 DCHECK(subtract > 0);
142 push_registration_count_ -= subtract; 149 if (was_pending) {
143 DCHECK(push_registration_count_ >= 0); 150 pending_push_registration_count_ -= subtract;
144 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, 151 DCHECK(pending_push_registration_count_ >= 0);
145 push_registration_count_); 152 } else {
146 if (push_registration_count_ == 0) { 153 push_registration_count_ -= subtract;
154 DCHECK(push_registration_count_ >= 0);
155 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount,
156 push_registration_count_);
157 }
158 if (push_registration_count_ + pending_push_registration_count_ == 0) {
147 gcm_profile_service_->driver()->RemoveAppHandler( 159 gcm_profile_service_->driver()->RemoveAppHandler(
148 kPushMessagingApplicationIdPrefix); 160 kPushMessagingApplicationIdPrefix);
149 } 161 }
150 } 162 }
151 163
152 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { 164 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const {
153 return PushMessagingApplicationId::Parse(app_id).IsValid(); 165 return PushMessagingApplicationId::Parse(app_id).IsValid();
154 } 166 }
155 167
156 void PushMessagingServiceImpl::ShutdownHandler() { 168 void PushMessagingServiceImpl::ShutdownHandler() {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 const content::PushMessagingService::RegisterCallback& callback) { 428 const content::PushMessagingService::RegisterCallback& callback) {
417 if (!gcm_profile_service_->driver()) { 429 if (!gcm_profile_service_->driver()) {
418 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; 430 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?";
419 return; 431 return;
420 } 432 }
421 433
422 PushMessagingApplicationId application_id = PushMessagingApplicationId( 434 PushMessagingApplicationId application_id = PushMessagingApplicationId(
423 requesting_origin, service_worker_registration_id); 435 requesting_origin, service_worker_registration_id);
424 DCHECK(application_id.IsValid()); 436 DCHECK(application_id.IsValid());
425 437
426 if (push_registration_count_ >= kMaxRegistrations) { 438 if (push_registration_count_ + pending_push_registration_count_
439 >= kMaxRegistrations) {
427 RegisterEnd(callback, 440 RegisterEnd(callback,
428 std::string(), 441 std::string(),
429 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); 442 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED);
430 return; 443 return;
431 } 444 }
432 445
433 content::RenderFrameHost* render_frame_host = 446 content::RenderFrameHost* render_frame_host =
434 content::RenderFrameHost::FromID(renderer_id, render_frame_id); 447 content::RenderFrameHost::FromID(renderer_id, render_frame_id);
435 if (!render_frame_host) 448 if (!render_frame_host)
436 return; 449 return;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 GURL embedding_origin = requesting_origin; 506 GURL embedding_origin = requesting_origin;
494 blink::WebPushPermissionStatus permission_status = 507 blink::WebPushPermissionStatus permission_status =
495 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, 508 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin,
496 embedding_origin); 509 embedding_origin);
497 if (permission_status != blink::WebPushPermissionStatusGranted) { 510 if (permission_status != blink::WebPushPermissionStatusGranted) {
498 RegisterEnd(register_callback, std::string(), 511 RegisterEnd(register_callback, std::string(),
499 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); 512 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED);
500 return; 513 return;
501 } 514 }
502 515
516 IncreasePushRegistrationCount(1, true /* is_pending */);
503 std::vector<std::string> sender_ids(1, sender_id); 517 std::vector<std::string> sender_ids(1, sender_id);
504 gcm_profile_service_->driver()->Register( 518 gcm_profile_service_->driver()->Register(
505 application_id.ToString(), sender_ids, 519 application_id.ToString(), sender_ids,
506 base::Bind(&PushMessagingServiceImpl::DidRegister, 520 base::Bind(&PushMessagingServiceImpl::DidRegister,
507 weak_factory_.GetWeakPtr(), register_callback)); 521 weak_factory_.GetWeakPtr(), register_callback));
508 } 522 }
509 523
510 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( 524 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus(
511 const GURL& requesting_origin, 525 const GURL& requesting_origin,
512 const GURL& embedding_origin) { 526 const GURL& embedding_origin) {
513 PushMessagingPermissionContext* permission_context = 527 PushMessagingPermissionContext* permission_context =
514 PushMessagingPermissionContextFactory::GetForProfile(profile_); 528 PushMessagingPermissionContextFactory::GetForProfile(profile_);
515 return ToPushPermission(permission_context->GetPermissionStatus( 529 return ToPushPermission(permission_context->GetPermissionStatus(
516 requesting_origin, embedding_origin)); 530 requesting_origin, embedding_origin));
517 } 531 }
518 532
519 void PushMessagingServiceImpl::RegisterEnd( 533 void PushMessagingServiceImpl::RegisterEnd(
520 const content::PushMessagingService::RegisterCallback& callback, 534 const content::PushMessagingService::RegisterCallback& callback,
521 const std::string& registration_id, 535 const std::string& registration_id,
522 content::PushRegistrationStatus status) { 536 content::PushRegistrationStatus status) {
523 callback.Run(registration_id, status); 537 callback.Run(registration_id, status);
524 if (status == content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE)
525 IncreasePushRegistrationCount(1);
526 } 538 }
527 539
528 void PushMessagingServiceImpl::DidRegister( 540 void PushMessagingServiceImpl::DidRegister(
529 const content::PushMessagingService::RegisterCallback& callback, 541 const content::PushMessagingService::RegisterCallback& callback,
530 const std::string& registration_id, 542 const std::string& registration_id,
531 GCMClient::Result result) { 543 GCMClient::Result result) {
532 content::PushRegistrationStatus status = 544 content::PushRegistrationStatus status =
533 result == GCMClient::SUCCESS 545 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR;
534 ? content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE 546 if (result == GCMClient::SUCCESS) {
535 : content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; 547 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE;
548 IncreasePushRegistrationCount(1, false /* is_pending */);
549 }
536 RegisterEnd(callback, registration_id, status); 550 RegisterEnd(callback, registration_id, status);
551 DecreasePushRegistrationCount(1, true /* was_pending */);
537 } 552 }
538 553
539 void PushMessagingServiceImpl::DidRequestPermission( 554 void PushMessagingServiceImpl::DidRequestPermission(
540 const PushMessagingApplicationId& application_id, 555 const PushMessagingApplicationId& application_id,
541 const std::string& sender_id, 556 const std::string& sender_id,
542 const content::PushMessagingService::RegisterCallback& register_callback, 557 const content::PushMessagingService::RegisterCallback& register_callback,
543 bool allow) { 558 bool allow) {
544 if (!allow) { 559 if (!allow) {
545 RegisterEnd(register_callback, 560 RegisterEnd(register_callback,
546 std::string(), 561 std::string(),
547 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); 562 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED);
548 return; 563 return;
549 } 564 }
550 565
551 // The GCMDriver could be NULL if GCMProfileService has been shut down. 566 // The GCMDriver could be NULL if GCMProfileService has been shut down.
552 if (!gcm_profile_service_->driver()) 567 if (!gcm_profile_service_->driver())
553 return; 568 return;
554 569
570 IncreasePushRegistrationCount(1, true /* is_pending */);
555 std::vector<std::string> sender_ids(1, sender_id); 571 std::vector<std::string> sender_ids(1, sender_id);
556
557 gcm_profile_service_->driver()->Register( 572 gcm_profile_service_->driver()->Register(
558 application_id.ToString(), 573 application_id.ToString(),
559 sender_ids, 574 sender_ids,
560 base::Bind(&PushMessagingServiceImpl::DidRegister, 575 base::Bind(&PushMessagingServiceImpl::DidRegister,
561 weak_factory_.GetWeakPtr(), 576 weak_factory_.GetWeakPtr(),
562 register_callback)); 577 register_callback));
563 } 578 }
564 579
565 void PushMessagingServiceImpl::Unregister( 580 void PushMessagingServiceImpl::Unregister(
566 const GURL& requesting_origin, 581 const GURL& requesting_origin,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR); 622 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR);
608 break; 623 break;
609 default: 624 default:
610 NOTREACHED() << "Unexpected GCMClient::Result value."; 625 NOTREACHED() << "Unexpected GCMClient::Result value.";
611 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR); 626 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR);
612 break; 627 break;
613 } 628 }
614 } 629 }
615 630
616 if (result == GCMClient::SUCCESS) 631 if (result == GCMClient::SUCCESS)
617 DecreasePushRegistrationCount(1); 632 DecreasePushRegistrationCount(1, false /* was_pending */);
618 } 633 }
619 634
620 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { 635 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) {
621 gcm::PushMessagingPermissionContext* permission_context = 636 gcm::PushMessagingPermissionContext* permission_context =
622 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); 637 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_);
623 DCHECK(permission_context); 638 DCHECK(permission_context);
624 639
625 return permission_context->GetPermissionStatus(origin, origin) == 640 return permission_context->GetPermissionStatus(origin, origin) ==
626 CONTENT_SETTING_ALLOW; 641 CONTENT_SETTING_ALLOW;
627 } 642 }
628 643
629 } // namespace gcm 644 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/push_messaging_service_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698