blob: 425c7ec6d83db9303a4e964835c33e1b6b0a331d [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
Azeem Arshadf3f1bf82019-01-25 01:20:1417#if defined(OS_CHROMEOS)
18#include "chrome/browser/chromeos/android_sms/android_sms_app_manager.h"
19#include "chromeos/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
20#endif
21
peter679ad3482015-05-07 12:06:4722class GURL;
23class Profile;
24
25namespace content {
mvanouwerkerk0035b7a2015-11-23 15:07:3826class WebContents;
Richard Knollca55419a2019-03-22 15:41:2727} // namespace content
Han Leon96d6b6e8c22018-09-06 06:21:0628
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:
Rayan Kansob2c21aa2019-05-08 21:49:4943 using EnforceRequirementsCallback =
44 base::OnceCallback<void(bool did_show_generic_notification)>;
45
peter679ad3482015-05-07 12:06:4746 explicit PushMessagingNotificationManager(Profile* profile);
47 ~PushMessagingNotificationManager();
48
49 // Enforces the requirements implied for push subscriptions which must display
50 // a Web Notification in response to an incoming message.
51 void EnforceUserVisibleOnlyRequirements(
mvanouwerkerk90698562015-11-19 20:20:2552 const GURL& origin,
peter679ad3482015-05-07 12:06:4753 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4954 EnforceRequirementsCallback message_handled_callback);
peter679ad3482015-05-07 12:06:4755
56 private:
mvanouwerkerk0035b7a2015-11-23 15:07:3857 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
peter2d356322016-02-03 13:09:2358 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
59 IsTabVisibleViewSource);
Azeem Arshadf3f1bf82019-01-25 01:20:1460 FRIEND_TEST_ALL_PREFIXES(
61 PushMessagingNotificationManagerTest,
62 SkipEnforceUserVisibleOnlyRequirementsForAndroidMessages);
mvanouwerkerk0035b7a2015-11-23 15:07:3863
Richard Knoll25d3a8d2020-07-26 03:26:4364 void DidCountVisibleNotifications(
mvanouwerkerk90698562015-11-19 20:20:2565 const GURL& origin,
peter679ad3482015-05-07 12:06:4766 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4967 EnforceRequirementsCallback message_handled_callback,
peter679ad3482015-05-07 12:06:4768 bool success,
Richard Knoll25d3a8d2020-07-26 03:26:4369 int notification_count);
peter679ad3482015-05-07 12:06:4770
mvanouwerkerk0035b7a2015-11-23 15:07:3871 // Checks whether |profile| is the one owning this instance,
72 // |active_web_contents| exists and its main frame is visible, and the URL
73 // currently visible to the user is for |origin|.
74 bool IsTabVisible(Profile* profile,
75 content::WebContents* active_web_contents,
76 const GURL& origin);
77
harkness6f6b41432016-09-08 09:17:2878 void ProcessSilentPush(const GURL& origin,
79 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4980 EnforceRequirementsCallback message_handled_callback,
harkness6f6b41432016-09-08 09:17:2881 bool silent_push_allowed);
peter679ad3482015-05-07 12:06:4782
Rayan Kansob2c21aa2019-05-08 21:49:4983 void DidWriteNotificationData(
84 EnforceRequirementsCallback message_handled_callback,
85 bool success,
86 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4787
Azeem Arshadf3f1bf82019-01-25 01:20:1488#if defined(OS_CHROMEOS)
89 bool ShouldSkipUserVisibleOnlyRequirements(const GURL& origin);
90
91 void SetTestMultiDeviceSetupClient(
92 chromeos::multidevice_setup::MultiDeviceSetupClient*
93 multidevice_setup_client);
94
95 void SetTestAndroidSmsAppManager(
96 chromeos::android_sms::AndroidSmsAppManager* android_sms_app_manager);
97#endif
98
peter679ad3482015-05-07 12:06:4799 // Weak. This manager is owned by a keyed service on this profile.
100 Profile* profile_;
101
Peter Beverlooafb1ab42018-08-20 13:03:45102 BudgetDatabase budget_database_;
103
Azeem Arshadf3f1bf82019-01-25 01:20:14104#if defined(OS_CHROMEOS)
105 chromeos::multidevice_setup::MultiDeviceSetupClient*
106 test_multidevice_setup_client_ = nullptr;
107
108 chromeos::android_sms::AndroidSmsAppManager* test_android_sms_app_manager_ =
109 nullptr;
110#endif
111
Jeremy Roman495db682019-07-12 16:03:24112 base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_{this};
peter679ad3482015-05-07 12:06:47113
114 DISALLOW_COPY_AND_ASSIGN(PushMessagingNotificationManager);
115};
116
117#endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_