| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/push_messaging/push_messaging_dispatcher.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "content/child/push_messaging/push_provider.h" | |
| 13 #include "content/child/service_worker/web_service_worker_registration_impl.h" | |
| 14 #include "content/common/push_messaging_messages.h" | |
| 15 #include "content/renderer/manifest/manifest_manager.h" | |
| 16 #include "content/renderer/render_frame_impl.h" | |
| 17 #include "ipc/ipc_message.h" | |
| 18 #include "third_party/WebKit/public/platform/WebString.h" | |
| 19 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushError
.h" | |
| 20 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
ription.h" | |
| 21 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
riptionOptions.h" | |
| 22 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerRegistration.h" | |
| 23 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame) | |
| 30 : RenderFrameObserver(render_frame) {} | |
| 31 | |
| 32 PushMessagingDispatcher::~PushMessagingDispatcher() {} | |
| 33 | |
| 34 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 35 bool handled = true; | |
| 36 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) | |
| 37 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentSuccess, | |
| 38 OnSubscribeFromDocumentSuccess) | |
| 39 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentError, | |
| 40 OnSubscribeFromDocumentError) | |
| 41 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 42 IPC_END_MESSAGE_MAP() | |
| 43 return handled; | |
| 44 } | |
| 45 | |
| 46 void PushMessagingDispatcher::OnDestruct() { | |
| 47 delete this; | |
| 48 } | |
| 49 | |
| 50 void PushMessagingDispatcher::subscribe( | |
| 51 blink::WebServiceWorkerRegistration* service_worker_registration, | |
| 52 const blink::WebPushSubscriptionOptions& options, | |
| 53 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { | |
| 54 DCHECK(service_worker_registration); | |
| 55 DCHECK(callbacks); | |
| 56 // If a developer provided an application server key in |options|, skip | |
| 57 // fetching the manifest. | |
| 58 if (options.applicationServerKey.isEmpty()) { | |
| 59 RenderFrameImpl::FromRoutingID(routing_id()) | |
| 60 ->manifest_manager() | |
| 61 ->GetManifest(base::Bind( | |
| 62 &PushMessagingDispatcher::DidGetManifest, base::Unretained(this), | |
| 63 service_worker_registration, options, base::Passed(&callbacks))); | |
| 64 } else { | |
| 65 PushSubscriptionOptions content_options; | |
| 66 content_options.user_visible_only = options.userVisibleOnly; | |
| 67 // Just treat the server key as a string of bytes and pass it to the push | |
| 68 // service. | |
| 69 content_options.sender_info = options.applicationServerKey.latin1(); | |
| 70 DoSubscribe(service_worker_registration, content_options, | |
| 71 std::move(callbacks)); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void PushMessagingDispatcher::DidGetManifest( | |
| 76 blink::WebServiceWorkerRegistration* service_worker_registration, | |
| 77 const blink::WebPushSubscriptionOptions& options, | |
| 78 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks, | |
| 79 const GURL& manifest_url, | |
| 80 const Manifest& manifest, | |
| 81 const ManifestDebugInfo&) { | |
| 82 // Get the sender_info from the manifest since it wasn't provided by | |
| 83 // the caller. | |
| 84 if (manifest.IsEmpty()) { | |
| 85 int request_id = subscription_callbacks_.Add(std::move(callbacks)); | |
| 86 OnSubscribeFromDocumentError( | |
| 87 request_id, PUSH_REGISTRATION_STATUS_MANIFEST_EMPTY_OR_MISSING); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 PushSubscriptionOptions content_options; | |
| 92 content_options.user_visible_only = options.userVisibleOnly; | |
| 93 if (!manifest.gcm_sender_id.is_null()) { | |
| 94 content_options.sender_info = | |
| 95 base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | |
| 96 } | |
| 97 | |
| 98 DoSubscribe(service_worker_registration, content_options, | |
| 99 std::move(callbacks)); | |
| 100 } | |
| 101 | |
| 102 void PushMessagingDispatcher::DoSubscribe( | |
| 103 blink::WebServiceWorkerRegistration* service_worker_registration, | |
| 104 const PushSubscriptionOptions& options, | |
| 105 std::unique_ptr<blink::WebPushSubscriptionCallbacks> callbacks) { | |
| 106 int request_id = subscription_callbacks_.Add(std::move(callbacks)); | |
| 107 int64_t service_worker_registration_id = | |
| 108 static_cast<WebServiceWorkerRegistrationImpl*>( | |
| 109 service_worker_registration) | |
| 110 ->registrationId(); | |
| 111 | |
| 112 if (options.sender_info.empty()) { | |
| 113 OnSubscribeFromDocumentError(request_id, | |
| 114 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | |
| 115 return; | |
| 116 } | |
| 117 Send(new PushMessagingHostMsg_Subscribe( | |
| 118 routing_id(), request_id, service_worker_registration_id, options)); | |
| 119 } | |
| 120 | |
| 121 void PushMessagingDispatcher::OnSubscribeFromDocumentSuccess( | |
| 122 int32_t request_id, | |
| 123 const GURL& endpoint, | |
| 124 const PushSubscriptionOptions& options, | |
| 125 const std::vector<uint8_t>& p256dh, | |
| 126 const std::vector<uint8_t>& auth) { | |
| 127 blink::WebPushSubscriptionCallbacks* callbacks = | |
| 128 subscription_callbacks_.Lookup(request_id); | |
| 129 DCHECK(callbacks); | |
| 130 | |
| 131 callbacks->onSuccess(base::MakeUnique<blink::WebPushSubscription>( | |
| 132 endpoint, options.user_visible_only, | |
| 133 blink::WebString::fromLatin1(options.sender_info), p256dh, auth)); | |
| 134 | |
| 135 subscription_callbacks_.Remove(request_id); | |
| 136 } | |
| 137 | |
| 138 void PushMessagingDispatcher::OnSubscribeFromDocumentError( | |
| 139 int32_t request_id, | |
| 140 PushRegistrationStatus status) { | |
| 141 blink::WebPushSubscriptionCallbacks* callbacks = | |
| 142 subscription_callbacks_.Lookup(request_id); | |
| 143 DCHECK(callbacks); | |
| 144 | |
| 145 callbacks->onError(PushRegistrationStatusToWebPushError(status)); | |
| 146 | |
| 147 subscription_callbacks_.Remove(request_id); | |
| 148 } | |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |