peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 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 | |
| 11 | #include "base/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" |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 17 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 18 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Henrique Ferreiro | c0e78b1 | 2021-09-22 20:55:20 | [diff] [blame] | 19 | #include "chrome/browser/ash/android_sms/android_sms_app_manager.h" |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 20 | #include "chromeos/services/multidevice_setup/public/cpp/multidevice_setup_client.h" |
| 21 | #endif |
| 22 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 23 | class GURL; |
| 24 | class Profile; |
| 25 | |
| 26 | namespace content { |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 27 | class WebContents; |
Richard Knoll | ca55419a | 2019-03-22 15:41:27 | [diff] [blame] | 28 | } // namespace content |
Han Leon | 96d6b6e8c2 | 2018-09-06 06:21:06 | [diff] [blame] | 29 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 30 | // Developers may be required to display a Web Notification in response to an |
| 31 | // incoming push message in order to clarify to the user that something has |
| 32 | // happened in the background. When they forget to do so, a default notification |
| 33 | // has to be displayed on their behalf. |
| 34 | // |
| 35 | // This class implements the heuristics for determining whether the default |
| 36 | // notification is necessary, as well as the functionality of displaying the |
| 37 | // default notification when it is. |
| 38 | // |
| 39 | // See the following document and bug for more context: |
| 40 | // https://docs.google.com/document/d/13VxFdLJbMwxHrvnpDm8RXnU41W2ZlcP0mdWWe9zXQT8/edit |
| 41 | // https://crbug.com/437277 |
| 42 | class PushMessagingNotificationManager { |
| 43 | public: |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 44 | using EnforceRequirementsCallback = |
| 45 | base::OnceCallback<void(bool did_show_generic_notification)>; |
| 46 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 47 | explicit PushMessagingNotificationManager(Profile* profile); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 48 | |
| 49 | PushMessagingNotificationManager(const PushMessagingNotificationManager&) = |
| 50 | delete; |
| 51 | PushMessagingNotificationManager& operator=( |
| 52 | const PushMessagingNotificationManager&) = delete; |
| 53 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 54 | ~PushMessagingNotificationManager(); |
| 55 | |
| 56 | // Enforces the requirements implied for push subscriptions which must display |
| 57 | // a Web Notification in response to an incoming message. |
| 58 | void EnforceUserVisibleOnlyRequirements( |
mvanouwerkerk | 9069856 | 2015-11-19 20:20:25 | [diff] [blame] | 59 | const GURL& origin, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 60 | int64_t service_worker_registration_id, |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 61 | EnforceRequirementsCallback message_handled_callback); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 62 | |
| 63 | private: |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 64 | FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible); |
peter | 2d35632 | 2016-02-03 13:09:23 | [diff] [blame] | 65 | FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, |
| 66 | IsTabVisibleViewSource); |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 67 | FRIEND_TEST_ALL_PREFIXES( |
| 68 | PushMessagingNotificationManagerTest, |
| 69 | SkipEnforceUserVisibleOnlyRequirementsForAndroidMessages); |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 70 | |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 71 | void DidCountVisibleNotifications( |
mvanouwerkerk | 9069856 | 2015-11-19 20:20:25 | [diff] [blame] | 72 | const GURL& origin, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 73 | int64_t service_worker_registration_id, |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 74 | EnforceRequirementsCallback message_handled_callback, |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 75 | bool success, |
Richard Knoll | 25d3a8d | 2020-07-26 03:26:43 | [diff] [blame] | 76 | int notification_count); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 77 | |
mvanouwerkerk | 0035b7a | 2015-11-23 15:07:38 | [diff] [blame] | 78 | // Checks whether |profile| is the one owning this instance, |
| 79 | // |active_web_contents| exists and its main frame is visible, and the URL |
| 80 | // currently visible to the user is for |origin|. |
| 81 | bool IsTabVisible(Profile* profile, |
| 82 | content::WebContents* active_web_contents, |
| 83 | const GURL& origin); |
| 84 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame] | 85 | void ProcessSilentPush(const GURL& origin, |
| 86 | int64_t service_worker_registration_id, |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 87 | EnforceRequirementsCallback message_handled_callback, |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame] | 88 | bool silent_push_allowed); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 89 | |
Rayan Kanso | b2c21aa | 2019-05-08 21:49:49 | [diff] [blame] | 90 | void DidWriteNotificationData( |
| 91 | EnforceRequirementsCallback message_handled_callback, |
| 92 | bool success, |
| 93 | const std::string& notification_id); |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 94 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 95 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 96 | bool ShouldSkipUserVisibleOnlyRequirements(const GURL& origin); |
| 97 | |
| 98 | void SetTestMultiDeviceSetupClient( |
| 99 | chromeos::multidevice_setup::MultiDeviceSetupClient* |
| 100 | multidevice_setup_client); |
| 101 | |
| 102 | void SetTestAndroidSmsAppManager( |
Henrique Ferreiro | 47ccaa17a | 2021-09-22 22:50:02 | [diff] [blame] | 103 | ash::android_sms::AndroidSmsAppManager* android_sms_app_manager); |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 104 | #endif |
| 105 | |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 106 | // Weak. This manager is owned by a keyed service on this profile. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame^] | 107 | raw_ptr<Profile> profile_; |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 108 | |
Peter Beverloo | afb1ab4 | 2018-08-20 13:03:45 | [diff] [blame] | 109 | BudgetDatabase budget_database_; |
| 110 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 111 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 112 | chromeos::multidevice_setup::MultiDeviceSetupClient* |
| 113 | test_multidevice_setup_client_ = nullptr; |
| 114 | |
Henrique Ferreiro | 47ccaa17a | 2021-09-22 22:50:02 | [diff] [blame] | 115 | ash::android_sms::AndroidSmsAppManager* test_android_sms_app_manager_ = |
Azeem Arshad | f3f1bf8 | 2019-01-25 01:20:14 | [diff] [blame] | 116 | nullptr; |
| 117 | #endif |
| 118 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 119 | base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_{this}; |
peter | 679ad348 | 2015-05-07 12:06:47 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_ |