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