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