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 #ifndef CHROME_BROWSER_UI_VIEWS_TABS_BROWSER_TAB_STRIP_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_VIEWS_TABS_BROWSER_TAB_STRIP_CONTROLLER_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/prefs/pref_change_registrar.h" 11 #include "chrome/browser/ui/tabs/hover_tab_selector.h" 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" 13 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h" 14 #include "chrome/browser/ui/views/tabs/tab_strip_controller.h" 15 16 class Browser; 17 class Tab; 18 class TabStrip; 19 struct TabRendererData; 20 21 namespace content { 22 class WebContents; 23 } 24 25 namespace ui { 26 class ListSelectionModel; 27 } 28 29 // An implementation of TabStripController that sources data from the 30 // WebContentses in a TabStripModel. 31 class BrowserTabStripController : public TabStripController, 32 public TabStripModelObserver { 33 public: 34 BrowserTabStripController(Browser* browser, TabStripModel* model); 35 virtual ~BrowserTabStripController(); 36 37 void InitFromModel(TabStrip* tabstrip); 38 model()39 TabStripModel* model() const { return model_; } 40 41 bool IsCommandEnabledForTab(TabStripModel::ContextMenuCommand command_id, 42 Tab* tab) const; 43 void ExecuteCommandForTab(TabStripModel::ContextMenuCommand command_id, 44 Tab* tab); 45 bool IsTabPinned(Tab* tab) const; 46 47 // TabStripController implementation: 48 virtual const ui::ListSelectionModel& GetSelectionModel() OVERRIDE; 49 virtual int GetCount() const OVERRIDE; 50 virtual bool IsValidIndex(int model_index) const OVERRIDE; 51 virtual bool IsActiveTab(int model_index) const OVERRIDE; 52 virtual int GetActiveIndex() const OVERRIDE; 53 virtual bool IsTabSelected(int model_index) const OVERRIDE; 54 virtual bool IsTabPinned(int model_index) const OVERRIDE; 55 virtual bool IsNewTabPage(int model_index) const OVERRIDE; 56 virtual void SelectTab(int model_index) OVERRIDE; 57 virtual void ExtendSelectionTo(int model_index) OVERRIDE; 58 virtual void ToggleSelected(int model_index) OVERRIDE; 59 virtual void AddSelectionFromAnchorTo(int model_index) OVERRIDE; 60 virtual void CloseTab(int model_index, CloseTabSource source) OVERRIDE; 61 virtual void ToggleTabAudioMute(int model_index) OVERRIDE; 62 virtual void ShowContextMenuForTab(Tab* tab, 63 const gfx::Point& p, 64 ui::MenuSourceType source_type) OVERRIDE; 65 virtual void UpdateLoadingAnimations() OVERRIDE; 66 virtual int HasAvailableDragActions() const OVERRIDE; 67 virtual void OnDropIndexUpdate(int index, bool drop_before) OVERRIDE; 68 virtual void PerformDrop(bool drop_before, 69 int index, 70 const GURL& url) OVERRIDE; 71 virtual bool IsCompatibleWith(TabStrip* other) const OVERRIDE; 72 virtual void CreateNewTab() OVERRIDE; 73 virtual void CreateNewTabWithLocation(const base::string16& loc) OVERRIDE; 74 virtual bool IsIncognito() OVERRIDE; 75 virtual void StackedLayoutMaybeChanged() OVERRIDE; 76 virtual void OnStartedDraggingTabs() OVERRIDE; 77 virtual void OnStoppedDraggingTabs() OVERRIDE; 78 virtual void CheckFileSupported(const GURL& url) OVERRIDE; 79 80 // TabStripModelObserver implementation: 81 virtual void TabInsertedAt(content::WebContents* contents, 82 int model_index, 83 bool is_active) OVERRIDE; 84 virtual void TabDetachedAt(content::WebContents* contents, 85 int model_index) OVERRIDE; 86 virtual void TabSelectionChanged( 87 TabStripModel* tab_strip_model, 88 const ui::ListSelectionModel& old_model) OVERRIDE; 89 virtual void TabMoved(content::WebContents* contents, 90 int from_model_index, 91 int to_model_index) OVERRIDE; 92 virtual void TabChangedAt(content::WebContents* contents, 93 int model_index, 94 TabChangeType change_type) OVERRIDE; 95 virtual void TabReplacedAt(TabStripModel* tab_strip_model, 96 content::WebContents* old_contents, 97 content::WebContents* new_contents, 98 int model_index) OVERRIDE; 99 virtual void TabPinnedStateChanged(content::WebContents* contents, 100 int model_index) OVERRIDE; 101 virtual void TabMiniStateChanged(content::WebContents* contents, 102 int model_index) OVERRIDE; 103 virtual void TabBlockedStateChanged(content::WebContents* contents, 104 int model_index) OVERRIDE; 105 106 protected: 107 // The context in which SetTabRendererDataFromModel is being called. 108 enum TabStatus { 109 NEW_TAB, 110 EXISTING_TAB 111 }; 112 113 // Sets the TabRendererData from the TabStripModel. 114 virtual void SetTabRendererDataFromModel(content::WebContents* contents, 115 int model_index, 116 TabRendererData* data, 117 TabStatus tab_status); 118 profile()119 Profile* profile() const { return model_->profile(); } 120 tabstrip()121 const TabStrip* tabstrip() const { return tabstrip_; } 122 browser()123 const Browser* browser() const { return browser_; } 124 125 private: 126 class TabContextMenuContents; 127 128 // Invokes tabstrip_->SetTabData. 129 void SetTabDataAt(content::WebContents* web_contents, int model_index); 130 131 void StartHighlightTabsForCommand( 132 TabStripModel::ContextMenuCommand command_id, 133 Tab* tab); 134 void StopHighlightTabsForCommand( 135 TabStripModel::ContextMenuCommand command_id, 136 Tab* tab); 137 138 // Adds a tab. 139 void AddTab(content::WebContents* contents, int index, bool is_active); 140 141 // Resets the tabstrips stacked layout (true or false) from prefs. 142 void UpdateStackedLayout(); 143 144 // Notifies the tabstrip whether |url| is supported once a MIME type request 145 // has completed. 146 void OnFindURLMimeTypeCompleted(const GURL& url, 147 const std::string& mime_type); 148 149 TabStripModel* model_; 150 151 TabStrip* tabstrip_; 152 153 // Non-owning pointer to the browser which is using this controller. 154 Browser* browser_; 155 156 // If non-NULL it means we're showing a menu for the tab. 157 scoped_ptr<TabContextMenuContents> context_menu_contents_; 158 159 // Helper for performing tab selection as a result of dragging over a tab. 160 HoverTabSelector hover_tab_selector_; 161 162 // Forces the tabs to use the regular (non-immersive) style and the 163 // top-of-window views to be revealed when the user is dragging |tabstrip|'s 164 // tabs. 165 scoped_ptr<ImmersiveRevealedLock> immersive_reveal_lock_; 166 167 PrefChangeRegistrar local_pref_registrar_; 168 169 base::WeakPtrFactory<BrowserTabStripController> weak_ptr_factory_; 170 171 DISALLOW_COPY_AND_ASSIGN(BrowserTabStripController); 172 }; 173 174 #endif // CHROME_BROWSER_UI_VIEWS_TABS_BROWSER_TAB_STRIP_CONTROLLER_H_ 175