summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sidebar/sidebar_manager.h
blob: a7fe8b17a96ee2b2c67fac950d6ce8b5ec8dcd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_
#define CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_

#include <map>
#include <string>

#include "base/ref_counted.h"
#include "base/string16.h"
#include "chrome/browser/sidebar/sidebar_container.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"

class GURL;
class PrefService;
class Profile;
class SidebarContainer;
class SkBitmap;
class TabContents;

///////////////////////////////////////////////////////////////////////////////
// SidebarManager
//
//  This class is a singleton that manages SidebarContainer instances and
//  maintains a connection between tabs and sidebars.
//
class SidebarManager : public NotificationObserver,
                       public base::RefCounted<SidebarManager>,
                       private SidebarContainer::Delegate {
 public:
  // Returns s singleton instance.
  static SidebarManager* GetInstance();

  // Returns true if sidebar is allowed to be displayed in the browser.
  static bool IsSidebarAllowed();

  SidebarManager();

  // Returns SidebarContainer registered for |tab| and active or NULL if
  // there is no alive and active SidebarContainer registered for |tab|.
  SidebarContainer* GetActiveSidebarContainerFor(TabContents* tab);

  // Returns SidebarContainer registered for |tab| and |content_id| or NULL if
  // there is no such SidebarContainer registered.
  SidebarContainer* GetSidebarContainerFor(TabContents* tab,
                                           const std::string& content_id);

  // Returns sidebar's TabContents registered for |tab| and |content_id|.
  TabContents* GetSidebarTabContents(TabContents* tab,
                                     const std::string& content_id);

  // Sends sidebar state change notification to extensions.
  void NotifyStateChanges(TabContents* was_active_sidebar_contents,
                          TabContents* active_sidebar_contents);

  // Functions supporting chrome.experimental.sidebar API.

  // Shows sidebar identified by |tab| and |content_id| (only sidebar's
  // mini tab is visible).
  void ShowSidebar(TabContents* tab, const std::string& content_id);

  // Expands sidebar identified by |tab| and |content_id|.
  void ExpandSidebar(TabContents* tab, const std::string& content_id);

  // Collapses sidebar identified by |tab| and |content_id| (has no effect
  // if sidebar is not expanded).
  void CollapseSidebar(TabContents* tab, const std::string& content_id);

  // Hides sidebar identified by |tab| and |content_id| (removes sidebar's
  // mini tab).
  void HideSidebar(TabContents* tab, const std::string& content_id);

  // Navigates sidebar identified by |tab| and |content_id| to |url|.
  void NavigateSidebar(TabContents* tab, const std::string& content_id,
                       const GURL& url);

  // Changes sidebar's badge text (displayed on the mini tab).
  void SetSidebarBadgeText(TabContents* tab, const std::string& content_id,
                           const string16& badge_text);

  // Changes sidebar's icon (displayed on the mini tab).
  void SetSidebarIcon(TabContents* tab, const std::string& content_id,
                      const SkBitmap& bitmap);

  // Changes sidebar's title (mini tab's tooltip).
  void SetSidebarTitle(TabContents* tab, const std::string& content_id,
                       const string16& title);

 private:
  friend class base::RefCounted<SidebarManager>;

  virtual ~SidebarManager();

  // Overridden from NotificationObserver.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  // Overridden from SidebarContainer::Delegate.
  virtual void UpdateSidebar(SidebarContainer* host);

  // Hides all sidebars registered for |tab|.
  void HideAllSidebars(TabContents* tab);

  // Returns SidebarContainer corresponding to |sidebar_contents|.
  SidebarContainer* FindSidebarContainerFor(TabContents* sidebar_contents);

  // Registers new SidebarContainer for |tab|. There must be no
  // other SidebarContainers registered for the RenderViewHost at the moment.
  void RegisterSidebarContainerFor(TabContents* tab,
                                   SidebarContainer* container);

  // Unregisters SidebarContainer identified by |tab| and |content_id|.
  void UnregisterSidebarContainerFor(TabContents* tab,
                                     const std::string& content_id);

  // Records the link between |tab| and |sidebar_host|.
  void BindSidebarHost(TabContents* tab, SidebarContainer* sidebar_host);

  // Forgets the link between |tab| and |sidebar_host|.
  void UnbindSidebarHost(TabContents* tab, SidebarContainer* sidebar_host);

  NotificationRegistrar registrar_;

  // This map stores sidebars linked to a particular tab. Sidebars are
  // identified by their unique content id (string).
  typedef std::map<std::string, SidebarContainer*>
      ContentIdToSidebarHostMap;
  // These two maps are for tracking dependencies between tabs and
  // their SidebarContainers.
  //
  // SidebarManager start listening to SidebarContainers when they are put
  // into these maps and removes them when they are closing.
  typedef struct {
    // Sidebars linked to this tab.
    ContentIdToSidebarHostMap content_id_to_sidebar_host;
    // Content id of the currently active (expanded and visible) sidebar.
    std::string active_content_id;
  } SidebarStateForTab;
  typedef std::map<TabContents*, SidebarStateForTab>
      TabToSidebarHostMap;
  TabToSidebarHostMap tab_to_sidebar_host_;

  typedef std::map<SidebarContainer*, TabContents*>
      SidebarHostToTabMap;
  SidebarHostToTabMap sidebar_host_to_tab_;

  DISALLOW_COPY_AND_ASSIGN(SidebarManager);
};

#endif  // CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_