blob: f682d90973e10581680c0ef8ab2a79d0c7a991a8 [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;
mvanouwerkerk0035b7a2015-11-23 15:07:3822class WebContents;
peter679ad3482015-05-07 12:06:4723}
24
Han Leon96d6b6e8c22018-09-06 06:21:0625namespace blink {
26struct PlatformNotificationData;
27}
28
peter679ad3482015-05-07 12:06:4729// Developers may be required to display a Web Notification in response to an
30// incoming push message in order to clarify to the user that something has
31// happened in the background. When they forget to do so, a default notification
32// has to be displayed on their behalf.
33//
34// This class implements the heuristics for determining whether the default
35// notification is necessary, as well as the functionality of displaying the
36// default notification when it is.
37//
38// See the following document and bug for more context:
39// https://docs.google.com/document/d/13VxFdLJbMwxHrvnpDm8RXnU41W2ZlcP0mdWWe9zXQT8/edit
40// https://crbug.com/437277
41class PushMessagingNotificationManager {
42 public:
43 explicit PushMessagingNotificationManager(Profile* profile);
44 ~PushMessagingNotificationManager();
45
46 // Enforces the requirements implied for push subscriptions which must display
47 // a Web Notification in response to an incoming message.
48 void EnforceUserVisibleOnlyRequirements(
mvanouwerkerk90698562015-11-19 20:20:2549 const GURL& origin,
peter679ad3482015-05-07 12:06:4750 int64_t service_worker_registration_id,
51 const base::Closure& message_handled_closure);
52
53 private:
mvanouwerkerk0035b7a2015-11-23 15:07:3854 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
peter2d356322016-02-03 13:09:2355 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
56 IsTabVisibleViewSource);
mvanouwerkerk0035b7a2015-11-23 15:07:3857
peter679ad3482015-05-07 12:06:4758 static void DidGetNotificationsFromDatabaseIOProxy(
59 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2560 const GURL& origin,
peter679ad3482015-05-07 12:06:4761 int64_t service_worker_registration_id,
62 const base::Closure& message_handled_closure,
63 bool success,
64 const std::vector<content::NotificationDatabaseData>& data);
65
66 void DidGetNotificationsFromDatabase(
mvanouwerkerk90698562015-11-19 20:20:2567 const GURL& origin,
peter679ad3482015-05-07 12:06:4768 int64_t service_worker_registration_id,
69 const base::Closure& message_handled_closure,
70 bool success,
71 const std::vector<content::NotificationDatabaseData>& data);
72
mvanouwerkerk0035b7a2015-11-23 15:07:3873 // 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
harkness6f6b41432016-09-08 09:17:2880 void ProcessSilentPush(const GURL& origin,
81 int64_t service_worker_registration_id,
82 const base::Closure& message_handled_closure,
83 bool silent_push_allowed);
peter679ad3482015-05-07 12:06:4784
85 static void DidWriteNotificationDataIOProxy(
86 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2587 const GURL& origin,
Han Leon96d6b6e8c22018-09-06 06:21:0688 const blink::PlatformNotificationData& notification_data,
peter679ad3482015-05-07 12:06:4789 const base::Closure& message_handled_closure,
90 bool success,
peterc45944c32016-09-13 14:35:5991 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4792
93 void DidWriteNotificationData(
mvanouwerkerk90698562015-11-19 20:20:2594 const GURL& origin,
Han Leon96d6b6e8c22018-09-06 06:21:0695 const blink::PlatformNotificationData& notification_data,
peter679ad3482015-05-07 12:06:4796 const base::Closure& message_handled_closure,
97 bool success,
peterc45944c32016-09-13 14:35:5998 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4799
peter679ad3482015-05-07 12:06:47100 // Weak. This manager is owned by a keyed service on this profile.
101 Profile* profile_;
102
Peter Beverlooafb1ab42018-08-20 13:03:45103 BudgetDatabase budget_database_;
104
peter679ad3482015-05-07 12:06:47105 base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_;
106
107 DISALLOW_COPY_AND_ASSIGN(PushMessagingNotificationManager);
108};
109
110#endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_