sidebar_manager.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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_SIDEBAR_SIDEBAR_MANAGER_H_
6#define CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_
7
8#include <map>
9#include <string>
10
11#include "base/ref_counted.h"
12#include "base/string16.h"
13#include "chrome/browser/sidebar/sidebar_container.h"
14#include "chrome/common/notification_observer.h"
15#include "chrome/common/notification_registrar.h"
16
17class GURL;
18class PrefService;
19class Profile;
20class SidebarContainer;
21class SkBitmap;
22class TabContents;
23
24///////////////////////////////////////////////////////////////////////////////
25// SidebarManager
26//
27//  This class is a singleton that manages SidebarContainer instances and
28//  maintains a connection between tabs and sidebars.
29//
30class SidebarManager : public NotificationObserver,
31                       public base::RefCounted<SidebarManager>,
32                       private SidebarContainer::Delegate {
33 public:
34  // Returns s singleton instance.
35  static SidebarManager* GetInstance();
36
37  // Returns true if sidebar is allowed to be displayed in the browser.
38  static bool IsSidebarAllowed();
39
40  SidebarManager();
41
42  // Returns SidebarContainer registered for |tab| and active or NULL if
43  // there is no alive and active SidebarContainer registered for |tab|.
44  SidebarContainer* GetActiveSidebarContainerFor(TabContents* tab);
45
46  // Returns SidebarContainer registered for |tab| and |content_id| or NULL if
47  // there is no such SidebarContainer registered.
48  SidebarContainer* GetSidebarContainerFor(TabContents* tab,
49                                           const std::string& content_id);
50
51  // Returns sidebar's TabContents registered for |tab| and |content_id|.
52  TabContents* GetSidebarTabContents(TabContents* tab,
53                                     const std::string& content_id);
54
55  // Sends sidebar state change notification to extensions.
56  void NotifyStateChanges(TabContents* was_active_sidebar_contents,
57                          TabContents* active_sidebar_contents);
58
59  // Functions supporting chrome.experimental.sidebar API.
60
61  // Shows sidebar identified by |tab| and |content_id| (only sidebar's
62  // mini tab is visible).
63  void ShowSidebar(TabContents* tab, const std::string& content_id);
64
65  // Expands sidebar identified by |tab| and |content_id|.
66  void ExpandSidebar(TabContents* tab, const std::string& content_id);
67
68  // Collapses sidebar identified by |tab| and |content_id| (has no effect
69  // if sidebar is not expanded).
70  void CollapseSidebar(TabContents* tab, const std::string& content_id);
71
72  // Hides sidebar identified by |tab| and |content_id| (removes sidebar's
73  // mini tab).
74  void HideSidebar(TabContents* tab, const std::string& content_id);
75
76  // Navigates sidebar identified by |tab| and |content_id| to |url|.
77  void NavigateSidebar(TabContents* tab,
78                       const std::string& content_id,
79                       const GURL& url);
80
81  // Changes sidebar's badge text (displayed on the mini tab).
82  void SetSidebarBadgeText(TabContents* tab,
83                           const std::string& content_id,
84                           const string16& badge_text);
85
86  // Changes sidebar's icon (displayed on the mini tab).
87  void SetSidebarIcon(TabContents* tab,
88                      const std::string& content_id,
89                      const SkBitmap& bitmap);
90
91  // Changes sidebar's title (mini tab's tooltip).
92  void SetSidebarTitle(TabContents* tab,
93                       const std::string& content_id,
94                       const string16& title);
95
96 private:
97  friend class base::RefCounted<SidebarManager>;
98
99  virtual ~SidebarManager();
100
101  // Overridden from NotificationObserver.
102  virtual void Observe(NotificationType type,
103                       const NotificationSource& source,
104                       const NotificationDetails& details);
105
106  // Overridden from SidebarContainer::Delegate.
107  virtual void UpdateSidebar(SidebarContainer* host);
108
109  // Hides all sidebars registered for |tab|.
110  void HideAllSidebars(TabContents* tab);
111
112  // Returns SidebarContainer corresponding to |sidebar_contents|.
113  SidebarContainer* FindSidebarContainerFor(TabContents* sidebar_contents);
114
115  // Registers new SidebarContainer for |tab|. There must be no
116  // other SidebarContainers registered for the RenderViewHost at the moment.
117  void RegisterSidebarContainerFor(TabContents* tab,
118                                   SidebarContainer* container);
119
120  // Unregisters SidebarContainer identified by |tab| and |content_id|.
121  void UnregisterSidebarContainerFor(TabContents* tab,
122                                     const std::string& content_id);
123
124  // Records the link between |tab| and |sidebar_host|.
125  void BindSidebarHost(TabContents* tab, SidebarContainer* sidebar_host);
126
127  // Forgets the link between |tab| and |sidebar_host|.
128  void UnbindSidebarHost(TabContents* tab, SidebarContainer* sidebar_host);
129
130  NotificationRegistrar registrar_;
131
132  // This map stores sidebars linked to a particular tab. Sidebars are
133  // identified by their unique content id (string).
134  typedef std::map<std::string, SidebarContainer*> ContentIdToSidebarHostMap;
135
136  // These two maps are for tracking dependencies between tabs and
137  // their SidebarContainers.
138  //
139  // SidebarManager start listening to SidebarContainers when they are put
140  // into these maps and removes them when they are closing.
141  struct SidebarStateForTab;
142  typedef std::map<TabContents*, SidebarStateForTab> TabToSidebarHostMap;
143  TabToSidebarHostMap tab_to_sidebar_host_;
144
145  typedef std::map<SidebarContainer*, TabContents*> SidebarHostToTabMap;
146  SidebarHostToTabMap sidebar_host_to_tab_;
147
148  DISALLOW_COPY_AND_ASSIGN(SidebarManager);
149};
150
151#endif  // CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_
152
153