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.
4
5 #include "content/renderer/push_messaging_dispatcher.h"
6
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"
15 #include "url/gurl.h"
16
17 using blink::WebString;
18
19 namespace content {
20
PushMessagingDispatcher(RenderFrame * render_frame)21 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame)
22 : RenderFrameObserver(render_frame) {
23 }
24
~PushMessagingDispatcher()25 PushMessagingDispatcher::~PushMessagingDispatcher() {}
26
OnMessageReceived(const IPC::Message & message)27 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) {
28 bool handled = true;
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)
33 IPC_END_MESSAGE_MAP()
34 return handled;
35 }
36
registerPushMessaging(const WebString & sender_id,blink::WebPushRegistrationCallbacks * callbacks)37 void PushMessagingDispatcher::registerPushMessaging(
38 const WebString& sender_id,
39 blink::WebPushRegistrationCallbacks* callbacks) {
40 DCHECK(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());
46 delete callbacks;
47 }
48
registerPushMessaging(const WebString & sender_id,blink::WebPushRegistrationCallbacks * callbacks,blink::WebServiceWorkerProvider * service_worker_provider)49 void PushMessagingDispatcher::registerPushMessaging(
50 const WebString& sender_id,
51 blink::WebPushRegistrationCallbacks* callbacks,
52 blink::WebServiceWorkerProvider* service_worker_provider) {
53 DCHECK(callbacks);
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(
58 routing_id(),
59 callbacks_id,
60 sender_id.utf8(),
61 blink::WebUserGestureIndicator::isProcessingUserGesture(),
62 service_worker_provider_id));
63 }
64
OnRegisterSuccess(int32 callbacks_id,const GURL & endpoint,const std::string & registration_id)65 void PushMessagingDispatcher::OnRegisterSuccess(
66 int32 callbacks_id,
67 const GURL& endpoint,
68 const std::string& registration_id) {
69 blink::WebPushRegistrationCallbacks* callbacks =
70 registration_callbacks_.Lookup(callbacks_id);
71 CHECK(callbacks);
72
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);
79 }
80
OnRegisterError(int32 callbacks_id,PushMessagingStatus status)81 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id,
82 PushMessagingStatus status) {
83 blink::WebPushRegistrationCallbacks* callbacks =
84 registration_callbacks_.Lookup(callbacks_id);
85 CHECK(callbacks);
86
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);
92 }
93
94 } // namespace content
95