| 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/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 Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 callback.Run(result == GCMClient::SUCCESS); |
| 430 } | |
| 431 | 444 |
| 432 DecreasePushRegistrationCount(1); | 445 if (result == GCMClient::SUCCESS) |
| 446 DecreasePushRegistrationCount(1); |
| 433 } | 447 } |
| 434 | 448 |
| 435 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 449 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
| 436 gcm::PushMessagingPermissionContext* permission_context = | 450 gcm::PushMessagingPermissionContext* permission_context = |
| 437 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 451 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 438 DCHECK(permission_context); | 452 DCHECK(permission_context); |
| 439 | 453 |
| 440 return permission_context->GetPermissionStatus(origin, origin) == | 454 return permission_context->GetPermissionStatus(origin, origin) == |
| 441 CONTENT_SETTING_ALLOW; | 455 CONTENT_SETTING_ALLOW; |
| 442 } | 456 } |
| 443 | 457 |
| 444 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { | 458 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { |
| 445 if (gcm_profile_service_->driver()->GetAppHandler( | 459 if (gcm_profile_service_->driver()->GetAppHandler( |
| 446 kPushMessagingApplicationIdPrefix) != this) | 460 kPushMessagingApplicationIdPrefix) != this) |
| 447 gcm_profile_service_->driver()->AddAppHandler( | 461 gcm_profile_service_->driver()->AddAppHandler( |
| 448 kPushMessagingApplicationIdPrefix, this); | 462 kPushMessagingApplicationIdPrefix, this); |
| 449 } | 463 } |
| 450 | 464 |
| 451 } // namespace gcm | 465 } // namespace gcm |
| OLD | NEW |