File: toast_manager_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (371 lines) | stat: -rw-r--r-- 11,781 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/toast/toast_manager_impl.h"

#include <algorithm>

#include "ash/public/cpp/system/scoped_toast_pause.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"

namespace ash {

namespace {

constexpr char NotifierFrameworkToastHistogram[] =
    "Ash.NotifierFramework.Toast";

// Used in histogram names.
std::string GetToastDismissedTimeRange(const base::TimeDelta& time) {
  if (time <= base::Seconds(2))
    return "Within2s";
  // Toast default duration is 6s, but with animation it's usually
  // around ~6.2s, so recording 7s as the default case.
  if (time <= base::Seconds(7))
    return "Within7s";
  return "After7s";
}

}  // namespace

///////////////////////////////////////////////////////////////////////////////
// PausableTimer:
// Timer class that owns a `base::OneShotTimer` that can be paused and resumed
// by the `ToastManagerImpl` to continue with the remainder of its duration.
// Different from `base::RetainingOneShotTimer` in that restarting it will set
// the duration for the remainder of the time that it had left when it was
// paused.
class ToastManagerImpl::PausableTimer {
 public:
  PausableTimer() = default;
  PausableTimer(const PausableTimer&) = delete;
  PausableTimer& operator=(const PausableTimer&) = delete;
  ~PausableTimer() = default;

  // Returns whether `timer_` is running.
  bool IsRunning() const { return timer_.IsRunning(); }

  // Starts `timer_` with a duration of `duration` and a scheduled task of
  // `task`.
  void Start(base::TimeDelta duration, base::RepeatingClosure task) {
    DCHECK(!duration.is_max());
    DCHECK(task);
    DCHECK(!IsRunning());
    duration_remaining_ = duration;
    task_ = task;
    timer_.Start(FROM_HERE, duration_remaining_, task_);
    time_last_started_ = base::TimeTicks::Now();
  }

  // Stops the timer, allowing for the user to call `Resume` at a later time to
  // continue the timer.
  void Pause() {
    DCHECK(IsRunning());
    timer_.Stop();
    duration_remaining_ -= base::TimeTicks::Now() - time_last_started_;
  }

  // Restarts the timer with a duration of `duration_remaining_`.
  void Resume() { Start(duration_remaining_, task_); }

  // Fully stops the timer without leaving a chance to call `Resume` later.
  void Stop() {
    task_.Reset();
    duration_remaining_ = base::Seconds(0);
    timer_.Stop();
  }

 private:
  // Task that will be run when `timer_` has elapsed.
  base::RepeatingClosure task_;

  // Time remaining for the timer. Allows for us to calculate how much time is
  // remaining when the timer is paused.
  base::TimeDelta duration_remaining_;

  // Tracks when `timer_` was last started so that we can calculate
  // `duration_remaining_` if the timer is later paused.
  base::TimeTicks time_last_started_;

