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_order_controller.h" | 5 #include "chrome/browser/tabs/tab_strip_model_order_controller.h" |
6 | 6 |
7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
8 #include "content/browser/tab_contents/tab_contents.h" | 8 #include "content/browser/tab_contents/tab_contents.h" |
9 | 9 |
| 10 using content::NavigationController; |
| 11 |
10 /////////////////////////////////////////////////////////////////////////////// | 12 /////////////////////////////////////////////////////////////////////////////// |
11 // TabStripModelOrderController, public: | 13 // TabStripModelOrderController, public: |
12 | 14 |
13 TabStripModelOrderController::TabStripModelOrderController( | 15 TabStripModelOrderController::TabStripModelOrderController( |
14 TabStripModel* tabstrip) | 16 TabStripModel* tabstrip) |
15 : tabstrip_(tabstrip), | 17 : tabstrip_(tabstrip), |
16 insertion_policy_(TabStripModel::INSERT_AFTER) { | 18 insertion_policy_(TabStripModel::INSERT_AFTER) { |
17 tabstrip_->AddObserver(this); | 19 tabstrip_->AddObserver(this); |
18 } | 20 } |
19 | 21 |
(...skipping 12 matching lines...) Expand all Loading... |
32 // NOTE: TabStripModel enforces that all non-mini-tabs occur after mini-tabs, | 34 // NOTE: TabStripModel enforces that all non-mini-tabs occur after mini-tabs, |
33 // so we don't have to check here too. | 35 // so we don't have to check here too. |
34 if (transition == content::PAGE_TRANSITION_LINK && | 36 if (transition == content::PAGE_TRANSITION_LINK && |
35 tabstrip_->active_index() != -1) { | 37 tabstrip_->active_index() != -1) { |
36 int delta = (insertion_policy_ == TabStripModel::INSERT_AFTER) ? 1 : 0; | 38 int delta = (insertion_policy_ == TabStripModel::INSERT_AFTER) ? 1 : 0; |
37 if (foreground) { | 39 if (foreground) { |
38 // If the page was opened in the foreground by a link click in another | 40 // If the page was opened in the foreground by a link click in another |
39 // tab, insert it adjacent to the tab that opened that link. | 41 // tab, insert it adjacent to the tab that opened that link. |
40 return tabstrip_->active_index() + delta; | 42 return tabstrip_->active_index() + delta; |
41 } | 43 } |
42 content::NavigationController* opener = | 44 NavigationController* opener = |
43 &tabstrip_->GetActiveTabContents()->web_contents()->GetController(); | 45 &tabstrip_->GetActiveTabContents()->web_contents()->GetController(); |
44 // Get the index of the next item opened by this tab, and insert after | 46 // Get the index of the next item opened by this tab, and insert after |
45 // it... | 47 // it... |
46 int index; | 48 int index; |
47 if (insertion_policy_ == TabStripModel::INSERT_AFTER) { | 49 if (insertion_policy_ == TabStripModel::INSERT_AFTER) { |
48 index = tabstrip_->GetIndexOfLastTabContentsOpenedBy( | 50 index = tabstrip_->GetIndexOfLastTabContentsOpenedBy( |
49 opener, tabstrip_->active_index()); | 51 opener, tabstrip_->active_index()); |
50 } else { | 52 } else { |
51 index = tabstrip_->GetIndexOfFirstTabContentsOpenedBy( | 53 index = tabstrip_->GetIndexOfFirstTabContentsOpenedBy( |
52 opener, tabstrip_->active_index()); | 54 opener, tabstrip_->active_index()); |
53 } | 55 } |
54 if (index != TabStripModel::kNoTab) | 56 if (index != TabStripModel::kNoTab) |
55 return index + delta; | 57 return index + delta; |
56 // Otherwise insert adjacent to opener... | 58 // Otherwise insert adjacent to opener... |
57 return tabstrip_->active_index() + delta; | 59 return tabstrip_->active_index() + delta; |
58 } | 60 } |
59 // In other cases, such as Ctrl+T, open at the end of the strip. | 61 // In other cases, such as Ctrl+T, open at the end of the strip. |
60 return DetermineInsertionIndexForAppending(); | 62 return DetermineInsertionIndexForAppending(); |
61 } | 63 } |
62 | 64 |
63 int TabStripModelOrderController::DetermineInsertionIndexForAppending() { | 65 int TabStripModelOrderController::DetermineInsertionIndexForAppending() { |
64 return (insertion_policy_ == TabStripModel::INSERT_AFTER) ? | 66 return (insertion_policy_ == TabStripModel::INSERT_AFTER) ? |
65 tabstrip_->count() : 0; | 67 tabstrip_->count() : 0; |
66 } | 68 } |
67 | 69 |
68 int TabStripModelOrderController::DetermineNewSelectedIndex( | 70 int TabStripModelOrderController::DetermineNewSelectedIndex( |
69 int removing_index) const { | 71 int removing_index) const { |
70 int tab_count = tabstrip_->count(); | 72 int tab_count = tabstrip_->count(); |
71 DCHECK(removing_index >= 0 && removing_index < tab_count); | 73 DCHECK(removing_index >= 0 && removing_index < tab_count); |
72 content::NavigationController* parent_opener = | 74 NavigationController* parent_opener = |
73 tabstrip_->GetOpenerOfTabContentsAt(removing_index); | 75 tabstrip_->GetOpenerOfTabContentsAt(removing_index); |
74 // First see if the index being removed has any "child" tabs. If it does, we | 76 // First see if the index being removed has any "child" tabs. If it does, we |
75 // want to select the first in that child group, not the next tab in the same | 77 // want to select the first in that child group, not the next tab in the same |
76 // group of the removed tab. | 78 // group of the removed tab. |
77 content::NavigationController* removed_controller = | 79 NavigationController* removed_controller = |
78 &tabstrip_->GetTabContentsAt(removing_index)-> | 80 &tabstrip_->GetTabContentsAt(removing_index)-> |
79 web_contents()->GetController(); | 81 web_contents()->GetController(); |
80 // The parent opener should never be the same as the controller being removed. | 82 // The parent opener should never be the same as the controller being removed. |
81 DCHECK(parent_opener != removed_controller); | 83 DCHECK(parent_opener != removed_controller); |
82 int index = tabstrip_->GetIndexOfNextTabContentsOpenedBy(removed_controller, | 84 int index = tabstrip_->GetIndexOfNextTabContentsOpenedBy(removed_controller, |
83 removing_index, | 85 removing_index, |
84 false); | 86 false); |
85 if (index != TabStripModel::kNoTab) | 87 if (index != TabStripModel::kNoTab) |
86 return GetValidIndex(index, removing_index); | 88 return GetValidIndex(index, removing_index); |
87 | 89 |
(...skipping 19 matching lines...) Expand all Loading... |
107 return selected_index - 1; | 109 return selected_index - 1; |
108 | 110 |
109 return selected_index; | 111 return selected_index; |
110 } | 112 } |
111 | 113 |
112 void TabStripModelOrderController::ActiveTabChanged( | 114 void TabStripModelOrderController::ActiveTabChanged( |
113 TabContentsWrapper* old_contents, | 115 TabContentsWrapper* old_contents, |
114 TabContentsWrapper* new_contents, | 116 TabContentsWrapper* new_contents, |
115 int index, | 117 int index, |
116 bool user_gesture) { | 118 bool user_gesture) { |
117 content::NavigationController* old_opener = NULL; | 119 NavigationController* old_opener = NULL; |
118 if (old_contents) { | 120 if (old_contents) { |
119 int index = tabstrip_->GetIndexOfTabContents(old_contents); | 121 int index = tabstrip_->GetIndexOfTabContents(old_contents); |
120 if (index != TabStripModel::kNoTab) { | 122 if (index != TabStripModel::kNoTab) { |
121 old_opener = tabstrip_->GetOpenerOfTabContentsAt(index); | 123 old_opener = tabstrip_->GetOpenerOfTabContentsAt(index); |
122 | 124 |
123 // Forget any group/opener relationships that need to be reset whenever | 125 // Forget any group/opener relationships that need to be reset whenever |
124 // selection changes (see comment in TabStripModel::AddTabContentsAt). | 126 // selection changes (see comment in TabStripModel::AddTabContentsAt). |
125 if (tabstrip_->ShouldResetGroupOnSelect(old_contents)) | 127 if (tabstrip_->ShouldResetGroupOnSelect(old_contents)) |
126 tabstrip_->ForgetGroup(old_contents); | 128 tabstrip_->ForgetGroup(old_contents); |
127 } | 129 } |
128 } | 130 } |
129 content::NavigationController* new_opener = | 131 NavigationController* new_opener = tabstrip_->GetOpenerOfTabContentsAt(index); |
130 tabstrip_->GetOpenerOfTabContentsAt(index); | |
131 | 132 |
132 if (user_gesture && new_opener != old_opener && | 133 if (user_gesture && new_opener != old_opener && |
133 ((old_contents == NULL && new_opener == NULL) || | 134 ((old_contents == NULL && new_opener == NULL) || |
134 new_opener != &old_contents->tab_contents()->GetController()) && | 135 new_opener != &old_contents->tab_contents()->GetController()) && |
135 ((new_contents == NULL && old_opener == NULL) || | 136 ((new_contents == NULL && old_opener == NULL) || |
136 old_opener != &new_contents->tab_contents()->GetController())) { | 137 old_opener != &new_contents->tab_contents()->GetController())) { |
137 tabstrip_->ForgetAllOpeners(); | 138 tabstrip_->ForgetAllOpeners(); |
138 } | 139 } |
139 } | 140 } |
140 | 141 |
141 /////////////////////////////////////////////////////////////////////////////// | 142 /////////////////////////////////////////////////////////////////////////////// |
142 // TabStripModelOrderController, private: | 143 // TabStripModelOrderController, private: |
143 | 144 |
144 int TabStripModelOrderController::GetValidIndex( | 145 int TabStripModelOrderController::GetValidIndex( |
145 int index, int removing_index) const { | 146 int index, int removing_index) const { |
146 if (removing_index < index) | 147 if (removing_index < index) |
147 index = std::max(0, index - 1); | 148 index = std::max(0, index - 1); |
148 return index; | 149 return index; |
149 } | 150 } |
OLD | NEW |