Skip to content

Commit

Permalink
Added SystemNudgePauseManager to support system nudge pause
Browse files Browse the repository at this point in the history
This CL added a SystemNudgePauseManager to manage all
SystemNudgeController so that we could add a ScopedNudgePause to
suppress them. SystemNudgePauseManager has a static getter to get the
singleton. When a ScopedNudgePause is constructed, the manager will
suppress all the system nudges immediately until ScopedNudgePause is
destroyed. Unittests are added in SystemNudgeTest.

Bug: b:271480256
Change-Id: I79ea2a3e263e3546c9239d9099c25855c1496085
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4736620
Reviewed-by: Ahmed Fakhry <afakhry@chromium.org>
Commit-Queue: Sylvie Liu <sylvieliu@chromium.org>
Reviewed-by: Alex Newcomer <newcomer@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1184302}
  • Loading branch information
Sylvie Liu authored and Chromium LUCI CQ committed Aug 16, 2023
1 parent e757559 commit e13f7b8
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 73 deletions.
2 changes: 2 additions & 0 deletions ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,8 @@ component("ash") {
"system/toast/anchored_nudge.h",
"system/toast/anchored_nudge_manager_impl.cc",
"system/toast/anchored_nudge_manager_impl.h",
"system/toast/system_nudge_pause_manager_impl.cc",
"system/toast/system_nudge_pause_manager_impl.h",
"system/toast/system_nudge_view.cc",
"system/toast/system_nudge_view.h",
"system/toast/toast_manager_impl.cc",
Expand Down
6 changes: 4 additions & 2 deletions ash/public/cpp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,12 @@ component("cpp") {
"system/anchored_nudge_manager.h",
"system/power/power_button_controller_base.cc",
"system/power/power_button_controller_base.h",
"system/scoped_anchored_nudge_pause.cc",
"system/scoped_anchored_nudge_pause.h",
"system/scoped_nudge_pause.cc",
"system/scoped_nudge_pause.h",
"system/scoped_toast_pause.cc",
"system/scoped_toast_pause.h",
"system/system_nudge_pause_manager.cc",
"system/system_nudge_pause_manager.h",
"system/toast_data.cc",
"system/toast_data.h",
"system/toast_manager.cc",
Expand Down
10 changes: 6 additions & 4 deletions ash/public/cpp/system/anchored_nudge_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ash {

struct AnchoredNudgeData;
class ScopedAnchoredNudgePause;
class ScopedNudgePause;

// Public interface to show anchored nudges.
class ASH_PUBLIC_EXPORT AnchoredNudgeManager {
Expand Down Expand Up @@ -43,15 +43,17 @@ class ASH_PUBLIC_EXPORT AnchoredNudgeManager {
// Returns true if the nudge with `id` is shown at the moment.
virtual bool IsNudgeShown(const std::string& id) = 0;

// Creates a `ScopedAnchoredNudgePause`.
virtual std::unique_ptr<ScopedAnchoredNudgePause> CreateScopedPause() = 0;
// Creates a `ScopedNudgePause`, which closes all `AnchoredNudge`'s and
// `SystemNudge`'s, and prevents more from being shown while any
// `ScopedNudgePause` is in scope.
virtual std::unique_ptr<ScopedNudgePause> CreateScopedPause() = 0;

protected:
AnchoredNudgeManager();
virtual ~AnchoredNudgeManager();

private:
friend class ScopedAnchoredNudgePause;
friend class ScopedNudgePause;

// `Pause()` will stop all the nudges from showing up, until `Resume()` is
// called.
Expand Down
19 changes: 0 additions & 19 deletions ash/public/cpp/system/scoped_anchored_nudge_pause.cc

This file was deleted.

23 changes: 0 additions & 23 deletions ash/public/cpp/system/scoped_anchored_nudge_pause.h

This file was deleted.

27 changes: 27 additions & 0 deletions ash/public/cpp/system/scoped_nudge_pause.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 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/public/cpp/system/scoped_nudge_pause.h"

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/public/cpp/system/system_nudge_pause_manager.h"

namespace ash {

ScopedNudgePause::ScopedNudgePause() {
if (features::IsSystemNudgeV2Enabled()) {
AnchoredNudgeManager::Get()->Pause();
}
SystemNudgePauseManager::Get()->Pause();
}

ScopedNudgePause::~ScopedNudgePause() {
if (features::IsSystemNudgeV2Enabled()) {
AnchoredNudgeManager::Get()->Resume();
}
SystemNudgePauseManager::Get()->Resume();
}

} // namespace ash
25 changes: 25 additions & 0 deletions ash/public/cpp/system/scoped_nudge_pause.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_PUBLIC_CPP_SYSTEM_SCOPED_NUDGE_PAUSE_H_
#define ASH_PUBLIC_CPP_SYSTEM_SCOPED_NUDGE_PAUSE_H_

#include "ash/public/cpp/ash_public_export.h"

namespace ash {

// An object that pauses both anchored nudge and system nudge for its lifetime.
// TODO(b/295378782): Move this class to `AnchoredNudgeManager` once complete
// migrating all nudges to `AnchoredNudgeManager`.
class ASH_PUBLIC_EXPORT ScopedNudgePause {
public:
ScopedNudgePause();
ScopedNudgePause(const ScopedNudgePause&) = delete;
ScopedNudgePause& operator=(const ScopedNudgePause&) = delete;
~ScopedNudgePause();
};

} // namespace ash

#endif // ASH_PUBLIC_CPP_SYSTEM_SCOPED_NUDGE_PAUSE_H_
32 changes: 32 additions & 0 deletions ash/public/cpp/system/system_nudge_pause_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 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/public/cpp/system/system_nudge_pause_manager.h"

#include "base/check_op.h"

namespace ash {

namespace {

SystemNudgePauseManager* g_instance = nullptr;

} // namespace

// static
SystemNudgePauseManager* SystemNudgePauseManager::Get() {
return g_instance;
}

SystemNudgePauseManager::SystemNudgePauseManager() {
CHECK(!g_instance);
g_instance = this;
}

SystemNudgePauseManager::~SystemNudgePauseManager() {
CHECK_EQ(this, g_instance);
g_instance = nullptr;
}

} // namespace ash
44 changes: 44 additions & 0 deletions ash/public/cpp/system/system_nudge_pause_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_PUBLIC_CPP_SYSTEM_SYSTEM_NUDGE_PAUSE_MANAGER_H_
#define ASH_PUBLIC_CPP_SYSTEM_SYSTEM_NUDGE_PAUSE_MANAGER_H_

#include <memory>

#include "ash/public/cpp/ash_public_export.h"

namespace ash {

class ScopedNudgePause;
class SystemNudgeController;

// Public interface to manage `SystemNudgeController`. This is needed for
// `ScopedNudgePause` since we need a singleton class to manage all the
// `SystemNudgeController`s to respond to `ScopedNudgePause`'s construction or
// destruction.
class ASH_PUBLIC_EXPORT SystemNudgePauseManager {
public:
// Returns the singleton `SystemNudgePauseManager`.
static SystemNudgePauseManager* Get();

// Creates a `ScopedNudgePause`.
virtual std::unique_ptr<ScopedNudgePause> CreateScopedPause() = 0;

protected:
SystemNudgePauseManager();
virtual ~SystemNudgePauseManager();

private:
friend class ScopedNudgePause;

// `Pause()` will stop all the nudges from showing up, until `Resume()` is
// called.
virtual void Pause() = 0;
virtual void Resume() = 0;
};

} // namespace ash

#endif // ASH_PUBLIC_CPP_SYSTEM_SYSTEM_NUDGE_PAUSE_MANAGER_H_
6 changes: 6 additions & 0 deletions ash/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
#include "ash/system/status_area_widget.h"
#include "ash/system/system_notification_controller.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/system/toast/system_nudge_pause_manager_impl.h"
#include "ash/system/toast/toast_manager_impl.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/system/usb_peripheral/usb_peripheral_notification_controller.h"
Expand Down Expand Up @@ -999,6 +1000,10 @@ Shell::~Shell() {
// |window_tree_host_manager_|.
clipboard_history_controller_.reset();

// Should be destroyed after `clipbaord_history_controller_` and
// `autozoom_controller_` since they will destruct `SystemNudgeController`.
system_nudge_pause_manager_.reset();

// This also deletes all RootWindows. Note that we invoke Shutdown() on
// WindowTreeHostManager before resetting |window_tree_host_manager_|, since
// destruction of its owned RootWindowControllers relies on the value.
Expand Down Expand Up @@ -1201,6 +1206,7 @@ void Shell::Init(
if (features::IsSystemNudgeV2Enabled()) {
anchored_nudge_manager_ = std::make_unique<AnchoredNudgeManagerImpl>();
}
system_nudge_pause_manager_ = std::make_unique<SystemNudgePauseManagerImpl>();

peripheral_battery_listener_ = std::make_unique<PeripheralBatteryListener>();

Expand Down
6 changes: 6 additions & 0 deletions ash/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/system_sounds_delegate.h"
#include "ash/quick_pair/keyed_service/quick_pair_mediator.h"
#include "ash/system/toast/system_nudge_pause_manager_impl.h"
#include "ash/wm/system_modal_container_event_filter_delegate.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
Expand Down Expand Up @@ -237,6 +238,7 @@ class StickyKeysController;
class SystemGestureEventFilter;
class SystemModalContainerEventFilter;
class SystemNotificationController;
class SystemNudgePauseManagerImpl;
class SystemSoundsDelegate;
class SystemTrayModel;
class SystemTrayNotifier;
Expand Down Expand Up @@ -722,6 +724,9 @@ class ASH_EXPORT Shell : public SessionObserver,
SystemNotificationController* system_notification_controller() {
return system_notification_controller_.get();
}
SystemNudgePauseManagerImpl* system_nudge_pause_manager() {
return system_nudge_pause_manager_.get();
}
SystemTrayModel* system_tray_model() { return system_tray_model_.get(); }
SystemTrayNotifier* system_tray_notifier() {
return system_tray_notifier_.get();
Expand Down Expand Up @@ -1040,6 +1045,7 @@ class ASH_EXPORT Shell : public SessionObserver,
std::unique_ptr<AcceleratorTracker> accelerator_tracker_;
std::unique_ptr<ShutdownControllerImpl> shutdown_controller_;
std::unique_ptr<SystemNotificationController> system_notification_controller_;
std::unique_ptr<SystemNudgePauseManagerImpl> system_nudge_pause_manager_;
std::unique_ptr<SystemTrayModel> system_tray_model_;
std::unique_ptr<SystemTrayNotifier> system_tray_notifier_;
std::unique_ptr<SystemSoundsDelegate> system_sounds_delegate_;
Expand Down
6 changes: 3 additions & 3 deletions ash/system/toast/anchored_nudge_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/public/cpp/system/scoped_anchored_nudge_pause.h"
#include "ash/public/cpp/system/scoped_nudge_pause.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/toast/anchored_nudge.h"
Expand Down Expand Up @@ -337,9 +337,9 @@ void AnchoredNudgeManagerImpl::MaybeRecordNudgeAction(
nudge_registry.erase(it);
}

std::unique_ptr<ScopedAnchoredNudgePause>
std::unique_ptr<ScopedNudgePause>
AnchoredNudgeManagerImpl::CreateScopedPause() {
return std::make_unique<ScopedAnchoredNudgePause>();
return std::make_unique<ScopedNudgePause>();
}

void AnchoredNudgeManagerImpl::CloseAllNudges() {
Expand Down
6 changes: 3 additions & 3 deletions ash/system/toast/anchored_nudge_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class View;
namespace ash {

struct AnchoredNudgeData;
class ScopedAnchoredNudgePause;
class ScopedNudgePause;

// Class managing anchored nudge requests.
class ASH_EXPORT AnchoredNudgeManagerImpl : public AnchoredNudgeManager,
Expand All @@ -40,7 +40,7 @@ class ASH_EXPORT AnchoredNudgeManagerImpl : public AnchoredNudgeManager,
void Show(AnchoredNudgeData& nudge_data) override;
void Cancel(const std::string& id) override;
void MaybeRecordNudgeAction(NudgeCatalogName catalog_name) override;
std::unique_ptr<ScopedAnchoredNudgePause> CreateScopedPause() override;
std::unique_ptr<ScopedNudgePause> CreateScopedPause() override;
bool IsNudgeShown(const std::string& id) override;

// Closes all `shown_nudges_`.
Expand Down Expand Up @@ -126,7 +126,7 @@ class ASH_EXPORT AnchoredNudgeManagerImpl : public AnchoredNudgeManager,
// after its duration has passed. Hovering over the nudge pauses the timer.
std::map<std::string, PausableTimer> dismiss_timers_;

// Keeps track of the number of `ScopedAnchoredNudgePause`.
// Keeps track of the number of `ScopedNudgePause`.
int pause_counter_ = 0;

base::WeakPtrFactory<AnchoredNudgeManagerImpl> weak_ptr_factory_{this};
Expand Down

0 comments on commit e13f7b8

Please sign in to comment.