| 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/child/notifications/notification_manager.h" | 5 #include "content/child/notifications/notification_manager.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| 11 #include "content/child/notifications/notification_data_conversions.h" | 11 #include "content/child/notifications/notification_data_conversions.h" |
| 12 #include "content/child/notifications/notification_dispatcher.h" | 12 #include "content/child/notifications/notification_dispatcher.h" |
| 13 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 13 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 14 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
| 15 #include "content/child/worker_task_runner.h" | 15 #include "content/child/worker_task_runner.h" |
| 16 #include "content/common/platform_notification_messages.h" | |
| 17 #include "content/public/common/platform_notification_data.h" | 16 #include "content/public/common/platform_notification_data.h" |
| 18 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 17 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" |
| 19 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" | 18 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 | 20 |
| 22 using blink::WebNotificationPermission; | 21 using blink::WebNotificationPermission; |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 pending_notifications_.FetchPersistentNotificationResources( | 106 pending_notifications_.FetchPersistentNotificationResources( |
| 108 notification_data, | 107 notification_data, |
| 109 base::Bind(&NotificationManager::DisplayPersistentNotification, | 108 base::Bind(&NotificationManager::DisplayPersistentNotification, |
| 110 base::Unretained(this), // this owns |pending_notifications_| | 109 base::Unretained(this), // this owns |pending_notifications_| |
| 111 origin, | 110 origin, |
| 112 notification_data, | 111 notification_data, |
| 113 service_worker_registration_id, | 112 service_worker_registration_id, |
| 114 base::Passed(&owned_callbacks))); | 113 base::Passed(&owned_callbacks))); |
| 115 } | 114 } |
| 116 | 115 |
| 116 void NotificationManager::getNotifications( |
| 117 const blink::WebString& filter_tag, |
| 118 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 119 blink::WebNotificationGetCallbacks* callbacks) { |
| 120 DCHECK(service_worker_registration); |
| 121 DCHECK(callbacks); |
| 122 |
| 123 WebServiceWorkerRegistrationImpl* service_worker_registration_impl = |
| 124 static_cast<WebServiceWorkerRegistrationImpl*>( |
| 125 service_worker_registration); |
| 126 |
| 127 GURL origin = GURL(service_worker_registration_impl->scope()).GetOrigin(); |
| 128 int64 service_worker_registration_id = |
| 129 service_worker_registration_impl->registration_id(); |
| 130 |
| 131 // TODO(peter): GenerateNotificationId is more of a request id. Consider |
| 132 // renaming the method in the NotificationDispatcher if this makes sense. |
| 133 int request_id = |
| 134 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 135 |
| 136 pending_get_notification_requests_.AddWithID(callbacks, request_id); |
| 137 |
| 138 thread_safe_sender_->Send( |
| 139 new PlatformNotificationHostMsg_GetNotifications( |
| 140 request_id, |
| 141 service_worker_registration_id, |
| 142 origin, |
| 143 base::UTF16ToUTF8(filter_tag))); |
| 144 } |
| 145 |
| 117 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { | 146 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { |
| 118 if (pending_notifications_.CancelPageNotificationFetches(delegate)) | 147 if (pending_notifications_.CancelPageNotificationFetches(delegate)) |
| 119 return; | 148 return; |
| 120 | 149 |
| 121 for (auto& iter : active_page_notifications_) { | 150 for (auto& iter : active_page_notifications_) { |
| 122 if (iter.second != delegate) | 151 if (iter.second != delegate) |
| 123 continue; | 152 continue; |
| 124 | 153 |
| 125 thread_safe_sender_->Send( | 154 thread_safe_sender_->Send( |
| 126 new PlatformNotificationHostMsg_Close(iter.first)); | 155 new PlatformNotificationHostMsg_Close(iter.first)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 193 |
| 165 return permission; | 194 return permission; |
| 166 } | 195 } |
| 167 | 196 |
| 168 bool NotificationManager::OnMessageReceived(const IPC::Message& message) { | 197 bool NotificationManager::OnMessageReceived(const IPC::Message& message) { |
| 169 bool handled = true; | 198 bool handled = true; |
| 170 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message) | 199 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message) |
| 171 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow); | 200 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow); |
| 172 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose); | 201 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose); |
| 173 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick); | 202 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick); |
| 203 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidGetNotifications, |
| 204 OnDidGetNotifications) |
| 174 IPC_MESSAGE_UNHANDLED(handled = false) | 205 IPC_MESSAGE_UNHANDLED(handled = false) |
| 175 IPC_END_MESSAGE_MAP() | 206 IPC_END_MESSAGE_MAP() |
| 176 | 207 |
| 177 return handled; | 208 return handled; |
| 178 } | 209 } |
| 179 | 210 |
| 180 void NotificationManager::OnDidShow(int notification_id) { | 211 void NotificationManager::OnDidShow(int notification_id) { |
| 181 const auto& iter = active_page_notifications_.find(notification_id); | 212 const auto& iter = active_page_notifications_.find(notification_id); |
| 182 if (iter == active_page_notifications_.end()) | 213 if (iter == active_page_notifications_.end()) |
| 183 return; | 214 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 195 } | 226 } |
| 196 | 227 |
| 197 void NotificationManager::OnDidClick(int notification_id) { | 228 void NotificationManager::OnDidClick(int notification_id) { |
| 198 const auto& iter = active_page_notifications_.find(notification_id); | 229 const auto& iter = active_page_notifications_.find(notification_id); |
| 199 if (iter == active_page_notifications_.end()) | 230 if (iter == active_page_notifications_.end()) |
| 200 return; | 231 return; |
| 201 | 232 |
| 202 iter->second->dispatchClickEvent(); | 233 iter->second->dispatchClickEvent(); |
| 203 } | 234 } |
| 204 | 235 |
| 236 void NotificationManager::OnDidGetNotifications( |
| 237 int request_id, |
| 238 const std::vector<PersistentNotificationInfo>& notification_infos) { |
| 239 blink::WebNotificationGetCallbacks* callbacks = |
| 240 pending_get_notification_requests_.Lookup(request_id); |
| 241 DCHECK(callbacks); |
| 242 if (!callbacks) |
| 243 return; |
| 244 |
| 245 scoped_ptr<blink::WebVector<blink::WebPersistentNotificationInfo>> |
| 246 notifications(new blink::WebVector<blink::WebPersistentNotificationInfo>( |
| 247 notification_infos.size())); |
| 248 |
| 249 for (size_t i = 0; i < notification_infos.size(); ++i) { |
| 250 blink::WebPersistentNotificationInfo web_notification_info; |
| 251 web_notification_info.persistentNotificationId = |
| 252 blink::WebString::fromUTF8(notification_infos[i].first); |
| 253 web_notification_info.data = |
| 254 ToWebNotificationData(notification_infos[i].second); |
| 255 |
| 256 (*notifications)[i] = web_notification_info; |
| 257 } |
| 258 |
| 259 callbacks->onSuccess(notifications.release()); |
| 260 |
| 261 pending_get_notification_requests_.Remove(request_id); |
| 262 } |
| 263 |
| 205 void NotificationManager::DisplayPageNotification( | 264 void NotificationManager::DisplayPageNotification( |
| 206 const blink::WebSerializedOrigin& origin, | 265 const blink::WebSerializedOrigin& origin, |
| 207 const blink::WebNotificationData& notification_data, | 266 const blink::WebNotificationData& notification_data, |
| 208 blink::WebNotificationDelegate* delegate, | 267 blink::WebNotificationDelegate* delegate, |
| 209 const SkBitmap& icon) { | 268 const SkBitmap& icon) { |
| 210 int notification_id = | 269 int notification_id = |
| 211 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 270 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 212 | 271 |
| 213 active_page_notifications_[notification_id] = delegate; | 272 active_page_notifications_[notification_id] = delegate; |
| 214 thread_safe_sender_->Send( | 273 thread_safe_sender_->Send( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 231 GURL(origin.string()), | 290 GURL(origin.string()), |
| 232 icon, | 291 icon, |
| 233 ToPlatformNotificationData(notification_data))); | 292 ToPlatformNotificationData(notification_data))); |
| 234 | 293 |
| 235 // There currently isn't a case in which the promise would be rejected per | 294 // There currently isn't a case in which the promise would be rejected per |
| 236 // our implementation, so always resolve it here. | 295 // our implementation, so always resolve it here. |
| 237 callbacks->onSuccess(); | 296 callbacks->onSuccess(); |
| 238 } | 297 } |
| 239 | 298 |
| 240 } // namespace content | 299 } // namespace content |
| OLD | NEW |