Chromium Code Reviews| 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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 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 // Checks whether |notification_data| specifies any non-empty resources that | |
| 36 // need to be fetched. | |
| 37 bool hasResourcesToFetch(const blink::WebNotificationData& notification_data) { | |
|
Peter Beverloo
2016/02/05 15:43:21
nit: HasResourcesToFetch (Chromium style)
Michael van Ouwerkerk
2016/02/08 14:38:52
Done.
| |
| 38 if (!notification_data.icon.isEmpty()) | |
| 39 return true; | |
| 40 for (const auto& action : notification_data.actions) { | |
| 41 if (!action.icon.isEmpty()) | |
| 42 return true; | |
| 43 } | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 35 } // namespace | 47 } // namespace |
| 36 | 48 |
| 37 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky | 49 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky |
| 38 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER; | 50 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER; |
| 39 | 51 |
| 40 NotificationManager::NotificationManager( | 52 NotificationManager::NotificationManager( |
| 41 ThreadSafeSender* thread_safe_sender, | 53 ThreadSafeSender* thread_safe_sender, |
| 42 base::SingleThreadTaskRunner* main_thread_task_runner, | 54 base::SingleThreadTaskRunner* main_thread_task_runner, |
| 43 NotificationDispatcher* notification_dispatcher) | 55 NotificationDispatcher* notification_dispatcher) |
| 44 : thread_safe_sender_(thread_safe_sender), | 56 : thread_safe_sender_(thread_safe_sender), |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 66 } | 78 } |
| 67 | 79 |
| 68 void NotificationManager::WillStopCurrentWorkerThread() { | 80 void NotificationManager::WillStopCurrentWorkerThread() { |
| 69 delete this; | 81 delete this; |
| 70 } | 82 } |
| 71 | 83 |
| 72 void NotificationManager::show( | 84 void NotificationManager::show( |
| 73 const blink::WebSecurityOrigin& origin, | 85 const blink::WebSecurityOrigin& origin, |
| 74 const blink::WebNotificationData& notification_data, | 86 const blink::WebNotificationData& notification_data, |
| 75 blink::WebNotificationDelegate* delegate) { | 87 blink::WebNotificationDelegate* delegate) { |
| 76 if (notification_data.icon.isEmpty()) { | 88 if (!hasResourcesToFetch(notification_data)) { |
| 77 DisplayPageNotification(origin, notification_data, delegate, | 89 DisplayPageNotification(origin, notification_data, delegate, |
| 78 NotificationResources()); | 90 NotificationResources()); |
| 79 return; | 91 return; |
| 80 } | 92 } |
| 81 | 93 |
| 82 notifications_tracker_.FetchPageNotificationResources( | 94 notifications_tracker_.FetchNotificationResources( |
|
Peter Beverloo
2016/02/05 15:43:21
DCHECK_EQ(0u, notification_data.actions.size());
Michael van Ouwerkerk
2016/02/08 14:38:52
Sure. Let's put that at the start of the function
| |
| 83 notification_data, delegate, | 95 notification_data, delegate, |
| 84 base::Bind(&NotificationManager::DisplayPageNotification, | 96 base::Bind(&NotificationManager::DisplayPageNotification, |
| 85 base::Unretained(this), // this owns |notifications_tracker_| | 97 base::Unretained(this), // this owns |notifications_tracker_| |
| 86 origin, notification_data, delegate)); | 98 origin, notification_data, delegate)); |
| 87 } | 99 } |
| 88 | 100 |
| 89 void NotificationManager::showPersistent( | 101 void NotificationManager::showPersistent( |
| 90 const blink::WebSecurityOrigin& origin, | 102 const blink::WebSecurityOrigin& origin, |
| 91 const blink::WebNotificationData& notification_data, | 103 const blink::WebNotificationData& notification_data, |
| 92 blink::WebServiceWorkerRegistration* service_worker_registration, | 104 blink::WebServiceWorkerRegistration* service_worker_registration, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 109 // an indication that something has gone wrong. | 121 // an indication that something has gone wrong. |
| 110 size_t author_data_size = notification_data.data.size(); | 122 size_t author_data_size = notification_data.data.size(); |
| 111 UMA_HISTOGRAM_MEMORY_KB("Notifications.AuthorDataSizeKB", | 123 UMA_HISTOGRAM_MEMORY_KB("Notifications.AuthorDataSizeKB", |
| 112 static_cast<int>(ceil(author_data_size / 1024.0))); | 124 static_cast<int>(ceil(author_data_size / 1024.0))); |
| 113 | 125 |
| 114 if (author_data_size > PlatformNotificationData::kMaximumDeveloperDataSize) { | 126 if (author_data_size > PlatformNotificationData::kMaximumDeveloperDataSize) { |
| 115 owned_callbacks->onError(); | 127 owned_callbacks->onError(); |
| 116 return; | 128 return; |
| 117 } | 129 } |
| 118 | 130 |
| 119 if (notification_data.icon.isEmpty()) { | 131 if (!hasResourcesToFetch(notification_data)) { |
| 132 NotificationResources notification_resources; | |
| 133 if (!notification_data.actions.isEmpty()) { | |
|
Peter Beverloo
2016/02/05 15:43:21
It would be great to document why this is signific
Michael van Ouwerkerk
2016/02/08 14:38:52
Yes, it's a subtle one.
| |
| 134 notification_resources.action_icons.resize( | |
| 135 notification_data.actions.size()); | |
| 136 } | |
| 120 DisplayPersistentNotification( | 137 DisplayPersistentNotification( |
| 121 origin, notification_data, service_worker_registration_id, | 138 origin, notification_data, service_worker_registration_id, |
| 122 std::move(owned_callbacks), NotificationResources()); | 139 std::move(owned_callbacks), notification_resources); |
| 123 return; | 140 return; |
| 124 } | 141 } |
| 125 | 142 |
| 126 notifications_tracker_.FetchPersistentNotificationResources( | 143 notifications_tracker_.FetchNotificationResources( |
| 127 notification_data, | 144 notification_data, nullptr /* delegate */, |
| 128 base::Bind(&NotificationManager::DisplayPersistentNotification, | 145 base::Bind(&NotificationManager::DisplayPersistentNotification, |
| 129 base::Unretained(this), // this owns |notifications_tracker_| | 146 base::Unretained(this), // this owns |notifications_tracker_| |
| 130 origin, notification_data, service_worker_registration_id, | 147 origin, notification_data, service_worker_registration_id, |
| 131 base::Passed(&owned_callbacks))); | 148 base::Passed(&owned_callbacks))); |
| 132 } | 149 } |
| 133 | 150 |
| 134 void NotificationManager::getNotifications( | 151 void NotificationManager::getNotifications( |
| 135 const blink::WebString& filter_tag, | 152 const blink::WebString& filter_tag, |
| 136 blink::WebServiceWorkerRegistration* service_worker_registration, | 153 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 137 blink::WebNotificationGetCallbacks* callbacks) { | 154 blink::WebNotificationGetCallbacks* callbacks) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 152 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 169 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 153 | 170 |
| 154 pending_get_notification_requests_.AddWithID(callbacks, request_id); | 171 pending_get_notification_requests_.AddWithID(callbacks, request_id); |
| 155 | 172 |
| 156 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications( | 173 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications( |
| 157 request_id, service_worker_registration_id, origin, | 174 request_id, service_worker_registration_id, origin, |
| 158 base::UTF16ToUTF8(base::StringPiece16(filter_tag)))); | 175 base::UTF16ToUTF8(base::StringPiece16(filter_tag)))); |
| 159 } | 176 } |
| 160 | 177 |
| 161 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { | 178 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { |
| 162 if (notifications_tracker_.CancelPageNotificationFetches(delegate)) | 179 if (notifications_tracker_.CancelResourceFetches(delegate)) |
| 163 return; | 180 return; |
| 164 | 181 |
| 165 for (auto& iter : active_page_notifications_) { | 182 for (auto& iter : active_page_notifications_) { |
| 166 if (iter.second != delegate) | 183 if (iter.second != delegate) |
| 167 continue; | 184 continue; |
| 168 | 185 |
| 169 thread_safe_sender_->Send( | 186 thread_safe_sender_->Send( |
| 170 new PlatformNotificationHostMsg_Close(iter.first)); | 187 new PlatformNotificationHostMsg_Close(iter.first)); |
| 171 active_page_notifications_.erase(iter.first); | 188 active_page_notifications_.erase(iter.first); |
| 172 return; | 189 return; |
| 173 } | 190 } |
| 174 | 191 |
| 175 // It should not be possible for Blink to call close() on a Notification which | 192 // It should not be possible for Blink to call close() on a Notification which |
| 176 // does not exist in either the pending or active notification lists. | 193 // does not exist in either the pending or active notification lists. |
| 177 NOTREACHED(); | 194 NOTREACHED(); |
| 178 } | 195 } |
| 179 | 196 |
| 180 void NotificationManager::closePersistent( | 197 void NotificationManager::closePersistent( |
| 181 const blink::WebSecurityOrigin& origin, | 198 const blink::WebSecurityOrigin& origin, |
| 182 int64_t persistent_notification_id) { | 199 int64_t persistent_notification_id) { |
| 183 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( | 200 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( |
| 184 // TODO(mkwst): This is potentially doing the wrong thing with unique | 201 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 185 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 202 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 186 // https://crbug.com/490074 for detail. | 203 // https://crbug.com/490074 for detail. |
| 187 blink::WebStringToGURL(origin.toString()), persistent_notification_id)); | 204 blink::WebStringToGURL(origin.toString()), persistent_notification_id)); |
| 188 } | 205 } |
| 189 | 206 |
| 190 void NotificationManager::notifyDelegateDestroyed( | 207 void NotificationManager::notifyDelegateDestroyed( |
| 191 blink::WebNotificationDelegate* delegate) { | 208 blink::WebNotificationDelegate* delegate) { |
| 192 if (notifications_tracker_.CancelPageNotificationFetches(delegate)) | 209 if (notifications_tracker_.CancelResourceFetches(delegate)) |
| 193 return; | 210 return; |
| 194 | 211 |
| 195 for (auto& iter : active_page_notifications_) { | 212 for (auto& iter : active_page_notifications_) { |
| 196 if (iter.second != delegate) | 213 if (iter.second != delegate) |
| 197 continue; | 214 continue; |
| 198 | 215 |
| 199 active_page_notifications_.erase(iter.first); | 216 active_page_notifications_.erase(iter.first); |
| 200 return; | 217 return; |
| 201 } | 218 } |
| 202 } | 219 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 callbacks->onSuccess(notifications); | 316 callbacks->onSuccess(notifications); |
| 300 | 317 |
| 301 pending_get_notification_requests_.Remove(request_id); | 318 pending_get_notification_requests_.Remove(request_id); |
| 302 } | 319 } |
| 303 | 320 |
| 304 void NotificationManager::DisplayPageNotification( | 321 void NotificationManager::DisplayPageNotification( |
| 305 const blink::WebSecurityOrigin& origin, | 322 const blink::WebSecurityOrigin& origin, |
| 306 const blink::WebNotificationData& notification_data, | 323 const blink::WebNotificationData& notification_data, |
| 307 blink::WebNotificationDelegate* delegate, | 324 blink::WebNotificationDelegate* delegate, |
| 308 const NotificationResources& notification_resources) { | 325 const NotificationResources& notification_resources) { |
| 326 DCHECK_EQ(notification_data.actions.size(), 0u); | |
| 327 DCHECK_EQ(notification_resources.action_icons.size(), 0u); | |
| 328 | |
| 309 int notification_id = | 329 int notification_id = |
| 310 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 330 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 311 | 331 |
| 312 active_page_notifications_[notification_id] = delegate; | 332 active_page_notifications_[notification_id] = delegate; |
| 313 // TODO(mkwst): This is potentially doing the wrong thing with unique | 333 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 314 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 334 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 315 // https://crbug.com/490074 for detail. | 335 // https://crbug.com/490074 for detail. |
| 316 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( | 336 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( |
| 317 notification_id, blink::WebStringToGURL(origin.toString()), | 337 notification_id, blink::WebStringToGURL(origin.toString()), |
| 318 ToPlatformNotificationData(notification_data), notification_resources)); | 338 ToPlatformNotificationData(notification_data), notification_resources)); |
| 319 } | 339 } |
| 320 | 340 |
| 321 void NotificationManager::DisplayPersistentNotification( | 341 void NotificationManager::DisplayPersistentNotification( |
| 322 const blink::WebSecurityOrigin& origin, | 342 const blink::WebSecurityOrigin& origin, |
| 323 const blink::WebNotificationData& notification_data, | 343 const blink::WebNotificationData& notification_data, |
| 324 int64_t service_worker_registration_id, | 344 int64_t service_worker_registration_id, |
| 325 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, | 345 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, |
| 326 const NotificationResources& notification_resources) { | 346 const NotificationResources& notification_resources) { |
| 347 DCHECK_EQ(notification_data.actions.size(), | |
| 348 notification_resources.action_icons.size()); | |
| 349 | |
| 327 // TODO(peter): GenerateNotificationId is more of a request id. Consider | 350 // TODO(peter): GenerateNotificationId is more of a request id. Consider |
| 328 // renaming the method in the NotificationDispatcher if this makes sense. | 351 // renaming the method in the NotificationDispatcher if this makes sense. |
| 329 int request_id = | 352 int request_id = |
| 330 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 353 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 331 | 354 |
| 332 pending_show_notification_requests_.AddWithID(callbacks.release(), | 355 pending_show_notification_requests_.AddWithID(callbacks.release(), |
| 333 request_id); | 356 request_id); |
| 334 | 357 |
| 335 // TODO(mkwst): This is potentially doing the wrong thing with unique | 358 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 336 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | 359 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 337 // https://crbug.com/490074 for detail. | 360 // https://crbug.com/490074 for detail. |
| 338 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent( | 361 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent( |
| 339 request_id, service_worker_registration_id, | 362 request_id, service_worker_registration_id, |
| 340 blink::WebStringToGURL(origin.toString()), | 363 blink::WebStringToGURL(origin.toString()), |
| 341 ToPlatformNotificationData(notification_data), notification_resources)); | 364 ToPlatformNotificationData(notification_data), notification_resources)); |
| 342 } | 365 } |
| 343 | 366 |
| 344 } // namespace content | 367 } // namespace content |
| OLD | NEW |