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