1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
6
7 #include <utility>
8
9 #include "base/check_op.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11
12 using content::WebContents;
13
14 TabStripModelChange::Insert::Insert() = default;
15 TabStripModelChange::Insert::Insert(Insert&& other) = default;
16 TabStripModelChange::Insert& TabStripModelChange::Insert::operator=(Insert&&) =
17 default;
18 TabStripModelChange::Insert::~Insert() = default;
19
20 TabStripModelChange::Remove::Remove() = default;
21 TabStripModelChange::Remove::Remove(Remove&& other) = default;
22 TabStripModelChange::Remove& TabStripModelChange::Remove::operator=(Remove&&) =
23 default;
24 TabStripModelChange::Remove::~Remove() = default;
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // TabStripModelChange
28 //
29 TabStripModelChange::TabStripModelChange() = default;
30
TabStripModelChange(Insert delta)31 TabStripModelChange::TabStripModelChange(Insert delta)
32 : TabStripModelChange(Type::kInserted,
33 std::make_unique<Insert>(std::move(delta))) {}
34
TabStripModelChange(Remove delta)35 TabStripModelChange::TabStripModelChange(Remove delta)
36 : TabStripModelChange(Type::kRemoved,
37 std::make_unique<Remove>(std::move(delta))) {}
38
TabStripModelChange(Move delta)39 TabStripModelChange::TabStripModelChange(Move delta)
40 : TabStripModelChange(Type::kMoved,
41 std::make_unique<Move>(std::move(delta))) {}
42
TabStripModelChange(Replace delta)43 TabStripModelChange::TabStripModelChange(Replace delta)
44 : TabStripModelChange(Type::kReplaced,
45 std::make_unique<Replace>(std::move(delta))) {}
46
47 TabStripModelChange::~TabStripModelChange() = default;
48
GetInsert() const49 const TabStripModelChange::Insert* TabStripModelChange::GetInsert() const {
50 DCHECK_EQ(type_, Type::kInserted);
51 return static_cast<const Insert*>(delta_.get());
52 }
53
GetRemove() const54 const TabStripModelChange::Remove* TabStripModelChange::GetRemove() const {
55 DCHECK_EQ(type_, Type::kRemoved);
56 return static_cast<const Remove*>(delta_.get());
57 }
58
GetMove() const59 const TabStripModelChange::Move* TabStripModelChange::GetMove() const {
60 DCHECK_EQ(type_, Type::kMoved);
61 return static_cast<const Move*>(delta_.get());
62 }
63
GetReplace() const64 const TabStripModelChange::Replace* TabStripModelChange::GetReplace() const {
65 DCHECK_EQ(type_, Type::kReplaced);
66 return static_cast<const Replace*>(delta_.get());
67 }
68
TabStripModelChange(Type type,std::unique_ptr<Delta> delta)69 TabStripModelChange::TabStripModelChange(Type type,
70 std::unique_ptr<Delta> delta)
71 : type_(type), delta_(std::move(delta)) {}
72
73 ////////////////////////////////////////////////////////////////////////////////
74 // TabStripSelectionChange
75 //
76 TabStripSelectionChange::TabStripSelectionChange() = default;
77
TabStripSelectionChange(content::WebContents * contents,const ui::ListSelectionModel & selection_model)78 TabStripSelectionChange::TabStripSelectionChange(
79 content::WebContents* contents,
80 const ui::ListSelectionModel& selection_model)
81 : old_contents(contents),
82 new_contents(contents),
83 old_model(selection_model),
84 new_model(selection_model),
85 reason(0) {}
86
87 TabStripSelectionChange::~TabStripSelectionChange() = default;
88
89 TabStripSelectionChange::TabStripSelectionChange(
90 const TabStripSelectionChange& other) = default;
91
92 TabStripSelectionChange& TabStripSelectionChange::operator=(
93 const TabStripSelectionChange& other) = default;
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // TabGroupChange
97 //
TabGroupChange(tab_groups::TabGroupId group,Type type)98 TabGroupChange::TabGroupChange(tab_groups::TabGroupId group, Type type)
99 : group(group), type(type) {}
100
101 TabGroupChange::~TabGroupChange() = default;
102
103 ////////////////////////////////////////////////////////////////////////////////
104 // TabStripModelObserver
105 //
TabStripModelObserver()106 TabStripModelObserver::TabStripModelObserver() {}
107
~TabStripModelObserver()108 TabStripModelObserver::~TabStripModelObserver() {
109 std::set<TabStripModel*> models(observed_models_.begin(),
110 observed_models_.end());
111 for (auto* model : models) {
112 model->RemoveObserver(this);
113 }
114 }
115
OnTabStripModelChanged(TabStripModel * tab_strip_model,const TabStripModelChange & change,const TabStripSelectionChange & selection)116 void TabStripModelObserver::OnTabStripModelChanged(
117 TabStripModel* tab_strip_model,
118 const TabStripModelChange& change,
119 const TabStripSelectionChange& selection) {}
120
OnTabGroupChanged(const TabGroupChange & change)121 void TabStripModelObserver::OnTabGroupChanged(const TabGroupChange& change) {}
122
TabChangedAt(WebContents * contents,int index,TabChangeType change_type)123 void TabStripModelObserver::TabChangedAt(WebContents* contents,
124 int index,
125 TabChangeType change_type) {
126 }
127
TabPinnedStateChanged(TabStripModel * tab_strip_model,WebContents * contents,int index)128 void TabStripModelObserver::TabPinnedStateChanged(
129 TabStripModel* tab_strip_model,
130 WebContents* contents,
131 int index) {
132 }
133
TabBlockedStateChanged(WebContents * contents,int index)134 void TabStripModelObserver::TabBlockedStateChanged(WebContents* contents,
135 int index) {
136 }
137
TabGroupedStateChanged(base::Optional<tab_groups::TabGroupId> group,content::WebContents * contents,int index)138 void TabStripModelObserver::TabGroupedStateChanged(
139 base::Optional<tab_groups::TabGroupId> group,
140 content::WebContents* contents,
141 int index) {}
142
TabStripEmpty()143 void TabStripModelObserver::TabStripEmpty() {
144 }
145
WillCloseAllTabs(TabStripModel * tab_strip_model)146 void TabStripModelObserver::WillCloseAllTabs(TabStripModel* tab_strip_model) {}
147
CloseAllTabsStopped(TabStripModel * tab_strip_model,CloseAllStoppedReason reason)148 void TabStripModelObserver::CloseAllTabsStopped(TabStripModel* tab_strip_model,
149 CloseAllStoppedReason reason) {}
SetTabNeedsAttentionAt(int index,bool attention)150 void TabStripModelObserver::SetTabNeedsAttentionAt(int index, bool attention) {}
OnTabStripModelDestroyed(TabStripModel * model)151 void TabStripModelObserver::OnTabStripModelDestroyed(TabStripModel* model) {}
152
153 // static
StopObservingAll(TabStripModelObserver * observer)154 void TabStripModelObserver::StopObservingAll(TabStripModelObserver* observer) {
155 while (!observer->observed_models_.empty()) {
156 (*observer->observed_models_.begin())->RemoveObserver(observer);
157 }
158 }
159
160 // static
IsObservingAny(TabStripModelObserver * observer)161 bool TabStripModelObserver::IsObservingAny(TabStripModelObserver* observer) {
162 return !observer->observed_models_.empty();
163 }
164
165 // static
CountObservedModels(TabStripModelObserver * observer)166 int TabStripModelObserver::CountObservedModels(
167 TabStripModelObserver* observer) {
168 return observer->observed_models_.size();
169 }
170
StartedObserving(TabStripModelObserver::ModelPasskey,TabStripModel * model)171 void TabStripModelObserver::StartedObserving(
172 TabStripModelObserver::ModelPasskey,
173 TabStripModel* model) {
174 // TODO(https://crbug.com/991308): Add this DCHECK here. This DCHECK enforces
175 // that a given TabStripModelObserver only observes a given TabStripModel
176 // once.
177 // DCHECK_EQ(observed_models_.count(model), 0U);
178 observed_models_.insert(model);
179 }
180
StoppedObserving(TabStripModelObserver::ModelPasskey,TabStripModel * model)181 void TabStripModelObserver::StoppedObserving(
182 TabStripModelObserver::ModelPasskey,
183 TabStripModel* model) {
184 // TODO(https://crbug.com/991308): Add this DCHECK here. This DCHECK enforces
185 // that a given TabStripModelObserver is only removed from a given
186 // TabStripModel once.
187 // DCHECK_EQ(observed_models_.count(model), 1U);
188 observed_models_.erase(model);
189 }
190
ModelDestroyed(TabStripModelObserver::ModelPasskey,TabStripModel * model)191 void TabStripModelObserver::ModelDestroyed(TabStripModelObserver::ModelPasskey,
192 TabStripModel* model) {
193 model->RemoveObserver(this);
194 OnTabStripModelDestroyed(model);
195 }
196