OLD | NEW |
(Empty) | |
| 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/common/push_messaging_messages.h" |
| 8 #include "content/renderer/render_view_impl.h" |
| 9 #include "third_party/WebKit/public/platform/WebPushRegistration.h" |
| 10 #include "third_party/WebKit/public/platform/WebString.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 PushMessagingDispatcher::PushMessagingDispatcher(RenderViewImpl* render_view) |
| 15 : RenderViewObserver(render_view) {} |
| 16 |
| 17 PushMessagingDispatcher::~PushMessagingDispatcher() {} |
| 18 |
| 19 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 20 bool handled = true; |
| 21 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) |
| 22 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess) |
| 23 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError) |
| 24 IPC_MESSAGE_UNHANDLED(handled = false) |
| 25 IPC_END_MESSAGE_MAP() |
| 26 return handled; |
| 27 } |
| 28 |
| 29 void PushMessagingDispatcher::registerPushMessaging( |
| 30 const blink::WebString& channel_name, |
| 31 blink::WebPushRegistrationCallbacks* callbacks) { |
| 32 DCHECK(callbacks); |
| 33 int callbacks_id = registration_callbacks_.Add(callbacks); |
| 34 Send(new PushMessagingHostMsg_Register( |
| 35 routing_id(), callbacks_id, channel_name.utf8())); |
| 36 } |
| 37 |
| 38 void PushMessagingDispatcher::OnRegisterSuccess( |
| 39 int32 callbacks_id, |
| 40 const std::string& endpoint, |
| 41 const std::string& registration_id, |
| 42 const std::string& channel_name) { |
| 43 blink::WebPushRegistrationCallbacks* callbacks = |
| 44 registration_callbacks_.Lookup(callbacks_id); |
| 45 DCHECK(callbacks); |
| 46 if (!callbacks) |
| 47 return; |
| 48 |
| 49 callbacks->onSuccess(new blink::WebPushRegistration( |
| 50 blink::WebString::fromUTF8(endpoint), |
| 51 blink::WebString::fromUTF8(registration_id), |
| 52 blink::WebString::fromUTF8(channel_name))); |
| 53 registration_callbacks_.Remove(callbacks_id); |
| 54 } |
| 55 |
| 56 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id, |
| 57 PushMessagingStatus status) { |
| 58 blink::WebPushRegistrationCallbacks* callbacks = |
| 59 registration_callbacks_.Lookup(callbacks_id); |
| 60 DCHECK(callbacks); |
| 61 if (!callbacks) |
| 62 return; |
| 63 |
| 64 scoped_ptr<blink::WebPushMessagingError> error( |
| 65 new blink::WebPushMessagingError( |
| 66 blink::WebPushMessagingError::AbortError, |
| 67 blink::WebString::fromUTF8(PushMessagingStatusToString(status)))); |
| 68 callbacks->onError(error.release()); |
| 69 registration_callbacks_.Remove(callbacks_id); |
| 70 } |
| 71 |
| 72 } // namespace content |
OLD | NEW |