File: tab_group_deletion_dialog_controller.h

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 (158 lines) | stat: -rw-r--r-- 5,510 bytes parent folder | download | duplicates (3)
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
// 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_TABS_TAB_GROUP_DELETION_DIALOG_CONTROLLER_H_
#define CHROME_BROWSER_UI_TABS_TAB_GROUP_DELETION_DIALOG_CONTROLLER_H_

#include <memory>
#include <optional>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"

namespace ui {
class DialogModel;
}

class BrowserWindowInterface;
class Profile;
namespace tab_groups {

typedef base::RepeatingCallback<void(std::unique_ptr<ui::DialogModel>)>
    ShowDialogModelCallback;

// Controller that is responsible for showing/hiding and performing callbacks
// for group deletions. Manages the state on a per-browser basis. Browsers can
// only have 1 of these dialogs at a time, therefore only 1 controller. An
// example of this showing up is on Ungroup from the tab group editor bubble.
class DeletionDialogController : public TabStripModelObserver {
 public:
  // Mapping of the different text strings and user preferences on this dialog.
  enum class DialogType {
    // Saved tab group dialogs.
    DeleteSingle,
    UngroupSingle,
    RemoveTabAndDelete,
    CloseTabAndDelete,
    // Shared tab group dialogs.
    DeleteSingleShared,
    CloseTabAndKeepOrLeaveGroup,
    CloseTabAndKeepOrDeleteGroup,
    LeaveGroup,
  };

  enum class DeletionDialogTiming {
    Synchronous,
    Asynchronous,
  };

  // Encapsulates metadata required to determine which strings should be
  // displayed in the deletion dialog.
  struct DialogMetadata {
    DialogMetadata(DialogType type,
                   int closing_group_count,
                   bool closing_multiple_tabs);
    // Used for testing.
    explicit DialogMetadata(DialogType type);
    DialogMetadata(const DialogMetadata&) = delete;

    ~DialogMetadata();

    DialogType type;
    int closing_group_count = 0;
    bool closing_multiple_tabs = false;
    std::optional<std::u16string> title_of_closing_group;
  };

  // State object that represents the current dialog that is being shown.
  struct DialogState {
    DialogState(DialogType type_,
                ui::DialogModel* dialog_model_,
                base::OnceCallback<void(DeletionDialogTiming)> callback_,
                std::optional<base::OnceClosure> keep_groups_);
    ~DialogState();

    // The type the dialog was initiated with.
    DialogType type;

    // A ptr to the original dialog model. Used to access the checkbox value
    // for setting the preference.
    raw_ptr<ui::DialogModel> dialog_model;

    // Callback that runs when the OK button is pressed.
    base::OnceCallback<void(DeletionDialogTiming)> callback;

    // Callback to handle the 'keep' case of CloseTabAndKeepOrLeaveGroup.
    std::optional<base::OnceClosure> keep_groups;
  };

  explicit DeletionDialogController(BrowserWindowInterface* browser);
  DeletionDialogController(BrowserWindowInterface* browser,
                           ShowDialogModelCallback show_dialog_model);

  DeletionDialogController(const DeletionDialogController&) = delete;
  DeletionDialogController& operator=(const DeletionDialogController&) = delete;
  ~DeletionDialogController() override;

  // If the BrowserWindow is currently in state where the dialog can be shown.
  bool CanShowDialog() const;

  // Whether the dialog is showing or not.
  bool IsShowingDialog() const;

  // Gets the dialog state for tests. Allows for calling the callbacks without
  // going through views code.
  void SimulateOkButtonForTesting() { OnDialogOk(); }

  void CreateDialogFromBrowser(BrowserWindowInterface* browser,
                               std::unique_ptr<ui::DialogModel> dialog_model);

  // Attempt to show the dialog. The dialog will only show if it is not already
  // showing, and if the skip dialog option hasn't been set to true.
  // `dialog_metadata` contains information that is used to help construct the
  // strings for the dialog.
  bool MaybeShowDialog(
      const DialogMetadata& dialog_metadata,
      base::OnceCallback<void(DeletionDialogTiming)> callback,
      std::optional<base::OnceCallback<void()>> keep_groups = std::nullopt);

  // TabStripModelObserver implementation:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override;

  void SetPrefsPreventShowingDialogForTesting(bool should_prevent_dialog);

 private:
  // Builds a DialogModel for showing the dialog.
  std::unique_ptr<ui::DialogModel> BuildDialogModel(
      const DialogMetadata& dialog_metadata);

  // Methods that are bound by the DialogModel to call the callbacks.
  void OnDialogOk();
  void OnDialogCancel();

  // The profile this controller is created for. Provides prefs and sync
  // settings.
  const raw_ptr<Profile> profile_;

  // The state needed for showing the dialog. Only exists if the dialog is
  // currently showing.
  std::optional<DialogState> state_;

  // The function used to show the dialog when requested. This is injected so
  // that tests can instrument showing the dialog model.
  ShowDialogModelCallback show_dialog_model_fn_;

  raw_ptr<views::Widget> widget_;
  raw_ptr<TabStripModel> tab_strip_model_;
};

}  // namespace tab_groups

#endif  // CHROME_BROWSER_UI_TABS_TAB_GROUP_DELETION_DIALOG_CONTROLLER_H_