Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
peter | 679ad348 | 2015-05-07 12:06:47 | [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 CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <vector> |
| 10 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 11 | #include "base/functional/callback_forward.h" |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 15 | #include "build/chromeos_buildflags.h" |
Peter Beverloo | afb1ab4 | 2018-08-20 13:03:45 | [diff] [blame] | 16 | #include "chrome/browser/push_messaging/budget_database.h" |
Justin Lulejian | f6a8800 | 2023-10-31 22:09:10 | [diff] [blame] | 17 | #include "extensions/buildflags/buildflags.h" |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 18 | |
| 19 | class GURL; |
| 20 | class Profile; |
| 21 | |
Nicola Tommasi | ad48708 | 2024-02-15 16:33:44 | [diff] [blame] | 22 | // These values are persisted to logs. Entries should not be renumbered and |
| 23 | // numeric values should never be reused. |
| 24 | enum class SilentPushEvent { |
| 25 | kSilentRequest = 0, |
| 26 | kNotificationEnforcementSkipped = 1, |
| 27 | kAllowedWithoutNotification = 2, |
| 28 | kAllowedWithGenericNotification = 3, |
| 29 | kMaxValue = kAllowedWithGenericNotification, |
| 30 | }; |
| 31 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 32 | namespace content { |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 33 | class WebContents; |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 34 | } // namespace content |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 35 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 36 | // Developers may be required to display a Web Notification in response to an |
| 37 | // incoming push message in order to clarify to the user that something has |
| 38 | // happened in the background. When they forget to do so, a default notification |
| 39 | // has to be displayed on their behalf. |
| 40 | // |
| 41 | // This class implements the heuristics for determining whether the default |
| 42 | // notification is necessary, as well as the functionality of displaying the |
| 43 | // default notification when it is. |
| 44 | // |
| 45 | // See the following document and bug for more context: |
| 46 | // https://docs.google.com/document/d/13VxFdLJbMwxHrvnpDm8RXnU41W2ZlcP0mdWWe9zXQT8/edit |
| 47 | // https://crbug.com/437277 |
| 48 | class PushMessagingNotificationManager { |
| 49 | public: |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 50 | using EnforceRequirementsCallback = |
| 51 | base::OnceCallback<void(bool did_show_generic_notification)>; |
| 52 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 53 | explicit PushMessagingNotificationManager(Profile* profile); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 54 | |
| 55 | PushMessagingNotificationManager(const PushMessagingNotificationManager&) = |
| 56 | delete; |
| 57 | PushMessagingNotificationManager& operator=( |
| 58 | const PushMessagingNotificationManager&) = delete; |
| 59 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 60 | ~PushMessagingNotificationManager(); |
| 61 | |
| 62 | // Enforces the requirements implied for push subscriptions which must display |
| 63 | // a Web Notification in response to an incoming message. |
Justin Lulejian | f6a8800 | 2023-10-31 22:09:10 | [diff] [blame] | 64 | // `requested_user_visible_only` is the userVisibleOnly value a worker based |
| 65 | // extension sets on push subscription. |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 66 | void EnforceUserVisibleOnlyRequirements( |
mvanouwerkerk | 9069856 | 2015-11-19 20:20:25 | [diff] [blame] | 67 | const GURL& origin, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 68 | int64_t service_worker_registration_id, |
Justin Lulejian | f6a8800 | 2023-10-31 22:09:10 | [diff] [blame] | 69 | EnforceRequirementsCallback message_handled_callback, |
| 70 | bool requested_user_visible_only); |
| 71 | |
Justin Lulejian | ed68ac78 | 2024-11-04 12:13:59 | [diff] [blame] | 72 | // Checks if the userVisibleOnly: true requirement or the notifications |
| 73 | // permission requirement can be bypassed in certain scenarios. |
| 74 | // |
| 75 | // Currently that is only allowed for extensions with workers that set |
| 76 | // userVisibleOnly: false on subscription. |
| 77 | bool ShouldBypassUserVisibleOnlyRequirement(const GURL& origin, |
| 78 | bool requested_user_visible_only); |
| 79 | bool ShouldBypassNotificationPermissionRequirement( |
| 80 | const GURL& origin, |
| 81 | bool requested_user_visible_only) { |
| 82 | return ShouldBypassUserVisibleOnlyRequirement(origin, |
| 83 | requested_user_visible_only); |
| 84 | } |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 85 | |
| 86 | private: |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 87 | FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible); |
peter | 2d35632 | 2016-02-03 13:09:23 | [diff] [blame] | 88 | FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, |
| 89 | IsTabVisibleViewSource); |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 90 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 91 | void DidCountVisibleNotifications( |
mvanouwerkerk | 9069856 | 2015-11-19 20:20:25 | [diff] [blame] | 92 | const GURL& origin, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 93 | int64_t service_worker_registration_id, |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 94 | EnforceRequirementsCallback message_handled_callback, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 95 | bool success, |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 96 | int notification_count); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 97 | |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 98 | // Checks whether |profile| is the one owning this instance, |
| 99 | // |active_web_contents| exists and its main frame is visible, and the URL |
| 100 | // currently visible to the user is for |origin|. |
| 101 | bool IsTabVisible(Profile* profile, |
| 102 | content::WebContents* active_web_contents, |
| 103 | const GURL& origin); |
| 104 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame] | 105 | void ProcessSilentPush(const GURL& origin, |
| 106 | int64_t service_worker_registration_id, |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 107 | EnforceRequirementsCallback message_handled_callback, |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame] | 108 | bool silent_push_allowed); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 109 | |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 110 | void DidWriteNotificationData( |
| 111 | EnforceRequirementsCallback message_handled_callback, |
| 112 | bool success, |
| 113 | const std::string& notification_id); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 114 | |
Nicola Tommasi | ad48708 | 2024-02-15 16:33:44 | [diff] [blame] | 115 | void LogSilentPushEvent(SilentPushEvent event); |
| 116 | |
Justin Lulejian | f6a8800 | 2023-10-31 22:09:10 | [diff] [blame] | 117 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 118 | // For extensions builds, skip userVisibleOnly requirement for worker-based |
| 119 | // extensions that set it to false. |
Justin Lulejian | ed68ac78 | 2024-11-04 12:13:59 | [diff] [blame] | 120 | bool ShouldExtensionsBypassUserVisibleOnlyRequirement( |
Justin Lulejian | f6a8800 | 2023-10-31 22:09:10 | [diff] [blame] | 121 | const GURL& origin, |
| 122 | bool requested_user_visible_only); |
| 123 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 124 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 125 | // Weak. This manager is owned by a keyed service on this profile. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 126 | raw_ptr<Profile> profile_; |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 127 | |
Peter Beverloo | afb1ab4 | 2018-08-20 13:03:45 | [diff] [blame] | 128 | BudgetDatabase budget_database_; |
| 129 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 130 | base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_{this}; |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_ |