sidebar_container.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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_SIDEBAR_SIDEBAR_CONTAINER_H_
6#define CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/scoped_ptr.h"
12#include "base/string16.h"
13#include "chrome/browser/extensions/image_loading_tracker.h"
14#include "chrome/browser/tab_contents/tab_contents_delegate.h"
15
16class BrowserWindow;
17class Profile;
18class RenderViewHost;
19class SkBitmap;
20class TabContents;
21
22///////////////////////////////////////////////////////////////////////////////
23// SidebarContainer
24//
25//  Stores one particular sidebar state: sidebar's content, its content id,
26//  tab it is linked to, mini tab icon, title etc.
27//
28class SidebarContainer
29    : public TabContentsDelegate,
30      private ImageLoadingTracker::Observer {
31 public:
32  // Interface to implement to listen for sidebar update notification.
33  class Delegate {
34   public:
35    Delegate() {}
36    virtual ~Delegate() {}
37    virtual void UpdateSidebar(SidebarContainer* host) = 0;
38   private:
39    DISALLOW_COPY_AND_ASSIGN(Delegate);
40  };
41
42  SidebarContainer(TabContents* tab,
43                   const std::string& content_id,
44                   Delegate* delegate);
45  virtual ~SidebarContainer();
46
47  // Called right before destroying this sidebar.
48  // Does all the necessary cleanup.
49  void SidebarClosing();
50
51  // Sets default sidebar parameters, as specified in extension manifest.
52  void LoadDefaults();
53
54  // Returns sidebar's content id.
55  const std::string& content_id() const { return content_id_; }
56
57  // Returns TabContents sidebar is linked to.
58  TabContents* tab_contents() const { return tab_; }
59
60  // Returns sidebar's TabContents.
61  TabContents* sidebar_contents() const { return sidebar_contents_.get(); }
62
63  // Accessor for the badge text.
64  const string16& badge_text() const { return badge_text_; }
65
66  // Accessor for the icon.
67  const SkBitmap& icon() const { return *icon_; }
68
69  // Accessor for the title.
70  const string16& title() const { return title_; }
71
72  // Functions supporting chrome.experimental.sidebar API.
73
74  // Notifies hosting window that this sidebar was expanded.
75  void Show();
76
77  // Notifies hosting window that this sidebar was expanded.
78  void Expand();
79
80  // Notifies hosting window that this sidebar was collapsed.
81  void Collapse();
82
83  // Navigates sidebar contents to the |url|.
84  void Navigate(const GURL& url);
85
86  // Changes sidebar's badge text.
87  void SetBadgeText(const string16& badge_text);
88
89  // Changes sidebar's icon.
90  void SetIcon(const SkBitmap& bitmap);
91
92  // Changes sidebar's title.
93  void SetTitle(const string16& title);
94
95 private:
96  // Overridden from TabContentsDelegate.
97  virtual void OpenURLFromTab(TabContents* source,
98                              const GURL& url,
99                              const GURL& referrer,
100                              WindowOpenDisposition disposition,
101                              PageTransition::Type transition) {}
102  virtual void NavigationStateChanged(const TabContents* source,
103                                      unsigned changed_flags) {}
104  virtual void AddNewContents(TabContents* source,
105                              TabContents* new_contents,
106                              WindowOpenDisposition disposition,
107                              const gfx::Rect& initial_pos,
108                              bool user_gesture) {}
109  virtual void ActivateContents(TabContents* contents) {}
110  virtual void DeactivateContents(TabContents* contents) {}
111  virtual void LoadingStateChanged(TabContents* source) {}
112  virtual void CloseContents(TabContents* source) {}
113  virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {}
114  virtual bool IsPopup(const TabContents* source) const;
115  virtual void UpdateTargetURL(TabContents* source, const GURL& url) {}
116  virtual void ToolbarSizeChanged(TabContents* source, bool is_animating) {}
117
118  // Overridden from ImageLoadingTracker::Observer.
119  virtual void OnImageLoaded(SkBitmap* image,
120                             ExtensionResource resource,
121                             int index);
122
123  // Returns an extension this sidebar belongs to.
124  const Extension* GetExtension() const;
125
126  // Contents of the tab this sidebar is linked to.
127  TabContents* tab_;
128
129  // Sidebar's content id. There might be more than one sidebar liked to each
130  // particular tab and they are identified by their unique content id.
131  const std::string content_id_;
132
133  // Sidebar update notification listener.
134  Delegate* delegate_;
135
136  // Sidebar contents.
137  scoped_ptr<TabContents> sidebar_contents_;
138
139  // Badge text displayed on the sidebar's mini tab.
140  string16 badge_text_;
141
142  // Icon displayed on the sidebar's mini tab.
143  scoped_ptr<SkBitmap> icon_;
144
145  // Sidebar's title, displayed as a tooltip for sidebar's mini tab.
146  string16 title_;
147
148  // On the first expand sidebar will be automatically navigated to the default
149  // page (specified in the extension manifest), but only if the extension has
150  // not explicitly navigated it yet. This variable is set to false on the first
151  // sidebar navigation.
152  bool navigate_to_default_page_on_expand_;
153  // Since the default icon (specified in the extension manifest) is loaded
154  // asynchronously, sidebar icon can already be set by the extension
155  // by the time it's loaded. This variable tracks whether the loaded default
156  // icon should be used or discarded.
157  bool use_default_icon_;
158
159  // Helper to load icons from extension asynchronously.
160  scoped_ptr<ImageLoadingTracker> image_loading_tracker_;
161
162  DISALLOW_COPY_AND_ASSIGN(SidebarContainer);
163};
164
165#endif  // CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_
166