| 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 "content/renderer/push_messaging/push_messaging_dispatcher.h" | 5 #include "content/renderer/push_messaging/push_messaging_dispatcher.h" | 
| 6 | 6 | 
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" | 
| 8 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 8 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 
| 9 #include "content/common/push_messaging_messages.h" | 9 #include "content/common/push_messaging_messages.h" | 
| 10 #include "content/renderer/manifest/manifest_manager.h" | 10 #include "content/renderer/manifest/manifest_manager.h" | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
| 37   IPC_END_MESSAGE_MAP() | 37   IPC_END_MESSAGE_MAP() | 
| 38   return handled; | 38   return handled; | 
| 39 } | 39 } | 
| 40 | 40 | 
| 41 void PushMessagingDispatcher::subscribe( | 41 void PushMessagingDispatcher::subscribe( | 
| 42     blink::WebServiceWorkerRegistration* service_worker_registration, | 42     blink::WebServiceWorkerRegistration* service_worker_registration, | 
| 43     const blink::WebPushSubscriptionOptions& options, | 43     const blink::WebPushSubscriptionOptions& options, | 
| 44     blink::WebPushSubscriptionCallbacks* callbacks) { | 44     blink::WebPushSubscriptionCallbacks* callbacks) { | 
| 45   DCHECK(service_worker_registration); | 45   DCHECK(service_worker_registration); | 
| 46   DCHECK(callbacks); | 46   DCHECK(callbacks); | 
| 47   // If a developer provided an application server key in |options|, skip | 47   RenderFrameImpl::FromRoutingID(routing_id()) | 
| 48   // fetching the manifest. | 48       ->manifest_manager() | 
| 49   if (options.applicationServerKey.isEmpty()) { | 49       ->GetManifest(base::Bind( | 
| 50     RenderFrameImpl::FromRoutingID(routing_id()) | 50           &PushMessagingDispatcher::DoSubscribe, base::Unretained(this), | 
| 51         ->manifest_manager() | 51           service_worker_registration, options, callbacks)); | 
| 52         ->GetManifest(base::Bind( |  | 
| 53             &PushMessagingDispatcher::DidGetManifest, base::Unretained(this), |  | 
| 54             service_worker_registration, options, callbacks)); |  | 
| 55   } else { |  | 
| 56     PushSubscriptionOptions content_options; |  | 
| 57     content_options.user_visible_only = options.userVisibleOnly; |  | 
| 58     content_options.sender_info = options.applicationServerKey.utf8(); |  | 
| 59     DoSubscribe(service_worker_registration, content_options, callbacks); |  | 
| 60   } |  | 
| 61 } | 52 } | 
| 62 | 53 | 
| 63 void PushMessagingDispatcher::DidGetManifest( | 54 void PushMessagingDispatcher::DoSubscribe( | 
| 64     blink::WebServiceWorkerRegistration* service_worker_registration, | 55     blink::WebServiceWorkerRegistration* service_worker_registration, | 
| 65     const blink::WebPushSubscriptionOptions& options, | 56     const blink::WebPushSubscriptionOptions& options, | 
| 66     blink::WebPushSubscriptionCallbacks* callbacks, | 57     blink::WebPushSubscriptionCallbacks* callbacks, | 
| 67     const Manifest& manifest) { | 58     const Manifest& manifest) { | 
| 68   int request_id = subscription_callbacks_.Add(callbacks); | 59   int request_id = subscription_callbacks_.Add(callbacks); | 
| 69   // Get the sender_info from the manifest since it wasn't provided by | 60   int64_t service_worker_registration_id = | 
| 70   // the caller. | 61       static_cast<WebServiceWorkerRegistrationImpl*>( | 
|  | 62           service_worker_registration) | 
|  | 63           ->registration_id(); | 
|  | 64 | 
| 71   if (manifest.IsEmpty()) { | 65   if (manifest.IsEmpty()) { | 
| 72     OnSubscribeFromDocumentError( | 66     OnSubscribeFromDocumentError( | 
| 73         request_id, PUSH_REGISTRATION_STATUS_MANIFEST_EMPTY_OR_MISSING); | 67         request_id, PUSH_REGISTRATION_STATUS_MANIFEST_EMPTY_OR_MISSING); | 
| 74     return; | 68     return; | 
| 75   } | 69   } | 
| 76 | 70 | 
| 77   PushSubscriptionOptions content_options; | 71   std::string sender_id = | 
| 78   content_options.user_visible_only = options.userVisibleOnly; | 72       manifest.gcm_sender_id.is_null() | 
| 79   if (!manifest.gcm_sender_id.is_null()) { | 73           ? std::string() | 
| 80     content_options.sender_info = | 74           : base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | 
| 81         base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | 75   if (sender_id.empty()) { | 
| 82   } |  | 
| 83 |  | 
| 84   DoSubscribe(service_worker_registration, content_options, callbacks); |  | 
| 85 } |  | 
| 86 |  | 
| 87 void PushMessagingDispatcher::DoSubscribe( |  | 
| 88     blink::WebServiceWorkerRegistration* service_worker_registration, |  | 
| 89     const PushSubscriptionOptions& options, |  | 
| 90     blink::WebPushSubscriptionCallbacks* callbacks) { |  | 
| 91   int request_id = subscription_callbacks_.Add(callbacks); |  | 
| 92   int64_t service_worker_registration_id = |  | 
| 93       static_cast<WebServiceWorkerRegistrationImpl*>( |  | 
| 94           service_worker_registration) |  | 
| 95           ->registration_id(); |  | 
| 96 |  | 
| 97   if (options.sender_info.empty()) { |  | 
| 98     OnSubscribeFromDocumentError(request_id, | 76     OnSubscribeFromDocumentError(request_id, | 
| 99                                  PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 77                                  PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 
| 100     return; | 78     return; | 
| 101   } | 79   } | 
|  | 80 | 
| 102   Send(new PushMessagingHostMsg_SubscribeFromDocument( | 81   Send(new PushMessagingHostMsg_SubscribeFromDocument( | 
| 103       routing_id(), request_id, options, service_worker_registration_id)); | 82       routing_id(), request_id, | 
|  | 83       manifest.gcm_sender_id.is_null() | 
|  | 84           ? std::string() | 
|  | 85           : base::UTF16ToUTF8(manifest.gcm_sender_id.string()), | 
|  | 86       options.userVisibleOnly, service_worker_registration_id)); | 
| 104 } | 87 } | 
| 105 | 88 | 
| 106 void PushMessagingDispatcher::OnSubscribeFromDocumentSuccess( | 89 void PushMessagingDispatcher::OnSubscribeFromDocumentSuccess( | 
| 107     int32_t request_id, | 90     int32_t request_id, | 
| 108     const GURL& endpoint, | 91     const GURL& endpoint, | 
| 109     const std::vector<uint8_t>& p256dh, | 92     const std::vector<uint8_t>& p256dh, | 
| 110     const std::vector<uint8_t>& auth) { | 93     const std::vector<uint8_t>& auth) { | 
| 111   blink::WebPushSubscriptionCallbacks* callbacks = | 94   blink::WebPushSubscriptionCallbacks* callbacks = | 
| 112       subscription_callbacks_.Lookup(request_id); | 95       subscription_callbacks_.Lookup(request_id); | 
| 113   DCHECK(callbacks); | 96   DCHECK(callbacks); | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 131           : blink::WebPushError::ErrorTypeAbort; | 114           : blink::WebPushError::ErrorTypeAbort; | 
| 132 | 115 | 
| 133   callbacks->onError(blink::WebPushError( | 116   callbacks->onError(blink::WebPushError( | 
| 134       error_type, | 117       error_type, | 
| 135       blink::WebString::fromUTF8(PushRegistrationStatusToString(status)))); | 118       blink::WebString::fromUTF8(PushRegistrationStatusToString(status)))); | 
| 136 | 119 | 
| 137   subscription_callbacks_.Remove(request_id); | 120   subscription_callbacks_.Remove(request_id); | 
| 138 } | 121 } | 
| 139 | 122 | 
| 140 }  // namespace content | 123 }  // namespace content | 
| OLD | NEW | 
|---|