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(
87 const GURL
& endpoint
) {
88 blink::WebPushSubscriptionCallbacks
* callbacks
=
89 subscription_callbacks_
.Lookup(request_id
);
91 // TODO(peter): Change this back to a DCHECK before M45 branches.
92 CHECK(callbacks
) << "Invalid request id received: " << request_id
;
94 scoped_ptr
<blink::WebPushSubscription
> subscription(
95 new blink::WebPushSubscription(endpoint
));
96 callbacks
->onSuccess(subscription
.release());
98 subscription_callbacks_
.Remove(request_id
);
101 void PushMessagingDispatcher::OnSubscribeFromDocumentError(
103 PushRegistrationStatus status
) {
104 blink::WebPushSubscriptionCallbacks
* callbacks
=
105 subscription_callbacks_
.Lookup(request_id
);
107 // TODO(peter): Change this back to a DCHECK before M45 branches.
108 CHECK(callbacks
) << "Invalid request id received: " << request_id
;
110 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
111 blink::WebPushError::ErrorTypeAbort
,
112 blink::WebString::fromUTF8(PushRegistrationStatusToString(status
))));
113 callbacks
->onError(error
.release());
115 subscription_callbacks_
.Remove(request_id
);
118 } // namespace content