OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/push_messaging/push_messaging_dispatcher.h" | 5 #include "content/renderer/push_messaging/push_messaging_dispatcher.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 8 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
9 #include "content/common/push_messaging_messages.h" | 9 #include "content/common/push_messaging_messages.h" |
10 #include "content/renderer/manifest/manifest_manager.h" | 10 #include "content/renderer/manifest/manifest_manager.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame) | 24 PushMessagingDispatcher::PushMessagingDispatcher(RenderFrame* render_frame) |
25 : RenderFrameObserver(render_frame) { | 25 : RenderFrameObserver(render_frame) { |
26 } | 26 } |
27 | 27 |
28 PushMessagingDispatcher::~PushMessagingDispatcher() {} | 28 PushMessagingDispatcher::~PushMessagingDispatcher() {} |
29 | 29 |
30 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { | 30 bool PushMessagingDispatcher::OnMessageReceived(const IPC::Message& message) { |
31 bool handled = true; | 31 bool handled = true; |
32 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) | 32 IPC_BEGIN_MESSAGE_MAP(PushMessagingDispatcher, message) |
33 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentSuccess, | 33 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentSuccess, |
34 OnRegisterFromDocumentSuccess) | 34 OnSubscribeFromDocumentSuccess) |
35 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromDocumentError, | 35 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromDocumentError, |
36 OnRegisterFromDocumentError) | 36 OnSubscribeFromDocumentError) |
37 IPC_MESSAGE_UNHANDLED(handled = false) | 37 IPC_MESSAGE_UNHANDLED(handled = false) |
38 IPC_END_MESSAGE_MAP() | 38 IPC_END_MESSAGE_MAP() |
39 return handled; | 39 return handled; |
40 } | 40 } |
41 | 41 |
42 void PushMessagingDispatcher::subscribe( | 42 void PushMessagingDispatcher::subscribe( |
43 blink::WebServiceWorkerRegistration* service_worker_registration, | 43 blink::WebServiceWorkerRegistration* service_worker_registration, |
44 const blink::WebPushSubscriptionOptions& options, | 44 const blink::WebPushSubscriptionOptions& options, |
45 blink::WebPushSubscriptionCallbacks* callbacks) { | 45 blink::WebPushSubscriptionCallbacks* callbacks) { |
46 DCHECK(service_worker_registration); | 46 DCHECK(service_worker_registration); |
(...skipping 15 matching lines...) Expand all Loading... |
62 int request_id = subscription_callbacks_.Add(callbacks); | 62 int request_id = subscription_callbacks_.Add(callbacks); |
63 int64_t service_worker_registration_id = | 63 int64_t service_worker_registration_id = |
64 static_cast<WebServiceWorkerRegistrationImpl*>( | 64 static_cast<WebServiceWorkerRegistrationImpl*>( |
65 service_worker_registration)->registration_id(); | 65 service_worker_registration)->registration_id(); |
66 | 66 |
67 std::string sender_id = | 67 std::string sender_id = |
68 manifest.gcm_sender_id.is_null() | 68 manifest.gcm_sender_id.is_null() |
69 ? std::string() | 69 ? std::string() |
70 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()); | 70 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()); |
71 if (sender_id.empty()) { | 71 if (sender_id.empty()) { |
72 OnRegisterFromDocumentError(request_id, | 72 OnSubscribeFromDocumentError(request_id, |
73 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | 73 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
74 return; | 74 return; |
75 } | 75 } |
76 | 76 |
77 // TODO(peter): Display a deprecation warning if gcm_user_visible_only is | 77 // TODO(peter): Display a deprecation warning if gcm_user_visible_only is |
78 // set to true. See https://crbug.com/471534 | 78 // set to true. See https://crbug.com/471534 |
79 const bool user_visible = manifest.gcm_user_visible_only || | 79 const bool user_visible = manifest.gcm_user_visible_only || |
80 options.userVisibleOnly; | 80 options.userVisibleOnly; |
81 | 81 |
82 Send(new PushMessagingHostMsg_RegisterFromDocument( | 82 Send(new PushMessagingHostMsg_SubscribeFromDocument( |
83 routing_id(), request_id, | 83 routing_id(), request_id, |
84 manifest.gcm_sender_id.is_null() | 84 manifest.gcm_sender_id.is_null() |
85 ? std::string() | 85 ? std::string() |
86 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()), | 86 : base::UTF16ToUTF8(manifest.gcm_sender_id.string()), |
87 user_visible, service_worker_registration_id)); | 87 user_visible, service_worker_registration_id)); |
88 } | 88 } |
89 | 89 |
90 void PushMessagingDispatcher::OnRegisterFromDocumentSuccess( | 90 void PushMessagingDispatcher::OnSubscribeFromDocumentSuccess( |
91 int32_t request_id, | 91 int32_t request_id, |
92 const GURL& endpoint, | 92 const GURL& endpoint, |
93 const std::string& registration_id) { | 93 const std::string& subscription_id) { |
94 blink::WebPushSubscriptionCallbacks* callbacks = | 94 blink::WebPushSubscriptionCallbacks* callbacks = |
95 subscription_callbacks_.Lookup(request_id); | 95 subscription_callbacks_.Lookup(request_id); |
96 DCHECK(callbacks); | 96 DCHECK(callbacks); |
97 | 97 |
98 scoped_ptr<blink::WebPushSubscription> subscription( | 98 scoped_ptr<blink::WebPushSubscription> subscription( |
99 new blink::WebPushSubscription( | 99 new blink::WebPushSubscription( |
100 WebString::fromUTF8(endpoint.spec()), | 100 WebString::fromUTF8(endpoint.spec()), |
101 WebString::fromUTF8(registration_id))); | 101 WebString::fromUTF8(subscription_id))); |
102 callbacks->onSuccess(subscription.release()); | 102 callbacks->onSuccess(subscription.release()); |
103 | 103 |
104 subscription_callbacks_.Remove(request_id); | 104 subscription_callbacks_.Remove(request_id); |
105 } | 105 } |
106 | 106 |
107 void PushMessagingDispatcher::OnRegisterFromDocumentError( | 107 void PushMessagingDispatcher::OnSubscribeFromDocumentError( |
108 int32_t request_id, | 108 int32_t request_id, |
109 PushRegistrationStatus status) { | 109 PushRegistrationStatus status) { |
110 blink::WebPushSubscriptionCallbacks* callbacks = | 110 blink::WebPushSubscriptionCallbacks* callbacks = |
111 subscription_callbacks_.Lookup(request_id); | 111 subscription_callbacks_.Lookup(request_id); |
112 DCHECK(callbacks); | 112 DCHECK(callbacks); |
113 | 113 |
114 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( | 114 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( |
115 blink::WebPushError::ErrorTypeAbort, | 115 blink::WebPushError::ErrorTypeAbort, |
116 WebString::fromUTF8(PushRegistrationStatusToString(status)))); | 116 WebString::fromUTF8(PushRegistrationStatusToString(status)))); |
117 callbacks->onError(error.release()); | 117 callbacks->onError(error.release()); |
118 | 118 |
119 subscription_callbacks_.Remove(request_id); | 119 subscription_callbacks_.Remove(request_id); |
120 } | 120 } |
121 | 121 |
122 } // namespace content | 122 } // namespace content |
OLD | NEW |