| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/tabs/tab_strip_model.h" | 5 #include "chrome/browser/tabs/tab_strip_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 } | 150 } |
| 151 data->opener = &selected_contents->controller(); | 151 data->opener = &selected_contents->controller(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 contents_data_.insert(contents_data_.begin() + index, data); | 154 contents_data_.insert(contents_data_.begin() + index, data); |
| 155 | 155 |
| 156 selection_model_.IncrementFrom(index); | 156 selection_model_.IncrementFrom(index); |
| 157 | 157 |
| 158 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 158 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 159 TabInsertedAt(contents, index, active)); | 159 TabInsertedAt(contents, index, active)); |
| 160 | 160 TabStripSelectionModel old_model; |
| 161 old_model.Copy(selection_model_); |
| 161 if (active) { | 162 if (active) { |
| 162 selection_model_.SetSelectedIndex(index); | 163 selection_model_.SetSelectedIndex(index); |
| 163 NotifyTabSelectedIfChanged(selected_contents, index, false); | 164 NotifyIfActiveOrSelectionChanged(selected_contents, false, old_model); |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( | 168 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( |
| 168 int index, | 169 int index, |
| 169 TabContentsWrapper* new_contents) { | 170 TabContentsWrapper* new_contents) { |
| 170 DCHECK(ContainsIndex(index)); | 171 DCHECK(ContainsIndex(index)); |
| 171 TabContentsWrapper* old_contents = GetContentsAt(index); | 172 TabContentsWrapper* old_contents = GetContentsAt(index); |
| 172 | 173 |
| 173 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); | 174 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 199 InternalCloseTabs(closing_tabs, CLOSE_NONE); | 200 InternalCloseTabs(closing_tabs, CLOSE_NONE); |
| 200 } | 201 } |
| 201 | 202 |
| 202 TabContentsWrapper* TabStripModel::DetachTabContentsAt(int index) { | 203 TabContentsWrapper* TabStripModel::DetachTabContentsAt(int index) { |
| 203 if (contents_data_.empty()) | 204 if (contents_data_.empty()) |
| 204 return NULL; | 205 return NULL; |
| 205 | 206 |
| 206 DCHECK(ContainsIndex(index)); | 207 DCHECK(ContainsIndex(index)); |
| 207 | 208 |
| 208 TabContentsWrapper* removed_contents = GetContentsAt(index); | 209 TabContentsWrapper* removed_contents = GetContentsAt(index); |
| 210 bool was_selected = IsTabSelected(index); |
| 209 int next_selected_index = order_controller_->DetermineNewSelectedIndex(index); | 211 int next_selected_index = order_controller_->DetermineNewSelectedIndex(index); |
| 210 delete contents_data_.at(index); | 212 delete contents_data_.at(index); |
| 211 contents_data_.erase(contents_data_.begin() + index); | 213 contents_data_.erase(contents_data_.begin() + index); |
| 212 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); | 214 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); |
| 213 if (empty()) | 215 if (empty()) |
| 214 closing_all_ = true; | 216 closing_all_ = true; |
| 215 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 217 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 216 TabDetachedAt(removed_contents, index)); | 218 TabDetachedAt(removed_contents, index)); |
| 217 if (empty()) { | 219 if (empty()) { |
| 218 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in | 220 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in |
| 219 // a second pass. | 221 // a second pass. |
| 220 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); | 222 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); |
| 221 } else { | 223 } else { |
| 222 int old_active = active_index(); | 224 int old_active = active_index(); |
| 223 selection_model_.DecrementFrom(index); | 225 selection_model_.DecrementFrom(index); |
| 226 TabStripSelectionModel old_model; |
| 227 old_model.Copy(selection_model_); |
| 224 if (index == old_active) { | 228 if (index == old_active) { |
| 225 if (!selection_model_.empty()) { | 229 if (!selection_model_.empty()) { |
| 226 // A selected tab was removed, but there is still something selected. | 230 // The active tab was removed, but there is still something selected. |
| 227 // Move the active and anchor to the first selected index. | 231 // Move the active and anchor to the first selected index. |
| 228 selection_model_.set_active(selection_model_.selected_indices()[0]); | 232 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 229 selection_model_.set_anchor(selection_model_.active()); | 233 selection_model_.set_anchor(selection_model_.active()); |
| 230 NotifyTabSelectedIfChanged(removed_contents, active_index(), false); | |
| 231 } else { | 234 } else { |
| 232 // The active tab was removed and nothing is selected. Reset the | 235 // The active tab was removed and nothing is selected. Reset the |
| 233 // selection and send out notification. | 236 // selection and send out notification. |
| 234 selection_model_.SetSelectedIndex(next_selected_index); | 237 selection_model_.SetSelectedIndex(next_selected_index); |
| 235 NotifyTabSelectedIfChanged(removed_contents, next_selected_index, | |
| 236 false); | |
| 237 } | 238 } |
| 239 NotifyIfActiveTabChanged(removed_contents, false); |
| 240 } |
| 241 |
| 242 // Sending notification in case the detached tab was selected. Using |
| 243 // NotifyIfActiveOrSelectionChanged() here would not guarantee that a |
| 244 // notification is sent even though the tab selection has changed because |
| 245 // |old_model| is stored after calling DecrementFrom(). |
| 246 if (was_selected) { |
| 247 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 248 TabSelectionChanged(old_model)); |
| 238 } | 249 } |
| 239 } | 250 } |
| 240 return removed_contents; | 251 return removed_contents; |
| 241 } | 252 } |
| 242 | 253 |
| 243 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { | 254 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { |
| 244 DCHECK(ContainsIndex(index)); | 255 DCHECK(ContainsIndex(index)); |
| 245 bool had_multi = selection_model_.selected_indices().size() > 1; | 256 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 246 TabContentsWrapper* old_contents = | 257 TabStripSelectionModel old_model; |
| 247 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? | 258 old_model.Copy(selection_model_); |
| 248 NULL : GetActiveTabContents(); | |
| 249 selection_model_.SetSelectedIndex(index); | 259 selection_model_.SetSelectedIndex(index); |
| 250 TabContentsWrapper* new_contents = GetContentsAt(index); | 260 NotifyIfActiveOrSelectionChanged(old_contents, user_gesture, old_model); |
| 251 if (old_contents != new_contents && old_contents) { | |
| 252 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 253 TabDeactivated(old_contents)); | |
| 254 } | |
| 255 if (old_contents != new_contents || had_multi) { | |
| 256 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 257 ActiveTabChanged(old_contents, new_contents, | |
| 258 active_index(), user_gesture)); | |
| 259 } | |
| 260 } | 261 } |
| 261 | 262 |
| 262 void TabStripModel::MoveTabContentsAt(int index, | 263 void TabStripModel::MoveTabContentsAt(int index, |
| 263 int to_position, | 264 int to_position, |
| 264 bool select_after_move) { | 265 bool select_after_move) { |
| 265 DCHECK(ContainsIndex(index)); | 266 DCHECK(ContainsIndex(index)); |
| 266 if (index == to_position) | 267 if (index == to_position) |
| 267 return; | 268 return; |
| 268 | 269 |
| 269 int first_non_mini_tab = IndexOfFirstNonMiniTab(); | 270 int first_non_mini_tab = IndexOfFirstNonMiniTab(); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 return count(); | 562 return count(); |
| 562 } | 563 } |
| 563 | 564 |
| 564 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { | 565 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { |
| 565 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : | 566 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : |
| 566 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); | 567 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); |
| 567 } | 568 } |
| 568 | 569 |
| 569 void TabStripModel::ExtendSelectionTo(int index) { | 570 void TabStripModel::ExtendSelectionTo(int index) { |
| 570 DCHECK(ContainsIndex(index)); | 571 DCHECK(ContainsIndex(index)); |
| 571 int old_active = active_index(); | 572 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 573 TabStripSelectionModel old_model; |
| 574 old_model.Copy(selection_model()); |
| 572 selection_model_.SetSelectionFromAnchorTo(index); | 575 selection_model_.SetSelectionFromAnchorTo(index); |
| 573 // This may not have resulted in a change, but we assume it did. | 576 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 574 NotifyActiveTabChanged(old_active); | |
| 575 } | 577 } |
| 576 | 578 |
| 577 void TabStripModel::ToggleSelectionAt(int index) { | 579 void TabStripModel::ToggleSelectionAt(int index) { |
| 578 DCHECK(ContainsIndex(index)); | 580 DCHECK(ContainsIndex(index)); |
| 579 int old_active = active_index(); | 581 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 582 TabStripSelectionModel old_model; |
| 583 old_model.Copy(selection_model()); |
| 580 if (selection_model_.IsSelected(index)) { | 584 if (selection_model_.IsSelected(index)) { |
| 581 if (selection_model_.size() == 1) { | 585 if (selection_model_.size() == 1) { |
| 582 // One tab must be selected and this tab is currently selected so we can't | 586 // One tab must be selected and this tab is currently selected so we can't |
| 583 // unselect it. | 587 // unselect it. |
| 584 return; | 588 return; |
| 585 } | 589 } |
| 586 selection_model_.RemoveIndexFromSelection(index); | 590 selection_model_.RemoveIndexFromSelection(index); |
| 587 selection_model_.set_anchor(index); | 591 selection_model_.set_anchor(index); |
| 588 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 592 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 589 selection_model_.set_active(selection_model_.selected_indices()[0]); | 593 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 590 } else { | 594 } else { |
| 591 selection_model_.AddIndexToSelection(index); | 595 selection_model_.AddIndexToSelection(index); |
| 592 selection_model_.set_anchor(index); | 596 selection_model_.set_anchor(index); |
| 593 selection_model_.set_active(index); | 597 selection_model_.set_active(index); |
| 594 } | 598 } |
| 595 NotifyActiveTabChanged(old_active); | 599 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 596 } | 600 } |
| 597 | 601 |
| 598 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 602 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 599 int old_active = active_index(); | 603 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 604 TabStripSelectionModel old_model; |
| 605 old_model.Copy(selection_model()); |
| 600 selection_model_.AddSelectionFromAnchorTo(index); | 606 selection_model_.AddSelectionFromAnchorTo(index); |
| 601 NotifyActiveTabChanged(old_active); | 607 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 602 } | 608 } |
| 603 | 609 |
| 604 bool TabStripModel::IsTabSelected(int index) const { | 610 bool TabStripModel::IsTabSelected(int index) const { |
| 605 DCHECK(ContainsIndex(index)); | 611 DCHECK(ContainsIndex(index)); |
| 606 return selection_model_.IsSelected(index); | 612 return selection_model_.IsSelected(index); |
| 607 } | 613 } |
| 608 | 614 |
| 609 void TabStripModel::SetSelectionFromModel( | 615 void TabStripModel::SetSelectionFromModel( |
| 610 const TabStripSelectionModel& source) { | 616 const TabStripSelectionModel& source) { |
| 611 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 617 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 612 int old_active_index = active_index(); | 618 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 619 TabStripSelectionModel old_model; |
| 620 old_model.Copy(selection_model()); |
| 613 selection_model_.Copy(source); | 621 selection_model_.Copy(source); |
| 614 // This may not have resulted in a change, but we assume it did. | 622 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 615 NotifyActiveTabChanged(old_active_index); | |
| 616 } | 623 } |
| 617 | 624 |
| 618 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 625 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 619 int index, | 626 int index, |
| 620 PageTransition::Type transition, | 627 PageTransition::Type transition, |
| 621 int add_types) { | 628 int add_types) { |
| 622 // If the newly-opened tab is part of the same task as the parent tab, we want | 629 // If the newly-opened tab is part of the same task as the parent tab, we want |
| 623 // to inherit the parent's "group" attribute, so that if this tab is then | 630 // to inherit the parent's "group" attribute, so that if this tab is then |
| 624 // closed we'll jump back to the parent tab. | 631 // closed we'll jump back to the parent tab. |
| 625 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 632 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 // and detach it. | 1207 // and detach it. |
| 1201 delete contents; | 1208 delete contents; |
| 1202 } | 1209 } |
| 1203 | 1210 |
| 1204 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { | 1211 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { |
| 1205 CHECK(ContainsIndex(index)) << | 1212 CHECK(ContainsIndex(index)) << |
| 1206 "Failed to find: " << index << " in: " << count() << " entries."; | 1213 "Failed to find: " << index << " in: " << count() << " entries."; |
| 1207 return contents_data_.at(index)->contents; | 1214 return contents_data_.at(index)->contents; |
| 1208 } | 1215 } |
| 1209 | 1216 |
| 1210 void TabStripModel::NotifyTabSelectedIfChanged(TabContentsWrapper* old_contents, | 1217 void TabStripModel::NotifyIfActiveTabChanged( |
| 1211 int to_index, | 1218 TabContentsWrapper* old_contents, |
| 1212 bool user_gesture) { | 1219 bool user_gesture) { |
| 1213 TabContentsWrapper* new_contents = GetContentsAt(to_index); | 1220 TabContentsWrapper* new_contents = GetContentsAt(active_index()); |
| 1214 if (old_contents == new_contents) | 1221 if (old_contents != new_contents) { |
| 1215 return; | 1222 if (old_contents) { |
| 1216 | 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1217 TabContentsWrapper* last_selected_contents = old_contents; | 1224 TabDeactivated(old_contents)); |
| 1218 if (last_selected_contents) { | 1225 } |
| 1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1226 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1220 TabDeactivated(last_selected_contents)); | 1227 ActiveTabChanged(old_contents, new_contents, |
| 1228 active_index(), user_gesture)); |
| 1221 } | 1229 } |
| 1222 | |
| 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1224 ActiveTabChanged(last_selected_contents, new_contents, | |
| 1225 active_index(), user_gesture)); | |
| 1226 } | 1230 } |
| 1227 | 1231 |
| 1228 void TabStripModel::NotifyActiveTabChanged(int old_active_index) { | 1232 void TabStripModel::NotifyIfActiveOrSelectionChanged( |
| 1229 TabContentsWrapper* old_tab = | 1233 TabContentsWrapper* old_contents, |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | 1234 bool user_gesture, |
| 1231 NULL : GetTabContentsAt(old_active_index); | 1235 const TabStripSelectionModel& old_model) { |
| 1232 TabContentsWrapper* new_tab = | 1236 NotifyIfActiveTabChanged(old_contents, user_gesture); |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | 1237 |
| 1234 NULL : GetTabContentsAt(active_index()); | 1238 if (!selection_model().Equals(old_model)) { |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1239 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | 1240 TabSelectionChanged(old_model)); |
| 1241 } |
| 1237 } | 1242 } |
| 1238 | 1243 |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1244 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1245 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1246 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1247 if (contents_data_.empty()) |
| 1243 return; | 1248 return; |
| 1244 | 1249 |
| 1245 int index = active_index(); | 1250 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1251 int delta = next ? 1 : -1; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1316 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1317 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1318 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1319 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1320 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1321 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1322 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1323 (*i)->opener = NULL; |
| 1319 } | 1324 } |
| 1320 } | 1325 } |
| OLD | NEW |