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