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.
5 #include "content/renderer/push_messaging/push_messaging_dispatcher.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/child/service_worker/web_service_worker_registration_impl.h"
9 #include "content/common/push_messaging_messages.h"
10 #include "content/renderer/manifest/manifest_manager.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "ipc/ipc_message.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushError.h"
15 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscription.h"
16 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscriptionOptions.h"
17 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
18 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
24 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame
* render_frame
)
25 : RenderFrameObserver(render_frame
) {
28 PushMessagingDispatcher::~PushMessagingDispatcher() {}
30 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message
& message
) {
32 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher
, message
)
33 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentSuccess
,
34 OnSubscribeFromDocumentSuccess
)
35 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentError
,
36 OnSubscribeFromDocumentError
)
37 IPC_MESSAGE_UNHANDLED(handled
= false)
42 void PushMessagingDispatcher::subscribe(
43 blink::WebServiceWorkerRegistration
* service_worker_registration
,
44 const blink::WebPushSubscriptionOptions
& options
,
45 blink::WebPushSubscriptionCallbacks
* callbacks
) {
46 DCHECK(service_worker_registration
);
48 RenderFrameImpl::FromRoutingID(routing_id())
50 ->GetManifest(base::Bind(&PushMessagingDispatcher::DoSubscribe
,
51 base::Unretained(this),
52 service_worker_registration
,
57 void PushMessagingDispatcher::DoSubscribe(
58 blink::WebServiceWorkerRegistration
* service_worker_registration
,
59 const blink::WebPushSubscriptionOptions
& options
,
60 blink::WebPushSubscriptionCallbacks
* callbacks
,
61 const Manifest
& manifest
) {
62 int request_id
= subscription_callbacks_
.Add(callbacks
);
63 int64_t service_worker_registration_id
=
64 static_cast<WebServiceWorkerRegistrationImpl
*>(
65 service_worker_registration
)->registration_id();
67 std::string sender_id
=
68 manifest
.gcm_sender_id
.is_null()
70 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string());
71 if (sender_id
.empty()) {
72 OnSubscribeFromDocumentError(request_id
,
73 PUSH_REGISTRATION_STATUS_NO_SENDER_ID
);
77 Send(new PushMessagingHostMsg_SubscribeFromDocument(
78 routing_id(), request_id
,
79 manifest
.gcm_sender_id
.is_null()
81 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string()),
82 options
.userVisibleOnly
, service_worker_registration_id
));
85 void PushMessagingDispatcher::OnSubscribeFromDocumentSuccess(
88 const std::vector
<uint8_t>& curve25519dh
) {
89 blink::WebPushSubscriptionCallbacks
* callbacks
=
90 subscription_callbacks_
.Lookup(request_id
);
93 callbacks
->onSuccess(blink::adoptWebPtr(
94 new blink::WebPushSubscription(endpoint
, curve25519dh
)));
96 subscription_callbacks_
.Remove(request_id
);
99 void PushMessagingDispatcher::OnSubscribeFromDocumentError(
101 PushRegistrationStatus status
) {
102 blink::WebPushSubscriptionCallbacks
* callbacks
=
103 subscription_callbacks_
.Lookup(request_id
);
106 blink::WebPushError::ErrorType error_type
=
107 status
== PUSH_REGISTRATION_STATUS_PERMISSION_DENIED
108 ? blink::WebPushError::ErrorTypePermissionDenied
109 : blink::WebPushError::ErrorTypeAbort
;
111 callbacks
->onError(blink::WebPushError(
113 blink::WebString::fromUTF8(PushRegistrationStatusToString(status
))));
115 subscription_callbacks_
.Remove(request_id
);
118 } // namespace content