Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/notifications/blink_notification_service_impl.h" |
| 6 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 7 | #include <string> |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 10 | #include "base/check_op.h" |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 11 | #include "base/feature_list.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
| 13 | #include "base/functional/callback_helpers.h" |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 14 | #include "base/metrics/histogram_functions.h" |
Anita Woodruff | f5ae28e | 2018-01-25 18:22:38 | [diff] [blame] | 15 | #include "content/browser/notifications/notification_event_dispatcher_impl.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 16 | #include "content/browser/notifications/platform_notification_context_impl.h" |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 17 | #include "content/browser/renderer_host/render_process_host_impl.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 18 | #include "content/public/browser/browser_task_traits.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 20 | #include "content/public/browser/notification_database_data.h" |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 21 | #include "content/public/browser/permission_controller.h" |
Florian Jacky | a857d58 | 2025-04-10 10:13:33 | [diff] [blame] | 22 | #include "content/public/browser/permission_descriptor_util.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 23 | #include "content/public/browser/platform_notification_service.h" |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 24 | #include "content/public/browser/render_process_host.h" |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 25 | #include "content/public/browser/weak_document_ptr.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 26 | #include "content/public/common/content_client.h" |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 27 | #include "content/public/common/content_features.h" |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 28 | #include "third_party/blink/public/common/notifications/notification_constants.h" |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 29 | #include "third_party/blink/public/common/notifications/notification_resources.h" |
| 30 | #include "third_party/blink/public/common/notifications/platform_notification_data.h" |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 31 | #include "third_party/blink/public/common/permissions/permission_utils.h" |
Han Leon | 90aedd86 | 2018-06-27 02:13:06 | [diff] [blame] | 32 | #include "third_party/blink/public/common/service_worker/service_worker_status_code.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 33 | |
| 34 | namespace content { |
| 35 | |
| 36 | namespace { |
| 37 | |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 38 | const char kBadMessageInvalidNotificationTriggerTimestamp[] = |
| 39 | "Received an invalid notification trigger timestamp."; |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 40 | const char kBadMessageInvalidNotificationActionButtons[] = |
| 41 | "Received a notification with a number of action images that does not " |
| 42 | "match the number of actions."; |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 43 | const char kBadMessageNonPersistentNotificationFromServiceWorker[] = |
| 44 | "Received a non-persistent notification from a service worker."; |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 45 | |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 46 | bool FilterByTag(const std::string& filter_tag, |
| 47 | const NotificationDatabaseData& database_data) { |
| 48 | // An empty filter tag matches all. |
| 49 | if (filter_tag.empty()) |
| 50 | return true; |
| 51 | // Otherwise we need an exact match. |
| 52 | return filter_tag == database_data.notification_data.tag; |
| 53 | } |
| 54 | |
| 55 | bool FilterByTriggered(bool include_triggered, |
| 56 | const NotificationDatabaseData& database_data) { |
| 57 | // Including triggered matches all. |
| 58 | if (include_triggered) |
| 59 | return true; |
| 60 | // Notifications without a trigger always match. |
Richard Knoll | 1ba12aee | 2019-03-07 17:33:42 | [diff] [blame] | 61 | if (!database_data.notification_data.show_trigger_timestamp) |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 62 | return true; |
| 63 | // Otherwise it has to be triggered already. |
| 64 | return database_data.has_triggered; |
| 65 | } |
| 66 | |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 67 | // Checks if this notification has a valid trigger. |
| 68 | bool CheckNotificationTriggerRange( |
| 69 | const blink::PlatformNotificationData& data) { |
| 70 | if (!data.show_trigger_timestamp) |
| 71 | return true; |
| 72 | |
| 73 | base::TimeDelta show_trigger_delay = |
| 74 | data.show_trigger_timestamp.value() - base::Time::Now(); |
| 75 | |
| 76 | return show_trigger_delay <= blink::kMaxNotificationShowTriggerDelay; |
| 77 | } |
| 78 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 81 | using blink::mojom::PersistentNotificationError; |
| 82 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 83 | BlinkNotificationServiceImpl::BlinkNotificationServiceImpl( |
| 84 | PlatformNotificationContextImpl* notification_context, |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 85 | BrowserContext* browser_context, |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 86 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 87 | RenderProcessHost* render_process_host, |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 88 | const blink::StorageKey& storage_key, |
Alexey Baskakov | cb895fb | 2021-04-20 00:07:12 | [diff] [blame] | 89 | const GURL& document_url, |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 90 | const WeakDocumentPtr& weak_document_ptr, |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 91 | RenderProcessHost::NotificationServiceCreatorType creator_type, |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 92 | mojo::PendingReceiver<blink::mojom::NotificationService> receiver) |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 93 | : notification_context_(notification_context), |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 94 | browser_context_(browser_context), |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 95 | service_worker_context_(std::move(service_worker_context)), |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 96 | render_process_host_id_(render_process_host->GetDeprecatedID()), |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 97 | storage_key_(storage_key), |
| 98 | storage_key_if_3psp_enabled( |
| 99 | storage_key.CopyWithForceEnabledThirdPartyStoragePartitioning()), |
Alexey Baskakov | cb895fb | 2021-04-20 00:07:12 | [diff] [blame] | 100 | document_url_(document_url), |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 101 | weak_document_ptr_(weak_document_ptr), |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 102 | creator_type_(creator_type), |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 103 | receiver_(this, std::move(receiver)) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 104 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 105 | DCHECK(notification_context_); |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 106 | DCHECK(browser_context_); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 107 | |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 108 | receiver_.set_disconnect_handler(base::BindOnce( |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 109 | &BlinkNotificationServiceImpl::OnConnectionError, |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 110 | base::Unretained(this) /* the channel is owned by |this| */)); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | BlinkNotificationServiceImpl::~BlinkNotificationServiceImpl() { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 114 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void BlinkNotificationServiceImpl::GetPermissionStatus( |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 118 | GetPermissionStatusCallback callback) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 119 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 120 | if (!browser_context_->GetPlatformNotificationService()) { |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 121 | std::move(callback).Run(blink::mojom::PermissionStatus::DENIED); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 125 | std::move(callback).Run(CheckPermissionStatus()); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void BlinkNotificationServiceImpl::OnConnectionError() { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 129 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 130 | notification_context_->RemoveService(this); |
| 131 | // |this| has now been deleted. |
| 132 | } |
| 133 | |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 134 | // Since a non-persistent notification cannot be created by service workers, we |
| 135 | // report the bad message here and raise a connection error. |
| 136 | bool BlinkNotificationServiceImpl::IsValidForNonPersistentNotification() { |
| 137 | switch (creator_type_) { |
| 138 | case RenderProcessHost::NotificationServiceCreatorType::kDocument: |
| 139 | case RenderProcessHost::NotificationServiceCreatorType::kDedicatedWorker: |
| 140 | case RenderProcessHost::NotificationServiceCreatorType::kSharedWorker: |
| 141 | return true; |
| 142 | case RenderProcessHost::NotificationServiceCreatorType::kServiceWorker: |
| 143 | receiver_.ReportBadMessage( |
| 144 | kBadMessageNonPersistentNotificationFromServiceWorker); |
| 145 | OnConnectionError(); |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 150 | void BlinkNotificationServiceImpl::DisplayNonPersistentNotification( |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 151 | const std::string& token, |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 152 | const blink::PlatformNotificationData& platform_notification_data, |
| 153 | const blink::NotificationResources& notification_resources, |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 154 | mojo::PendingRemote<blink::mojom::NonPersistentNotificationListener> |
| 155 | event_listener_remote) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 156 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 157 | if (!ValidateNotificationDataAndResources(platform_notification_data, |
| 158 | notification_resources)) |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 159 | return; |
| 160 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 161 | if (!browser_context_->GetPlatformNotificationService()) |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 162 | return; |
Anita Woodruff | e5fb71e | 2017-12-19 13:28:29 | [diff] [blame] | 163 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 164 | if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) |
| 165 | return; |
| 166 | |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 167 | if (!IsValidForNonPersistentNotification()) |
| 168 | return; |
| 169 | |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 170 | base::UmaHistogramBoolean( |
| 171 | "Notifications.NonPersistentNotificationThirdPartyCount", |
| 172 | storage_key_if_3psp_enabled.IsThirdPartyContext()); |
| 173 | |
Anita Woodruff | e5fb71e | 2017-12-19 13:28:29 | [diff] [blame] | 174 | std::string notification_id = |
| 175 | notification_context_->notification_id_generator() |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 176 | ->GenerateForNonPersistentNotification(storage_key_.origin(), token); |
Anita Woodruff | e5fb71e | 2017-12-19 13:28:29 | [diff] [blame] | 177 | |
Anita Woodruff | f5ae28e | 2018-01-25 18:22:38 | [diff] [blame] | 178 | NotificationEventDispatcherImpl* event_dispatcher = |
| 179 | NotificationEventDispatcherImpl::GetInstance(); |
| 180 | event_dispatcher->RegisterNonPersistentNotificationListener( |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 181 | notification_id, std::move(event_listener_remote), weak_document_ptr_, |
| 182 | creator_type_); |
Anita Woodruff | f5ae28e | 2018-01-25 18:22:38 | [diff] [blame] | 183 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 184 | browser_context_->GetPlatformNotificationService()->DisplayNotification( |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 185 | notification_id, storage_key_.origin().GetURL(), document_url_, |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 186 | platform_notification_data, notification_resources); |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 187 | } |
| 188 | |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 189 | void BlinkNotificationServiceImpl::CloseNonPersistentNotification( |
| 190 | const std::string& token) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 191 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 192 | if (!browser_context_->GetPlatformNotificationService()) |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 193 | return; |
| 194 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 195 | if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) |
| 196 | return; |
| 197 | |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 198 | if (!IsValidForNonPersistentNotification()) |
| 199 | return; |
| 200 | |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 201 | std::string notification_id = |
| 202 | notification_context_->notification_id_generator() |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 203 | ->GenerateForNonPersistentNotification(storage_key_.origin(), token); |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 204 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 205 | browser_context_->GetPlatformNotificationService()->CloseNotification( |
| 206 | notification_id); |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 207 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 208 | // TODO(crbug.com/40398221): Pass a callback here to focus the tab |
Anita Woodruff | 60036df9c | 2018-05-02 12:41:14 | [diff] [blame] | 209 | // which created the notification, unless the event is canceled. |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 210 | NotificationEventDispatcherImpl::GetInstance() |
Anita Woodruff | 60036df9c | 2018-05-02 12:41:14 | [diff] [blame] | 211 | ->DispatchNonPersistentCloseEvent(notification_id, base::DoNothing()); |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 212 | } |
| 213 | |
Anita Woodruff | 608feab | 2018-02-07 17:33:37 | [diff] [blame] | 214 | blink::mojom::PermissionStatus |
| 215 | BlinkNotificationServiceImpl::CheckPermissionStatus() { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 216 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Florian Jacky | a857d58 | 2025-04-10 10:13:33 | [diff] [blame] | 217 | const auto permission_descriptor = content::PermissionDescriptorUtil:: |
| 218 | CreatePermissionDescriptorForPermissionType( |
| 219 | blink::PermissionType::NOTIFICATIONS); |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 220 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 221 | // TODO(crbug.com/40637582): It is odd that a service instance can be created |
Balazs Engedy | f39e22b | 2019-07-30 11:16:24 | [diff] [blame] | 222 | // for cross-origin subframes, yet the instance is completely oblivious of |
| 223 | // whether it is serving a top-level browsing context or an embedded one. |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 224 | if (creator_type_ == |
| 225 | RenderProcessHost::NotificationServiceCreatorType::kDocument) { |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 226 | RenderFrameHost* rfh = weak_document_ptr_.AsRenderFrameHostIfValid(); |
| 227 | if (!rfh) { |
| 228 | return blink::mojom::PermissionStatus::DENIED; |
| 229 | } |
| 230 | return browser_context_->GetPermissionController() |
Florian Jacky | a857d58 | 2025-04-10 10:13:33 | [diff] [blame] | 231 | ->GetPermissionStatusForCurrentDocument(permission_descriptor, rfh); |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 232 | } else { |
| 233 | RenderProcessHost* rph = RenderProcessHost::FromID(render_process_host_id_); |
| 234 | if (!rph) { |
| 235 | return blink::mojom::PermissionStatus::DENIED; |
| 236 | } |
| 237 | return browser_context_->GetPermissionController() |
Florian Jacky | a857d58 | 2025-04-10 10:13:33 | [diff] [blame] | 238 | ->GetPermissionStatusForWorker(permission_descriptor, rph, |
| 239 | storage_key_.origin()); |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 240 | } |
Anita Woodruff | 608feab | 2018-02-07 17:33:37 | [diff] [blame] | 241 | } |
| 242 | |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 243 | bool BlinkNotificationServiceImpl::ValidateNotificationDataAndResources( |
| 244 | const blink::PlatformNotificationData& platform_notification_data, |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 245 | const blink::NotificationResources& notification_resources) { |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 246 | if (platform_notification_data.actions.size() != |
| 247 | notification_resources.action_icons.size()) { |
| 248 | receiver_.ReportBadMessage(kBadMessageInvalidNotificationActionButtons); |
| 249 | OnConnectionError(); |
| 250 | return false; |
| 251 | } |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 252 | |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 253 | if (!CheckNotificationTriggerRange(platform_notification_data)) { |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 254 | receiver_.ReportBadMessage(kBadMessageInvalidNotificationTriggerTimestamp); |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 255 | OnConnectionError(); |
| 256 | return false; |
| 257 | } |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 261 | void BlinkNotificationServiceImpl::DisplayPersistentNotification( |
| 262 | int64_t service_worker_registration_id, |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 263 | const blink::PlatformNotificationData& platform_notification_data, |
| 264 | const blink::NotificationResources& notification_resources, |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 265 | DisplayPersistentNotificationCallback callback) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 266 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
sisidovski | d55a237 | 2022-06-24 11:22:11 | [diff] [blame] | 267 | |
| 268 | // The renderer should have checked and disallowed the request for fenced |
| 269 | // frames and thrown an error in |
| 270 | // blink::ServiceWorkerRegistrationNotifications. Report a bad message if the |
| 271 | // renderer if the renderer side check didn't happen for some reason. |
| 272 | scoped_refptr<ServiceWorkerRegistration> registration = |
| 273 | service_worker_context_->GetLiveRegistration( |
| 274 | service_worker_registration_id); |
| 275 | if (registration && registration->ancestor_frame_type() == |
| 276 | blink::mojom::AncestorFrameType::kFencedFrame) { |
| 277 | mojo::ReportBadMessage("Notification is not allowed in a fenced frame"); |
| 278 | return; |
| 279 | } |
| 280 | |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 281 | if (!ValidateNotificationDataAndResources(platform_notification_data, |
| 282 | notification_resources)) |
Richard Knoll | 01c9a53 | 2019-04-04 09:42:00 | [diff] [blame] | 283 | return; |
| 284 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 285 | if (!browser_context_->GetPlatformNotificationService()) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 286 | std::move(callback).Run(PersistentNotificationError::INTERNAL_ERROR); |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 287 | return; |
| 288 | } |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 289 | |
| 290 | if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) { |
| 291 | std::move(callback).Run(PersistentNotificationError::PERMISSION_DENIED); |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 295 | base::UmaHistogramBoolean( |
| 296 | "Notifications.PersistentNotificationThirdPartyCount", |
| 297 | storage_key_if_3psp_enabled.IsThirdPartyContext()); |
| 298 | |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 299 | int64_t next_persistent_id = |
| 300 | browser_context_->GetPlatformNotificationService() |
| 301 | ->ReadNextPersistentNotificationId(); |
Sharon Yang | c670690 | 2018-07-16 13:06:19 | [diff] [blame] | 302 | |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 303 | NotificationDatabaseData database_data; |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 304 | database_data.origin = storage_key_.origin().GetURL(); |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 305 | database_data.service_worker_registration_id = service_worker_registration_id; |
| 306 | database_data.notification_data = platform_notification_data; |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 307 | database_data.notification_resources = notification_resources; |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 308 | |
Alison Gale | d94ce4f | 2024-04-22 15:20:39 | [diff] [blame] | 309 | // TODO(crbug.com/41405589): Validate resources are not too big (either |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 310 | // here or in the mojo struct traits). |
| 311 | |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 312 | notification_context_->WriteNotificationData( |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 313 | next_persistent_id, service_worker_registration_id, |
| 314 | storage_key_.origin().GetURL(), database_data, |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 315 | base::BindOnce(&BlinkNotificationServiceImpl::DidWriteNotificationData, |
| 316 | weak_factory_for_ui_.GetWeakPtr(), std::move(callback))); |
Anita Woodruff | 980756a | 2018-02-26 17:10:49 | [diff] [blame] | 317 | } |
| 318 | |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 319 | void BlinkNotificationServiceImpl::DidWriteNotificationData( |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 320 | DisplayPersistentNotificationCallback callback, |
| 321 | bool success, |
| 322 | const std::string& notification_id) { |
| 323 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 324 | std::move(callback).Run(success |
| 325 | ? PersistentNotificationError::NONE |
| 326 | : PersistentNotificationError::INTERNAL_ERROR); |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 327 | } |
| 328 | |
Anita Woodruff | f645418 | 2018-04-18 05:25:59 | [diff] [blame] | 329 | void BlinkNotificationServiceImpl::ClosePersistentNotification( |
| 330 | const std::string& notification_id) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 331 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 332 | if (!browser_context_->GetPlatformNotificationService()) |
Anita Woodruff | f645418 | 2018-04-18 05:25:59 | [diff] [blame] | 333 | return; |
| 334 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 335 | if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) |
| 336 | return; |
| 337 | |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 338 | notification_context_->DeleteNotificationData( |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 339 | notification_id, storage_key_.origin().GetURL(), |
| 340 | /* close_notification= */ true, base::DoNothing()); |
Anita Woodruff | f645418 | 2018-04-18 05:25:59 | [diff] [blame] | 341 | } |
| 342 | |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 343 | void BlinkNotificationServiceImpl::GetNotifications( |
| 344 | int64_t service_worker_registration_id, |
| 345 | const std::string& filter_tag, |
Richard Knoll | a6397f4 | 2019-02-27 19:13:56 | [diff] [blame] | 346 | bool include_triggered, |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 347 | GetNotificationsCallback callback) { |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 348 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Richard Knoll | 77b8b2b | 2021-09-08 09:29:01 | [diff] [blame] | 349 | if (!browser_context_->GetPlatformNotificationService() || |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 350 | CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) { |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 351 | // No permission has been granted for the given origin. It is harmless to |
| 352 | // try to get notifications without permission, so return empty vectors |
| 353 | // indicating that no (accessible) notifications exist at this time. |
| 354 | std::move(callback).Run(std::vector<std::string>(), |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 355 | std::vector<blink::PlatformNotificationData>()); |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
Richard Knoll | a6397f4 | 2019-02-27 19:13:56 | [diff] [blame] | 359 | auto read_notification_data_callback = |
| 360 | base::BindOnce(&BlinkNotificationServiceImpl::DidGetNotifications, |
| 361 | weak_factory_for_ui_.GetWeakPtr(), filter_tag, |
| 362 | include_triggered, std::move(callback)); |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 363 | |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 364 | notification_context_->ReadAllNotificationDataForServiceWorkerRegistration( |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 365 | storage_key_.origin().GetURL(), service_worker_registration_id, |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 366 | std::move(read_notification_data_callback)); |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 367 | } |
| 368 | |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 369 | void BlinkNotificationServiceImpl::DidGetNotifications( |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 370 | const std::string& filter_tag, |
Richard Knoll | a6397f4 | 2019-02-27 19:13:56 | [diff] [blame] | 371 | bool include_triggered, |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 372 | GetNotificationsCallback callback, |
| 373 | bool success, |
| 374 | const std::vector<NotificationDatabaseData>& notifications) { |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 375 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 376 | |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 377 | std::vector<std::string> ids; |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 378 | std::vector<blink::PlatformNotificationData> datas; |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 379 | |
| 380 | for (const NotificationDatabaseData& database_data : notifications) { |
Richard Knoll | 2e136ef | 2019-03-07 09:45:11 | [diff] [blame] | 381 | if (FilterByTag(filter_tag, database_data) && |
| 382 | FilterByTriggered(include_triggered, database_data)) { |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 383 | ids.push_back(database_data.notification_id); |
| 384 | datas.push_back(database_data.notification_data); |
| 385 | } |
| 386 | } |
| 387 | |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 388 | std::move(callback).Run(std::move(ids), std::move(datas)); |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 389 | } |
| 390 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 391 | } // namespace content |