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_dispatcher.h"
7 #include "content/child/service_worker/web_service_worker_provider_impl.h"
8 #include "content/common/push_messaging_messages.h"
9 #include "ipc/ipc_message.h"
10 #include "third_party/WebKit/public/platform/WebPushError.h"
11 #include "third_party/WebKit/public/platform/WebPushRegistration.h"
12 #include "third_party/WebKit/public/platform/WebServiceWorkerProvider.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
17 using blink::WebString
;
21 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame
* render_frame
)
22 : RenderFrameObserver(render_frame
) {
25 PushMessagingDispatcher::~PushMessagingDispatcher() {}
27 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message
& message
) {
29 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher
, message
)
30 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess
, OnRegisterSuccess
)
31 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError
, OnRegisterError
)
32 IPC_MESSAGE_UNHANDLED(handled
= false)
37 void PushMessagingDispatcher::registerPushMessaging(
38 const WebString
& sender_id
,
39 blink::WebPushRegistrationCallbacks
* callbacks
) {
41 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
42 blink::WebPushError::ErrorTypeAbort
,
43 WebString::fromUTF8(PushMessagingStatusToString(
44 PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER
))));
45 callbacks
->onError(error
.release());
49 void PushMessagingDispatcher::registerPushMessaging(
50 const WebString
& sender_id
,
51 blink::WebPushRegistrationCallbacks
* callbacks
,
52 blink::WebServiceWorkerProvider
* service_worker_provider
) {
54 int callbacks_id
= registration_callbacks_
.Add(callbacks
);
55 int service_worker_provider_id
= static_cast<WebServiceWorkerProviderImpl
*>(
56 service_worker_provider
)->provider_id();
57 Send(new PushMessagingHostMsg_Register(
61 blink::WebUserGestureIndicator::isProcessingUserGesture(),
62 service_worker_provider_id
));
65 void PushMessagingDispatcher::OnRegisterSuccess(
68 const std::string
& registration_id
) {
69 blink::WebPushRegistrationCallbacks
* callbacks
=
70 registration_callbacks_
.Lookup(callbacks_id
);
73 scoped_ptr
<blink::WebPushRegistration
> registration(
74 new blink::WebPushRegistration(
75 WebString::fromUTF8(endpoint
.spec()),
76 WebString::fromUTF8(registration_id
)));
77 callbacks
->onSuccess(registration
.release());
78 registration_callbacks_
.Remove(callbacks_id
);
81 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id
,
82 PushMessagingStatus status
) {
83 blink::WebPushRegistrationCallbacks
* callbacks
=
84 registration_callbacks_
.Lookup(callbacks_id
);
87 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
88 blink::WebPushError::ErrorTypeAbort
,
89 WebString::fromUTF8(PushMessagingStatusToString(status
))));
90 callbacks
->onError(error
.release());
91 registration_callbacks_
.Remove(callbacks_id
);
94 } // namespace content