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
|
// 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 CHROME_BROWSER_UI_TOASTS_TOAST_CONTROLLER_H_
#define CHROME_BROWSER_UI_TOASTS_TOAST_CONTROLLER_H_
#include <optional>
#include <string>
#include <vector>
#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/timer/timer.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_observer.h"
#include "chrome/browser/ui/omnibox/omnibox_tab_helper.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/base/models/image_model.h"
#include "ui/views/widget/widget_observer.h"
class BrowserWindowInterface;
class ToastRegistry;
class ToastSpecification;
enum class ToastId;
namespace content {
class Page;
}
namespace toasts {
enum class ToastCloseReason;
class ToastView;
} // namespace toasts
namespace ui {
class MenuModel;
}
namespace views {
class Widget;
}
struct ToastParams {
explicit ToastParams(ToastId id);
ToastParams(ToastParams&& other) noexcept;
ToastParams& operator=(ToastParams&& other) noexcept;
~ToastParams();
ToastId toast_id;
std::vector<std::u16string> body_string_replacement_params;
std::vector<std::u16string> action_button_string_replacement_params;
std::optional<int> body_string_cardinality_param;
std::optional<std::u16string> body_string_override;
std::optional<ui::ImageModel> image_override;
std::unique_ptr<ui::MenuModel> menu_model;
};
// Manages the toast that is displayed for a particular browser. Only one toast
// can be displayed at a time. Subsequent calls to MaybeShowToast() will dismiss
// the current toast and automatically display the requested one.
class ToastController : public views::WidgetObserver,
public FullscreenObserver,
public OmniboxTabHelper::Observer,
public content::WebContentsObserver {
public:
explicit ToastController(BrowserWindowInterface* browser_window_interface,
const ToastRegistry* toast_registry);
~ToastController() override;
void Init();
bool IsShowingToast() const;
bool CanShowToast(ToastId id) const;
std::optional<ToastId> GetCurrentToastId() const;
// Attempts to show the toast and returns true if the toast was successfully
// shown, otherwise return false.
bool MaybeShowToast(ToastParams params);
// views::WidgetObserver:
#if BUILDFLAG(IS_MAC)
void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
#endif
void OnWidgetDestroyed(views::Widget* widget) override;
// content::WebContentsObserver:
void PrimaryPageChanged(content::Page& page) override;
// OmniboxTabHelper::Observer:
void OnOmniboxInputStateChanged() override {}
void OnOmniboxInputInProgress(bool in_progress) override;
void OnOmniboxFocusChanged(OmniboxFocusState state,
OmniboxFocusChangeReason reason) override;
void OnOmniboxPopupVisibilityChanged(bool popup_is_open) override;
// content::WebContentsObserver:
void WebContentsDestroyed() override;
views::Widget* GetToastWidgetForTesting() { return toast_widget_; }
toasts::ToastView* GetToastViewForTesting() { return toast_view_; }
base::OneShotTimer* GetToastCloseTimerForTesting();
static constexpr base::TimeDelta kToastDefaultTimeout = base::Seconds(4);
static constexpr base::TimeDelta kToastWithActionTimeout = base::Seconds(8);
private:
void OnActiveTabChanged(BrowserWindowInterface* browser_interface);
void QueueToast(ToastParams params);
void ShowToast(ToastParams params);
virtual void CreateToast(ToastParams params, const ToastSpecification* spec);
virtual void CloseToast(toasts::ToastCloseReason reason);
std::u16string FormatString(int string_id,
std::vector<std::u16string> replacement,
std::optional<int> cardinality);
void ClearTabScopedToasts();
void UpdateToastWidgetVisibility(bool show_toast_widget);
bool ShouldRenderToastOverWebContents();
// FullscreenObserver:
void OnFullscreenStateChanged() override;
const raw_ptr<BrowserWindowInterface> browser_window_interface_;
const raw_ptr<const ToastRegistry> toast_registry_;
// Used to transition between the current toast and the queued one.
std::optional<ToastParams> next_toast_params_;
std::optional<ToastId> currently_showing_toast_id_;
base::OneShotTimer toast_close_timer_;
bool is_omnibox_popup_showing_ = false;
// Observer to check for browser window entering fullscreen.
base::ScopedObservation<FullscreenController, FullscreenObserver>
fullscreen_observation_{this};
// Observer to check when the toast is destroyed.
base::ScopedObservation<views::Widget, views::WidgetObserver> toast_observer_{
this};
base::ScopedObservation<OmniboxTabHelper, OmniboxTabHelper::Observer>
omnibox_helper_observer_{this};
raw_ptr<toasts::ToastView> toast_view_;
raw_ptr<views::Widget> toast_widget_;
std::vector<base::CallbackListSubscription> browser_subscriptions_;
};
#endif // CHROME_BROWSER_UI_TOASTS_TOAST_CONTROLLER_H_
|