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 switch (result) { |
444 case GCMClient::SUCCESS: | |
445 callback.Run(content::PUSH_UNREGISTRATION_SUCCESS_UNREGISTER); | |
446 break; | |
447 case GCMClient::NETWORK_ERROR: | |
448 case GCMClient::SERVER_ERROR: | |
johnme
2014/12/16 16:31:59
I don't know if SERVER_ERROR ("Problem at the serv
mlamouri (slow - plz ping)
2014/12/16 18:26:16
Done.
| |
449 case GCMClient::TTL_EXCEEDED: | |
450 callback.Run(content::PUSH_UNREGISTRATION_NETWORK_ERROR); | |
451 break; | |
452 case GCMClient::INVALID_PARAMETER: | |
453 case GCMClient::GCM_DISABLED: | |
454 case GCMClient::NOT_SIGNED_IN: | |
455 case GCMClient::ASYNC_OPERATION_PENDING: | |
456 case GCMClient::UNKNOWN_ERROR: | |
457 callback.Run(content::PUSH_UNREGISTRATION_UNKNOWN_ERROR); | |
458 break; | |
459 default: | |
460 NOTREACHED() << "Unexpected GCMClient::Result value."; | |
461 callback.Run(content::PUSH_UNREGISTRATION_UNKNOWN_ERROR); | |
462 break; | |
430 } | 463 } |
431 | 464 |
432 DecreasePushRegistrationCount(1); | 465 if (result == GCMClient::SUCCESS) |
466 DecreasePushRegistrationCount(1); | |
433 } | 467 } |
434 | 468 |
435 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 469 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
436 gcm::PushMessagingPermissionContext* permission_context = | 470 gcm::PushMessagingPermissionContext* permission_context = |
437 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 471 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
438 DCHECK(permission_context); | 472 DCHECK(permission_context); |
439 | 473 |
440 return permission_context->GetPermissionStatus(origin, origin) == | 474 return permission_context->GetPermissionStatus(origin, origin) == |
441 CONTENT_SETTING_ALLOW; | 475 CONTENT_SETTING_ALLOW; |
442 } | 476 } |
443 | 477 |
444 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { | 478 void PushMessagingServiceImpl::AddAppHandlerIfNecessary() { |
445 if (gcm_profile_service_->driver()->GetAppHandler( | 479 if (gcm_profile_service_->driver()->GetAppHandler( |
446 kPushMessagingApplicationIdPrefix) != this) | 480 kPushMessagingApplicationIdPrefix) != this) |
447 gcm_profile_service_->driver()->AddAppHandler( | 481 gcm_profile_service_->driver()->AddAppHandler( |
448 kPushMessagingApplicationIdPrefix, this); | 482 kPushMessagingApplicationIdPrefix, this); |
449 } | 483 } |
450 | 484 |
451 } // namespace gcm | 485 } // namespace gcm |
OLD | NEW |