| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 DCHECK(push_registration_count_ >= 0); | 138 DCHECK(push_registration_count_ >= 0); |
| 139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 139 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
| 140 push_registration_count_); | 140 push_registration_count_); |
| 141 } | 141 } |
| 142 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 142 if (push_registration_count_ + pending_push_registration_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::Get(profile_, app_id).IsValid(); | 148 return !PushMessagingAppIdentifier::Get(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 |
| 153 // handlers of gcm::GCMDriver so this shouldn't ever been called. | 153 // handlers of gcm::GCMDriver so this shouldn't ever been called. |
| 154 NOTREACHED(); | 154 NOTREACHED(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // OnMessage methods ----------------------------------------------------------- | 157 // OnMessage methods ----------------------------------------------------------- |
| 158 | 158 |
| 159 void PushMessagingServiceImpl::OnMessage( | 159 void PushMessagingServiceImpl::OnMessage( |
| 160 const std::string& app_id, | 160 const std::string& app_id, |
| 161 const gcm::GCMClient::IncomingMessage& message) { | 161 const gcm::GCMClient::IncomingMessage& message) { |
| 162 base::Closure message_handled_closure = | 162 base::Closure message_handled_closure = |
| 163 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) | 163 message_callback_for_testing_.is_null() ? base::Bind(&base::DoNothing) |
| 164 : message_callback_for_testing_; | 164 : message_callback_for_testing_; |
| 165 PushMessagingAppIdentifier app_identifier = | 165 PushMessagingAppIdentifier app_identifier = |
| 166 PushMessagingAppIdentifier::Get(profile_, app_id); | 166 PushMessagingAppIdentifier::Get(profile_, app_id); |
| 167 // Drop message and unregister if app_id was unknown (maybe recently deleted). | 167 // Drop message and unregister if app_id was unknown (maybe recently deleted). |
| 168 if (!app_identifier.IsValid()) { | 168 if (app_identifier.is_null()) { |
| 169 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, | 169 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, |
| 170 message_handled_closure, | 170 message_handled_closure, |
| 171 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); | 171 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 // Drop message and unregister if |origin| has lost push permission. | 174 // Drop message and unregister if |origin| has lost push permission. |
| 175 if (!HasPermission(app_identifier.origin())) { | 175 if (!HasPermission(app_identifier.origin())) { |
| 176 DeliverMessageCallback(app_id, app_identifier.origin(), | 176 DeliverMessageCallback(app_id, app_identifier.origin(), |
| 177 app_identifier.service_worker_registration_id(), | 177 app_identifier.service_worker_registration_id(), |
| 178 message, message_handled_closure, | 178 message, message_handled_closure, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 DCHECK(app_identifier.IsValid()); | |
| 308 | 307 |
| 309 if (push_registration_count_ + pending_push_registration_count_ | 308 if (push_registration_count_ + pending_push_registration_count_ |
| 310 >= kMaxRegistrations) { | 309 >= kMaxRegistrations) { |
| 311 RegisterEnd(callback, | 310 RegisterEnd(callback, |
| 312 std::string(), | 311 std::string(), |
| 313 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 312 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 314 return; | 313 return; |
| 315 } | 314 } |
| 316 | 315 |
| 317 content::RenderFrameHost* render_frame_host = | 316 content::RenderFrameHost* render_frame_host = |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 354 |
| 356 void PushMessagingServiceImpl::RegisterFromWorker( | 355 void PushMessagingServiceImpl::RegisterFromWorker( |
| 357 const GURL& requesting_origin, | 356 const GURL& requesting_origin, |
| 358 int64 service_worker_registration_id, | 357 int64 service_worker_registration_id, |
| 359 const std::string& sender_id, | 358 const std::string& sender_id, |
| 360 bool user_visible, | 359 bool user_visible, |
| 361 const content::PushMessagingService::RegisterCallback& register_callback) { | 360 const content::PushMessagingService::RegisterCallback& register_callback) { |
| 362 PushMessagingAppIdentifier app_identifier = | 361 PushMessagingAppIdentifier app_identifier = |
| 363 PushMessagingAppIdentifier::Generate(requesting_origin, | 362 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 364 service_worker_registration_id); | 363 service_worker_registration_id); |
| 365 DCHECK(app_identifier.IsValid()); | |
| 366 | 364 |
| 367 if (profile_->GetPrefs()->GetInteger( | 365 if (profile_->GetPrefs()->GetInteger( |
| 368 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 366 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
| 369 RegisterEnd(register_callback, std::string(), | 367 RegisterEnd(register_callback, std::string(), |
| 370 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 368 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 371 return; | 369 return; |
| 372 } | 370 } |
| 373 | 371 |
| 374 // TODO(peter): Consider |user_visible| when getting the permission status | 372 // TODO(peter): Consider |user_visible| when getting the permission status |
| 375 // for registering from a worker. | 373 // for registering from a worker. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 void PushMessagingServiceImpl::DidRegister( | 414 void PushMessagingServiceImpl::DidRegister( |
| 417 const PushMessagingAppIdentifier& app_identifier, | 415 const PushMessagingAppIdentifier& app_identifier, |
| 418 const content::PushMessagingService::RegisterCallback& callback, | 416 const content::PushMessagingService::RegisterCallback& callback, |
| 419 const std::string& registration_id, | 417 const std::string& registration_id, |
| 420 gcm::GCMClient::Result result) { | 418 gcm::GCMClient::Result result) { |
| 421 content::PushRegistrationStatus status = | 419 content::PushRegistrationStatus status = |
| 422 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 420 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 423 switch (result) { | 421 switch (result) { |
| 424 case gcm::GCMClient::SUCCESS: | 422 case gcm::GCMClient::SUCCESS: |
| 425 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 423 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
| 426 app_identifier.PersistToDisk(profile_); | 424 app_identifier.PersistToPrefs(profile_); |
| 427 IncreasePushRegistrationCount(1, false /* is_pending */); | 425 IncreasePushRegistrationCount(1, false /* is_pending */); |
| 428 break; | 426 break; |
| 429 case gcm::GCMClient::INVALID_PARAMETER: | 427 case gcm::GCMClient::INVALID_PARAMETER: |
| 430 case gcm::GCMClient::GCM_DISABLED: | 428 case gcm::GCMClient::GCM_DISABLED: |
| 431 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 429 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 432 case gcm::GCMClient::SERVER_ERROR: | 430 case gcm::GCMClient::SERVER_ERROR: |
| 433 case gcm::GCMClient::UNKNOWN_ERROR: | 431 case gcm::GCMClient::UNKNOWN_ERROR: |
| 434 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 432 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 435 break; | 433 break; |
| 436 case gcm::GCMClient::NETWORK_ERROR: | 434 case gcm::GCMClient::NETWORK_ERROR: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 466 | 464 |
| 467 // Unregister methods ---------------------------------------------------------- | 465 // Unregister methods ---------------------------------------------------------- |
| 468 | 466 |
| 469 void PushMessagingServiceImpl::Unregister( | 467 void PushMessagingServiceImpl::Unregister( |
| 470 const GURL& requesting_origin, | 468 const GURL& requesting_origin, |
| 471 int64 service_worker_registration_id, | 469 int64 service_worker_registration_id, |
| 472 const std::string& sender_id, | 470 const std::string& sender_id, |
| 473 const content::PushMessagingService::UnregisterCallback& callback) { | 471 const content::PushMessagingService::UnregisterCallback& callback) { |
| 474 PushMessagingAppIdentifier app_identifier = PushMessagingAppIdentifier::Get( | 472 PushMessagingAppIdentifier app_identifier = PushMessagingAppIdentifier::Get( |
| 475 profile_, requesting_origin, service_worker_registration_id); | 473 profile_, requesting_origin, service_worker_registration_id); |
| 476 if (!app_identifier.IsValid()) { | 474 if (app_identifier.is_null()) { |
| 477 if (!callback.is_null()) { | 475 if (!callback.is_null()) { |
| 478 callback.Run( | 476 callback.Run( |
| 479 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 477 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
| 480 } | 478 } |
| 481 return; | 479 return; |
| 482 } | 480 } |
| 483 | 481 |
| 484 Unregister(app_identifier.app_id(), sender_id, callback); | 482 Unregister(app_identifier.app_id(), sender_id, callback); |
| 485 } | 483 } |
| 486 | 484 |
| 487 void PushMessagingServiceImpl::Unregister( | 485 void PushMessagingServiceImpl::Unregister( |
| 488 const std::string& app_id, | 486 const std::string& app_id, |
| 489 const std::string& sender_id, | 487 const std::string& sender_id, |
| 490 const content::PushMessagingService::UnregisterCallback& callback) { | 488 const content::PushMessagingService::UnregisterCallback& callback) { |
| 491 // Delete the mapping for this app_id, to guarantee that no messages get | 489 // Delete the mapping for this app_id, to guarantee that no messages get |
| 492 // delivered in future (even if unregistration fails). | 490 // delivered in future (even if unregistration fails). |
| 493 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and | 491 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, and |
| 494 // retry unregistration if it fails due to network errors (crbug.com/465399). | 492 // retry unregistration if it fails due to network errors (crbug.com/465399). |
| 495 PushMessagingAppIdentifier app_identifier = | 493 PushMessagingAppIdentifier app_identifier = |
| 496 PushMessagingAppIdentifier::Get(profile_, app_id); | 494 PushMessagingAppIdentifier::Get(profile_, app_id); |
| 497 bool was_registered = app_identifier.IsValid(); | 495 bool was_registered = !app_identifier.is_null(); |
| 498 if (was_registered) | 496 if (was_registered) |
| 499 app_identifier.DeleteFromDisk(profile_); | 497 app_identifier.DeleteFromPrefs(profile_); |
| 500 | 498 |
| 501 const auto& unregister_callback = | 499 const auto& unregister_callback = |
| 502 base::Bind(&PushMessagingServiceImpl::DidUnregister, | 500 base::Bind(&PushMessagingServiceImpl::DidUnregister, |
| 503 weak_factory_.GetWeakPtr(), | 501 weak_factory_.GetWeakPtr(), |
| 504 was_registered, callback); | 502 was_registered, callback); |
| 505 #if defined(OS_ANDROID) | 503 #if defined(OS_ANDROID) |
| 506 // 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. |
| 507 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. | 505 // UnregisterBecausePermissionRevoked sometimes calls us with an empty one. |
| 508 if (sender_id.empty()) | 506 if (sender_id.empty()) |
| 509 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); | 507 unregister_callback.Run(gcm::GCMClient::INVALID_PARAMETER); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 CONTENT_SETTING_ALLOW; | 638 CONTENT_SETTING_ALLOW; |
| 641 } | 639 } |
| 642 | 640 |
| 643 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 641 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 644 gcm::GCMProfileService* gcm_profile_service = | 642 gcm::GCMProfileService* gcm_profile_service = |
| 645 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 643 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 646 CHECK(gcm_profile_service); | 644 CHECK(gcm_profile_service); |
| 647 CHECK(gcm_profile_service->driver()); | 645 CHECK(gcm_profile_service->driver()); |
| 648 return gcm_profile_service->driver(); | 646 return gcm_profile_service->driver(); |
| 649 } | 647 } |
| OLD | NEW |