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

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

Issue 793403002: Implement WebPushProvider.unregister() in Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mvan_2
Patch Set: jochen review comments Created 6 years 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
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 <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // } 151 // }
152 // TODO(johnme): Make sure this is clearly documented for developers. 152 // TODO(johnme): Make sure this is clearly documented for developers.
153 PushMessagingApplicationId application_id = 153 PushMessagingApplicationId application_id =
154 PushMessagingApplicationId::Parse(app_id); 154 PushMessagingApplicationId::Parse(app_id);
155 DCHECK(application_id.IsValid()); 155 DCHECK(application_id.IsValid());
156 GCMClient::MessageData::const_iterator it = message.data.find("data"); 156 GCMClient::MessageData::const_iterator it = message.data.find("data");
157 if (application_id.IsValid() && it != message.data.end()) { 157 if (application_id.IsValid() && it != message.data.end()) {
158 if (!HasPermission(application_id.origin)) { 158 if (!HasPermission(application_id.origin)) {
159 // The |origin| lost push permission. We need to unregister and drop this 159 // The |origin| lost push permission. We need to unregister and drop this
160 // message. 160 // message.
161 Unregister(application_id); 161 Unregister(application_id, UnregisterCallback());
162 return; 162 return;
163 } 163 }
164 164
165 const std::string& data = it->second; 165 const std::string& data = it->second;
166 content::BrowserContext::DeliverPushMessage( 166 content::BrowserContext::DeliverPushMessage(
167 profile_, 167 profile_,
168 application_id.origin, 168 application_id.origin,
169 application_id.service_worker_registration_id, 169 application_id.service_worker_registration_id,
170 data, 170 data,
171 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, 171 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback,
(...skipping 18 matching lines...) Expand all
190 const GCMClient::IncomingMessage& message, 190 const GCMClient::IncomingMessage& message,
191 content::PushDeliveryStatus status) { 191 content::PushDeliveryStatus status) {
192 // TODO(mvanouwerkerk): UMA logging. 192 // TODO(mvanouwerkerk): UMA logging.
193 // TODO(mvanouwerkerk): Is there a way to recover from failure? 193 // TODO(mvanouwerkerk): Is there a way to recover from failure?
194 switch (status) { 194 switch (status) {
195 case content::PUSH_DELIVERY_STATUS_SUCCESS: 195 case content::PUSH_DELIVERY_STATUS_SUCCESS:
196 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: 196 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR:
197 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: 197 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED:
198 break; 198 break;
199 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: 199 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER:
200 Unregister(application_id); 200 Unregister(application_id, UnregisterCallback());
201 break; 201 break;
202 } 202 }
203 } 203 }
204 204
205 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { 205 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) {
206 // TODO(mvanouwerkerk): Fire push error event on the Service Worker 206 // TODO(mvanouwerkerk): Fire push error event on the Service Worker
207 // corresponding to app_id. 207 // corresponding to app_id.
208 } 208 }
209 209
210 void PushMessagingServiceImpl::OnSendError( 210 void PushMessagingServiceImpl::OnSendError(
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 407
408 gcm_profile_service_->driver()->Register( 408 gcm_profile_service_->driver()->Register(
409 application_id.ToString(), 409 application_id.ToString(),
410 sender_ids, 410 sender_ids,
411 base::Bind(&PushMessagingServiceImpl::DidRegister, 411 base::Bind(&PushMessagingServiceImpl::DidRegister,
412 weak_factory_.GetWeakPtr(), 412 weak_factory_.GetWeakPtr(),
413 register_callback)); 413 register_callback));
414 } 414 }
415 415
416 void PushMessagingServiceImpl::Unregister( 416 void PushMessagingServiceImpl::Unregister(
417 const PushMessagingApplicationId& application_id) { 417 const GURL& requesting_origin,
418 int64 service_worker_registration_id,
419 const content::PushMessagingService::UnregisterCallback& callback) {
420 DCHECK(gcm_profile_service_->driver());
421
422 PushMessagingApplicationId application_id = PushMessagingApplicationId(
423 requesting_origin, service_worker_registration_id);
424 DCHECK(application_id.IsValid());
425
426 Unregister(application_id, callback);
427 }
428
429 void PushMessagingServiceImpl::Unregister(
430 const PushMessagingApplicationId& application_id,
431 const content::PushMessagingService::UnregisterCallback& callback) {
418 DCHECK(gcm_profile_service_->driver()); 432 DCHECK(gcm_profile_service_->driver());
419 433
420 gcm_profile_service_->driver()->Unregister( 434 gcm_profile_service_->driver()->Unregister(
421 application_id.ToString(), 435 application_id.ToString(),
422 base::Bind(&PushMessagingServiceImpl::DidUnregister, 436 base::Bind(&PushMessagingServiceImpl::DidUnregister,
423 weak_factory_.GetWeakPtr())); 437 weak_factory_.GetWeakPtr(), callback));
424 } 438 }
425 439
426 void PushMessagingServiceImpl::DidUnregister(GCMClient::Result result) { 440 void PushMessagingServiceImpl::DidUnregister(
427 if (result != GCMClient::SUCCESS) { 441 const content::PushMessagingService::UnregisterCallback& callback,
428 DVLOG(1) << "GCM unregistration failed."; 442 GCMClient::Result result) {
429 return; 443 // Internal calls pass a null callback.
444 if (!callback.is_null()) {
445 switch (result) {
446 case GCMClient::SUCCESS:
447 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTER);
448 break;
449 case GCMClient::NETWORK_ERROR:
450 case GCMClient::TTL_EXCEEDED:
451 callback.Run(content::PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR);
452 break;
453 case GCMClient::SERVER_ERROR:
454 case GCMClient::INVALID_PARAMETER:
455 case GCMClient::GCM_DISABLED:
456 case GCMClient::NOT_SIGNED_IN:
457 case GCMClient::ASYNC_OPERATION_PENDING:
458 case GCMClient::UNKNOWN_ERROR:
459 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR);
460 break;
461 default:
462 NOTREACHED() << "Unexpected GCMClient::Result value.";
463 callback.Run(content::PUSH_UNREGISTRATION_STATUS_UNKNOWN_ERROR);
464 break;
465 }
430 } 466 }
431 467
432 DecreasePushRegistrationCount(1); 468 if (result == GCMClient::SUCCESS)
469 DecreasePushRegistrationCount(1);
433 } 470 }
434 471
435 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { 472 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) {
436 gcm::PushMessagingPermissionContext* permission_context = 473 gcm::PushMessagingPermissionContext* permission_context =
437 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); 474 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_);
438 DCHECK(permission_context); 475 DCHECK(permission_context);
439 476
440 return permission_context->GetPermissionStatus(origin, origin) == 477 return permission_context->GetPermissionStatus(origin, origin) ==
441 CONTENT_SETTING_ALLOW; 478 CONTENT_SETTING_ALLOW;
442 } 479 }
443 480
444 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { 481 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() {
445 if (gcm_profile_service_->driver()->GetAppHandler( 482 if (gcm_profile_service_->driver()->GetAppHandler(
446 kPushMessagingApplicationIdPrefix) != this) 483 kPushMessagingApplicationIdPrefix) != this)
447 gcm_profile_service_->driver()->AddAppHandler( 484 gcm_profile_service_->driver()->AddAppHandler(
448 kPushMessagingApplicationIdPrefix, this); 485 kPushMessagingApplicationIdPrefix, this);
449 } 486 }
450 487
451 } // namespace gcm 488 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/push_messaging_service_impl.h ('k') | chrome/test/data/push_messaging/push_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698