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/memory/scoped_ptr.h"
12#include "base/string16.h"
13#include "chrome/browser/extensions/image_loading_tracker.h"
14#include "content/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
117  // Overridden from ImageLoadingTracker::Observer.
118  virtual void OnImageLoaded(SkBitmap* image,
119                             const ExtensionResource& resource,
120                             int index);
121
122  // Returns an extension this sidebar belongs to.
123  const Extension* GetExtension() const;
124
125  // Contents of the tab this sidebar is linked to.
126  TabContents* tab_;
127
128  // Sidebar's content id. There might be more than one sidebar liked to each
129  // particular tab and they are identified by their unique content id.
130  const std::string content_id_;
131
132  // Sidebar update notification listener.
133  Delegate* delegate_;
134
135  // Sidebar contents.
136  scoped_ptr<TabContents> sidebar_contents_;
137
138  // Badge text displayed on the sidebar's mini tab.
139  string16 badge_text_;
140
141  // Icon displayed on the sidebar's mini tab.
142  scoped_ptr<SkBitmap> icon_;
143
144  // Sidebar's title, displayed as a tooltip for sidebar's mini tab.
145  string16 title_;
146
147  // On the first expand sidebar will be automatically navigated to the default
148  // page (specified in the extension manifest), but only if the extension has
149  // not explicitly navigated it yet. This variable is set to false on the first
150  // sidebar navigation.
151  bool navigate_to_default_page_on_expand_;
152  // Since the default icon (specified in the extension manifest) is loaded
153  // asynchronously, sidebar icon can already be set by the extension
154  // by the time it's loaded. This variable tracks whether the loaded default
155  // icon should be used or discarded.
156  bool use_default_icon_;
157
158  // Helper to load icons from extension asynchronously.
159  scoped_ptr<ImageLoadingTracker> image_loading_tracker_;
160
161  DISALLOW_COPY_AND_ASSIGN(SidebarContainer);
162};
163
164#endif  // CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_
165