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/WebPushRegistration.h"
19 using blink::WebString
;
23 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame
* render_frame
)
24 : RenderFrameObserver(render_frame
) {
27 PushMessagingDispatcher::~PushMessagingDispatcher() {}
29 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message
& message
) {
31 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher
, message
)
32 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentSuccess
,
33 OnRegisterFromDocumentSuccess
)
34 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentError
,
35 OnRegisterFromDocumentError
)
36 IPC_MESSAGE_UNHANDLED(handled
= false)
41 void PushMessagingDispatcher::registerPushMessaging(
42 blink::WebServiceWorkerRegistration
* service_worker_registration
,
43 blink::WebPushRegistrationCallbacks
* callbacks
) {
44 DCHECK(service_worker_registration
);
46 RenderFrameImpl::FromRoutingID(routing_id())
48 ->GetManifest(base::Bind(&PushMessagingDispatcher::DoRegister
,
49 base::Unretained(this),
50 service_worker_registration
, callbacks
));
53 void PushMessagingDispatcher::DoRegister(
54 blink::WebServiceWorkerRegistration
* service_worker_registration
,
55 blink::WebPushRegistrationCallbacks
* callbacks
,
56 const Manifest
& manifest
) {
57 int request_id
= registration_callbacks_
.Add(callbacks
);
58 int64 service_worker_registration_id
=
59 static_cast<WebServiceWorkerRegistrationImpl
*>(
60 service_worker_registration
)->registration_id();
62 std::string sender_id
=
63 manifest
.gcm_sender_id
.is_null()
65 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string());
66 if (sender_id
.empty()) {
67 OnRegisterFromDocumentError(request_id
,
68 PUSH_REGISTRATION_STATUS_NO_SENDER_ID
);
72 Send(new PushMessagingHostMsg_RegisterFromDocument(
73 routing_id(), request_id
,
74 manifest
.gcm_sender_id
.is_null()
76 : base::UTF16ToUTF8(manifest
.gcm_sender_id
.string()),
77 manifest
.gcm_user_visible_only
, service_worker_registration_id
));
80 void PushMessagingDispatcher::OnRegisterFromDocumentSuccess(
83 const std::string
& registration_id
) {
84 blink::WebPushRegistrationCallbacks
* callbacks
=
85 registration_callbacks_
.Lookup(request_id
);
88 scoped_ptr
<blink::WebPushRegistration
> registration(
89 new blink::WebPushRegistration(
90 WebString::fromUTF8(endpoint
.spec()),
91 WebString::fromUTF8(registration_id
)));
92 callbacks
->onSuccess(registration
.release());
93 registration_callbacks_
.Remove(request_id
);
96 void PushMessagingDispatcher::OnRegisterFromDocumentError(
98 PushRegistrationStatus status
) {
99 blink::WebPushRegistrationCallbacks
* callbacks
=
100 registration_callbacks_
.Lookup(request_id
);
103 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
104 blink::WebPushError::ErrorTypeAbort
,
105 WebString::fromUTF8(PushRegistrationStatusToString(status
))));
106 callbacks
->onError(error
.release());
107 registration_callbacks_
.Remove(request_id
);
110 } // namespace content