This source file includes following definitions.
- DetermineInsertionIndex
- DetermineNewSelectedIndex
- ActiveTabChanged
- GetValidIndex
#include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
#include "content/public/browser/web_contents.h"
TabStripModelOrderController::TabStripModelOrderController(
TabStripModel* tabstrip)
: tabstrip_(tabstrip) {
tabstrip_->AddObserver(this);
}
TabStripModelOrderController::~TabStripModelOrderController() {
tabstrip_->RemoveObserver(this);
}
int TabStripModelOrderController::DetermineInsertionIndex(
content::PageTransition transition,
bool foreground) {
int tab_count = tabstrip_->count();
if (!tab_count)
return 0;
if (transition == content::PAGE_TRANSITION_LINK &&
tabstrip_->active_index() != -1) {
if (foreground) {
return tabstrip_->active_index() + 1;
}
content::WebContents* opener = tabstrip_->GetActiveWebContents();
int index = tabstrip_->GetIndexOfLastWebContentsOpenedBy(
opener, tabstrip_->active_index());
if (index != TabStripModel::kNoTab)
return index + 1;
return tabstrip_->active_index() + 1;
}
return tabstrip_->count();
}
int TabStripModelOrderController::DetermineNewSelectedIndex(
int removing_index) const {
int tab_count = tabstrip_->count();
DCHECK(removing_index >= 0 && removing_index < tab_count);
content::WebContents* parent_opener =
tabstrip_->GetOpenerOfWebContentsAt(removing_index);
content::WebContents* removed_contents =
tabstrip_->GetWebContentsAt(removing_index);
DCHECK(parent_opener != removed_contents);
int index = tabstrip_->GetIndexOfNextWebContentsOpenedBy(removed_contents,
removing_index,
false);
if (index != TabStripModel::kNoTab)
return GetValidIndex(index, removing_index);
if (parent_opener) {
int index = tabstrip_->GetIndexOfNextWebContentsOpenedBy(parent_opener,
removing_index,
false);
if (index != TabStripModel::kNoTab)
return GetValidIndex(index, removing_index);
index = tabstrip_->GetIndexOfWebContents(parent_opener);
if (index != TabStripModel::kNoTab)
return GetValidIndex(index, removing_index);
}
int selected_index = tabstrip_->active_index();
if (selected_index >= (tab_count - 1))
return selected_index - 1;
return selected_index;
}
void TabStripModelOrderController::ActiveTabChanged(
content::WebContents* old_contents,
content::WebContents* new_contents,
int index,
int reason) {
content::WebContents* old_opener = NULL;
if (old_contents) {
int index = tabstrip_->GetIndexOfWebContents(old_contents);
if (index != TabStripModel::kNoTab) {
old_opener = tabstrip_->GetOpenerOfWebContentsAt(index);
if (tabstrip_->ShouldResetGroupOnSelect(old_contents))
tabstrip_->ForgetGroup(old_contents);
}
}
content::WebContents* new_opener = tabstrip_->GetOpenerOfWebContentsAt(index);
if ((reason & CHANGE_REASON_USER_GESTURE) && new_opener != old_opener &&
((old_contents == NULL && new_opener == NULL) ||
new_opener != old_contents) &&
((new_contents == NULL && old_opener == NULL) ||
old_opener != new_contents)) {
tabstrip_->ForgetAllOpeners();
}
}
int TabStripModelOrderController::GetValidIndex(
int index, int removing_index) const {
if (removing_index < index)
index = std::max(0, index - 1);
return index;
}