| 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 <cmath> | 7 #include <cmath> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread_local.h" | 14 #include "base/threading/thread_local.h" |
| 15 #include "content/child/notifications/notification_data_conversions.h" | 15 #include "content/child/notifications/notification_data_conversions.h" |
| 16 #include "content/child/notifications/notification_dispatcher.h" | 16 #include "content/child/notifications/notification_dispatcher.h" |
| 17 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 17 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 18 #include "content/child/thread_safe_sender.h" | 18 #include "content/child/thread_safe_sender.h" |
| 19 #include "content/common/notification_constants.h" | 19 #include "content/common/notification_constants.h" |
| 20 #include "content/public/common/notification_resources.h" |
| 20 #include "content/public/common/platform_notification_data.h" | 21 #include "content/public/common/platform_notification_data.h" |
| 21 #include "third_party/WebKit/public/platform/URLConversion.h" | 22 #include "third_party/WebKit/public/platform/URLConversion.h" |
| 22 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | 23 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| 23 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" | 24 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onDelegate.h" |
| 24 #include "third_party/skia/include/core/SkBitmap.h" | |
| 25 | 25 |
| 26 using blink::WebNotificationPermission; | 26 using blink::WebNotificationPermission; |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 int CurrentWorkerId() { | 31 int CurrentWorkerId() { |
| 32 return WorkerThread::GetCurrentId(); | 32 return WorkerThread::GetCurrentId(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky | 37 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky |
| 38 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER; | 38 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER; |
| 39 | 39 |
| 40 NotificationManager::NotificationManager( | 40 NotificationManager::NotificationManager( |
| 41 ThreadSafeSender* thread_safe_sender, | 41 ThreadSafeSender* thread_safe_sender, |
| 42 base::SingleThreadTaskRunner* main_thread_task_runner, | 42 base::SingleThreadTaskRunner* main_thread_task_runner, |
| 43 NotificationDispatcher* notification_dispatcher) | 43 NotificationDispatcher* notification_dispatcher) |
| 44 : thread_safe_sender_(thread_safe_sender), | 44 : thread_safe_sender_(thread_safe_sender), |
| 45 notification_dispatcher_(notification_dispatcher), | 45 notification_dispatcher_(notification_dispatcher), |
| 46 pending_notifications_(main_thread_task_runner) { | 46 notifications_tracker_(main_thread_task_runner) { |
| 47 g_notification_manager_tls.Pointer()->Set(this); | 47 g_notification_manager_tls.Pointer()->Set(this); |
| 48 } | 48 } |
| 49 | 49 |
| 50 NotificationManager::~NotificationManager() { | 50 NotificationManager::~NotificationManager() { |
| 51 g_notification_manager_tls.Pointer()->Set(nullptr); | 51 g_notification_manager_tls.Pointer()->Set(nullptr); |
| 52 } | 52 } |
| 53 | 53 |
| 54 NotificationManager* NotificationManager::ThreadSpecificInstance( | 54 NotificationManager* NotificationManager::ThreadSpecificInstance( |
| 55 ThreadSafeSender* thread_safe_sender, | 55 ThreadSafeSender* thread_safe_sender, |
| 56 base::SingleThreadTaskRunner* main_thread_task_runner, | 56 base::SingleThreadTaskRunner* main_thread_task_runner, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 67 | 67 |
| 68 void NotificationManager::WillStopCurrentWorkerThread() { | 68 void NotificationManager::WillStopCurrentWorkerThread() { |
| 69 delete this; | 69 delete this; |
| 70 } | 70 } |
| 71 | 71 |
| 72 void NotificationManager::show( | 72 void NotificationManager::show( |
| 73 const blink::WebSecurityOrigin& origin, | 73 const blink::WebSecurityOrigin& origin, |
| 74 const blink::WebNotificationData& notification_data, | 74 const blink::WebNotificationData& notification_data, |
| 75 blink::WebNotificationDelegate* delegate) { | 75 blink::WebNotificationDelegate* delegate) { |
| 76 if (notification_data.icon.isEmpty()) { | 76 if (notification_data.icon.isEmpty()) { |
| 77 DisplayPageNotification(origin, notification_data, delegate, SkBitmap()); | 77 DisplayPageNotification(origin, notification_data, delegate, |
| 78 NotificationResources()); |
| 78 return; | 79 return; |
| 79 } | 80 } |
| 80 | 81 |
| 81 pending_notifications_.FetchPageNotificationResources( | 82 notifications_tracker_.FetchPageNotificationResources( |
| 82 notification_data, delegate, | 83 notification_data, delegate, |
| 83 base::Bind(&NotificationManager::DisplayPageNotification, | 84 base::Bind(&NotificationManager::DisplayPageNotification, |
| 84 base::Unretained(this), // this owns |pending_notifications_| | 85 base::Unretained(this), // this owns |notifications_tracker_| |
| 85 origin, notification_data, delegate)); | 86 origin, notification_data, delegate)); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void NotificationManager::showPersistent( | 89 void NotificationManager::showPersistent( |
| 89 const blink::WebSecurityOrigin& origin, | 90 const blink::WebSecurityOrigin& origin, |
| 90 const blink::WebNotificationData& notification_data, | 91 const blink::WebNotificationData& notification_data, |
| 91 blink::WebServiceWorkerRegistration* service_worker_registration, | 92 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 92 blink::WebNotificationShowCallbacks* callbacks) { | 93 blink::WebNotificationShowCallbacks* callbacks) { |
| 93 DCHECK(service_worker_registration); | 94 DCHECK(service_worker_registration); |
| 94 int64_t service_worker_registration_id = | 95 int64_t service_worker_registration_id = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 size_t author_data_size = notification_data.data.size(); | 110 size_t author_data_size = notification_data.data.size(); |
| 110 UMA_HISTOGRAM_MEMORY_KB("Notifications.AuthorDataSizeKB", | 111 UMA_HISTOGRAM_MEMORY_KB("Notifications.AuthorDataSizeKB", |
| 111 static_cast<int>(ceil(author_data_size / 1024.0))); | 112 static_cast<int>(ceil(author_data_size / 1024.0))); |
| 112 | 113 |
| 113 if (author_data_size > PlatformNotificationData::kMaximumDeveloperDataSize) { | 114 if (author_data_size > PlatformNotificationData::kMaximumDeveloperDataSize) { |
| 114 owned_callbacks->onError(); | 115 owned_callbacks->onError(); |
| 115 return; | 116 return; |
| 116 } | 117 } |
| 117 | 118 |
| 118 if (notification_data.icon.isEmpty()) { | 119 if (notification_data.icon.isEmpty()) { |
| 119 DisplayPersistentNotification(origin, notification_data, | 120 DisplayPersistentNotification( |
| 120 service_worker_registration_id, | 121 origin, notification_data, service_worker_registration_id, |
| 121 std::move(owned_callbacks), SkBitmap()); | 122 std::move(owned_callbacks), NotificationResources()); |
| 122 return; | 123 return; |
| 123 } | 124 } |
| 124 | 125 |
| 125 pending_notifications_.FetchPersistentNotificationResources( | 126 notifications_tracker_.FetchPersistentNotificationResources( |
| 126 notification_data, | 127 notification_data, |
| 127 base::Bind(&NotificationManager::DisplayPersistentNotification, | 128 base::Bind(&NotificationManager::DisplayPersistentNotification, |
| 128 base::Unretained(this), // this owns |pending_notifications_| | 129 base::Unretained(this), // this owns |notifications_tracker_| |
| 129 origin, notification_data, service_worker_registration_id, | 130 origin, notification_data, service_worker_registration_id, |
| 130 base::Passed(&owned_callbacks))); | 131 base::Passed(&owned_callbacks))); |
| 131 } | 132 } |
| 132 | 133 |
| 133 void NotificationManager::getNotifications( | 134 void NotificationManager::getNotifications( |
| 134 const blink::WebString& filter_tag, | 135 const blink::WebString& filter_tag, |
| 135 blink::WebServiceWorkerRegistration* service_worker_registration, | 136 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 136 blink::WebNotificationGetCallbacks* callbacks) { | 137 blink::WebNotificationGetCallbacks* callbacks) { |
| 137 DCHECK(service_worker_registration); | 138 DCHECK(service_worker_registration); |
| 138 DCHECK(callbacks); | 139 DCHECK(callbacks); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 151 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 152 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 152 | 153 |
| 153 pending_get_notification_requests_.AddWithID(callbacks, request_id); | 154 pending_get_notification_requests_.AddWithID(callbacks, request_id); |
| 154 | 155 |
| 155 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications( | 156 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications( |
| 156 request_id, service_worker_registration_id, origin, | 157 request_id, service_worker_registration_id, origin, |
| 157 base::UTF16ToUTF8(base::StringPiece16(filter_tag)))); | 158 base::UTF16ToUTF8(base::StringPiece16(filter_tag)))); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { | 161 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { |
| 161 if (pending_notifications_.CancelPageNotificationFetches(delegate)) | 162 if (notifications_tracker_.CancelPageNotificationFetches(delegate)) |
| 162 return; | 163 return; |
| 163 | 164 |
| 164 for (auto& iter : active_page_notifications_) { | 165 for (auto& iter : active_page_notifications_) { |
| 165 if (iter.second != delegate) | 166 if (iter.second != delegate) |
| 166 continue; | 167 continue; |
| 167 | 168 |
| 168 thread_safe_sender_->Send( | 169 thread_safe_sender_->Send( |
| 169 new PlatformNotificationHostMsg_Close(iter.first)); | 170 new PlatformNotificationHostMsg_Close(iter.first)); |
| 170 active_page_notifications_.erase(iter.first); | 171 active_page_notifications_.erase(iter.first); |
| 171 return; | 172 return; |
| 172 } | 173 } |
| 173 | 174 |
| 174 // It should not be possible for Blink to call close() on a Notification which | 175 // It should not be possible for Blink to call close() on a Notification which |
| 175 // does not exist in either the pending or active notification lists. | 176 // does not exist in either the pending or active notification lists. |
| 176 NOTREACHED(); | 177 NOTREACHED(); |
| 177 } | 178 } |
| 178 | 179 |
| 179 void NotificationManager::closePersistent( | 180 void NotificationManager::closePersistent( |
| 180 const blink::WebSecurityOrigin& origin, | 181 const blink::WebSecurityOrigin& origin, |
| 181 int64_t persistent_notification_id) { | 182 int64_t persistent_notification_id) { |
| 182 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( | 183 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( |
| 183 // TODO(mkwst): This is potentially doing the wrong thing with unique | 184 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 184 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 185 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 185 // https://crbug.com/490074 for detail. | 186 // https://crbug.com/490074 for detail. |
| 186 blink::WebStringToGURL(origin.toString()), persistent_notification_id)); | 187 blink::WebStringToGURL(origin.toString()), persistent_notification_id)); |
| 187 } | 188 } |
| 188 | 189 |
| 189 void NotificationManager::notifyDelegateDestroyed( | 190 void NotificationManager::notifyDelegateDestroyed( |
| 190 blink::WebNotificationDelegate* delegate) { | 191 blink::WebNotificationDelegate* delegate) { |
| 191 if (pending_notifications_.CancelPageNotificationFetches(delegate)) | 192 if (notifications_tracker_.CancelPageNotificationFetches(delegate)) |
| 192 return; | 193 return; |
| 193 | 194 |
| 194 for (auto& iter : active_page_notifications_) { | 195 for (auto& iter : active_page_notifications_) { |
| 195 if (iter.second != delegate) | 196 if (iter.second != delegate) |
| 196 continue; | 197 continue; |
| 197 | 198 |
| 198 active_page_notifications_.erase(iter.first); | 199 active_page_notifications_.erase(iter.first); |
| 199 return; | 200 return; |
| 200 } | 201 } |
| 201 } | 202 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 298 |
| 298 callbacks->onSuccess(notifications); | 299 callbacks->onSuccess(notifications); |
| 299 | 300 |
| 300 pending_get_notification_requests_.Remove(request_id); | 301 pending_get_notification_requests_.Remove(request_id); |
| 301 } | 302 } |
| 302 | 303 |
| 303 void NotificationManager::DisplayPageNotification( | 304 void NotificationManager::DisplayPageNotification( |
| 304 const blink::WebSecurityOrigin& origin, | 305 const blink::WebSecurityOrigin& origin, |
| 305 const blink::WebNotificationData& notification_data, | 306 const blink::WebNotificationData& notification_data, |
| 306 blink::WebNotificationDelegate* delegate, | 307 blink::WebNotificationDelegate* delegate, |
| 307 const SkBitmap& icon) { | 308 const NotificationResources& notification_resources) { |
| 308 int notification_id = | 309 int notification_id = |
| 309 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 310 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 310 | 311 |
| 311 active_page_notifications_[notification_id] = delegate; | 312 active_page_notifications_[notification_id] = delegate; |
| 312 // TODO(mkwst): This is potentially doing the wrong thing with unique | 313 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 313 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 314 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 314 // https://crbug.com/490074 for detail. | 315 // https://crbug.com/490074 for detail. |
| 315 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( | 316 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( |
| 316 notification_id, blink::WebStringToGURL(origin.toString()), icon, | 317 notification_id, blink::WebStringToGURL(origin.toString()), |
| 317 ToPlatformNotificationData(notification_data))); | 318 ToPlatformNotificationData(notification_data), notification_resources)); |
| 318 } | 319 } |
| 319 | 320 |
| 320 void NotificationManager::DisplayPersistentNotification( | 321 void NotificationManager::DisplayPersistentNotification( |
| 321 const blink::WebSecurityOrigin& origin, | 322 const blink::WebSecurityOrigin& origin, |
| 322 const blink::WebNotificationData& notification_data, | 323 const blink::WebNotificationData& notification_data, |
| 323 int64_t service_worker_registration_id, | 324 int64_t service_worker_registration_id, |
| 324 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, | 325 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, |
| 325 const SkBitmap& icon) { | 326 const NotificationResources& notification_resources) { |
| 326 // TODO(peter): GenerateNotificationId is more of a request id. Consider | 327 // TODO(peter): GenerateNotificationId is more of a request id. Consider |
| 327 // renaming the method in the NotificationDispatcher if this makes sense. | 328 // renaming the method in the NotificationDispatcher if this makes sense. |
| 328 int request_id = | 329 int request_id = |
| 329 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 330 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 330 | 331 |
| 331 pending_show_notification_requests_.AddWithID(callbacks.release(), | 332 pending_show_notification_requests_.AddWithID(callbacks.release(), |
| 332 request_id); | 333 request_id); |
| 333 | 334 |
| 334 // TODO(mkwst): This is potentially doing the wrong thing with unique | 335 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 335 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 336 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 336 // https://crbug.com/490074 for detail. | 337 // https://crbug.com/490074 for detail. |
| 337 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent( | 338 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent( |
| 338 request_id, service_worker_registration_id, | 339 request_id, service_worker_registration_id, |
| 339 blink::WebStringToGURL(origin.toString()), icon, | 340 blink::WebStringToGURL(origin.toString()), |
| 340 ToPlatformNotificationData(notification_data))); | 341 ToPlatformNotificationData(notification_data), notification_resources)); |
| 341 } | 342 } |
| 342 | 343 |
| 343 } // namespace content | 344 } // namespace content |
| OLD | NEW |