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 | #ifndef CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_ |
| 7 | |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Anita Woodruff | f5ae28e | 2018-01-25 18:22:38 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 13 | #include "content/browser/service_worker/service_worker_context_wrapper.h" |
Anita Woodruff | 7d80539e | 2018-02-19 17:07:42 | [diff] [blame] | 14 | #include "content/common/content_export.h" |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 15 | #include "content/public/browser/browser_context.h" |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 16 | #include "content/public/browser/render_process_host.h" |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 17 | #include "content/public/browser/weak_document_ptr.h" |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 18 | #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 Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 21 | #include "third_party/blink/public/common/storage_key/storage_key.h" |
Julie Jeongeun Kim | 5413450 | 2019-03-22 10:40:13 | [diff] [blame] | 22 | #include "third_party/blink/public/mojom/notifications/notification_service.mojom.h" |
Julie Jeongeun Kim | 162c2d28 | 2019-03-13 07:23:52 | [diff] [blame] | 23 | #include "third_party/blink/public/mojom/permissions/permission_status.mojom.h" |
Alexey Baskakov | cb895fb | 2021-04-20 00:07:12 | [diff] [blame] | 24 | #include "url/gurl.h" |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 25 | |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 26 | namespace blink { |
| 27 | struct PlatformNotificationData; |
| 28 | } |
| 29 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 30 | namespace content { |
| 31 | |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 32 | struct NotificationDatabaseData; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 33 | class PlatformNotificationContextImpl; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 34 | |
| 35 | // Implementation of the NotificationService used for Web Notifications. Is |
| 36 | // responsible for displaying, updating and reading of both non-persistent |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 37 | // and persistent notifications. Primarily lives on the UI thread, but jumps to |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 38 | // the IO thread when needing to interact with the ServiceWorkerContextWrapper. |
Anita Woodruff | 7d80539e | 2018-02-19 17:07:42 | [diff] [blame] | 39 | class CONTENT_EXPORT BlinkNotificationServiceImpl |
| 40 | : public blink::mojom::NotificationService { |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 41 | public: |
| 42 | BlinkNotificationServiceImpl( |
| 43 | PlatformNotificationContextImpl* notification_context, |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 44 | BrowserContext* browser_context, |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 45 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 46 | RenderProcessHost* render_process_host, |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 47 | const blink::StorageKey& storage_key, |
Alexey Baskakov | cb895fb | 2021-04-20 00:07:12 | [diff] [blame] | 48 | const GURL& document_url, |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 49 | const WeakDocumentPtr& weak_document_ptr, |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 50 | RenderProcessHost::NotificationServiceCreatorType creator_type, |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 51 | mojo::PendingReceiver<blink::mojom::NotificationService> receiver); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 52 | |
| 53 | BlinkNotificationServiceImpl(const BlinkNotificationServiceImpl&) = delete; |
| 54 | BlinkNotificationServiceImpl& operator=(const BlinkNotificationServiceImpl&) = |
| 55 | delete; |
| 56 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 57 | ~BlinkNotificationServiceImpl() override; |
| 58 | |
| 59 | // blink::mojom::NotificationService implementation. |
Peter Beverloo | d6e38b4 | 2017-11-28 20:37:43 | [diff] [blame] | 60 | void GetPermissionStatus(GetPermissionStatusCallback callback) override; |
Anita Woodruff | 3de15be | 2018-01-17 12:06:11 | [diff] [blame] | 61 | void DisplayNonPersistentNotification( |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 62 | const std::string& token, |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 63 | const blink::PlatformNotificationData& platform_notification_data, |
| 64 | const blink::NotificationResources& notification_resources, |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 65 | mojo::PendingRemote<blink::mojom::NonPersistentNotificationListener> |
| 66 | listener_remote) override; |
Anita Woodruff | 34eb031 | 2018-02-07 15:48:43 | [diff] [blame] | 67 | void CloseNonPersistentNotification(const std::string& token) override; |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 68 | void DisplayPersistentNotification( |
| 69 | int64_t service_worker_registration_id, |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 70 | const blink::PlatformNotificationData& platform_notification_data, |
| 71 | const blink::NotificationResources& notification_resources, |
Anita Woodruff | 6c4108a | 2018-02-22 19:23:25 | [diff] [blame] | 72 | DisplayPersistentNotificationCallback) override; |
Anita Woodruff | f645418 | 2018-04-18 05:25:59 | [diff] [blame] | 73 | void ClosePersistentNotification(const std::string& notification_id) override; |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 74 | void GetNotifications(int64_t service_worker_registration_id, |
| 75 | const std::string& filter_tag, |
Richard Knoll | a6397f4 | 2019-02-27 19:13:56 | [diff] [blame] | 76 | bool include_triggered, |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 77 | GetNotificationsCallback callback) override; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | // Called when an error is detected on binding_. |
| 81 | void OnConnectionError(); |
| 82 | |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 83 | // Check the permission status for the current `storage_key_`. |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 84 | blink::mojom::PermissionStatus CheckPermissionStatus(); |
Peter Beverloo | 76651e9 | 2018-06-05 11:58:06 | [diff] [blame] | 85 | |
Justin DeWitt | 3b28dc5 | 2021-04-23 19:15:56 | [diff] [blame] | 86 | // 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 Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 93 | const blink::NotificationResources& notification_resources); |
Han Leon | b17c071 | 2018-09-05 02:30:47 | [diff] [blame] | 94 | |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 95 | void DidWriteNotificationData(DisplayPersistentNotificationCallback callback, |
| 96 | bool success, |
| 97 | const std::string& notification_id); |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 98 | |
Richard Knoll | d989c182 | 2019-01-28 13:17:42 | [diff] [blame] | 99 | void DidGetNotifications( |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 100 | const std::string& filter_tag, |
Richard Knoll | a6397f4 | 2019-02-27 19:13:56 | [diff] [blame] | 101 | bool include_triggered, |
Anita Woodruff | 108f6589 | 2018-04-12 21:44:33 | [diff] [blame] | 102 | GetNotificationsCallback callback, |
| 103 | bool success, |
| 104 | const std::vector<NotificationDatabaseData>& notifications); |
| 105 | |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 106 | // 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 | |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 112 | // The notification context that owns this service instance. |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 113 | raw_ptr<PlatformNotificationContextImpl, DanglingUntriaged> |
| 114 | notification_context_; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 115 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 116 | raw_ptr<BrowserContext> browser_context_; |
Anita Woodruff | 0c78779 | 2017-12-07 15:22:10 | [diff] [blame] | 117 | |
Anita Woodruff | 03cec26 | 2018-03-13 19:54:47 | [diff] [blame] | 118 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 119 | |
Robbie McElrath | 8d5602a | 2022-04-01 17:39:18 | [diff] [blame] | 120 | int render_process_host_id_; |
| 121 | |
Andrew Williams | fc09056 | 2022-12-05 17:53:48 | [diff] [blame] | 122 | // 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 Baskakov | cb895fb | 2021-04-20 00:07:12 | [diff] [blame] | 131 | // The document url that this notification service is communicating with. |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 132 | // 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 Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 135 | const GURL document_url_; |
| 136 | // The weak document pointer that this notification service is communicating |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 137 | // with. This is either the document that created the notification or the |
| 138 | // ancestor document of the dedicated worker that created the notification. |
Yoshisato Yanagisawa | c1451af5a | 2022-05-31 00:17:31 | [diff] [blame] | 139 | const WeakDocumentPtr weak_document_ptr_; |
Mingyu Lei | 0ece2ab | 2022-10-28 11:58:54 | [diff] [blame] | 140 | // The type of the notification service's creator. |
| 141 | // See RenderProcessHost::NotificationServiceCreatorType. |
| 142 | const RenderProcessHost::NotificationServiceCreatorType creator_type_; |
Peter Beverloo | d6e38b4 | 2017-11-28 20:37:43 | [diff] [blame] | 143 | |
Mario Sanchez Prada | 829706e0 | 2019-07-23 19:08:59 | [diff] [blame] | 144 | mojo::Receiver<blink::mojom::NotificationService> receiver_; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 145 | |
Peter Beverloo | b6742ce | 2018-06-07 10:17:31 | [diff] [blame] | 146 | base::WeakPtrFactory<BlinkNotificationServiceImpl> weak_factory_for_io_{this}; |
| 147 | base::WeakPtrFactory<BlinkNotificationServiceImpl> weak_factory_for_ui_{this}; |
peter | f28cb7f | 2016-06-03 14:09:23 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace content |
| 151 | |
| 152 | #endif // CONTENT_BROWSER_NOTIFICATIONS_BLINK_NOTIFICATION_SERVICE_IMPL_H_ |