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

Side by Side Diff: chrome/browser/push_messaging/push_messaging_service_impl.cc

Issue 1134733006: Push API: use (un)subscription instead of (un)registration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated Patch Set Created 5 years, 7 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
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/push_messaging/push_messaging_service_impl.h" 5 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // defined in a regular profile are visible in the corresponding incognito 86 // defined in a regular profile are visible in the corresponding incognito
87 // profile unless overridden. 87 // profile unless overridden.
88 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes. 88 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes.
89 int count = profile->GetPrefs()->GetInteger( 89 int count = profile->GetPrefs()->GetInteger(
90 prefs::kPushMessagingRegistrationCount); 90 prefs::kPushMessagingRegistrationCount);
91 if (count <= 0) 91 if (count <= 0)
92 return; 92 return;
93 93
94 PushMessagingServiceImpl* push_service = 94 PushMessagingServiceImpl* push_service =
95 PushMessagingServiceFactory::GetForProfile(profile); 95 PushMessagingServiceFactory::GetForProfile(profile);
96 push_service->IncreasePushRegistrationCount(count, false /* is_pending */); 96 push_service->IncreasePushSubscriptionCount(count, false /* is_pending */);
97 } 97 }
98 98
99 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) 99 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile)
100 : profile_(profile), 100 : profile_(profile),
101 push_registration_count_(0), 101 push_subscription_count_(0),
102 pending_push_registration_count_(0), 102 pending_push_subscription_count_(0),
103 #if defined(ENABLE_NOTIFICATIONS) 103 #if defined(ENABLE_NOTIFICATIONS)
104 notification_manager_(profile), 104 notification_manager_(profile),
105 #endif 105 #endif
106 weak_factory_(this) { 106 weak_factory_(this) {
107 DCHECK(profile); 107 DCHECK(profile);
108 profile_->GetHostContentSettingsMap()->AddObserver(this); 108 profile_->GetHostContentSettingsMap()->AddObserver(this);
109 } 109 }
110 110
111 PushMessagingServiceImpl::~PushMessagingServiceImpl() { 111 PushMessagingServiceImpl::~PushMessagingServiceImpl() {
112 profile_->GetHostContentSettingsMap()->RemoveObserver(this); 112 profile_->GetHostContentSettingsMap()->RemoveObserver(this);
113 } 113 }
114 114
115 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, 115 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add,
116 bool is_pending) { 116 bool is_pending) {
117 DCHECK(add > 0); 117 DCHECK(add > 0);
118 if (push_registration_count_ + pending_push_registration_count_ == 0) { 118 if (push_subscription_count_ + pending_push_subscription_count_ == 0) {
119 GetGCMDriver()->AddAppHandler(kPushMessagingAppIdentifierPrefix, this); 119 GetGCMDriver()->AddAppHandler(kPushMessagingAppIdentifierPrefix, this);
120 } 120 }
121 if (is_pending) { 121 if (is_pending) {
122 pending_push_registration_count_ += add; 122 pending_push_subscription_count_ += add;
123 } else { 123 } else {
124 push_registration_count_ += add; 124 push_subscription_count_ += add;
125 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, 125 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount,
126 push_registration_count_); 126 push_subscription_count_);
127 } 127 }
128 } 128 }
129 129
130 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract, 130 void PushMessagingServiceImpl::DecreasePushSubscriptionCount(int subtract,
131 bool was_pending) { 131 bool was_pending) {
132 DCHECK(subtract > 0); 132 DCHECK(subtract > 0);
133 if (was_pending) { 133 if (was_pending) {
134 pending_push_registration_count_ -= subtract; 134 pending_push_subscription_count_ -= subtract;
135 DCHECK(pending_push_registration_count_ >= 0); 135 DCHECK(pending_push_subscription_count_ >= 0);
136 } else { 136 } else {
137 push_registration_count_ -= subtract; 137 push_subscription_count_ -= subtract;
138 DCHECK(push_registration_count_ >= 0); 138 DCHECK(push_subscription_count_ >= 0);
139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, 139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount,
140 push_registration_count_); 140 push_subscription_count_);
141 } 141 }
142 if (push_registration_count_ + pending_push_registration_count_ == 0) { 142 if (push_subscription_count_ + pending_push_subscription_count_ == 0) {
143 GetGCMDriver()->RemoveAppHandler(kPushMessagingAppIdentifierPrefix); 143 GetGCMDriver()->RemoveAppHandler(kPushMessagingAppIdentifierPrefix);
144 } 144 }
145 } 145 }
146 146
147 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { 147 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const {
148 return !PushMessagingAppIdentifier::FindByAppId(profile_, app_id).is_null(); 148 return !PushMessagingAppIdentifier::FindByAppId(profile_, app_id).is_null();
149 } 149 }
150 150
151 void PushMessagingServiceImpl::ShutdownHandler() { 151 void PushMessagingServiceImpl::ShutdownHandler() {
152 // Shutdown() should come before and it removes us from the list of app 152 // Shutdown() should come before and it removes us from the list of app
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 message_handled_closure.Run(); 246 message_handled_closure.Run();
247 #endif 247 #endif
248 break; 248 break;
249 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: 249 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE:
250 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: 250 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR:
251 message_handled_closure.Run(); 251 message_handled_closure.Run();
252 break; 252 break;
253 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: 253 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID:
254 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: 254 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED:
255 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: 255 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER:
256 Unregister(app_id, message.sender_id, 256 Unsubscribe(app_id, message.sender_id,
257 base::Bind(&UnregisterCallbackToClosure, 257 base::Bind(&UnregisterCallbackToClosure,
258 message_handled_closure)); 258 message_handled_closure));
259 break; 259 break;
260 } 260 }
261 RecordDeliveryStatus(status); 261 RecordDeliveryStatus(status);
262 } 262 }
263 263
264 void PushMessagingServiceImpl::SetMessageCallbackForTesting( 264 void PushMessagingServiceImpl::SetMessageCallbackForTesting(
265 const base::Closure& callback) { 265 const base::Closure& callback) {
266 message_callback_for_testing_ = callback; 266 message_callback_for_testing_ = callback;
(...skipping 17 matching lines...) Expand all
284 const std::string& message_id) { 284 const std::string& message_id) {
285 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; 285 NOTREACHED() << "The Push API shouldn't have sent messages upstream";
286 } 286 }
287 287
288 // GetPushEndpoint method ------------------------------------------------------ 288 // GetPushEndpoint method ------------------------------------------------------
289 289
290 GURL PushMessagingServiceImpl::GetPushEndpoint() { 290 GURL PushMessagingServiceImpl::GetPushEndpoint() {
291 return GURL(std::string(kPushMessagingEndpoint)); 291 return GURL(std::string(kPushMessagingEndpoint));
292 } 292 }
293 293
294 // Register and GetPermissionStatus methods ------------------------------------ 294 // Subscribe and GetPermissionStatus methods ----------------------------------- -
295 295
296 void PushMessagingServiceImpl::RegisterFromDocument( 296 void PushMessagingServiceImpl::SubscribeFromDocument(
297 const GURL& requesting_origin, 297 const GURL& requesting_origin,
298 int64 service_worker_registration_id, 298 int64 service_worker_registration_id,
299 const std::string& sender_id, 299 const std::string& sender_id,
300 int renderer_id, 300 int renderer_id,
301 int render_frame_id, 301 int render_frame_id,
302 bool user_visible, 302 bool user_visible,
303 const content::PushMessagingService::RegisterCallback& callback) { 303 const content::PushMessagingService::RegisterCallback& callback) {
304 PushMessagingAppIdentifier app_identifier = 304 PushMessagingAppIdentifier app_identifier =
305 PushMessagingAppIdentifier::Generate(requesting_origin, 305 PushMessagingAppIdentifier::Generate(requesting_origin,
306 service_worker_registration_id); 306 service_worker_registration_id);
307 307
308 if (push_registration_count_ + pending_push_registration_count_ 308 if (push_subscription_count_ + pending_push_subscription_count_
309 >= kMaxRegistrations) { 309 >= kMaxRegistrations) {
310 RegisterEnd(callback, 310 SubscribeFail(callback,
311 std::string(), 311 std::string(),
312 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); 312 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED);
313 return; 313 return;
314 } 314 }
315 315
316 content::RenderFrameHost* render_frame_host = 316 content::RenderFrameHost* render_frame_host =
317 content::RenderFrameHost::FromID(renderer_id, render_frame_id); 317 content::RenderFrameHost::FromID(renderer_id, render_frame_id);
318 if (!render_frame_host) 318 if (!render_frame_host)
319 return; 319 return;
320 320
321 content::WebContents* web_contents = 321 content::WebContents* web_contents =
322 content::WebContents::FromRenderFrameHost(render_frame_host); 322 content::WebContents::FromRenderFrameHost(render_frame_host);
323 if (!web_contents) 323 if (!web_contents)
324 return; 324 return;
325 325
326 // TODO(miguelg) need to send this over IPC when bubble support is 326 // TODO(miguelg) need to send this over IPC when bubble support is
327 // implemented. 327 // implemented.
328 int bridge_id = -1; 328 int bridge_id = -1;
329 329
330 const PermissionRequestID id( 330 const PermissionRequestID id(
331 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); 331 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL());
332 332
333 PushMessagingPermissionContext* permission_context = 333 PushMessagingPermissionContext* permission_context =
334 PushMessagingPermissionContextFactory::GetForProfile(profile_); 334 PushMessagingPermissionContextFactory::GetForProfile(profile_);
335 335
336 if (permission_context == NULL || !user_visible) { 336 if (permission_context == NULL || !user_visible) {
337 RegisterEnd(callback, 337 SubscribeFail(callback,
338 std::string(), 338 std::string(),
339 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); 339 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED);
340 return; 340 return;
341 } 341 }
342 342
343 // TODO(miguelg): Consider the value of |user_visible| when making the 343 // TODO(miguelg): Consider the value of |user_visible| when making the
344 // permission request. 344 // permission request.
345 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and 345 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and
346 // re-introduce the ability of |user_gesture| when bubbles require this. 346 // re-introduce the ability of |user_gesture| when bubbles require this.
347 // https://crbug.com/423770. 347 // https://crbug.com/423770.
348 permission_context->RequestPermission( 348 permission_context->RequestPermission(
349 web_contents, id, requesting_origin, true /* user_gesture */, 349 web_contents, id, requesting_origin, true /* user_gesture */,
350 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, 350 base::Bind(&PushMessagingServiceImpl::DidRequestPermission,
351 weak_factory_.GetWeakPtr(), app_identifier, sender_id, 351 weak_factory_.GetWeakPtr(), app_identifier, sender_id,
352 callback)); 352 callback));
353 } 353 }
354 354
355 void PushMessagingServiceImpl::RegisterFromWorker( 355 void PushMessagingServiceImpl::SubscribeFromWorker(
356 const GURL& requesting_origin, 356 const GURL& requesting_origin,
357 int64 service_worker_registration_id, 357 int64 service_worker_registration_id,
358 const std::string& sender_id, 358 const std::string& sender_id,
359 bool user_visible, 359 bool user_visible,
360 const content::PushMessagingService::RegisterCallback& register_callback) { 360 const content::PushMessagingService::RegisterCallback& register_callback) {
361 PushMessagingAppIdentifier app_identifier = 361 PushMessagingAppIdentifier app_identifier =
362 PushMessagingAppIdentifier::Generate(requesting_origin, 362 PushMessagingAppIdentifier::Generate(requesting_origin,
363 service_worker_registration_id); 363 service_worker_registration_id);
364 364
365 if (profile_->GetPrefs()->GetInteger( 365 if (profile_->GetPrefs()->GetInteger(
366 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { 366 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) {
367 RegisterEnd(register_callback, std::string(), 367 SubscribeFail(register_callback, std::string(),
368 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); 368 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED);
369 return; 369 return;
370 } 370 }
371 371
372 // TODO(peter): Consider |user_visible| when getting the permission status 372 // TODO(peter): Consider |user_visible| when getting the permission status
373 // for registering from a worker. 373 // for registering from a worker.
374 374
375 GURL embedding_origin = requesting_origin; 375 GURL embedding_origin = requesting_origin;
376 blink::WebPushPermissionStatus permission_status = 376 blink::WebPushPermissionStatus permission_status =
377 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, 377 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin,
378 embedding_origin, 378 embedding_origin,
379 user_visible); 379 user_visible);
380 if (permission_status != blink::WebPushPermissionStatusGranted) { 380 if (permission_status != blink::WebPushPermissionStatusGranted) {
381 RegisterEnd(register_callback, std::string(), 381 SubscribeFail(register_callback, std::string(),
382 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); 382 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED);
383 return; 383 return;
384 } 384 }
385 385
386 IncreasePushRegistrationCount(1, true /* is_pending */); 386 IncreasePushSubscriptionCount(1, true /* is_pending */);
387 std::vector<std::string> sender_ids(1, sender_id); 387 std::vector<std::string> sender_ids(1, sender_id);
388 GetGCMDriver()->Register( 388 GetGCMDriver()->Register(
389 app_identifier.app_id(), sender_ids, 389 app_identifier.app_id(), sender_ids,
390 base::Bind(&PushMessagingServiceImpl::DidRegister, 390 base::Bind(&PushMessagingServiceImpl::DidSubscribe,
391 weak_factory_.GetWeakPtr(), 391 weak_factory_.GetWeakPtr(),
392 app_identifier, register_callback)); 392 app_identifier, register_callback));
393 } 393 }
394 394
395 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( 395 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus(
396 const GURL& requesting_origin, 396 const GURL& requesting_origin,
397 const GURL& embedding_origin, 397 const GURL& embedding_origin,
398 bool user_visible) { 398 bool user_visible) {
399 // TODO(peter): Consider |user_visible| when checking Push permission. 399 // TODO(peter): Consider |user_visible| when checking Push permission.
400 400
401 PushMessagingPermissionContext* permission_context = 401 PushMessagingPermissionContext* permission_context =
402 PushMessagingPermissionContextFactory::GetForProfile(profile_); 402 PushMessagingPermissionContextFactory::GetForProfile(profile_);
403 return ToPushPermission(permission_context->GetPermissionStatus( 403 return ToPushPermission(permission_context->GetPermissionStatus(
404 requesting_origin, embedding_origin)); 404 requesting_origin, embedding_origin));
405 } 405 }
406 406
407 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { 407 bool PushMessagingServiceImpl::SupportNonVisibleMessages() {
408 return false; 408 return false;
409 } 409 }
410 410
411 void PushMessagingServiceImpl::RegisterEnd( 411 void PushMessagingServiceImpl::SubscribeFail(
412 const content::PushMessagingService::RegisterCallback& callback, 412 const content::PushMessagingService::RegisterCallback& callback,
413 const std::string& registration_id, 413 const std::string& subscription_id,
414 content::PushRegistrationStatus status) { 414 content::PushRegistrationStatus status) {
415 callback.Run(registration_id, status); 415 callback.Run(subscription_id, status);
416 } 416 }
417 417
418 void PushMessagingServiceImpl::DidRegister( 418 void PushMessagingServiceImpl::DidSubscribe(
419 const PushMessagingAppIdentifier& app_identifier, 419 const PushMessagingAppIdentifier& app_identifier,
420 const content::PushMessagingService::RegisterCallback& callback, 420 const content::PushMessagingService::RegisterCallback& callback,
421 const std::string& registration_id, 421 const std::string& subscription_id,
422 gcm::GCMClient::Result result) { 422 gcm::GCMClient::Result result) {
423 content::PushRegistrationStatus status = 423 content::PushRegistrationStatus status =
424 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; 424 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR;
425 switch (result) { 425 switch (result) {
426 case gcm::GCMClient::SUCCESS: 426 case gcm::GCMClient::SUCCESS:
427 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; 427 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE;
428 app_identifier.PersistToPrefs(profile_); 428 app_identifier.PersistToPrefs(profile_);
429 IncreasePushRegistrationCount(1, false /* is_pending */); 429 IncreasePushSubscriptionCount(1, false /* is_pending */);
430 break; 430 break;
431 case gcm::GCMClient::INVALID_PARAMETER: 431 case gcm::GCMClient::INVALID_PARAMETER:
432 case gcm::GCMClient::GCM_DISABLED: 432 case gcm::GCMClient::GCM_DISABLED:
433 case gcm::GCMClient::ASYNC_OPERATION_PENDING: 433 case gcm::GCMClient::ASYNC_OPERATION_PENDING:
434 case gcm::GCMClient::SERVER_ERROR: 434 case gcm::GCMClient::SERVER_ERROR:
435 case gcm::GCMClient::UNKNOWN_ERROR: 435 case gcm::GCMClient::UNKNOWN_ERROR:
436 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; 436 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR;
437 break; 437 break;
438 case gcm::GCMClient::NETWORK_ERROR: 438 case gcm::GCMClient::NETWORK_ERROR:
439 case gcm::GCMClient::TTL_EXCEEDED: 439 case gcm::GCMClient::TTL_EXCEEDED:
440 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; 440 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR;
441 break; 441 break;
442 } 442 }
443 RegisterEnd(callback, registration_id, status); 443 SubscribeFail(callback, subscription_id, status);
444 DecreasePushRegistrationCount(1, true /* was_pending */); 444 DecreasePushSubscriptionCount(1, true /* was_pending */);
445 } 445 }
446 446
447 void PushMessagingServiceImpl::DidRequestPermission( 447 void PushMessagingServiceImpl::DidRequestPermission(
448 const PushMessagingAppIdentifier& app_identifier, 448 const PushMessagingAppIdentifier& app_identifier,
449 const std::string& sender_id, 449 const std::string& sender_id,
450 const content::PushMessagingService::RegisterCallback& register_callback, 450 const content::PushMessagingService::RegisterCallback& register_callback,
451 ContentSetting content_setting) { 451 ContentSetting content_setting) {
452 if (content_setting != CONTENT_SETTING_ALLOW) { 452 if (content_setting != CONTENT_SETTING_ALLOW) {
453 RegisterEnd(register_callback, 453 SubscribeFail(register_callback,
454 std::string(), 454 std::string(),
455 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); 455 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED);
456 return; 456 return;
457 } 457 }
458 458
459 IncreasePushRegistrationCount(1, true /* is_pending */); 459 IncreasePushSubscriptionCount(1, true /* is_pending */);
460 std::vector<std::string> sender_ids(1, sender_id); 460 std::vector<std::string> sender_ids(1, sender_id);
461 GetGCMDriver()->Register( 461 GetGCMDriver()->Register(
462 app_identifier.app_id(), 462 app_identifier.app_id(),
463 sender_ids, 463 sender_ids,
464 base::Bind(&PushMessagingServiceImpl::DidRegister, 464 base::Bind(&PushMessagingServiceImpl::DidSubscribe,
465 weak_factory_.GetWeakPtr(), 465 weak_factory_.GetWeakPtr(),
466 app_identifier, register_callback)); 466 app_identifier, register_callback));
467 } 467 }
468 468
469 // Unregister methods ---------------------------------------------------------- 469 // Unsubscribe methods --------------------------------------------------------- -
470 470
471 void PushMessagingServiceImpl::Unregister( 471 void PushMessagingServiceImpl::Unsubscribe(
472 const GURL& requesting_origin, 472 const GURL& requesting_origin,
473 int64 service_worker_registration_id, 473 int64 service_worker_registration_id,
474 const std::string& sender_id, 474 const std::string& sender_id,
475 const content::PushMessagingService::UnregisterCallback& callback) { 475 const content::PushMessagingService::UnregisterCallback& callback) {
476 PushMessagingAppIdentifier app_identifier = 476 PushMessagingAppIdentifier app_identifier =
477 PushMessagingAppIdentifier::FindByServiceWorker( 477 PushMessagingAppIdentifier::FindByServiceWorker(
478 profile_, requesting_origin, service_worker_registration_id); 478 profile_, requesting_origin, service_worker_registration_id);
479 if (app_identifier.is_null()) { 479 if (app_identifier.is_null()) {
480 if (!callback.is_null()) { 480 if (!callback.is_null()) {
481 callback.Run( 481 callback.Run(
482 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); 482 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED);
483 } 483 }
484 return; 484 return;
485 } 485 }
486 486
487 Unregister(app_identifier.app_id(), sender_id, callback); 487 Unsubscribe(app_identifier.app_id(), sender_id, callback);
488 } 488 }
489 489
490 void PushMessagingServiceImpl::Unregister( 490 void PushMessagingServiceImpl::Unsubscribe(
491 const std::string& app_id, 491 const std::string& app_id,
492 const std::string& sender_id, 492 const std::string& sender_id,
493 const content::PushMessagingService::UnregisterCallback& callback) { 493 const content::PushMessagingService::UnregisterCallback& callback) {
494 // Delete the mapping for this app_id, to guarantee that no messages get 494 // Delete the mapping for this app_id, to guarantee that no messages get
495 // delivered in future (even if unregistration fails). 495 // delivered in future (even if unregistration fails).
496 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and 496 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and
497 // retry unregistration if it fails due to network errors (crbug.com/465399). 497 // retry unregistration if it fails due to network errors (crbug.com/465399).
498 PushMessagingAppIdentifier app_identifier = 498 PushMessagingAppIdentifier app_identifier =
499 PushMessagingAppIdentifier::FindByAppId(profile_, app_id); 499 PushMessagingAppIdentifier::FindByAppId(profile_, app_id);
500 bool was_registered = !app_identifier.is_null(); 500 bool was_registered = !app_identifier.is_null();
501 if (was_registered) 501 if (was_registered)
502 app_identifier.DeleteFromPrefs(profile_); 502 app_identifier.DeleteFromPrefs(profile_);
503 503
504 const auto& unregister_callback = 504 const auto& unregister_callback =
505 base::Bind(&PushMessagingServiceImpl::DidUnregister, 505 base::Bind(&PushMessagingServiceImpl::DidUnsubscribe,
506 weak_factory_.GetWeakPtr(), 506 weak_factory_.GetWeakPtr(),
507 was_registered, callback); 507 was_registered, callback);
508 #if defined(OS_ANDROID) 508 #if defined(OS_ANDROID)
509 // On Android the backend is different, and requires the original sender_id. 509 // On Android the backend is different, and requires the original sender_id.
510 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. 510 // UnsubscribeBecausePermissionRevoked sometimes calls us with an empty one.
511 if (sender_id.empty()) 511 if (sender_id.empty())
512 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); 512 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER);
513 else 513 else
514 GetGCMDriver()->UnregisterWithSenderId(app_id, sender_id, 514 GetGCMDriver()->UnregisterWithSenderId(app_id, sender_id,
515 unregister_callback); 515 unregister_callback);
516 #else 516 #else
517 GetGCMDriver()->Unregister(app_id, unregister_callback); 517 GetGCMDriver()->Unregister(app_id, unregister_callback);
518 #endif 518 #endif
519 } 519 }
520 520
521 void PushMessagingServiceImpl::DidUnregister( 521 void PushMessagingServiceImpl::DidUnsubscribe(
522 bool was_registered, 522 bool was_subscribed,
523 const content::PushMessagingService::UnregisterCallback& callback, 523 const content::PushMessagingService::UnregisterCallback& callback,
524 gcm::GCMClient::Result result) { 524 gcm::GCMClient::Result result) {
525 if (was_registered) 525 if (was_subscribed)
526 DecreasePushRegistrationCount(1, false /* was_pending */); 526 DecreasePushSubscriptionCount(1, false /* was_pending */);
527 527
528 // Internal calls pass a null callback. 528 // Internal calls pass a null callback.
529 if (callback.is_null()) 529 if (callback.is_null())
530 return; 530 return;
531 531
532 if (!was_registered) { 532 if (!was_subscribed) {
533 callback.Run( 533 callback.Run(
534 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); 534 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED);
535 return; 535 return;
536 } 536 }
537 switch (result) { 537 switch (result) {
538 case gcm::GCMClient::SUCCESS: 538 case gcm::GCMClient::SUCCESS:
539 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); 539 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED);
540 break; 540 break;
541 case gcm::GCMClient::INVALID_PARAMETER: 541 case gcm::GCMClient::INVALID_PARAMETER:
542 case gcm::GCMClient::GCM_DISABLED: 542 case gcm::GCMClient::GCM_DISABLED:
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 587
588 if (HasPermission(app_identifier.origin())) { 588 if (HasPermission(app_identifier.origin())) {
589 barrier_closure.Run(); 589 barrier_closure.Run();
590 continue; 590 continue;
591 } 591 }
592 592
593 GetSenderId( 593 GetSenderId(
594 profile_, app_identifier.origin(), 594 profile_, app_identifier.origin(),
595 app_identifier.service_worker_registration_id(), 595 app_identifier.service_worker_registration_id(),
596 base::Bind( 596 base::Bind(
597 &PushMessagingServiceImpl::UnregisterBecausePermissionRevoked, 597 &PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked,
598 weak_factory_.GetWeakPtr(), app_identifier, barrier_closure)); 598 weak_factory_.GetWeakPtr(), app_identifier, barrier_closure));
599 } 599 }
600 } 600 }
601 601
602 void PushMessagingServiceImpl::UnregisterBecausePermissionRevoked( 602 void PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked(
603 const PushMessagingAppIdentifier& app_identifier, 603 const PushMessagingAppIdentifier& app_identifier,
604 const base::Closure& closure, const std::string& sender_id, 604 const base::Closure& closure, const std::string& sender_id,
605 bool success, bool not_found) { 605 bool success, bool not_found) {
606 base::Closure barrier_closure = base::BarrierClosure(2, closure); 606 base::Closure barrier_closure = base::BarrierClosure(2, closure);
607 607
608 // Unregister the PushMessagingAppIdentifier with the push service. 608 // Unsubscribe the PushMessagingAppIdentifier with the push service.
609 // It's possible for GetSenderId to have failed and sender_id to be empty, if 609 // It's possible for GetSenderId to have failed and sender_id to be empty, if
610 // cookies (and the SW database) for an origin got cleared before permissions 610 // cookies (and the SW database) for an origin got cleared before permissions
611 // are cleared for the origin. In that case Unregister will just delete the 611 // are cleared for the origin. In that case Unsubscribe will just delete the
612 // app identifier to block future messages. 612 // app identifier to block future messages.
613 // TODO(johnme): Auto-unregister before SW DB is cleared 613 // TODO(johnme): Auto-unregister before SW DB is cleared
614 // (https://crbug.com/402458). 614 // (https://crbug.com/402458).
615 Unregister(app_identifier.app_id(), sender_id, 615 Unsubscribe(app_identifier.app_id(), sender_id,
616 base::Bind(&UnregisterCallbackToClosure, barrier_closure)); 616 base::Bind(&UnregisterCallbackToClosure, barrier_closure));
617 617
618 // Clear the associated service worker push registration id. 618 // Clear the associated service worker push registration id.
619 ClearPushRegistrationID(profile_, app_identifier.origin(), 619 ClearPushSubscriptionID(profile_, app_identifier.origin(),
620 app_identifier.service_worker_registration_id(), 620 app_identifier.service_worker_registration_id(),
621 barrier_closure); 621 barrier_closure);
622 } 622 }
623 623
624 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( 624 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting(
625 const base::Closure& callback) { 625 const base::Closure& callback) {
626 content_setting_changed_callback_for_testing_ = callback; 626 content_setting_changed_callback_for_testing_ = callback;
627 } 627 }
628 628
629 // KeyedService methods ------------------------------------------------------- 629 // KeyedService methods -------------------------------------------------------
(...skipping 13 matching lines...) Expand all
643 CONTENT_SETTING_ALLOW; 643 CONTENT_SETTING_ALLOW;
644 } 644 }
645 645
646 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { 646 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const {
647 gcm::GCMProfileService* gcm_profile_service = 647 gcm::GCMProfileService* gcm_profile_service =
648 gcm::GCMProfileServiceFactory::GetForProfile(profile_); 648 gcm::GCMProfileServiceFactory::GetForProfile(profile_);
649 CHECK(gcm_profile_service); 649 CHECK(gcm_profile_service);
650 CHECK(gcm_profile_service->driver()); 650 CHECK(gcm_profile_service->driver());
651 return gcm_profile_service->driver(); 651 return gcm_profile_service->driver();
652 } 652 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698