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 |
| 11 using blink::WebString; |
| 12 |
| 13 namespace content { |
| 14 |
| 15 PushMessagingDispatcher::PushMessagingDispatcher(RenderViewImpl* render_view) |
| 16 : RenderViewObserver(render_view) {} |
| 17 |
| 18 PushMessagingDispatcher::~PushMessagingDispatcher() {} |
| 19 |
| 20 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 21 bool handled = true; |
| 22 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) |
| 23 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterSuccess, OnRegisterSuccess) |
| 24 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterError, OnRegisterError) |
| 25 IPC_MESSAGE_UNHANDLED(handled = false) |
| 26 IPC_END_MESSAGE_MAP() |
| 27 return handled; |
| 28 } |
| 29 |
| 30 void PushMessagingDispatcher::registerPushMessaging( |
| 31 const WebString& sender_id, |
| 32 blink::WebPushRegistrationCallbacks* callbacks) { |
| 33 DCHECK(callbacks); |
| 34 int callbacks_id = registration_callbacks_.Add(callbacks); |
| 35 Send(new PushMessagingHostMsg_Register( |
| 36 routing_id(), callbacks_id, sender_id.utf8())); |
| 37 } |
| 38 |
| 39 void PushMessagingDispatcher::OnRegisterSuccess( |
| 40 int32 callbacks_id, |
| 41 const GURL& endpoint, |
| 42 const std::string& registration_id) { |
| 43 blink::WebPushRegistrationCallbacks* callbacks = |
| 44 registration_callbacks_.Lookup(callbacks_id); |
| 45 CHECK(callbacks); |
| 46 |
| 47 scoped_ptr<blink::WebPushRegistration> registration( |
| 48 new blink::WebPushRegistration( |
| 49 WebString::fromUTF8(endpoint.spec()), |
| 50 WebString::fromUTF8(registration_id))); |
| 51 callbacks->onSuccess(registration.release()); |
| 52 registration_callbacks_.Remove(callbacks_id); |
| 53 } |
| 54 |
| 55 void PushMessagingDispatcher::OnRegisterError(int32 callbacks_id) { |
| 56 const std::string kAbortErrorReason = "Registration failed."; |
| 57 blink::WebPushRegistrationCallbacks* callbacks = |
| 58 registration_callbacks_.Lookup(callbacks_id); |
| 59 CHECK(callbacks); |
| 60 |
| 61 scoped_ptr<blink::WebPushError> error( |
| 62 new blink::WebPushError( |
| 63 blink::WebPushError::ErrorTypeAbort, |
| 64 WebString::fromUTF8(kAbortErrorReason))); |
| 65 callbacks->onError(error.release()); |
| 66 registration_callbacks_.Remove(callbacks_id); |
| 67 } |
| 68 |
| 69 } // namespace content |
OLD | NEW |