OLD | NEW |
(Empty) | |
| 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/mru_tab_controller.h" |
| 6 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 7 |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 9 |
| 10 /////////////////////////////////////////////////////////////////////////////// |
| 11 // MRUTabController, public: |
| 12 |
| 13 /////////////////////////////////////////////////////////////////////////////// |
| 14 // MRUTabController, TabStripModelObserver implementation: |
| 15 |
| 16 MRUTabController::MRUTabController(TabStripModel* tab_strip_model) |
| 17 : tab_strip_model_(tab_strip_model), |
| 18 can_update_stack_(true) { |
| 19 tab_strip_model_->AddObserver(this); |
| 20 } |
| 21 |
| 22 MRUTabController::~MRUTabController() { |
| 23 tabs_mru_list_.clear(); |
| 24 if (tab_strip_model_) |
| 25 tab_strip_model_->RemoveObserver(this); |
| 26 } |
| 27 |
| 28 void MRUTabController::TabInsertedAt(TabContents* contents, |
| 29 int index, |
| 30 bool foreground) { |
| 31 if (foreground) |
| 32 tabs_mru_list_.push_front(contents); |
| 33 else |
| 34 tabs_mru_list_.push_back(contents); |
| 35 } |
| 36 |
| 37 void MRUTabController::TabDetachedAt(TabContents* contents, |
| 38 int index) { |
| 39 // Remove the index if found in MRU List |
| 40 std::list<TabContents*>::iterator it = |
| 41 std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), contents); |
| 42 if (it != tabs_mru_list_.end()) { |
| 43 tabs_mru_list_.erase(it); |
| 44 } |
| 45 } |
| 46 |
| 47 void MRUTabController::ActiveTabChanged(TabContents* old_contents, |
| 48 TabContents* new_contents, |
| 49 int index, |
| 50 bool user_gesture) { |
| 51 if (!can_update_stack_) |
| 52 return; |
| 53 |
| 54 // Bring the activated tab content to the front of the list |
| 55 std::list<TabContents*>::iterator it = |
| 56 std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), new_contents); |
| 57 if (it != tabs_mru_list_.end()) { |
| 58 tabs_mru_list_.push_front(*it); |
| 59 tabs_mru_list_.erase(it); |
| 60 } |
| 61 } |
| 62 |
| 63 void MRUTabController::TabReplacedAt(TabStripModel* tab_strip_model, |
| 64 TabContents* old_contents, |
| 65 TabContents* new_contents, |
| 66 int index) { |
| 67 // Replace the tab contents accordingly in the list. If the tab contents are |
| 68 // not found add to the end of the list. |
| 69 std::list<TabContents*>::iterator it = |
| 70 std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), old_contents); |
| 71 if (it != tabs_mru_list_.end() && *it != tabs_mru_list_.front()) { |
| 72 tabs_mru_list_.insert(it, new_contents); |
| 73 tabs_mru_list_.erase(it); |
| 74 } else { |
| 75 NOTREACHED(); |
| 76 } |
| 77 } |
| 78 |
| 79 void MRUTabController::TabStripEmpty() { |
| 80 tabs_mru_list_.clear(); |
| 81 } |
| 82 |
| 83 TabContents* MRUTabController::GetPreviousMRUTab() { |
| 84 // Only if there are more than one open tab we can switch. |
| 85 if (tabs_mru_list_.size() < 2) |
| 86 return NULL; |
| 87 |
| 88 std::list<TabContents*>::iterator it = tabs_mru_list_.begin(); |
| 89 return *(++it); |
| 90 } |
| 91 |
| 92 void MRUTabController::PauseStackUpdates() { |
| 93 can_update_stack_ = false; |
| 94 } |
| 95 |
| 96 void MRUTabController::CommitActiveTabChanges() { |
| 97 if (can_update_stack_) |
| 98 return; |
| 99 |
| 100 can_update_stack_ = true; |
| 101 TabContents* active_tab = tab_strip_model_->GetActiveTabContents(); |
| 102 ActiveTabChanged(active_tab, active_tab, 0, true); |
| 103 } |
OLD | NEW |