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
17using blink::WebString;
18
19namespace content {
20
21PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame)
22    : RenderFrameObserver(render_frame) {
23}
24
25PushMessagingDispatcher::~PushMessagingDispatcher() {}
26
27bool 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
37void 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
49void 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
65void 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
81void 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