blob: 1b3d77e66432b1f7b8c6f87634644776cd9ccb5b [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2016 The Chromium Authors
peterf28cb7f2016-06-03 14:09:232// 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örriead587c32021-03-11 14:09:277#include <string>
Richard Knolld989c1822019-01-28 13:17:428#include <utility>
9
Hans Wennborg0917de892020-04-28 20:21:1510#include "base/check_op.h"
Han Leonb17c0712018-09-05 02:30:4711#include "base/feature_list.h"
Avi Drissmanadac21992023-01-11 23:46:3912#include "base/functional/bind.h"
13#include "base/functional/callback_helpers.h"
Andrew Williamsfc090562022-12-05 17:53:4814#include "base/metrics/histogram_functions.h"
Anita Woodrufff5ae28e2018-01-25 18:22:3815#include "content/browser/notifications/notification_event_dispatcher_impl.h"
peterf28cb7f2016-06-03 14:09:2316#include "content/browser/notifications/platform_notification_context_impl.h"
Mingyu Lei0ece2ab2022-10-28 11:58:5417#include "content/browser/renderer_host/render_process_host_impl.h"
Eric Seckler8652dcd52018-09-20 10:42:2818#include "content/public/browser/browser_task_traits.h"
peterf28cb7f2016-06-03 14:09:2319#include "content/public/browser/browser_thread.h"
Anita Woodruff980756a2018-02-26 17:10:4920#include "content/public/browser/notification_database_data.h"
Andrey Lushnikovebff0442018-07-12 20:02:5821#include "content/public/browser/permission_controller.h"
Florian Jackya857d582025-04-10 10:13:3322#include "content/public/browser/permission_descriptor_util.h"
peterf28cb7f2016-06-03 14:09:2323#include "content/public/browser/platform_notification_service.h"
Robbie McElrath8d5602a2022-04-01 17:39:1824#include "content/public/browser/render_process_host.h"
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:3125#include "content/public/browser/weak_document_ptr.h"
peterf28cb7f2016-06-03 14:09:2326#include "content/public/common/content_client.h"
Han Leonb17c0712018-09-05 02:30:4727#include "content/public/common/content_features.h"
Richard Knoll01c9a532019-04-04 09:42:0028#include "third_party/blink/public/common/notifications/notification_constants.h"
Han Leon96d6b6e8c22018-09-06 06:21:0629#include "third_party/blink/public/common/notifications/notification_resources.h"
30#include "third_party/blink/public/common/notifications/platform_notification_data.h"
Andy Paicua6d6d852022-04-28 18:08:3631#include "third_party/blink/public/common/permissions/permission_utils.h"
Han Leon90aedd862018-06-27 02:13:0632#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
peterf28cb7f2016-06-03 14:09:2333
34namespace content {
35
36namespace {
37
Richard Knoll01c9a532019-04-04 09:42:0038const char kBadMessageInvalidNotificationTriggerTimestamp[] =
39 "Received an invalid notification trigger timestamp.";
Justin DeWitt3b28dc52021-04-23 19:15:5640const char kBadMessageInvalidNotificationActionButtons[] =
41 "Received a notification with a number of action images that does not "
42 "match the number of actions.";
Mingyu Lei0ece2ab2022-10-28 11:58:5443const char kBadMessageNonPersistentNotificationFromServiceWorker[] =
44 "Received a non-persistent notification from a service worker.";
Han Leonb17c0712018-09-05 02:30:4745
Richard Knoll2e136ef2019-03-07 09:45:1146bool 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
55bool 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 Knoll1ba12aee2019-03-07 17:33:4261 if (!database_data.notification_data.show_trigger_timestamp)
Richard Knoll2e136ef2019-03-07 09:45:1162 return true;
63 // Otherwise it has to be triggered already.
64 return database_data.has_triggered;
65}
66
Richard Knoll01c9a532019-04-04 09:42:0067// Checks if this notification has a valid trigger.
68bool 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
peterf28cb7f2016-06-03 14:09:2379} // namespace
80
Peter Beverloob6742ce2018-06-07 10:17:3181using blink::mojom::PersistentNotificationError;
82
peterf28cb7f2016-06-03 14:09:2383BlinkNotificationServiceImpl::BlinkNotificationServiceImpl(
84 PlatformNotificationContextImpl* notification_context,
Anita Woodruff0c787792017-12-07 15:22:1085 BrowserContext* browser_context,
Anita Woodruff03cec262018-03-13 19:54:4786 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
Robbie McElrath8d5602a2022-04-01 17:39:1887 RenderProcessHost* render_process_host,
Andrew Williamsfc090562022-12-05 17:53:4888 const blink::StorageKey& storage_key,
Alexey Baskakovcb895fb2021-04-20 00:07:1289 const GURL& document_url,
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:3190 const WeakDocumentPtr& weak_document_ptr,
Mingyu Lei0ece2ab2022-10-28 11:58:5491 RenderProcessHost::NotificationServiceCreatorType creator_type,
Mario Sanchez Prada829706e02019-07-23 19:08:5992 mojo::PendingReceiver<blink::mojom::NotificationService> receiver)
peterf28cb7f2016-06-03 14:09:2393 : notification_context_(notification_context),
Anita Woodruff0c787792017-12-07 15:22:1094 browser_context_(browser_context),
Anita Woodruff03cec262018-03-13 19:54:4795 service_worker_context_(std::move(service_worker_context)),
Emily Andrewsd15fd762024-12-10 20:41:5496 render_process_host_id_(render_process_host->GetDeprecatedID()),
Andrew Williamsfc090562022-12-05 17:53:4897 storage_key_(storage_key),
98 storage_key_if_3psp_enabled(
99 storage_key.CopyWithForceEnabledThirdPartyStoragePartitioning()),
Alexey Baskakovcb895fb2021-04-20 00:07:12100 document_url_(document_url),
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31101 weak_document_ptr_(weak_document_ptr),
Mingyu Lei0ece2ab2022-10-28 11:58:54102 creator_type_(creator_type),
Mario Sanchez Prada829706e02019-07-23 19:08:59103 receiver_(this, std::move(receiver)) {
Peter Beverloob6742ce2018-06-07 10:17:31104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
peterf28cb7f2016-06-03 14:09:23105 DCHECK(notification_context_);
Anita Woodruff0c787792017-12-07 15:22:10106 DCHECK(browser_context_);
peterf28cb7f2016-06-03 14:09:23107
Mario Sanchez Prada829706e02019-07-23 19:08:59108 receiver_.set_disconnect_handler(base::BindOnce(
tzike2aca992017-09-05 08:50:54109 &BlinkNotificationServiceImpl::OnConnectionError,
Anita Woodruff0c787792017-12-07 15:22:10110 base::Unretained(this) /* the channel is owned by |this| */));
peterf28cb7f2016-06-03 14:09:23111}
112
113BlinkNotificationServiceImpl::~BlinkNotificationServiceImpl() {
Peter Beverloob6742ce2018-06-07 10:17:31114 DCHECK_CURRENTLY_ON(BrowserThread::UI);
peterf28cb7f2016-06-03 14:09:23115}
116
117void BlinkNotificationServiceImpl::GetPermissionStatus(
tzikcf7bcd652017-06-15 04:19:30118 GetPermissionStatusCallback callback) {
Peter Beverloob6742ce2018-06-07 10:17:31119 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Richard Knoll77b8b2b2021-09-08 09:29:01120 if (!browser_context_->GetPlatformNotificationService()) {
tzikcf7bcd652017-06-15 04:19:30121 std::move(callback).Run(blink::mojom::PermissionStatus::DENIED);
peterf28cb7f2016-06-03 14:09:23122 return;
123 }
124
Peter Beverloob6742ce2018-06-07 10:17:31125 std::move(callback).Run(CheckPermissionStatus());
peterf28cb7f2016-06-03 14:09:23126}
127
128void BlinkNotificationServiceImpl::OnConnectionError() {
Peter Beverloob6742ce2018-06-07 10:17:31129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
peterf28cb7f2016-06-03 14:09:23130 notification_context_->RemoveService(this);
131 // |this| has now been deleted.
132}
133
Mingyu Lei0ece2ab2022-10-28 11:58:54134// Since a non-persistent notification cannot be created by service workers, we
135// report the bad message here and raise a connection error.
136bool 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 Woodruff0c787792017-12-07 15:22:10150void BlinkNotificationServiceImpl::DisplayNonPersistentNotification(
Anita Woodruff34eb0312018-02-07 15:48:43151 const std::string& token,
Han Leon96d6b6e8c22018-09-06 06:21:06152 const blink::PlatformNotificationData& platform_notification_data,
153 const blink::NotificationResources& notification_resources,
Mario Sanchez Prada829706e02019-07-23 19:08:59154 mojo::PendingRemote<blink::mojom::NonPersistentNotificationListener>
155 event_listener_remote) {
Peter Beverloob6742ce2018-06-07 10:17:31156 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Justin DeWitt3b28dc52021-04-23 19:15:56157 if (!ValidateNotificationDataAndResources(platform_notification_data,
158 notification_resources))
Han Leonb17c0712018-09-05 02:30:47159 return;
160
Richard Knoll77b8b2b2021-09-08 09:29:01161 if (!browser_context_->GetPlatformNotificationService())
Anita Woodruff0c787792017-12-07 15:22:10162 return;
Anita Woodruffe5fb71e2017-12-19 13:28:29163
Peter Beverloob6742ce2018-06-07 10:17:31164 if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED)
165 return;
166
Mingyu Lei0ece2ab2022-10-28 11:58:54167 if (!IsValidForNonPersistentNotification())
168 return;
169
Andrew Williamsfc090562022-12-05 17:53:48170 base::UmaHistogramBoolean(
171 "Notifications.NonPersistentNotificationThirdPartyCount",
172 storage_key_if_3psp_enabled.IsThirdPartyContext());
173
Anita Woodruffe5fb71e2017-12-19 13:28:29174 std::string notification_id =
175 notification_context_->notification_id_generator()
Andrew Williamsfc090562022-12-05 17:53:48176 ->GenerateForNonPersistentNotification(storage_key_.origin(), token);
Anita Woodruffe5fb71e2017-12-19 13:28:29177
Anita Woodrufff5ae28e2018-01-25 18:22:38178 NotificationEventDispatcherImpl* event_dispatcher =
179 NotificationEventDispatcherImpl::GetInstance();
180 event_dispatcher->RegisterNonPersistentNotificationListener(
Mingyu Lei0ece2ab2022-10-28 11:58:54181 notification_id, std::move(event_listener_remote), weak_document_ptr_,
182 creator_type_);
Anita Woodrufff5ae28e2018-01-25 18:22:38183
Richard Knoll77b8b2b2021-09-08 09:29:01184 browser_context_->GetPlatformNotificationService()->DisplayNotification(
Andrew Williamsfc090562022-12-05 17:53:48185 notification_id, storage_key_.origin().GetURL(), document_url_,
Richard Knoll77b8b2b2021-09-08 09:29:01186 platform_notification_data, notification_resources);
Anita Woodruff0c787792017-12-07 15:22:10187}
188
Anita Woodruff34eb0312018-02-07 15:48:43189void BlinkNotificationServiceImpl::CloseNonPersistentNotification(
190 const std::string& token) {
Peter Beverloob6742ce2018-06-07 10:17:31191 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Richard Knoll77b8b2b2021-09-08 09:29:01192 if (!browser_context_->GetPlatformNotificationService())
Anita Woodruff34eb0312018-02-07 15:48:43193 return;
194
Peter Beverloob6742ce2018-06-07 10:17:31195 if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED)
196 return;
197
Mingyu Lei0ece2ab2022-10-28 11:58:54198 if (!IsValidForNonPersistentNotification())
199 return;
200
Anita Woodruff34eb0312018-02-07 15:48:43201 std::string notification_id =
202 notification_context_->notification_id_generator()
Andrew Williamsfc090562022-12-05 17:53:48203 ->GenerateForNonPersistentNotification(storage_key_.origin(), token);
Anita Woodruff34eb0312018-02-07 15:48:43204
Richard Knoll77b8b2b2021-09-08 09:29:01205 browser_context_->GetPlatformNotificationService()->CloseNotification(
206 notification_id);
Anita Woodruff34eb0312018-02-07 15:48:43207
Alison Gale81f4f2c72024-04-22 19:33:31208 // TODO(crbug.com/40398221): Pass a callback here to focus the tab
Anita Woodruff60036df9c2018-05-02 12:41:14209 // which created the notification, unless the event is canceled.
Anita Woodruff34eb0312018-02-07 15:48:43210 NotificationEventDispatcherImpl::GetInstance()
Anita Woodruff60036df9c2018-05-02 12:41:14211 ->DispatchNonPersistentCloseEvent(notification_id, base::DoNothing());
Anita Woodruff34eb0312018-02-07 15:48:43212}
213
Anita Woodruff608feab2018-02-07 17:33:37214blink::mojom::PermissionStatus
215BlinkNotificationServiceImpl::CheckPermissionStatus() {
Peter Beverloob6742ce2018-06-07 10:17:31216 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Florian Jackya857d582025-04-10 10:13:33217 const auto permission_descriptor = content::PermissionDescriptorUtil::
218 CreatePermissionDescriptorForPermissionType(
219 blink::PermissionType::NOTIFICATIONS);
Robbie McElrath8d5602a2022-04-01 17:39:18220
Alison Gale81f4f2c72024-04-22 19:33:31221 // TODO(crbug.com/40637582): It is odd that a service instance can be created
Balazs Engedyf39e22b2019-07-30 11:16:24222 // 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 Lei0ece2ab2022-10-28 11:58:54224 if (creator_type_ ==
225 RenderProcessHost::NotificationServiceCreatorType::kDocument) {
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31226 RenderFrameHost* rfh = weak_document_ptr_.AsRenderFrameHostIfValid();
227 if (!rfh) {
228 return blink::mojom::PermissionStatus::DENIED;
229 }
230 return browser_context_->GetPermissionController()
Florian Jackya857d582025-04-10 10:13:33231 ->GetPermissionStatusForCurrentDocument(permission_descriptor, rfh);
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31232 } 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 Jackya857d582025-04-10 10:13:33238 ->GetPermissionStatusForWorker(permission_descriptor, rph,
239 storage_key_.origin());
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31240 }
Anita Woodruff608feab2018-02-07 17:33:37241}
242
Justin DeWitt3b28dc52021-04-23 19:15:56243bool BlinkNotificationServiceImpl::ValidateNotificationDataAndResources(
244 const blink::PlatformNotificationData& platform_notification_data,
Han Leon96d6b6e8c22018-09-06 06:21:06245 const blink::NotificationResources& notification_resources) {
Justin DeWitt3b28dc52021-04-23 19:15:56246 if (platform_notification_data.actions.size() !=
247 notification_resources.action_icons.size()) {
248 receiver_.ReportBadMessage(kBadMessageInvalidNotificationActionButtons);
249 OnConnectionError();
250 return false;
251 }
Han Leonb17c0712018-09-05 02:30:47252
Justin DeWitt3b28dc52021-04-23 19:15:56253 if (!CheckNotificationTriggerRange(platform_notification_data)) {
Mario Sanchez Prada829706e02019-07-23 19:08:59254 receiver_.ReportBadMessage(kBadMessageInvalidNotificationTriggerTimestamp);
Richard Knoll01c9a532019-04-04 09:42:00255 OnConnectionError();
256 return false;
257 }
Richard Knoll01c9a532019-04-04 09:42:00258 return true;
259}
260
Anita Woodruff6c4108a2018-02-22 19:23:25261void BlinkNotificationServiceImpl::DisplayPersistentNotification(
262 int64_t service_worker_registration_id,
Han Leon96d6b6e8c22018-09-06 06:21:06263 const blink::PlatformNotificationData& platform_notification_data,
264 const blink::NotificationResources& notification_resources,
Anita Woodruff6c4108a2018-02-22 19:23:25265 DisplayPersistentNotificationCallback callback) {
Peter Beverloob6742ce2018-06-07 10:17:31266 DCHECK_CURRENTLY_ON(BrowserThread::UI);
sisidovskid55a2372022-06-24 11:22:11267
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 DeWitt3b28dc52021-04-23 19:15:56281 if (!ValidateNotificationDataAndResources(platform_notification_data,
282 notification_resources))
Richard Knoll01c9a532019-04-04 09:42:00283 return;
284
Richard Knoll77b8b2b2021-09-08 09:29:01285 if (!browser_context_->GetPlatformNotificationService()) {
Peter Beverloob6742ce2018-06-07 10:17:31286 std::move(callback).Run(PersistentNotificationError::INTERNAL_ERROR);
Anita Woodruff6c4108a2018-02-22 19:23:25287 return;
288 }
Peter Beverloob6742ce2018-06-07 10:17:31289
290 if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) {
291 std::move(callback).Run(PersistentNotificationError::PERMISSION_DENIED);
Anita Woodruff6c4108a2018-02-22 19:23:25292 return;
293 }
294
Andrew Williamsfc090562022-12-05 17:53:48295 base::UmaHistogramBoolean(
296 "Notifications.PersistentNotificationThirdPartyCount",
297 storage_key_if_3psp_enabled.IsThirdPartyContext());
298
Richard Knoll77b8b2b2021-09-08 09:29:01299 int64_t next_persistent_id =
300 browser_context_->GetPlatformNotificationService()
301 ->ReadNextPersistentNotificationId();
Sharon Yangc6706902018-07-16 13:06:19302
Anita Woodruff980756a2018-02-26 17:10:49303 NotificationDatabaseData database_data;
Andrew Williamsfc090562022-12-05 17:53:48304 database_data.origin = storage_key_.origin().GetURL();
Anita Woodruff980756a2018-02-26 17:10:49305 database_data.service_worker_registration_id = service_worker_registration_id;
306 database_data.notification_data = platform_notification_data;
Richard Knollca55419a2019-03-22 15:41:27307 database_data.notification_resources = notification_resources;
Anita Woodruff980756a2018-02-26 17:10:49308
Alison Galed94ce4f2024-04-22 15:20:39309 // TODO(crbug.com/41405589): Validate resources are not too big (either
Richard Knolld989c1822019-01-28 13:17:42310 // here or in the mojo struct traits).
311
Anita Woodruff980756a2018-02-26 17:10:49312 notification_context_->WriteNotificationData(
Andrew Williamsfc090562022-12-05 17:53:48313 next_persistent_id, service_worker_registration_id,
314 storage_key_.origin().GetURL(), database_data,
Richard Knollca55419a2019-03-22 15:41:27315 base::BindOnce(&BlinkNotificationServiceImpl::DidWriteNotificationData,
316 weak_factory_for_ui_.GetWeakPtr(), std::move(callback)));
Anita Woodruff980756a2018-02-26 17:10:49317}
318
Richard Knollca55419a2019-03-22 15:41:27319void BlinkNotificationServiceImpl::DidWriteNotificationData(
Richard Knolld989c1822019-01-28 13:17:42320 DisplayPersistentNotificationCallback callback,
321 bool success,
322 const std::string& notification_id) {
323 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Richard Knollca55419a2019-03-22 15:41:27324 std::move(callback).Run(success
325 ? PersistentNotificationError::NONE
326 : PersistentNotificationError::INTERNAL_ERROR);
Anita Woodruff6c4108a2018-02-22 19:23:25327}
328
Anita Woodrufff6454182018-04-18 05:25:59329void BlinkNotificationServiceImpl::ClosePersistentNotification(
330 const std::string& notification_id) {
Peter Beverloob6742ce2018-06-07 10:17:31331 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Richard Knoll77b8b2b2021-09-08 09:29:01332 if (!browser_context_->GetPlatformNotificationService())
Anita Woodrufff6454182018-04-18 05:25:59333 return;
334
Peter Beverloob6742ce2018-06-07 10:17:31335 if (CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED)
336 return;
337
Richard Knolld989c1822019-01-28 13:17:42338 notification_context_->DeleteNotificationData(
Andrew Williamsfc090562022-12-05 17:53:48339 notification_id, storage_key_.origin().GetURL(),
340 /* close_notification= */ true, base::DoNothing());
Anita Woodrufff6454182018-04-18 05:25:59341}
342
Anita Woodruff108f65892018-04-12 21:44:33343void BlinkNotificationServiceImpl::GetNotifications(
344 int64_t service_worker_registration_id,
345 const std::string& filter_tag,
Richard Knolla6397f42019-02-27 19:13:56346 bool include_triggered,
Anita Woodruff108f65892018-04-12 21:44:33347 GetNotificationsCallback callback) {
Peter Beverloob6742ce2018-06-07 10:17:31348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Richard Knoll77b8b2b2021-09-08 09:29:01349 if (!browser_context_->GetPlatformNotificationService() ||
Peter Beverloob6742ce2018-06-07 10:17:31350 CheckPermissionStatus() != blink::mojom::PermissionStatus::GRANTED) {
Anita Woodruff108f65892018-04-12 21:44:33351 // 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 Leon96d6b6e8c22018-09-06 06:21:06355 std::vector<blink::PlatformNotificationData>());
Anita Woodruff108f65892018-04-12 21:44:33356 return;
357 }
358
Richard Knolla6397f42019-02-27 19:13:56359 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 Beverloob6742ce2018-06-07 10:17:31363
Richard Knolld989c1822019-01-28 13:17:42364 notification_context_->ReadAllNotificationDataForServiceWorkerRegistration(
Andrew Williamsfc090562022-12-05 17:53:48365 storage_key_.origin().GetURL(), service_worker_registration_id,
Richard Knoll2e136ef2019-03-07 09:45:11366 std::move(read_notification_data_callback));
Anita Woodruff108f65892018-04-12 21:44:33367}
368
Richard Knolld989c1822019-01-28 13:17:42369void BlinkNotificationServiceImpl::DidGetNotifications(
Anita Woodruff108f65892018-04-12 21:44:33370 const std::string& filter_tag,
Richard Knolla6397f42019-02-27 19:13:56371 bool include_triggered,
Anita Woodruff108f65892018-04-12 21:44:33372 GetNotificationsCallback callback,
373 bool success,
374 const std::vector<NotificationDatabaseData>& notifications) {
Richard Knolld989c1822019-01-28 13:17:42375 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Peter Beverloob6742ce2018-06-07 10:17:31376
Anita Woodruff108f65892018-04-12 21:44:33377 std::vector<std::string> ids;
Han Leon96d6b6e8c22018-09-06 06:21:06378 std::vector<blink::PlatformNotificationData> datas;
Anita Woodruff108f65892018-04-12 21:44:33379
380 for (const NotificationDatabaseData& database_data : notifications) {
Richard Knoll2e136ef2019-03-07 09:45:11381 if (FilterByTag(filter_tag, database_data) &&
382 FilterByTriggered(include_triggered, database_data)) {
Anita Woodruff108f65892018-04-12 21:44:33383 ids.push_back(database_data.notification_id);
384 datas.push_back(database_data.notification_data);
385 }
386 }
387
Richard Knolld989c1822019-01-28 13:17:42388 std::move(callback).Run(std::move(ids), std::move(datas));
Anita Woodruff108f65892018-04-12 21:44:33389}
390
peterf28cb7f2016-06-03 14:09:23391} // namespace content