File: on_task_notifications_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (161 lines) | stat: -rw-r--r-- 6,442 bytes parent folder | download | duplicates (6)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_NOTIFICATIONS_MANAGER_H_
#define CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_NOTIFICATIONS_MANAGER_H_

#include <map>
#include <memory>
#include <string>

#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/system/toast_data.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/delayed_task_handle.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/boca/on_task/notification_constants.h"
#include "chromeos/ash/components/boca/on_task/on_task_notification_blocker.h"
#include "ui/message_center/public/cpp/notifier_id.h"

namespace ash::boca {

// Notifications manager implementation that is primarily used for triggering
// and managing OnTask specific toasts and notifications.
class OnTaskNotificationsManager {
 public:
  // Encapsulation of params needed for toast creation. This also includes
  // the completion callback that is triggered after the countdown ends.
  // TODO (crbug.com/376170581): Countdown helpers should be associated with
  // notifications instead.
  struct ToastCreateParams {
    ToastCreateParams(
        std::string id,
        ToastCatalogName catalog_name,
        base::RepeatingCallback<std::u16string(base::TimeDelta)>
            text_description_callback,
        base::RepeatingClosure completion_callback = base::DoNothing(),
        base::TimeDelta countdown_period =
            base::Seconds(1));  // Show toast for 1 second at the very least.
    ToastCreateParams(const ToastCreateParams& other);
    ToastCreateParams& operator=(const ToastCreateParams& other);
    ToastCreateParams(ToastCreateParams&& other);
    ToastCreateParams& operator=(ToastCreateParams&& other);
    ~ToastCreateParams();

    std::string id;
    ToastCatalogName catalog_name;
    base::RepeatingCallback<std::u16string(base::TimeDelta)>
        text_description_callback;
    base::RepeatingClosure completion_callback;
    base::TimeDelta countdown_period;
  };

  // Encapsulation of params needed for creating notifications.
  struct NotificationCreateParams {
    NotificationCreateParams(
        std::string id,
        std::u16string title,
        int message_id,
        message_center::NotifierId notifier_id,
        base::RepeatingClosure completion_callback = base::DoNothing(),
        base::TimeDelta countdown_period =
            kDefaultOnTaskNotificationCountdownDuration,
        bool is_counting_down = false);
    NotificationCreateParams(const NotificationCreateParams& other);
    NotificationCreateParams& operator=(const NotificationCreateParams& other);
    NotificationCreateParams(NotificationCreateParams&& other);
    NotificationCreateParams& operator=(NotificationCreateParams&& other);
    ~NotificationCreateParams();

    std::string id;
    std::u16string title;
    int message_id;
    message_center::NotifierId notifier_id;
    base::RepeatingClosure completion_callback;
    base::TimeDelta countdown_period;
    bool is_counting_down;
  };

  // Delegate implementation that can be overridden by tests to stub toast
  // display actions. Especially relevant for tests that cannot leverage Ash UI.
  class Delegate {
   public:
    Delegate() = default;
    Delegate(const Delegate&) = delete;
    Delegate& operator=(const Delegate&) = delete;
    virtual ~Delegate() = default;

    // Display toast using the specified data.
    virtual void ShowToast(ToastData toast_data);

    // Display specified notification.
    virtual void ShowNotification(
        std::unique_ptr<message_center::Notification> notification);

    // Clears the notification with the specified id, if it exists.
    virtual void ClearNotification(const std::string& notification_id);
  };

  // Static factory helpers.
  static std::unique_ptr<OnTaskNotificationsManager> Create();
  static std::unique_ptr<OnTaskNotificationsManager> CreateForTest(
      std::unique_ptr<Delegate> delegate);

  OnTaskNotificationsManager(const OnTaskNotificationsManager&) = delete;
  OnTaskNotificationsManager& operator=(const OnTaskNotificationsManager&) =
      delete;
  ~OnTaskNotificationsManager();

  // Creates a toast using the specified params. Includes a one second timer
  // delay before the toast is surfaced.
  void CreateToast(ToastCreateParams params);

  // Creates a notification using the specified params.
  void CreateNotification(NotificationCreateParams params);

  // Stops processing of the specified notification. Normally triggered when the
  // notification has been processed or when there is an override.
  void StopProcessingNotification(const std::string& notification_id);

  // Clears the notification with the specified id, if it exists.
  void ClearNotification(const std::string& notification_id);

  // Prepare notification manager for the specified locked mode. Includes
  // setting up the notification blocker to prevent surfacing notifications that
  // possibly allow users to exit this mode.
  void ConfigureForLockedMode(bool locked);

  // Retrieves the notification blocker for testing purposes.
  OnTaskNotificationBlocker* GetNotificationBlockerForTesting();

 private:
  explicit OnTaskNotificationsManager(std::unique_ptr<Delegate> delegate);

  // Internal helper used by the timer to surface toast notifications
  // periodically.
  void CreateToastInternal(ToastCreateParams& params);

  // Internal helper used by the timer to surface system notifications
  // periodically.
  void CreateNotificationInternal(NotificationCreateParams& params,
                                  const base::TimeTicks end_time);

  SEQUENCE_CHECKER(sequence_checker_);
  const std::unique_ptr<Delegate> delegate_;
  std::map<std::string, std::unique_ptr<base::RepeatingTimer>>
      pending_notifications_map_ GUARDED_BY_CONTEXT(sequence_checker_);
  std::unique_ptr<OnTaskNotificationBlocker> notification_blocker_
      GUARDED_BY_CONTEXT(sequence_checker_);

  base::WeakPtrFactory<OnTaskNotificationsManager> weak_ptr_factory_{this};
};

}  // namespace ash::boca

#endif  // CHROMEOS_ASH_COMPONENTS_BOCA_ON_TASK_ON_TASK_NOTIFICATIONS_MANAGER_H_