blob: b2621f55c5379d05d68f11a8383765791e5734e1 [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"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
peter679ad3482015-05-07 12:06:4714#include "base/memory/weak_ptr.h"
Yuta Hijikata235fc62b2020-12-08 03:48:3215#include "build/chromeos_buildflags.h"
Peter Beverlooafb1ab42018-08-20 13:03:4516#include "chrome/browser/push_messaging/budget_database.h"
peter679ad3482015-05-07 12:06:4717
Yuta Hijikata235fc62b2020-12-08 03:48:3218#if BUILDFLAG(IS_CHROMEOS_ASH)
Henrique Ferreiroc0e78b12021-09-22 20:55:2019#include "chrome/browser/ash/android_sms/android_sms_app_manager.h"
Azeem Arshadf3f1bf82019-01-25 01:20:1420#include "chromeos/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
21#endif
22
peter679ad3482015-05-07 12:06:4723class GURL;
24class Profile;
25
26namespace content {
mvanouwerkerk0035b7a2015-11-23 15:07:3827class WebContents;
Richard Knollca55419a2019-03-22 15:41:2728} // namespace content
Han Leon96d6b6e8c22018-09-06 06:21:0629
peter679ad3482015-05-07 12:06:4730// Developers may be required to display a Web Notification in response to an
31// incoming push message in order to clarify to the user that something has
32// happened in the background. When they forget to do so, a default notification
33// has to be displayed on their behalf.
34//
35// This class implements the heuristics for determining whether the default
36// notification is necessary, as well as the functionality of displaying the
37// default notification when it is.
38//
39// See the following document and bug for more context:
40// https://docs.google.com/document/d/13VxFdLJbMwxHrvnpDm8RXnU41W2ZlcP0mdWWe9zXQT8/edit
41// https://crbug.com/437277
42class PushMessagingNotificationManager {
43 public:
Rayan Kansob2c21aa2019-05-08 21:49:4944 using EnforceRequirementsCallback =
45 base::OnceCallback<void(bool did_show_generic_notification)>;
46
peter679ad3482015-05-07 12:06:4747 explicit PushMessagingNotificationManager(Profile* profile);
Peter Boström53c6c5952021-09-17 09:41:2648
49 PushMessagingNotificationManager(const PushMessagingNotificationManager&) =
50 delete;
51 PushMessagingNotificationManager& operator=(
52 const PushMessagingNotificationManager&) = delete;
53
peter679ad3482015-05-07 12:06:4754 ~PushMessagingNotificationManager();
55
56 // Enforces the requirements implied for push subscriptions which must display
57 // a Web Notification in response to an incoming message.
58 void EnforceUserVisibleOnlyRequirements(
mvanouwerkerk90698562015-11-19 20:20:2559 const GURL& origin,
peter679ad3482015-05-07 12:06:4760 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4961 EnforceRequirementsCallback message_handled_callback);
peter679ad3482015-05-07 12:06:4762
63 private:
mvanouwerkerk0035b7a2015-11-23 15:07:3864 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
peter2d356322016-02-03 13:09:2365 FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
66 IsTabVisibleViewSource);
Azeem Arshadf3f1bf82019-01-25 01:20:1467 FRIEND_TEST_ALL_PREFIXES(
68 PushMessagingNotificationManagerTest,
69 SkipEnforceUserVisibleOnlyRequirementsForAndroidMessages);
mvanouwerkerk0035b7a2015-11-23 15:07:3870
Richard Knoll25d3a8d2020-07-26 03:26:4371 void DidCountVisibleNotifications(
mvanouwerkerk90698562015-11-19 20:20:2572 const GURL& origin,
peter679ad3482015-05-07 12:06:4773 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4974 EnforceRequirementsCallback message_handled_callback,
peter679ad3482015-05-07 12:06:4775 bool success,
Richard Knoll25d3a8d2020-07-26 03:26:4376 int notification_count);
peter679ad3482015-05-07 12:06:4777
mvanouwerkerk0035b7a2015-11-23 15:07:3878 // Checks whether |profile| is the one owning this instance,
79 // |active_web_contents| exists and its main frame is visible, and the URL
80 // currently visible to the user is for |origin|.
81 bool IsTabVisible(Profile* profile,
82 content::WebContents* active_web_contents,
83 const GURL& origin);
84
harkness6f6b41432016-09-08 09:17:2885 void ProcessSilentPush(const GURL& origin,
86 int64_t service_worker_registration_id,
Rayan Kansob2c21aa2019-05-08 21:49:4987 EnforceRequirementsCallback message_handled_callback,
harkness6f6b41432016-09-08 09:17:2888 bool silent_push_allowed);
peter679ad3482015-05-07 12:06:4789
Rayan Kansob2c21aa2019-05-08 21:49:4990 void DidWriteNotificationData(
91 EnforceRequirementsCallback message_handled_callback,
92 bool success,
93 const std::string& notification_id);
peter679ad3482015-05-07 12:06:4794
Yuta Hijikata235fc62b2020-12-08 03:48:3295#if BUILDFLAG(IS_CHROMEOS_ASH)
Azeem Arshadf3f1bf82019-01-25 01:20:1496 bool ShouldSkipUserVisibleOnlyRequirements(const GURL& origin);
97
98 void SetTestMultiDeviceSetupClient(
99 chromeos::multidevice_setup::MultiDeviceSetupClient*
100 multidevice_setup_client);
101
102 void SetTestAndroidSmsAppManager(
Henrique Ferreiro47ccaa17a2021-09-22 22:50:02103 ash::android_sms::AndroidSmsAppManager* android_sms_app_manager);
Azeem Arshadf3f1bf82019-01-25 01:20:14104#endif
105
peter679ad3482015-05-07 12:06:47106 // Weak. This manager is owned by a keyed service on this profile.
Keishi Hattori0e45c022021-11-27 09:25:52107 raw_ptr<Profile> profile_;
peter679ad3482015-05-07 12:06:47108
Peter Beverlooafb1ab42018-08-20 13:03:45109 BudgetDatabase budget_database_;
110
Yuta Hijikata235fc62b2020-12-08 03:48:32111#if BUILDFLAG(IS_CHROMEOS_ASH)
Azeem Arshadf3f1bf82019-01-25 01:20:14112 chromeos::multidevice_setup::MultiDeviceSetupClient*
113 test_multidevice_setup_client_ = nullptr;
114
Henrique Ferreiro47ccaa17a2021-09-22 22:50:02115 ash::android_sms::AndroidSmsAppManager* test_android_sms_app_manager_ =
Azeem Arshadf3f1bf82019-01-25 01:20:14116 nullptr;
117#endif
118
Jeremy Roman495db682019-07-12 16:03:24119 base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_{this};
peter679ad3482015-05-07 12:06:47120};
121
122#endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_