blob: e7cc73f375ada7ee8715d331c3d372e0f59a0cdf [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#ifndef CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_
6#define CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_
7
Mario Sanchez Prada829706e02019-07-23 19:08:598#include <string>
9#include <vector>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Anita Woodrufff5ae28e2018-01-25 18:22:3812#include "base/memory/weak_ptr.h"
Anita Woodruff03cec262018-03-13 19:54:4713#include "content/browser/service_worker/service_worker_context_wrapper.h"
Anita Woodruff7d80539e2018-02-19 17:07:4214#include "content/common/content_export.h"
Anita Woodruff0c787792017-12-07 15:22:1015#include "content/public/browser/browser_context.h"
Mingyu Lei0ece2ab2022-10-28 11:58:5416#include "content/public/browser/render_process_host.h"
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:3117#include "content/public/browser/weak_document_ptr.h"
Mario Sanchez Prada829706e02019-07-23 19:08:5918#include "mojo/public/cpp/bindings/pending_receiver.h"
19#include "mojo/public/cpp/bindings/pending_remote.h"
20#include "mojo/public/cpp/bindings/receiver.h"
Andrew Williamsfc090562022-12-05 17:53:4821#include "third_party/blink/public/common/storage_key/storage_key.h"
Julie Jeongeun Kim54134502019-03-22 10:40:1322#include "third_party/blink/public/mojom/notifications/notification_service.mojom.h"
Julie Jeongeun Kim162c2d282019-03-13 07:23:5223#include "third_party/blink/public/mojom/permissions/permission_status.mojom.h"
Alexey Baskakovcb895fb2021-04-20 00:07:1224#include "url/gurl.h"
peterf28cb7f2016-06-03 14:09:2325
Han Leon96d6b6e8c22018-09-06 06:21:0626namespace blink {
27struct PlatformNotificationData;
28}
29
peterf28cb7f2016-06-03 14:09:2330namespace content {
31
Anita Woodruff108f65892018-04-12 21:44:3332struct NotificationDatabaseData;
peterf28cb7f2016-06-03 14:09:2333class PlatformNotificationContextImpl;
peterf28cb7f2016-06-03 14:09:2334
35// Implementation of the NotificationService used for Web Notifications. Is
36// responsible for displaying, updating and reading of both non-persistent
Peter Beverloob6742ce2018-06-07 10:17:3137// and persistent notifications. Primarily lives on the UI thread, but jumps to
Richard Knolld989c1822019-01-28 13:17:4238// the IO thread when needing to interact with the ServiceWorkerContextWrapper.
Anita Woodruff7d80539e2018-02-19 17:07:4239class CONTENT_EXPORT BlinkNotificationServiceImpl
40 : public blink::mojom::NotificationService {
peterf28cb7f2016-06-03 14:09:2341 public:
42 BlinkNotificationServiceImpl(
43 PlatformNotificationContextImpl* notification_context,
Anita Woodruff0c787792017-12-07 15:22:1044 BrowserContext* browser_context,
Anita Woodruff03cec262018-03-13 19:54:4745 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
Robbie McElrath8d5602a2022-04-01 17:39:1846 RenderProcessHost* render_process_host,
Andrew Williamsfc090562022-12-05 17:53:4847 const blink::StorageKey& storage_key,
Alexey Baskakovcb895fb2021-04-20 00:07:1248 const GURL& document_url,
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:3149 const WeakDocumentPtr& weak_document_ptr,
Mingyu Lei0ece2ab2022-10-28 11:58:5450 RenderProcessHost::NotificationServiceCreatorType creator_type,
Mario Sanchez Prada829706e02019-07-23 19:08:5951 mojo::PendingReceiver<blink::mojom::NotificationService> receiver);
Peter Boström828b9022021-09-21 02:28:4352
53 BlinkNotificationServiceImpl(const BlinkNotificationServiceImpl&) = delete;
54 BlinkNotificationServiceImpl& operator=(const BlinkNotificationServiceImpl&) =
55 delete;
56
peterf28cb7f2016-06-03 14:09:2357 ~BlinkNotificationServiceImpl() override;
58
59 // blink::mojom::NotificationService implementation.
Peter Beverlood6e38b42017-11-28 20:37:4360 void GetPermissionStatus(GetPermissionStatusCallback callback) override;
Anita Woodruff3de15be2018-01-17 12:06:1161 void DisplayNonPersistentNotification(
Anita Woodruff34eb0312018-02-07 15:48:4362 const std::string& token,
Han Leon96d6b6e8c22018-09-06 06:21:0663 const blink::PlatformNotificationData& platform_notification_data,
64 const blink::NotificationResources& notification_resources,
Mario Sanchez Prada829706e02019-07-23 19:08:5965 mojo::PendingRemote<blink::mojom::NonPersistentNotificationListener>
66 listener_remote) override;
Anita Woodruff34eb0312018-02-07 15:48:4367 void CloseNonPersistentNotification(const std::string& token) override;
Anita Woodruff6c4108a2018-02-22 19:23:2568 void DisplayPersistentNotification(
69 int64_t service_worker_registration_id,
Han Leon96d6b6e8c22018-09-06 06:21:0670 const blink::PlatformNotificationData& platform_notification_data,
71 const blink::NotificationResources& notification_resources,
Anita Woodruff6c4108a2018-02-22 19:23:2572 DisplayPersistentNotificationCallback) override;
Anita Woodrufff6454182018-04-18 05:25:5973 void ClosePersistentNotification(const std::string& notification_id) override;
Anita Woodruff108f65892018-04-12 21:44:3374 void GetNotifications(int64_t service_worker_registration_id,
75 const std::string& filter_tag,
Richard Knolla6397f42019-02-27 19:13:5676 bool include_triggered,
Anita Woodruff108f65892018-04-12 21:44:3377 GetNotificationsCallback callback) override;
peterf28cb7f2016-06-03 14:09:2378
79 private:
80 // Called when an error is detected on binding_.
81 void OnConnectionError();
82
Andrew Williamsfc090562022-12-05 17:53:4883 // Check the permission status for the current `storage_key_`.
Peter Beverloob6742ce2018-06-07 10:17:3184 blink::mojom::PermissionStatus CheckPermissionStatus();
Peter Beverloo76651e92018-06-05 11:58:0685
Justin DeWitt3b28dc52021-04-23 19:15:5686 // Validate |notification_data| and |notification_resources| received in a
87 // Mojo IPC message. If the validation failed, we'd close the Mojo connection
88 // |binding_| and destroy |this| by calling OnConnectionError() directly, then
89 // return false. So, please do not touch |this| again after you got a false
90 // return value.
91 bool ValidateNotificationDataAndResources(
92 const blink::PlatformNotificationData& notification_data,
Han Leon96d6b6e8c22018-09-06 06:21:0693 const blink::NotificationResources& notification_resources);
Han Leonb17c0712018-09-05 02:30:4794
Richard Knollca55419a2019-03-22 15:41:2795 void DidWriteNotificationData(DisplayPersistentNotificationCallback callback,
96 bool success,
97 const std::string& notification_id);
Anita Woodruff03cec262018-03-13 19:54:4798
Richard Knolld989c1822019-01-28 13:17:4299 void DidGetNotifications(
Anita Woodruff108f65892018-04-12 21:44:33100 const std::string& filter_tag,
Richard Knolla6397f42019-02-27 19:13:56101 bool include_triggered,
Anita Woodruff108f65892018-04-12 21:44:33102 GetNotificationsCallback callback,
103 bool success,
104 const std::vector<NotificationDatabaseData>& notifications);
105
Mingyu Lei0ece2ab2022-10-28 11:58:54106 // Returns if the current notification service is valid for displaying and
107 // handling non persistent notifications.
108 // This may report bad message to the receiver and raise connection error if
109 // the checks fail.
110 bool IsValidForNonPersistentNotification();
111
peterf28cb7f2016-06-03 14:09:23112 // The notification context that owns this service instance.
Arthur Sonzognie98d2142023-06-01 15:02:25113 raw_ptr<PlatformNotificationContextImpl, DanglingUntriaged>
114 notification_context_;
peterf28cb7f2016-06-03 14:09:23115
Keishi Hattori0e45c022021-11-27 09:25:52116 raw_ptr<BrowserContext> browser_context_;
Anita Woodruff0c787792017-12-07 15:22:10117
Anita Woodruff03cec262018-03-13 19:54:47118 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
119
Robbie McElrath8d5602a2022-04-01 17:39:18120 int render_process_host_id_;
121
Andrew Williamsfc090562022-12-05 17:53:48122 // The storage key associated with the context that this notification service
123 // is communicating with.
124 const blink::StorageKey storage_key_;
125 // The same as `storage_key_` above but set as if third-party storage
126 // partitioning is enabled (regardless of whether it actually is). This is
127 // used for collecting metrics when deciding how best to partition
128 // notifications.
129 const blink::StorageKey storage_key_if_3psp_enabled;
130
Alexey Baskakovcb895fb2021-04-20 00:07:12131 // The document url that this notification service is communicating with.
Mingyu Lei0ece2ab2022-10-28 11:58:54132 // This is empty when the notification service is created by a worker
133 // (including dedicated workers, thus the URL might not be the same as
134 // document that `weak_document_ptr_` is pointing at).
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31135 const GURL document_url_;
136 // The weak document pointer that this notification service is communicating
Mingyu Lei0ece2ab2022-10-28 11:58:54137 // with. This is either the document that created the notification or the
138 // ancestor document of the dedicated worker that created the notification.
Yoshisato Yanagisawac1451af5a2022-05-31 00:17:31139 const WeakDocumentPtr weak_document_ptr_;
Mingyu Lei0ece2ab2022-10-28 11:58:54140 // The type of the notification service's creator.
141 // See RenderProcessHost::NotificationServiceCreatorType.
142 const RenderProcessHost::NotificationServiceCreatorType creator_type_;
Peter Beverlood6e38b42017-11-28 20:37:43143
Mario Sanchez Prada829706e02019-07-23 19:08:59144 mojo::Receiver<blink::mojom::NotificationService> receiver_;
peterf28cb7f2016-06-03 14:09:23145
Peter Beverloob6742ce2018-06-07 10:17:31146 base::WeakPtrFactory<BlinkNotificationServiceImpl> weak_factory_for_io_{this};
147 base::WeakPtrFactory<BlinkNotificationServiceImpl> weak_factory_for_ui_{this};
peterf28cb7f2016-06-03 14:09:23148};
149
150} // namespace content
151
152#endif // CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_