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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // "registration_ids": ["FOO", "BAR"], | 204 // "registration_ids": ["FOO", "BAR"], |
205 // "data": { | 205 // "data": { |
206 // "data": "BAZ", | 206 // "data": "BAZ", |
207 // }, | 207 // }, |
208 // "delay_while_idle": true, | 208 // "delay_while_idle": true, |
209 // } | 209 // } |
210 // TODO(johnme): Make sure this is clearly documented for developers. | 210 // TODO(johnme): Make sure this is clearly documented for developers. |
211 std::string data; | 211 std::string data; |
212 // TODO(peter): Message payloads are disabled pending mandatory encryption. | 212 // TODO(peter): Message payloads are disabled pending mandatory encryption. |
213 // https://crbug.com/449184 | 213 // https://crbug.com/449184 |
214 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 214 if (EnableMessagePayloads()) { |
215 switches::kEnablePushMessagePayload)) { | |
216 gcm::MessageData::const_iterator it = message.data.find("data"); | 215 gcm::MessageData::const_iterator it = message.data.find("data"); |
217 if (it != message.data.end()) | 216 if (it != message.data.end()) |
218 data = it->second; | 217 data = it->second; |
219 } | 218 } |
220 | 219 |
221 content::BrowserContext::DeliverPushMessage( | 220 content::BrowserContext::DeliverPushMessage( |
222 profile_, | 221 profile_, |
223 app_identifier.origin(), | 222 app_identifier.origin(), |
224 app_identifier.service_worker_registration_id(), | 223 app_identifier.service_worker_registration_id(), |
225 data, | 224 data, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 const std::string& message_id) { | 307 const std::string& message_id) { |
309 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 308 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
310 } | 309 } |
311 | 310 |
312 // GetPushEndpoint method ------------------------------------------------------ | 311 // GetPushEndpoint method ------------------------------------------------------ |
313 | 312 |
314 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 313 GURL PushMessagingServiceImpl::GetPushEndpoint() { |
315 return GURL(std::string(kPushMessagingEndpoint)); | 314 return GURL(std::string(kPushMessagingEndpoint)); |
316 } | 315 } |
317 | 316 |
318 // GetPublicEncryptionKey method ----------------------------------------------- | 317 // GetPublicEncryptionKey methods ---------------------------------------------- |
319 | 318 |
320 void PushMessagingServiceImpl::GetPublicEncryptionKey( | 319 void PushMessagingServiceImpl::GetPublicEncryptionKey( |
321 const GURL& origin, | 320 const GURL& origin, |
322 int64_t service_worker_registration_id, | 321 int64_t service_worker_registration_id, |
323 const PushMessagingService::PublicKeyCallback& callback) { | 322 const PushMessagingService::PublicKeyCallback& callback) { |
324 // TODO(peter): Get the public key from the GCM Driver. Right now we have to | 323 // An empty public key will be returned if payloads are not enabled. |
325 // return success=true here, otherwise subscriptions would fail. | 324 if (!EnableMessagePayloads()) { |
326 callback.Run(true /* success */, std::vector<uint8_t>()); | 325 callback.Run(true /* success */, std::vector<uint8_t>()); |
| 326 return; |
| 327 } |
| 328 |
| 329 PushMessagingAppIdentifier app_identifier = |
| 330 PushMessagingAppIdentifier::FindByServiceWorker( |
| 331 profile_, origin, service_worker_registration_id); |
| 332 |
| 333 DCHECK(!app_identifier.is_null()); |
| 334 |
| 335 GetGCMDriver()->GetPublicEncryptionKey( |
| 336 app_identifier.app_id(), |
| 337 base::Bind(&PushMessagingServiceImpl::DidGetPublicEncryptionKey, |
| 338 weak_factory_.GetWeakPtr(), callback)); |
| 339 } |
| 340 |
| 341 void PushMessagingServiceImpl::DidGetPublicEncryptionKey( |
| 342 const PushMessagingService::PublicKeyCallback& callback, |
| 343 const std::vector<uint8_t>& public_key) const { |
| 344 // I/O errors might prevent the GCM Driver from retrieving a key-pair. |
| 345 const bool success = public_key.size(); |
| 346 |
| 347 callback.Run(success, public_key); |
327 } | 348 } |
328 | 349 |
329 // Subscribe and GetPermissionStatus methods ----------------------------------- | 350 // Subscribe and GetPermissionStatus methods ----------------------------------- |
330 | 351 |
331 void PushMessagingServiceImpl::SubscribeFromDocument( | 352 void PushMessagingServiceImpl::SubscribeFromDocument( |
332 const GURL& requesting_origin, | 353 const GURL& requesting_origin, |
333 int64 service_worker_registration_id, | 354 int64 service_worker_registration_id, |
334 const std::string& sender_id, | 355 const std::string& sender_id, |
335 int renderer_id, | 356 int renderer_id, |
336 int render_frame_id, | 357 int render_frame_id, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 std::vector<uint8_t>() /* curve25519dh */, status); | 463 std::vector<uint8_t>() /* curve25519dh */, status); |
443 } | 464 } |
444 | 465 |
445 void PushMessagingServiceImpl::DidSubscribe( | 466 void PushMessagingServiceImpl::DidSubscribe( |
446 const PushMessagingAppIdentifier& app_identifier, | 467 const PushMessagingAppIdentifier& app_identifier, |
447 const content::PushMessagingService::RegisterCallback& callback, | 468 const content::PushMessagingService::RegisterCallback& callback, |
448 const std::string& subscription_id, | 469 const std::string& subscription_id, |
449 gcm::GCMClient::Result result) { | 470 gcm::GCMClient::Result result) { |
450 content::PushRegistrationStatus status = | 471 content::PushRegistrationStatus status = |
451 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 472 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
452 std::vector<uint8_t> curve25519dh; | |
453 | 473 |
454 switch (result) { | 474 switch (result) { |
455 case gcm::GCMClient::SUCCESS: | 475 case gcm::GCMClient::SUCCESS: |
456 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 476 // Do not get a certificate if message payloads have not been enabled. |
457 app_identifier.PersistToPrefs(profile_); | 477 if (!EnableMessagePayloads()) { |
| 478 DidSubscribeWithPublicEncryptionKey( |
| 479 app_identifier, callback, subscription_id, |
| 480 std::vector<uint8_t>() /* public_key */); |
| 481 return; |
| 482 } |
458 | 483 |
459 // TODO(peter): Hook up getting the keys from the GCM Driver. | 484 // Make sure that this subscription has associated encryption keys prior |
| 485 // to returning it to the developer - they'll need this information in |
| 486 // order to send payloads to the user. |
| 487 GetGCMDriver()->GetPublicEncryptionKey( |
| 488 app_identifier.app_id(), |
| 489 base::Bind( |
| 490 &PushMessagingServiceImpl::DidSubscribeWithPublicEncryptionKey, |
| 491 weak_factory_.GetWeakPtr(), app_identifier, callback, |
| 492 subscription_id)); |
460 | 493 |
461 IncreasePushSubscriptionCount(1, false /* is_pending */); | 494 return; |
462 break; | |
463 case gcm::GCMClient::INVALID_PARAMETER: | 495 case gcm::GCMClient::INVALID_PARAMETER: |
464 case gcm::GCMClient::GCM_DISABLED: | 496 case gcm::GCMClient::GCM_DISABLED: |
465 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 497 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
466 case gcm::GCMClient::SERVER_ERROR: | 498 case gcm::GCMClient::SERVER_ERROR: |
467 case gcm::GCMClient::UNKNOWN_ERROR: | 499 case gcm::GCMClient::UNKNOWN_ERROR: |
468 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 500 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
469 break; | 501 break; |
470 case gcm::GCMClient::NETWORK_ERROR: | 502 case gcm::GCMClient::NETWORK_ERROR: |
471 case gcm::GCMClient::TTL_EXCEEDED: | 503 case gcm::GCMClient::TTL_EXCEEDED: |
472 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 504 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
473 break; | 505 break; |
474 } | 506 } |
475 | 507 |
476 SubscribeEnd(callback, subscription_id, curve25519dh, status); | 508 SubscribeEndWithError(callback, status); |
| 509 } |
| 510 |
| 511 void PushMessagingServiceImpl::DidSubscribeWithPublicEncryptionKey( |
| 512 const PushMessagingAppIdentifier& app_identifier, |
| 513 const content::PushMessagingService::RegisterCallback& callback, |
| 514 const std::string& subscription_id, |
| 515 const std::vector<uint8_t>& public_key) { |
| 516 if (!public_key.size() && EnableMessagePayloads()) { |
| 517 SubscribeEndWithError( |
| 518 callback, content::PUSH_REGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE); |
| 519 return; |
| 520 } |
| 521 |
| 522 app_identifier.PersistToPrefs(profile_); |
| 523 |
| 524 IncreasePushSubscriptionCount(1, false /* is_pending */); |
477 DecreasePushSubscriptionCount(1, true /* was_pending */); | 525 DecreasePushSubscriptionCount(1, true /* was_pending */); |
| 526 |
| 527 SubscribeEnd(callback, subscription_id, public_key, |
| 528 content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE); |
478 } | 529 } |
479 | 530 |
480 void PushMessagingServiceImpl::DidRequestPermission( | 531 void PushMessagingServiceImpl::DidRequestPermission( |
481 const PushMessagingAppIdentifier& app_identifier, | 532 const PushMessagingAppIdentifier& app_identifier, |
482 const std::string& sender_id, | 533 const std::string& sender_id, |
483 const content::PushMessagingService::RegisterCallback& register_callback, | 534 const content::PushMessagingService::RegisterCallback& register_callback, |
484 content::PermissionStatus permission_status) { | 535 content::PermissionStatus permission_status) { |
485 if (permission_status != content::PERMISSION_STATUS_GRANTED) { | 536 if (permission_status != content::PERMISSION_STATUS_GRANTED) { |
486 SubscribeEndWithError(register_callback, | 537 SubscribeEndWithError(register_callback, |
487 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 538 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 | 716 |
666 // Helper methods -------------------------------------------------------------- | 717 // Helper methods -------------------------------------------------------------- |
667 | 718 |
668 // Assumes user_visible always since this is just meant to check | 719 // Assumes user_visible always since this is just meant to check |
669 // if the permission was previously granted and not revoked. | 720 // if the permission was previously granted and not revoked. |
670 bool PushMessagingServiceImpl::IsPermissionSet(const GURL& origin) { | 721 bool PushMessagingServiceImpl::IsPermissionSet(const GURL& origin) { |
671 return GetPermissionStatus(origin, origin, true /* user_visible */) == | 722 return GetPermissionStatus(origin, origin, true /* user_visible */) == |
672 blink::WebPushPermissionStatusGranted; | 723 blink::WebPushPermissionStatusGranted; |
673 } | 724 } |
674 | 725 |
| 726 bool PushMessagingServiceImpl::EnableMessagePayloads() const { |
| 727 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 728 switches::kEnablePushMessagePayload); |
| 729 } |
| 730 |
675 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 731 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
676 gcm::GCMProfileService* gcm_profile_service = | 732 gcm::GCMProfileService* gcm_profile_service = |
677 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 733 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
678 CHECK(gcm_profile_service); | 734 CHECK(gcm_profile_service); |
679 CHECK(gcm_profile_service->driver()); | 735 CHECK(gcm_profile_service->driver()); |
680 return gcm_profile_service->driver(); | 736 return gcm_profile_service->driver(); |
681 } | 737 } |
OLD | NEW |