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