blob: dfe67f62228eeece1a3d881629bc94d03cd3926f [file] [log] [blame]
peter679ad3482015-05-07 12:06:471// 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"
mvanouwerkerk0035b7a2015-11-23 15:07:3812#include "base/gtest_prod_util.h"
peter679ad3482015-05-07 12:06:4713#include "base/macros.h"
14#include "base/memory/weak_ptr.h"
Peter Beverlooafb1ab42018-08-20 13:03:4515#include "chrome/browser/push_messaging/budget_database.h"
peter679ad3482015-05-07 12:06:4716
17class GURL;
18class Profile;
19
20namespace content {
21struct NotificationDatabaseData;
22struct PlatformNotificationData;
mvanouwerkerk0035b7a2015-11-23 15:07:3823class WebContents;
peter679ad3482015-05-07 12:06:4724}
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
38class 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(
mvanouwerkerk90698562015-11-19 20:20:2546 const GURL& origin,
peter679ad3482015-05-07 12:06:4747 int64_t service_worker_registration_id,
48 const base::Closure& message_handled_closure);
49
50 private:
mvanouwerkerk0035b7a2015-11-23 15:07:3851 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
peter2d356322016-02-03 13:09:2352 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
53 IsTabVisibleViewSource);
mvanouwerkerk0035b7a2015-11-23 15:07:3854
peter679ad3482015-05-07 12:06:4755 static void DidGetNotificationsFromDatabaseIOProxy(
56 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2557 const GURL& origin,
peter679ad3482015-05-07 12:06:4758 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(
mvanouwerkerk90698562015-11-19 20:20:2564 const GURL& origin,
peter679ad3482015-05-07 12:06:4765 int64_t service_worker_registration_id,
66 const base::Closure& message_handled_closure,
67 bool success,
68 const std::vector<content::NotificationDatabaseData>& data);
69
mvanouwerkerk0035b7a2015-11-23 15:07:3870 // 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
harkness6f6b41432016-09-08 09:17:2877 void ProcessSilentPush(const GURL& origin,
78 int64_t service_worker_registration_id,
79 const base::Closure& message_handled_closure,
80 bool silent_push_allowed);
peter679ad3482015-05-07 12:06:4781
82 static void DidWriteNotificationDataIOProxy(
83 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2584 const GURL& origin,
peter679ad3482015-05-07 12:06:4785 const content::PlatformNotificationData& notification_data,
86 const base::Closure& message_handled_closure,
87 bool success,
peterc45944c32016-09-13 14:35:5988 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4789
90 void DidWriteNotificationData(
mvanouwerkerk90698562015-11-19 20:20:2591 const GURL& origin,
peter679ad3482015-05-07 12:06:4792 const content::PlatformNotificationData& notification_data,
93 const base::Closure& message_handled_closure,
94 bool success,
peterc45944c32016-09-13 14:35:5995 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4796
peter679ad3482015-05-07 12:06:4797 // Weak. This manager is owned by a keyed service on this profile.
98 Profile* profile_;
99
Peter Beverlooafb1ab42018-08-20 13:03:45100 BudgetDatabase budget_database_;
101
peter679ad3482015-05-07 12:06:47102 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_