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 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
| 14 #include "content/child/notifications/notification_data_conversions.h" | 14 #include "content/child/notifications/notification_data_conversions.h" |
| 15 #include "content/child/notifications/notification_dispatcher.h" | 15 #include "content/child/notifications/notification_dispatcher.h" |
| 16 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 16 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 17 #include "content/child/thread_safe_sender.h" | 17 #include "content/child/thread_safe_sender.h" |
| 18 #include "content/child/worker_task_runner.h" | 18 #include "content/child/worker_task_runner.h" |
| 19 #include "content/public/common/platform_notification_data.h" | 19 #include "content/public/common/platform_notification_data.h" |
| 20 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 20 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| 21 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onDelegate.h" | 21 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onDelegate.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 | 23 |
| 24 using blink::WebNotificationPermission; | 24 using blink::WebNotificationPermission; |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 int CurrentWorkerId() { | 29 int CurrentWorkerId() { |
| 30 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | 30 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 61 if (CurrentWorkerId()) | 61 if (CurrentWorkerId()) |
| 62 WorkerTaskRunner::Instance()->AddStopObserver(manager); | 62 WorkerTaskRunner::Instance()->AddStopObserver(manager); |
| 63 return manager; | 63 return manager; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void NotificationManager::OnWorkerRunLoopStopped() { | 66 void NotificationManager::OnWorkerRunLoopStopped() { |
| 67 delete this; | 67 delete this; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void NotificationManager::show( | 70 void NotificationManager::show( |
| 71 const blink::WebSerializedOrigin& origin, | 71 const blink::WebSecurityOrigin& origin, |
| 72 const blink::WebNotificationData& notification_data, | 72 const blink::WebNotificationData& notification_data, |
| 73 blink::WebNotificationDelegate* delegate) { | 73 blink::WebNotificationDelegate* delegate) { |
| 74 if (notification_data.icon.isEmpty()) { | 74 if (notification_data.icon.isEmpty()) { |
| 75 DisplayPageNotification(origin, notification_data, delegate, SkBitmap()); | 75 DisplayPageNotification(origin, notification_data, delegate, SkBitmap()); |
| 76 return; | 76 return; |
| 77 } | 77 } |
| 78 | 78 |
| 79 pending_notifications_.FetchPageNotificationResources( | 79 pending_notifications_.FetchPageNotificationResources( |
| 80 notification_data, | 80 notification_data, |
| 81 delegate, | 81 delegate, |
| 82 base::Bind(&NotificationManager::DisplayPageNotification, | 82 base::Bind(&NotificationManager::DisplayPageNotification, |
| 83 base::Unretained(this), // this owns |pending_notifications_| | 83 base::Unretained(this), // this owns |pending_notifications_| |
| 84 origin, | 84 origin, |
| 85 notification_data, | 85 notification_data, |
| 86 delegate)); | 86 delegate)); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void NotificationManager::showPersistent( | 89 void NotificationManager::showPersistent( |
| 90 const blink::WebSerializedOrigin& origin, | 90 const blink::WebSecurityOrigin& origin, |
| 91 const blink::WebNotificationData& notification_data, | 91 const blink::WebNotificationData& notification_data, |
| 92 blink::WebServiceWorkerRegistration* service_worker_registration, | 92 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 93 blink::WebNotificationShowCallbacks* callbacks) { | 93 blink::WebNotificationShowCallbacks* callbacks) { |
| 94 DCHECK(service_worker_registration); | 94 DCHECK(service_worker_registration); |
| 95 int64_t service_worker_registration_id = | 95 int64_t service_worker_registration_id = |
| 96 static_cast<WebServiceWorkerRegistrationImpl*>( | 96 static_cast<WebServiceWorkerRegistrationImpl*>( |
| 97 service_worker_registration)->registration_id(); | 97 service_worker_registration)->registration_id(); |
| 98 | 98 |
| 99 scoped_ptr<blink::WebNotificationShowCallbacks> owned_callbacks(callbacks); | 99 scoped_ptr<blink::WebNotificationShowCallbacks> owned_callbacks(callbacks); |
| 100 | 100 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 active_page_notifications_.erase(iter.first); | 177 active_page_notifications_.erase(iter.first); |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 | 180 |
| 181 // It should not be possible for Blink to call close() on a Notification which | 181 // It should not be possible for Blink to call close() on a Notification which |
| 182 // does not exist in either the pending or active notification lists. | 182 // does not exist in either the pending or active notification lists. |
| 183 NOTREACHED(); | 183 NOTREACHED(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void NotificationManager::closePersistent( | 186 void NotificationManager::closePersistent( |
| 187 const blink::WebSerializedOrigin& origin, | 187 const blink::WebSecurityOrigin& origin, |
| 188 int64_t persistent_notification_id) { | 188 int64_t persistent_notification_id) { |
| 189 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( | 189 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( |
| 190 GURL(origin.string()), | 190 // TODO(mkwst): This is potentially doing the wrong thing with unique |
|
Peter Beverloo
2015/07/10 14:19:17
nit: We usually have these TODOs outside statement
| |
| 191 persistent_notification_id)); | 191 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 192 // https://crbug.com/490074 for detail. | |
|
Peter Beverloo
2015/07/10 14:19:17
There's a notifications browser test to verify tha
| |
| 193 GURL(origin.toString()), persistent_notification_id)); | |
| 192 } | 194 } |
| 193 | 195 |
| 194 void NotificationManager::notifyDelegateDestroyed( | 196 void NotificationManager::notifyDelegateDestroyed( |
| 195 blink::WebNotificationDelegate* delegate) { | 197 blink::WebNotificationDelegate* delegate) { |
| 196 if (pending_notifications_.CancelPageNotificationFetches(delegate)) | 198 if (pending_notifications_.CancelPageNotificationFetches(delegate)) |
| 197 return; | 199 return; |
| 198 | 200 |
| 199 for (auto& iter : active_page_notifications_) { | 201 for (auto& iter : active_page_notifications_) { |
| 200 if (iter.second != delegate) | 202 if (iter.second != delegate) |
| 201 continue; | 203 continue; |
| 202 | 204 |
| 203 active_page_notifications_.erase(iter.first); | 205 active_page_notifications_.erase(iter.first); |
| 204 return; | 206 return; |
| 205 } | 207 } |
| 206 } | 208 } |
| 207 | 209 |
| 208 WebNotificationPermission NotificationManager::checkPermission( | 210 WebNotificationPermission NotificationManager::checkPermission( |
| 209 const blink::WebSerializedOrigin& origin) { | 211 const blink::WebSecurityOrigin& origin) { |
| 210 WebNotificationPermission permission = | 212 WebNotificationPermission permission = |
| 211 blink::WebNotificationPermissionAllowed; | 213 blink::WebNotificationPermissionAllowed; |
| 214 // TODO(mkwst): This is potentially doing the wrong thing with unique | |
| 215 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See | |
| 216 // https://crbug.com/490074 for detail. | |
| 212 thread_safe_sender_->Send(new PlatformNotificationHostMsg_CheckPermission( | 217 thread_safe_sender_->Send(new PlatformNotificationHostMsg_CheckPermission( |
| 213 GURL(origin.string()), &permission)); | 218 GURL(origin.toString()), &permission)); |
| 214 | 219 |
| 215 return permission; | 220 return permission; |
| 216 } | 221 } |
| 217 | 222 |
| 218 bool NotificationManager::OnMessageReceived(const IPC::Message& message) { | 223 bool NotificationManager::OnMessageReceived(const IPC::Message& message) { |
| 219 bool handled = true; | 224 bool handled = true; |
| 220 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message) | 225 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message) |
| 221 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow); | 226 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow); |
| 222 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShowPersistent, | 227 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShowPersistent, |
| 223 OnDidShowPersistent) | 228 OnDidShowPersistent) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 298 |
| 294 (*notifications)[i] = web_notification_info; | 299 (*notifications)[i] = web_notification_info; |
| 295 } | 300 } |
| 296 | 301 |
| 297 callbacks->onSuccess(notifications.release()); | 302 callbacks->onSuccess(notifications.release()); |
| 298 | 303 |
| 299 pending_get_notification_requests_.Remove(request_id); | 304 pending_get_notification_requests_.Remove(request_id); |
| 300 } | 305 } |
| 301 | 306 |
| 302 void NotificationManager::DisplayPageNotification( | 307 void NotificationManager::DisplayPageNotification( |
| 303 const blink::WebSerializedOrigin& origin, | 308 const blink::WebSecurityOrigin& origin, |
| 304 const blink::WebNotificationData& notification_data, | 309 const blink::WebNotificationData& notification_data, |
| 305 blink::WebNotificationDelegate* delegate, | 310 blink::WebNotificationDelegate* delegate, |
| 306 const SkBitmap& icon) { | 311 const SkBitmap& icon) { |
| 307 int notification_id = | 312 int notification_id = |
| 308 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 313 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 309 | 314 |
| 310 active_page_notifications_[notification_id] = delegate; | 315 active_page_notifications_[notification_id] = delegate; |
| 311 thread_safe_sender_->Send( | 316 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 312 new PlatformNotificationHostMsg_Show( | 317 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 313 notification_id, | 318 // https://crbug.com/490074 for detail. |
| 314 GURL(origin.string()), | 319 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( |
| 315 icon, | 320 notification_id, GURL(origin.toString()), icon, |
| 316 ToPlatformNotificationData(notification_data))); | 321 ToPlatformNotificationData(notification_data))); |
| 317 } | 322 } |
| 318 | 323 |
| 319 void NotificationManager::DisplayPersistentNotification( | 324 void NotificationManager::DisplayPersistentNotification( |
| 320 const blink::WebSerializedOrigin& origin, | 325 const blink::WebSecurityOrigin& origin, |
| 321 const blink::WebNotificationData& notification_data, | 326 const blink::WebNotificationData& notification_data, |
| 322 int64_t service_worker_registration_id, | 327 int64_t service_worker_registration_id, |
| 323 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, | 328 scoped_ptr<blink::WebNotificationShowCallbacks> callbacks, |
| 324 const SkBitmap& icon) { | 329 const SkBitmap& icon) { |
| 325 // TODO(peter): GenerateNotificationId is more of a request id. Consider | 330 // TODO(peter): GenerateNotificationId is more of a request id. Consider |
| 326 // renaming the method in the NotificationDispatcher if this makes sense. | 331 // renaming the method in the NotificationDispatcher if this makes sense. |
| 327 int request_id = | 332 int request_id = |
| 328 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); | 333 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); |
| 329 | 334 |
| 330 pending_show_notification_requests_.AddWithID(callbacks.release(), | 335 pending_show_notification_requests_.AddWithID(callbacks.release(), |
| 331 request_id); | 336 request_id); |
| 332 | 337 |
| 333 thread_safe_sender_->Send( | 338 // TODO(mkwst): This is potentially doing the wrong thing with unique |
| 334 new PlatformNotificationHostMsg_ShowPersistent( | 339 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See |
| 335 request_id, | 340 // https://crbug.com/490074 for detail. |
| 336 service_worker_registration_id, | 341 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent( |
| 337 GURL(origin.string()), | 342 request_id, service_worker_registration_id, GURL(origin.toString()), icon, |
| 338 icon, | 343 ToPlatformNotificationData(notification_data))); |
| 339 ToPlatformNotificationData(notification_data))); | |
| 340 } | 344 } |
| 341 | 345 |
| 342 } // namespace content | 346 } // namespace content |
| OLD | NEW |