File: push_messaging_notification_manager.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 5,134 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_
#define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_

#include <stdint.h>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/push_messaging/budget_database.h"
#include "extensions/buildflags/buildflags.h"

class GURL;
class Profile;

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class SilentPushEvent {
  kSilentRequest = 0,
  kNotificationEnforcementSkipped = 1,
  kAllowedWithoutNotification = 2,
  kAllowedWithGenericNotification = 3,
  kMaxValue = kAllowedWithGenericNotification,
};

namespace content {
class WebContents;
}  // namespace content

// Developers may be required to display a Web Notification in response to an
// incoming push message in order to clarify to the user that something has
// happened in the background. When they forget to do so, a default notification
// has to be displayed on their behalf.
//
// This class implements the heuristics for determining whether the default
// notification is necessary, as well as the functionality of displaying the
// default notification when it is.
//
// See the following document and bug for more context:
// https://docs.google.com/document/d/13VxFdLJbMwxHrvnpDm8RXnU41W2ZlcP0mdWWe9zXQT8/edit
// https://crbug.com/437277
class PushMessagingNotificationManager {
 public:
  using EnforceRequirementsCallback =
      base::OnceCallback<void(bool did_show_generic_notification)>;

  explicit PushMessagingNotificationManager(Profile* profile);

  PushMessagingNotificationManager(const PushMessagingNotificationManager&) =
      delete;
  PushMessagingNotificationManager& operator=(
      const PushMessagingNotificationManager&) = delete;

  ~PushMessagingNotificationManager();

  // Enforces the requirements implied for push subscriptions which must display
  // a Web Notification in response to an incoming message.
  // `requested_user_visible_only` is the userVisibleOnly value a worker based
  // extension sets on push subscription.
  void EnforceUserVisibleOnlyRequirements(
      const GURL& origin,
      int64_t service_worker_registration_id,
      EnforceRequirementsCallback message_handled_callback,
      bool requested_user_visible_only);

  // Checks if the userVisibleOnly: true requirement or the notifications
  // permission requirement can be bypassed in certain scenarios.
  //
  // Currently that is only allowed for extensions with workers that set
  // userVisibleOnly: false on subscription.
  bool ShouldBypassUserVisibleOnlyRequirement(const GURL& origin,
                                              bool requested_user_visible_only);
  bool ShouldBypassNotificationPermissionRequirement(
      const GURL& origin,
      bool requested_user_visible_only) {
    return ShouldBypassUserVisibleOnlyRequirement(origin,
                                                  requested_user_visible_only);
  }

 private:
  FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
  FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest,
                           IsTabVisibleViewSource);

  void DidCountVisibleNotifications(
      const GURL& origin,
      int64_t service_worker_registration_id,
      EnforceRequirementsCallback message_handled_callback,
      bool success,
      int notification_count);

  // Checks whether |profile| is the one owning this instance,
  // |active_web_contents| exists and its main frame is visible, and the URL
  // currently visible to the user is for |origin|.
  bool IsTabVisible(Profile* profile,
                    content::WebContents* active_web_contents,
                    const GURL& origin);

  void ProcessSilentPush(const GURL& origin,
                         int64_t service_worker_registration_id,
                         EnforceRequirementsCallback message_handled_callback,
                         bool silent_push_allowed);

  void DidWriteNotificationData(
      EnforceRequirementsCallback message_handled_callback,
      bool success,
      const std::string& notification_id);

  void LogSilentPushEvent(SilentPushEvent event);

#if BUILDFLAG(ENABLE_EXTENSIONS)
  // For extensions builds, skip userVisibleOnly requirement for worker-based
  // extensions that set it to false.
  bool ShouldExtensionsBypassUserVisibleOnlyRequirement(
      const GURL& origin,
      bool requested_user_visible_only);
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

  // Weak. This manager is owned by a keyed service on this profile.
  raw_ptr<Profile> profile_;

  BudgetDatabase budget_database_;

  base::WeakPtrFactory<PushMessagingNotificationManager> weak_factory_{this};
};

#endif  // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_NOTIFICATION_MANAGER_H_