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"
20 using blink::WebString
;
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_RegisterFromDocumentSuccess
,
34 OnRegisterFromDocumentSuccess
)
35 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentError
,
36 OnRegisterFromDocumentError
)
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::DoRegister
,
51 base::Unretained(this),
52 service_worker_registration
,
57 void PushMessagingDispatcher::registerPushMessaging(
58 blink::WebServiceWorkerRegistration
* service_worker_registration
,
59 blink::WebPushSubscriptionCallbacks
* callbacks
) {
60 subscribe(service_worker_registration
,
61 blink::WebPushSubscriptionOptions(),
65 void PushMessagingDispatcher::DoRegister(
66 blink::WebServiceWorkerRegistration
* service_worker_registration
,
67 const blink::WebPushSubscriptionOptions
& options
,
68 blink::WebPushSubscriptionCallbacks
* callbacks
,
69 const Manifest
& manifest
) {
70 int request_id
= subscription_callbacks_
.Add(callbacks
);
71 int64_t service_worker_registration_id
=
72 static_cast<WebServiceWorkerRegistrationImpl
*>(
73 service_worker_registration
)->registration_id();
75 std::string sender_id
=
76 manifest
.gcm_sender_id
.is_null()
78 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string());
79 if (sender_id
.empty()) {
80 OnRegisterFromDocumentError(request_id
,
81 PUSH_REGISTRATION_STATUS_NO_SENDER_ID
);
85 // TODO(peter): Display a deprecation warning if gcm_user_visible_only is
86 // set to true. See https://crbug.com/471534
87 const bool user_visible
= manifest
.gcm_user_visible_only
||
90 Send(new PushMessagingHostMsg_RegisterFromDocument(
91 routing_id(), request_id
,
92 manifest
.gcm_sender_id
.is_null()
94 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string()),
95 user_visible
, service_worker_registration_id
));
98 void PushMessagingDispatcher::OnRegisterFromDocumentSuccess(
100 const GURL
& endpoint
,
101 const std::string
& registration_id
) {
102 blink::WebPushSubscriptionCallbacks
* callbacks
=
103 subscription_callbacks_
.Lookup(request_id
);
106 scoped_ptr
<blink::WebPushSubscription
> subscription(
107 new blink::WebPushSubscription(
108 WebString::fromUTF8(endpoint
.spec()),
109 WebString::fromUTF8(registration_id
)));
110 callbacks
->onSuccess(subscription
.release());
112 subscription_callbacks_
.Remove(request_id
);
115 void PushMessagingDispatcher::OnRegisterFromDocumentError(
117 PushRegistrationStatus status
) {
118 blink::WebPushSubscriptionCallbacks
* callbacks
=
119 subscription_callbacks_
.Lookup(request_id
);
122 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
123 blink::WebPushError::ErrorTypeAbort
,
124 WebString::fromUTF8(PushRegistrationStatusToString(status
))));
125 callbacks
->onError(error
.release());
127 subscription_callbacks_
.Remove(request_id
);
130 } // namespace content