blob: 6b5079f2b09cb7b7976681213ce46bdbd9a33faa [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"
15
16class GURL;
17class Profile;
18
19namespace content {
20struct NotificationDatabaseData;
21struct PlatformNotificationData;
mvanouwerkerk0035b7a2015-11-23 15:07:3822class WebContents;
peter679ad3482015-05-07 12:06:4723}
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
37class 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(
mvanouwerkerk90698562015-11-19 20:20:2545 const GURL& origin,
peter679ad3482015-05-07 12:06:4746 int64_t service_worker_registration_id,
47 const base::Closure& message_handled_closure);
48
49 private:
mvanouwerkerk0035b7a2015-11-23 15:07:3850 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
peter2d356322016-02-03 13:09:2351 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
52 IsTabVisibleViewSource);
mvanouwerkerk0035b7a2015-11-23 15:07:3853
peter679ad3482015-05-07 12:06:4754 static void DidGetNotificationsFromDatabaseIOProxy(
55 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2556 const GURL& origin,
peter679ad3482015-05-07 12:06:4757 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(
mvanouwerkerk90698562015-11-19 20:20:2563 const GURL& origin,
peter679ad3482015-05-07 12:06:4764 int64_t service_worker_registration_id,
65 const base::Closure& message_handled_closure,
66 bool success,
67 const std::vector<content::NotificationDatabaseData>& data);
68
mvanouwerkerk0035b7a2015-11-23 15:07:3869 // 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
harkness6f6b41432016-09-08 09:17:2876 void ProcessSilentPush(const GURL& origin,
77 int64_t service_worker_registration_id,
78 const base::Closure& message_handled_closure,
79 bool silent_push_allowed);
peter679ad3482015-05-07 12:06:4780
81 static void DidWriteNotificationDataIOProxy(
82 const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
mvanouwerkerk90698562015-11-19 20:20:2583 const GURL& origin,
peter679ad3482015-05-07 12:06:4784 const content::PlatformNotificationData& notification_data,
85 const base::Closure& message_handled_closure,
86 bool success,
peterc45944c32016-09-13 14:35:5987 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4788
89 void DidWriteNotificationData(
mvanouwerkerk90698562015-11-19 20:20:2590 const GURL& origin,
peter679ad3482015-05-07 12:06:4791 const content::PlatformNotificationData& notification_data,
92 const base::Closure& message_handled_closure,
93 bool success,
peterc45944c32016-09-13 14:35:5994 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4795
peter679ad3482015-05-07 12:06:4796 // 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_