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/WebServiceWorkerRegistration.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
15 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushError.h"
16 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscription.h"
17 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscriptionOptions.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 scoped_ptr
<blink::WebPushSubscription
> subscription(
94 new blink::WebPushSubscription(endpoint
, curve25519dh
));
95 callbacks
->onSuccess(subscription
.release());
97 subscription_callbacks_
.Remove(request_id
);
100 void PushMessagingDispatcher::OnSubscribeFromDocumentError(
102 PushRegistrationStatus status
) {
103 blink::WebPushSubscriptionCallbacks
* callbacks
=
104 subscription_callbacks_
.Lookup(request_id
);
107 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
108 blink::WebPushError::ErrorTypeAbort
,
109 blink::WebString::fromUTF8(PushRegistrationStatusToString(status
))));
110 callbacks
->onError(error
.release());
112 subscription_callbacks_
.Remove(request_id
);
115 } // namespace content