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 "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/WebPushError.h" | |
14 #include "third_party/WebKit/public/platform/WebPushRegistration.h" | |
15 #include "third_party/WebKit/public/platform/WebServiceWorkerRegistration.h" | |
16 #include "third_party/WebKit/public/platform/WebString.h" | |
17 #include "url/gurl.h" | |
18 | |
19 using blink::WebString; | |
20 | |
21 namespace content { | |
22 | |
23 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame) | |
24 : RenderFrameObserver(render_frame) { | |
25 } | |
26 | |
27 PushMessagingDispatcher::~PushMessagingDispatcher() {} | |
28 | |
29 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { | |
30 bool handled = true; | |
31 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) | |
32 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentSuccess, | |
33 OnRegisterFromDocumentSuccess) | |
34 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentError, | |
35 OnRegisterFromDocumentError) | |
36 IPC_MESSAGE_UNHANDLED(handled = false) | |
37 IPC_END_MESSAGE_MAP() | |
38 return handled; | |
39 } | |
40 | |
41 void PushMessagingDispatcher::registerPushMessaging( | |
42 blink::WebServiceWorkerRegistration* service_worker_registration, | |
43 blink::WebPushRegistrationCallbacks* callbacks) { | |
44 DCHECK(service_worker_registration); | |
45 DCHECK(callbacks); | |
46 RenderFrameImpl::FromRoutingID(routing_id()) | |
47 ->manifest_manager() | |
48 ->GetManifest(base::Bind(&PushMessagingDispatcher::DoRegister, | |
49 base::Unretained(this), | |
50 service_worker_registration, callbacks)); | |
51 } | |
52 | |
53 void PushMessagingDispatcher::DoRegister( | |
54 blink::WebServiceWorkerRegistration* service_worker_registration, | |
55 blink::WebPushRegistrationCallbacks* callbacks, | |
56 const Manifest& manifest) { | |
57 int request_id = registration_callbacks_.Add(callbacks); | |
58 int64 service_worker_registration_id = | |
59 static_cast<WebServiceWorkerRegistrationImpl*>( | |
60 service_worker_registration)->registration_id(); | |
61 | |
62 std::string sender_id = | |
63 manifest.gcm_sender_id.is_null() | |
64 ? std::string() | |
65 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | |
66 if (sender_id.empty()) { | |
67 OnRegisterFromDocumentError(request_id, | |
68 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | |
69 return; | |
70 } | |
71 | |
72 Send(new PushMessagingHostMsg_RegisterFromDocument( | |
73 routing_id(), request_id, | |
74 manifest.gcm_sender_id.is_null() | |
75 ? std::string() | |
76 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()), | |
77 manifest.gcm_user_visible_only, service_worker_registration_id)); | |
78 } | |
79 | |
80 void PushMessagingDispatcher::OnRegisterFromDocumentSuccess( | |
81 int32 request_id, | |
82 const GURL& endpoint, | |
83 const std::string& registration_id) { | |
84 blink::WebPushRegistrationCallbacks* callbacks = | |
85 registration_callbacks_.Lookup(request_id); | |
86 DCHECK(callbacks); | |
87 | |
88 scoped_ptr<blink::WebPushRegistration> registration( | |
89 new blink::WebPushRegistration( | |
90 WebString::fromUTF8(endpoint.spec()), | |
91 WebString::fromUTF8(registration_id))); | |
92 callbacks->onSuccess(registration.release()); | |
93 registration_callbacks_.Remove(request_id); | |
94 } | |
95 | |
96 void PushMessagingDispatcher::OnRegisterFromDocumentError( | |
97 int32 request_id, | |
98 PushRegistrationStatus status) { | |
99 blink::WebPushRegistrationCallbacks* callbacks = | |
100 registration_callbacks_.Lookup(request_id); | |
101 DCHECK(callbacks); | |
102 | |
103 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( | |
104 blink::WebPushError::ErrorTypeAbort, | |
105 WebString::fromUTF8(PushRegistrationStatusToString(status)))); | |
106 callbacks->onError(error.release()); | |
107 registration_callbacks_.Remove(request_id); | |
108 } | |
109 | |
110 } // namespace content | |
OLD | NEW |