  // A timer that will run `task_` when `duration_remaining_` has elapsed if
  // it is not paused before then.
  base::OneShotTimer timer_;
};

///////////////////////////////////////////////////////////////////////////////
// ToastManagerImpl:
ToastManagerImpl::ToastManagerImpl()
    : current_toast_expiration_timer_(std::make_unique<PausableTimer>()),
      locked_(Shell::Get()->session_controller()->IsScreenLocked()) {
  Shell::Get()->AddShellObserver(this);
}

ToastManagerImpl::~ToastManagerImpl() {
  Shell::Get()->RemoveShellObserver(this);

  // If there are live `ToastOverlay`s, destroying `current_toast_data_` can
  // call into the `ToastOverlay`s and then back into `ToastManagerImpl`, which
  // then tries to destroy the already-being-destroyed `current_toast_data_`.
  CloseAllToastsWithoutAnimation();
}

void ToastManagerImpl::Show(ToastData data) {
  std::string_view id = data.id;
  DCHECK(!id.empty());

  LOG(ERROR) << "Show toast called, toast id: " << id;

  // If `pause_counter_` is greater than 0, no toasts should be shown.
  if (pause_counter_ > 0) {
    LOG(ERROR)
        << "Toast not shown, pause_counter_ is creater than 0, toast id: "
        << id;
    return;
  }

  auto existing_toast = std::ranges::find(queue_, id, &ToastData::id);

  if (existing_toast != queue_.end()) {
    LOG(ERROR)
        << "Toast requested show with a matching ID toast already queued.";
    // Assigns given `data` to existing queued toast, but keeps the existing
    // toast's `time_created` value.
    const base::TimeTicks old_time_created = existing_toast->time_created;
    *existing_toast = std::move(data);
    existing_toast->time_created = old_time_created;
  } else {
    if (IsToastShown(id)) {
      LOG(ERROR) << "Toast with matching ID requested show while it was "
                    "already shown.";
      // Replace the visible toast by adding the new toast data to the front of
      // the queue and hiding the visible toast. Once the visible toast finishes
      // hiding, the new toast will be displayed.
      queue_.emplace_front(std::move(data));

      CloseAllToastsWithAnimation();
      return;
    }

    LOG(ERROR) << "Placing Toast in the back of the queue.";
    queue_.emplace_back(std::move(data));
  }

  if (queue_.size() == 1 && !HasActiveToasts()) {
    LOG(ERROR) << "Toast is the only one in the queue after being requested, "
                  "so showing it.";
    ShowLatest();
  }
}

void ToastManagerImpl::Cancel(std::string_view id) {
  if (IsToastShown(id)) {
    CloseAllToastsWithAnimation();
    return;
  }

  auto cancelled_toast = std::ranges::find(queue_, id, &ToastData::id);
  if (cancelled_toast != queue_.end())
    queue_.erase(cancelled_toast);
}

bool ToastManagerImpl::RequestFocusOnActiveToastButton(std::string_view id) {
  CHECK(IsToastShown(id));
  for (auto& [_, overlay] : root_window_to_overlay_) {
    if (overlay && overlay->RequestFocusOnActiveToastButton()) {
      return true;
    }
  }
  return false;
}

bool ToastManagerImpl::IsToastShown(std::string_view id) const {
  return HasActiveToasts() && current_toast_data_ &&
         current_toast_data_->id == id;
}

bool ToastManagerImpl::IsToastButtonFocused(std::string_view id) const {
  if (!IsToastShown(id)) {
    return false;
  }

  for (const auto& [_, overlay] : root_window_to_overlay_) {
    if (overlay && overlay->IsButtonFocused()) {
      return true;
    }
  }

  return false;
}

std::unique_ptr<ScopedToastPause> ToastManagerImpl::CreateScopedPause() {
  return std::make_unique<ScopedToastPause>();
}

void ToastManagerImpl::CloseToast() {
  const base::TimeDelta user_journey_time =
      base::TimeTicks::Now() - current_toast_data_->time_start_showing;
  const std::string time_range = GetToastDismissedTimeRange(user_journey_time);
  base::UmaHistogramEnumeration(
      base::StringPrintf("%s.Dismissed.%s", NotifierFrameworkToastHistogram,
                         time_range.c_str()),
      current_toast_data_->catalog_name);

  CloseAllToastsWithoutAnimation();

  current_toast_data_.reset();
  current_toast_expiration_timer_->Stop();

  // Show the next toast if available.
  // Note that don't show during the lock state is changing, since we reshow
  // manually after the state is changed. See OnLockStateChanged.
  if (!queue_.empty())
    ShowLatest();
}

void ToastManagerImpl::OnToastHoverStateChanged(bool is_hovering) {
  DCHECK(current_toast_data_->persist_on_hover);

  if (is_hovering != current_toast_expiration_timer_->IsRunning())
    return;

  is_hovering ? current_toast_expiration_timer_->Pause()
              : current_toast_expiration_timer_->Resume();
}

void ToastManagerImpl::OnSessionStateChanged(
    session_manager::SessionState state) {
  locked_ = state != session_manager::SessionState::ACTIVE;
  current_toast_data_.reset();
  CloseAllToastsWithoutAnimation();
}

void ToastManagerImpl::ShowLatest() {
  DCHECK(!HasActiveToasts());
  DCHECK(!current_toast_data_);

  auto it = locked_ ? std::ranges::find(queue_, true,
                                        &ToastData::visible_on_lock_screen)
                    : queue_.begin();
  if (it == queue_.end()) {
    LOG(ERROR) << "Toast Queue empty.";
    return;
  }

  current_toast_data_ = std::move(*it);
  queue_.erase(it);

  LOG(ERROR) << "Showing latest toast, toast id: " << current_toast_data_->id;
  serial_++;

  if (current_toast_data_->show_on_all_root_windows) {
    for (aura::Window* root_window : Shell::GetAllRootWindows()) {
      CreateToastOverlayForRoot(root_window);
    }
  } else {
    CreateToastOverlayForRoot(Shell::GetRootWindowForNewWindows());
  }

  DCHECK(!current_toast_expiration_timer_->IsRunning());

  current_toast_expiration_timer_->Start(
      current_toast_data_->duration,
      base::BindRepeating(&ToastManagerImpl::CloseAllToastsWithAnimation,
                          base::Unretained(this)));

  base::UmaHistogramEnumeration("Ash.NotifierFramework.Toast.ShownCount",
                                current_toast_data_->catalog_name);
  base::UmaHistogramMediumTimes(
      "Ash.NotifierFramework.Toast.TimeInQueue",
      base::TimeTicks::Now() - current_toast_data_->time_created);
}

void ToastManagerImpl::CreateToastOverlayForRoot(aura::Window* root_window) {
  auto& new_overlay = root_window_to_overlay_[root_window];
  DCHECK(!new_overlay);
  DCHECK(current_toast_data_);
  new_overlay = std::make_unique<ToastOverlay>(
      /*delegate=*/this, *current_toast_data_, root_window);
  new_overlay->Show(true);

  // We only want to record this value when the first instance of the toast is
  // initialized.
  if (current_toast_data_->time_start_showing.is_null())
    current_toast_data_->time_start_showing = base::TimeTicks::Now();
}

void ToastManagerImpl::CloseAllToastsWithAnimation() {
  for (auto& [_, overlay] : root_window_to_overlay_) {
    if (overlay) {
      overlay->Show(false);
    }
  }
}

void ToastManagerImpl::CloseAllToastsWithoutAnimation() {
  for (auto& [_, overlay] : root_window_to_overlay_) {
    overlay.reset();
  }

  // `OnClosed` (the other place where we stop the
  // `current_toast_expiration_timer_`) is only called when the toast is being
  // closed with animation, so we still want to stop the timer here for when it
  // is not animating to close.
  current_toast_expiration_timer_->Stop();
}

bool ToastManagerImpl::HasActiveToasts() const {
  for (const auto& [_, overlay] : root_window_to_overlay_) {
    if (overlay) {
      return true;
    }
  }

  return false;
}

ToastOverlay* ToastManagerImpl::GetCurrentOverlayForTesting(
    aura::Window* root_window) {
  return root_window_to_overlay_[root_window].get();
}

void ToastManagerImpl::OnRootWindowAdded(aura::Window* root_window) {
  if (HasActiveToasts() && current_toast_data_ &&
      current_toast_data_->show_on_all_root_windows) {
    CreateToastOverlayForRoot(root_window);
  }
}

void ToastManagerImpl::OnRootWindowWillShutdown(aura::Window* root_window) {
  // If the toast only exists in the root window that is being closed, inform
  // the manager that the toast should be closed.
  if (root_window_to_overlay_[root_window] &&
      !current_toast_data_->show_on_all_root_windows) {
    CloseToast();
  }

  root_window_to_overlay_.erase(root_window);
}

void ToastManagerImpl::Pause() {
  ++pause_counter_;

  // Immediately closes all the toasts. Since `OnClosed` will not be called,
  // manually resets `current_toast_data_` and `queue_`.
  CloseAllToastsWithoutAnimation();
  current_toast_data_.reset();
  queue_.clear();
}

void ToastManagerImpl::Resume() {
  CHECK_GT(pause_counter_, 0);
  --pause_counter_;
}

}  // namespace ash