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