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
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#pragma once
8
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/tabs/tab_strip_model.h"
11#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
12#include "content/common/notification_observer.h"
13#include "content/common/notification_registrar.h"
14
15class BaseTab;
16class BaseTabStrip;
17class Browser;
18
19struct TabRendererData;
20
21// An implementation of TabStripController that sources data from the
22// TabContentsWrappers in a TabStripModel.
23class BrowserTabStripController : public TabStripController,
24                                  public TabStripModelObserver,
25                                  public NotificationObserver {
26 public:
27  BrowserTabStripController(Browser* browser, TabStripModel* model);
28  virtual ~BrowserTabStripController();
29
30  void InitFromModel(BaseTabStrip* tabstrip);
31
32  TabStripModel* model() const { return model_; }
33
34  bool IsCommandEnabledForTab(TabStripModel::ContextMenuCommand command_id,
35                              BaseTab* tab) const;
36  bool IsCommandCheckedForTab(TabStripModel::ContextMenuCommand command_id,
37                              BaseTab* tab) const;
38  void ExecuteCommandForTab(TabStripModel::ContextMenuCommand command_id,
39                            BaseTab* tab);
40  bool IsTabPinned(BaseTab* tab) const;
41
42  // TabStripController implementation:
43  virtual int GetCount() const OVERRIDE;
44  virtual bool IsValidIndex(int model_index) const OVERRIDE;
45  virtual bool IsActiveTab(int model_index) const;
46  virtual bool IsTabSelected(int model_index) const OVERRIDE;
47  virtual bool IsTabPinned(int model_index) const OVERRIDE;
48  virtual bool IsTabCloseable(int model_index) const OVERRIDE;
49  virtual bool IsNewTabPage(int model_index) const OVERRIDE;
50  virtual void SelectTab(int model_index) OVERRIDE;
51  virtual void ExtendSelectionTo(int model_index) OVERRIDE;
52  virtual void ToggleSelected(int model_index) OVERRIDE;
53  virtual void AddSelectionFromAnchorTo(int model_index) OVERRIDE;
54  virtual void CloseTab(int model_index) OVERRIDE;
55  virtual void ShowContextMenuForTab(BaseTab* tab,
56                                     const gfx::Point& p) OVERRIDE;
57  virtual void UpdateLoadingAnimations() OVERRIDE;
58  virtual int HasAvailableDragActions() const OVERRIDE;
59  virtual void PerformDrop(bool drop_before,
60                           int index,
61                           const GURL& url) OVERRIDE;
62  virtual bool IsCompatibleWith(BaseTabStrip* other) const OVERRIDE;
63  virtual void CreateNewTab() OVERRIDE;
64
65  // TabStripModelObserver implementation:
66  virtual void TabInsertedAt(TabContentsWrapper* contents,
67                             int model_index,
68                             bool active) OVERRIDE;
69  virtual void TabDetachedAt(TabContentsWrapper* contents,
70                             int model_index) OVERRIDE;
71  virtual void TabSelectedAt(TabContentsWrapper* old_contents,
72                             TabContentsWrapper* contents,
73                             int model_index,
74                             bool user_gesture) OVERRIDE;
75  virtual void TabMoved(TabContentsWrapper* contents,
76                        int from_model_index,
77                        int to_model_index) OVERRIDE;
78  virtual void TabChangedAt(TabContentsWrapper* contents,
79                            int model_index,
80                            TabChangeType change_type) OVERRIDE;
81  virtual void TabReplacedAt(TabStripModel* tab_strip_model,
82                             TabContentsWrapper* old_contents,
83                             TabContentsWrapper* new_contents,
84                             int model_index) OVERRIDE;
85  virtual void TabPinnedStateChanged(TabContentsWrapper* contents,
86                                     int model_index) OVERRIDE;
87  virtual void TabMiniStateChanged(TabContentsWrapper* contents,
88                                   int model_index) OVERRIDE;
89  virtual void TabBlockedStateChanged(TabContentsWrapper* contents,
90                                      int model_index) OVERRIDE;
91
92  // NotificationObserver implementation:
93  virtual void Observe(NotificationType type,
94                       const NotificationSource& source,
95                       const NotificationDetails& details) OVERRIDE;
96
97 private:
98  class TabContextMenuContents;
99
100  // Invokes tabstrip_->SetTabData.
101  void SetTabDataAt(TabContentsWrapper* contents, int model_index);
102
103  // Sets the TabRendererData from the TabStripModel.
104  void SetTabRendererDataFromModel(TabContents* contents,
105                                   int model_index,
106                                   TabRendererData* data);
107
108  void StartHighlightTabsForCommand(
109      TabStripModel::ContextMenuCommand command_id,
110      BaseTab* tab);
111  void StopHighlightTabsForCommand(
112      TabStripModel::ContextMenuCommand command_id,
113      BaseTab* tab);
114
115  Profile* profile() const { return model_->profile(); }
116
117  TabStripModel* model_;
118
119  BaseTabStrip* tabstrip_;
120
121  // Non-owning pointer to the browser which is using this controller.
122  Browser* browser_;
123
124  // If non-NULL it means we're showing a menu for the tab.
125  scoped_ptr<TabContextMenuContents> context_menu_contents_;
126
127  NotificationRegistrar notification_registrar_;
128
129  DISALLOW_COPY_AND_ASSIGN(BrowserTabStripController);
130};
131
132#endif  // CHROME_BROWSER_UI_VIEWS_TABS_BROWSER_TAB_STRIP_CONTROLLER_H_
133
134