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 "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 Loading... |
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 Loading... |
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( |
257 base::Bind(&UnregisterCallbackToClosure, | 257 app_id, message.sender_id, |
258 message_handled_closure)); | 258 base::Bind(&UnregisterCallbackToClosure, 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; |
267 } | 267 } |
268 | 268 |
(...skipping 15 matching lines...) Expand all Loading... |
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 SubscribeEnd(callback, std::string(), |
311 std::string(), | 311 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
312 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | |
313 return; | 312 return; |
314 } | 313 } |
315 | 314 |
316 content::RenderFrameHost* render_frame_host = | 315 content::RenderFrameHost* render_frame_host = |
317 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 316 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
318 if (!render_frame_host) | 317 if (!render_frame_host) |
319 return; | 318 return; |
320 | 319 |
321 content::WebContents* web_contents = | 320 content::WebContents* web_contents = |
322 content::WebContents::FromRenderFrameHost(render_frame_host); | 321 content::WebContents::FromRenderFrameHost(render_frame_host); |
323 if (!web_contents) | 322 if (!web_contents) |
324 return; | 323 return; |
325 | 324 |
326 // TODO(miguelg) need to send this over IPC when bubble support is | 325 // TODO(miguelg) need to send this over IPC when bubble support is |
327 // implemented. | 326 // implemented. |
328 int bridge_id = -1; | 327 int bridge_id = -1; |
329 | 328 |
330 const PermissionRequestID id( | 329 const PermissionRequestID id( |
331 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); | 330 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); |
332 | 331 |
333 PushMessagingPermissionContext* permission_context = | 332 PushMessagingPermissionContext* permission_context = |
334 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 333 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
335 | 334 |
336 if (permission_context == NULL || !user_visible) { | 335 if (permission_context == NULL || !user_visible) { |
337 RegisterEnd(callback, | 336 SubscribeEnd(callback, |
338 std::string(), | 337 std::string(), |
339 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 338 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
340 return; | 339 return; |
341 } | 340 } |
342 | 341 |
343 // TODO(miguelg): Consider the value of |user_visible| when making the | 342 // TODO(miguelg): Consider the value of |user_visible| when making the |
344 // permission request. | 343 // permission request. |
345 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and | 344 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and |
346 // re-introduce the ability of |user_gesture| when bubbles require this. | 345 // re-introduce the ability of |user_gesture| when bubbles require this. |
347 // https://crbug.com/423770. | 346 // https://crbug.com/423770. |
348 permission_context->RequestPermission( | 347 permission_context->RequestPermission( |
349 web_contents, id, requesting_origin, true /* user_gesture */, | 348 web_contents, id, requesting_origin, true /* user_gesture */, |
350 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 349 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
351 weak_factory_.GetWeakPtr(), app_identifier, sender_id, | 350 weak_factory_.GetWeakPtr(), app_identifier, sender_id, |
352 callback)); | 351 callback)); |
353 } | 352 } |
354 | 353 |
355 void PushMessagingServiceImpl::RegisterFromWorker( | 354 void PushMessagingServiceImpl::SubscribeFromWorker( |
356 const GURL& requesting_origin, | 355 const GURL& requesting_origin, |
357 int64 service_worker_registration_id, | 356 int64 service_worker_registration_id, |
358 const std::string& sender_id, | 357 const std::string& sender_id, |
359 bool user_visible, | 358 bool user_visible, |
360 const content::PushMessagingService::RegisterCallback& register_callback) { | 359 const content::PushMessagingService::RegisterCallback& register_callback) { |
361 PushMessagingAppIdentifier app_identifier = | 360 PushMessagingAppIdentifier app_identifier = |
362 PushMessagingAppIdentifier::Generate(requesting_origin, | 361 PushMessagingAppIdentifier::Generate(requesting_origin, |
363 service_worker_registration_id); | 362 service_worker_registration_id); |
364 | 363 |
365 if (profile_->GetPrefs()->GetInteger( | 364 if (profile_->GetPrefs()->GetInteger( |
366 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 365 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
367 RegisterEnd(register_callback, std::string(), | 366 SubscribeEnd(register_callback, std::string(), |
368 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 367 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
369 return; | 368 return; |
370 } | 369 } |
371 | 370 |
372 // TODO(peter): Consider |user_visible| when getting the permission status | 371 // TODO(peter): Consider |user_visible| when getting the permission status |
373 // for registering from a worker. | 372 // for registering from a worker. |
374 | 373 |
375 GURL embedding_origin = requesting_origin; | 374 GURL embedding_origin = requesting_origin; |
376 blink::WebPushPermissionStatus permission_status = | 375 blink::WebPushPermissionStatus permission_status = |
377 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 376 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
378 embedding_origin, | 377 embedding_origin, |
379 user_visible); | 378 user_visible); |
380 if (permission_status != blink::WebPushPermissionStatusGranted) { | 379 if (permission_status != blink::WebPushPermissionStatusGranted) { |
381 RegisterEnd(register_callback, std::string(), | 380 SubscribeEnd(register_callback, std::string(), |
382 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 381 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
383 return; | 382 return; |
384 } | 383 } |
385 | 384 |
386 IncreasePushRegistrationCount(1, true /* is_pending */); | 385 IncreasePushSubscriptionCount(1, true /* is_pending */); |
387 std::vector<std::string> sender_ids(1, sender_id); | 386 std::vector<std::string> sender_ids(1, sender_id); |
388 GetGCMDriver()->Register( | 387 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
389 app_identifier.app_id(), sender_ids, | 388 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
390 base::Bind(&PushMessagingServiceImpl::DidRegister, | 389 weak_factory_.GetWeakPtr(), |
391 weak_factory_.GetWeakPtr(), | 390 app_identifier, register_callback)); |
392 app_identifier, register_callback)); | |
393 } | 391 } |
394 | 392 |
395 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( | 393 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( |
396 const GURL& requesting_origin, | 394 const GURL& requesting_origin, |
397 const GURL& embedding_origin, | 395 const GURL& embedding_origin, |
398 bool user_visible) { | 396 bool user_visible) { |
399 // TODO(peter): Consider |user_visible| when checking Push permission. | 397 // TODO(peter): Consider |user_visible| when checking Push permission. |
400 | 398 |
401 PushMessagingPermissionContext* permission_context = | 399 PushMessagingPermissionContext* permission_context = |
402 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 400 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
403 return ToPushPermission(permission_context->GetPermissionStatus( | 401 return ToPushPermission(permission_context->GetPermissionStatus( |
404 requesting_origin, embedding_origin)); | 402 requesting_origin, embedding_origin)); |
405 } | 403 } |
406 | 404 |
407 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { | 405 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { |
408 return false; | 406 return false; |
409 } | 407 } |
410 | 408 |
411 void PushMessagingServiceImpl::RegisterEnd( | 409 void PushMessagingServiceImpl::SubscribeEnd( |
412 const content::PushMessagingService::RegisterCallback& callback, | 410 const content::PushMessagingService::RegisterCallback& callback, |
413 const std::string& registration_id, | 411 const std::string& subscription_id, |
414 content::PushRegistrationStatus status) { | 412 content::PushRegistrationStatus status) { |
415 callback.Run(registration_id, status); | 413 callback.Run(subscription_id, status); |
416 } | 414 } |
417 | 415 |
418 void PushMessagingServiceImpl::DidRegister( | 416 void PushMessagingServiceImpl::DidSubscribe( |
419 const PushMessagingAppIdentifier& app_identifier, | 417 const PushMessagingAppIdentifier& app_identifier, |
420 const content::PushMessagingService::RegisterCallback& callback, | 418 const content::PushMessagingService::RegisterCallback& callback, |
421 const std::string& registration_id, | 419 const std::string& subscription_id, |
422 gcm::GCMClient::Result result) { | 420 gcm::GCMClient::Result result) { |
423 content::PushRegistrationStatus status = | 421 content::PushRegistrationStatus status = |
424 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 422 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
425 switch (result) { | 423 switch (result) { |
426 case gcm::GCMClient::SUCCESS: | 424 case gcm::GCMClient::SUCCESS: |
427 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 425 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
428 app_identifier.PersistToPrefs(profile_); | 426 app_identifier.PersistToPrefs(profile_); |
429 IncreasePushRegistrationCount(1, false /* is_pending */); | 427 IncreasePushSubscriptionCount(1, false /* is_pending */); |
430 break; | 428 break; |
431 case gcm::GCMClient::INVALID_PARAMETER: | 429 case gcm::GCMClient::INVALID_PARAMETER: |
432 case gcm::GCMClient::GCM_DISABLED: | 430 case gcm::GCMClient::GCM_DISABLED: |
433 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 431 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
434 case gcm::GCMClient::SERVER_ERROR: | 432 case gcm::GCMClient::SERVER_ERROR: |
435 case gcm::GCMClient::UNKNOWN_ERROR: | 433 case gcm::GCMClient::UNKNOWN_ERROR: |
436 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 434 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
437 break; | 435 break; |
438 case gcm::GCMClient::NETWORK_ERROR: | 436 case gcm::GCMClient::NETWORK_ERROR: |
439 case gcm::GCMClient::TTL_EXCEEDED: | 437 case gcm::GCMClient::TTL_EXCEEDED: |
440 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 438 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
441 break; | 439 break; |
442 } | 440 } |
443 RegisterEnd(callback, registration_id, status); | 441 SubscribeEnd(callback, subscription_id, status); |
444 DecreasePushRegistrationCount(1, true /* was_pending */); | 442 DecreasePushSubscriptionCount(1, true /* was_pending */); |
445 } | 443 } |
446 | 444 |
447 void PushMessagingServiceImpl::DidRequestPermission( | 445 void PushMessagingServiceImpl::DidRequestPermission( |
448 const PushMessagingAppIdentifier& app_identifier, | 446 const PushMessagingAppIdentifier& app_identifier, |
449 const std::string& sender_id, | 447 const std::string& sender_id, |
450 const content::PushMessagingService::RegisterCallback& register_callback, | 448 const content::PushMessagingService::RegisterCallback& register_callback, |
451 ContentSetting content_setting) { | 449 ContentSetting content_setting) { |
452 if (content_setting != CONTENT_SETTING_ALLOW) { | 450 if (content_setting != CONTENT_SETTING_ALLOW) { |
453 RegisterEnd(register_callback, | 451 SubscribeEnd(register_callback, |
454 std::string(), | 452 std::string(), |
455 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 453 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
456 return; | 454 return; |
457 } | 455 } |
458 | 456 |
459 IncreasePushRegistrationCount(1, true /* is_pending */); | 457 IncreasePushSubscriptionCount(1, true /* is_pending */); |
460 std::vector<std::string> sender_ids(1, sender_id); | 458 std::vector<std::string> sender_ids(1, sender_id); |
461 GetGCMDriver()->Register( | 459 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
462 app_identifier.app_id(), | 460 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
463 sender_ids, | 461 weak_factory_.GetWeakPtr(), |
464 base::Bind(&PushMessagingServiceImpl::DidRegister, | 462 app_identifier, register_callback)); |
465 weak_factory_.GetWeakPtr(), | |
466 app_identifier, register_callback)); | |
467 } | 463 } |
468 | 464 |
469 // Unregister methods ---------------------------------------------------------- | 465 // Unsubscribe methods --------------------------------------------------------- |
470 | 466 |
471 void PushMessagingServiceImpl::Unregister( | 467 void PushMessagingServiceImpl::Unsubscribe( |
472 const GURL& requesting_origin, | 468 const GURL& requesting_origin, |
473 int64 service_worker_registration_id, | 469 int64 service_worker_registration_id, |
474 const std::string& sender_id, | 470 const std::string& sender_id, |
475 const content::PushMessagingService::UnregisterCallback& callback) { | 471 const content::PushMessagingService::UnregisterCallback& callback) { |
476 PushMessagingAppIdentifier app_identifier = | 472 PushMessagingAppIdentifier app_identifier = |
477 PushMessagingAppIdentifier::FindByServiceWorker( | 473 PushMessagingAppIdentifier::FindByServiceWorker( |
478 profile_, requesting_origin, service_worker_registration_id); | 474 profile_, requesting_origin, service_worker_registration_id); |
479 if (app_identifier.is_null()) { | 475 if (app_identifier.is_null()) { |
480 if (!callback.is_null()) { | 476 if (!callback.is_null()) { |
481 callback.Run( | 477 callback.Run( |
482 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 478 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
483 } | 479 } |
484 return; | 480 return; |
485 } | 481 } |
486 | 482 |
487 Unregister(app_identifier.app_id(), sender_id, callback); | 483 Unsubscribe(app_identifier.app_id(), sender_id, callback); |
488 } | 484 } |
489 | 485 |
490 void PushMessagingServiceImpl::Unregister( | 486 void PushMessagingServiceImpl::Unsubscribe( |
491 const std::string& app_id, | 487 const std::string& app_id, |
492 const std::string& sender_id, | 488 const std::string& sender_id, |
493 const content::PushMessagingService::UnregisterCallback& callback) { | 489 const content::PushMessagingService::UnregisterCallback& callback) { |
494 // Delete the mapping for this app_id, to guarantee that no messages get | 490 // Delete the mapping for this app_id, to guarantee that no messages get |
495 // delivered in future (even if unregistration fails). | 491 // delivered in future (even if unregistration fails). |
496 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and | 492 // 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). | 493 // retry unregistration if it fails due to network errors (crbug.com/465399). |
498 PushMessagingAppIdentifier app_identifier = | 494 PushMessagingAppIdentifier app_identifier = |
499 PushMessagingAppIdentifier::FindByAppId(profile_, app_id); | 495 PushMessagingAppIdentifier::FindByAppId(profile_, app_id); |
500 bool was_registered = !app_identifier.is_null(); | 496 bool was_registered = !app_identifier.is_null(); |
501 if (was_registered) | 497 if (was_registered) |
502 app_identifier.DeleteFromPrefs(profile_); | 498 app_identifier.DeleteFromPrefs(profile_); |
503 | 499 |
504 const auto& unregister_callback = | 500 const auto& unregister_callback = |
505 base::Bind(&PushMessagingServiceImpl::DidUnregister, | 501 base::Bind(&PushMessagingServiceImpl::DidUnsubscribe, |
506 weak_factory_.GetWeakPtr(), | 502 weak_factory_.GetWeakPtr(), was_registered, callback); |
507 was_registered, callback); | |
508 #if defined(OS_ANDROID) | 503 #if defined(OS_ANDROID) |
509 // On Android the backend is different, and requires the original sender_id. | 504 // On Android the backend is different, and requires the original sender_id. |
510 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. | 505 // UnsubscribeBecausePermissionRevoked sometimes calls us with an empty one. |
511 if (sender_id.empty()) | 506 if (sender_id.empty()) |
512 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); | 507 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); |
513 else | 508 else |
514 GetGCMDriver()->UnregisterWithSenderId(app_id, sender_id, | 509 GetGCMDriver()->UnregisterWithSenderId(app_id, sender_id, |
515 unregister_callback); | 510 unregister_callback); |
516 #else | 511 #else |
517 GetGCMDriver()->Unregister(app_id, unregister_callback); | 512 GetGCMDriver()->Unregister(app_id, unregister_callback); |
518 #endif | 513 #endif |
519 } | 514 } |
520 | 515 |
521 void PushMessagingServiceImpl::DidUnregister( | 516 void PushMessagingServiceImpl::DidUnsubscribe( |
522 bool was_registered, | 517 bool was_subscribed, |
523 const content::PushMessagingService::UnregisterCallback& callback, | 518 const content::PushMessagingService::UnregisterCallback& callback, |
524 gcm::GCMClient::Result result) { | 519 gcm::GCMClient::Result result) { |
525 if (was_registered) | 520 if (was_subscribed) |
526 DecreasePushRegistrationCount(1, false /* was_pending */); | 521 DecreasePushSubscriptionCount(1, false /* was_pending */); |
527 | 522 |
528 // Internal calls pass a null callback. | 523 // Internal calls pass a null callback. |
529 if (callback.is_null()) | 524 if (callback.is_null()) |
530 return; | 525 return; |
531 | 526 |
532 if (!was_registered) { | 527 if (!was_subscribed) { |
533 callback.Run( | 528 callback.Run( |
534 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 529 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
535 return; | 530 return; |
536 } | 531 } |
537 switch (result) { | 532 switch (result) { |
538 case gcm::GCMClient::SUCCESS: | 533 case gcm::GCMClient::SUCCESS: |
539 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); | 534 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); |
540 break; | 535 break; |
541 case gcm::GCMClient::INVALID_PARAMETER: | 536 case gcm::GCMClient::INVALID_PARAMETER: |
542 case gcm::GCMClient::GCM_DISABLED: | 537 case gcm::GCMClient::GCM_DISABLED: |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 | 582 |
588 if (HasPermission(app_identifier.origin())) { | 583 if (HasPermission(app_identifier.origin())) { |
589 barrier_closure.Run(); | 584 barrier_closure.Run(); |
590 continue; | 585 continue; |
591 } | 586 } |
592 | 587 |
593 GetSenderId( | 588 GetSenderId( |
594 profile_, app_identifier.origin(), | 589 profile_, app_identifier.origin(), |
595 app_identifier.service_worker_registration_id(), | 590 app_identifier.service_worker_registration_id(), |
596 base::Bind( | 591 base::Bind( |
597 &PushMessagingServiceImpl::UnregisterBecausePermissionRevoked, | 592 &PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked, |
598 weak_factory_.GetWeakPtr(), app_identifier, barrier_closure)); | 593 weak_factory_.GetWeakPtr(), app_identifier, barrier_closure)); |
599 } | 594 } |
600 } | 595 } |
601 | 596 |
602 void PushMessagingServiceImpl::UnregisterBecausePermissionRevoked( | 597 void PushMessagingServiceImpl::UnsubscribeBecausePermissionRevoked( |
603 const PushMessagingAppIdentifier& app_identifier, | 598 const PushMessagingAppIdentifier& app_identifier, |
604 const base::Closure& closure, const std::string& sender_id, | 599 const base::Closure& closure, |
605 bool success, bool not_found) { | 600 const std::string& sender_id, |
| 601 bool success, |
| 602 bool not_found) { |
606 base::Closure barrier_closure = base::BarrierClosure(2, closure); | 603 base::Closure barrier_closure = base::BarrierClosure(2, closure); |
607 | 604 |
608 // Unregister the PushMessagingAppIdentifier with the push service. | 605 // Unsubscribe the PushMessagingAppIdentifier with the push service. |
609 // It's possible for GetSenderId to have failed and sender_id to be empty, if | 606 // 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 | 607 // 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 | 608 // are cleared for the origin. In that case Unsubscribe will just delete the |
612 // app identifier to block future messages. | 609 // app identifier to block future messages. |
613 // TODO(johnme): Auto-unregister before SW DB is cleared | 610 // TODO(johnme): Auto-unregister before SW DB is cleared |
614 // (https://crbug.com/402458). | 611 // (https://crbug.com/402458). |
615 Unregister(app_identifier.app_id(), sender_id, | 612 Unsubscribe(app_identifier.app_id(), sender_id, |
616 base::Bind(&UnregisterCallbackToClosure, barrier_closure)); | 613 base::Bind(&UnregisterCallbackToClosure, barrier_closure)); |
617 | 614 |
618 // Clear the associated service worker push registration id. | 615 // Clear the associated service worker push registration id. |
619 ClearPushRegistrationID(profile_, app_identifier.origin(), | 616 ClearPushSubscriptionID(profile_, app_identifier.origin(), |
620 app_identifier.service_worker_registration_id(), | 617 app_identifier.service_worker_registration_id(), |
621 barrier_closure); | 618 barrier_closure); |
622 } | 619 } |
623 | 620 |
624 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( | 621 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( |
625 const base::Closure& callback) { | 622 const base::Closure& callback) { |
626 content_setting_changed_callback_for_testing_ = callback; | 623 content_setting_changed_callback_for_testing_ = callback; |
627 } | 624 } |
628 | 625 |
629 // KeyedService methods ------------------------------------------------------- | 626 // KeyedService methods ------------------------------------------------------- |
(...skipping 13 matching lines...) Expand all Loading... |
643 CONTENT_SETTING_ALLOW; | 640 CONTENT_SETTING_ALLOW; |
644 } | 641 } |
645 | 642 |
646 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 643 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
647 gcm::GCMProfileService* gcm_profile_service = | 644 gcm::GCMProfileService* gcm_profile_service = |
648 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 645 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
649 CHECK(gcm_profile_service); | 646 CHECK(gcm_profile_service); |
650 CHECK(gcm_profile_service->driver()); | 647 CHECK(gcm_profile_service->driver()); |
651 return gcm_profile_service->driver(); | 648 return gcm_profile_service->driver(); |
652 } | 649 } |
OLD | NEW |