172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// Use of this source code is governed by a BSD-style license that can be 3bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// found in the LICENSE file. 4bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 5bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#ifndef CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_ 6bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#define CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_ 7bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 8bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include <string> 9bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 10bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "base/basictypes.h" 11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h" 12bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "base/string16.h" 1372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/browser/extensions/image_loading_tracker.h" 14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/tab_contents/tab_contents_delegate.h" 15bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 16bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass BrowserWindow; 17bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass Profile; 18bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass RenderViewHost; 19bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass SkBitmap; 20bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass TabContents; 21bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 22bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen/////////////////////////////////////////////////////////////////////////////// 23bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// SidebarContainer 24bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// 25bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// Stores one particular sidebar state: sidebar's content, its content id, 26bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// tab it is linked to, mini tab icon, title etc. 27bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// 28bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenclass SidebarContainer 2972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen : public TabContentsDelegate, 3072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen private ImageLoadingTracker::Observer { 31bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen public: 32bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Interface to implement to listen for sidebar update notification. 33bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen class Delegate { 34bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen public: 35bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Delegate() {} 36bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual ~Delegate() {} 37bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void UpdateSidebar(SidebarContainer* host) = 0; 38bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen private: 39bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DISALLOW_COPY_AND_ASSIGN(Delegate); 40bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen }; 41bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 4272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen SidebarContainer(TabContents* tab, 4372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const std::string& content_id, 44bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Delegate* delegate); 45bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual ~SidebarContainer(); 46bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 47bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Called right before destroying this sidebar. 48bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Does all the necessary cleanup. 49bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void SidebarClosing(); 50bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 5172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Sets default sidebar parameters, as specified in extension manifest. 5272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void LoadDefaults(); 5372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 54bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Returns sidebar's content id. 55bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id() const { return content_id_; } 56bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 57bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Returns TabContents sidebar is linked to. 58bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab_contents() const { return tab_; } 59bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 60bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Returns sidebar's TabContents. 61bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* sidebar_contents() const { return sidebar_contents_.get(); } 62bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 63bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Accessor for the badge text. 64bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const string16& badge_text() const { return badge_text_; } 65bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 66bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Accessor for the icon. 67bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const SkBitmap& icon() const { return *icon_; } 68bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 69bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Accessor for the title. 70bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const string16& title() const { return title_; } 71bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 72bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Functions supporting chrome.experimental.sidebar API. 73bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 74bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Notifies hosting window that this sidebar was expanded. 75bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void Show(); 76bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 77bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Notifies hosting window that this sidebar was expanded. 78bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void Expand(); 79bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 80bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Notifies hosting window that this sidebar was collapsed. 81bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void Collapse(); 82bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 83bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Navigates sidebar contents to the |url|. 84bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void Navigate(const GURL& url); 85bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 86bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Changes sidebar's badge text. 87bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void SetBadgeText(const string16& badge_text); 88bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 89bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Changes sidebar's icon. 90bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void SetIcon(const SkBitmap& bitmap); 91bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 92bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Changes sidebar's title. 93bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen void SetTitle(const string16& title); 94bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 95bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen private: 96bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Overridden from TabContentsDelegate. 97bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void OpenURLFromTab(TabContents* source, 98bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const GURL& url, 99bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const GURL& referrer, 100bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen WindowOpenDisposition disposition, 101bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen PageTransition::Type transition) {} 102bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void NavigationStateChanged(const TabContents* source, 103bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen unsigned changed_flags) {} 104bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void AddNewContents(TabContents* source, 105bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* new_contents, 106bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen WindowOpenDisposition disposition, 107bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const gfx::Rect& initial_pos, 108bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen bool user_gesture) {} 109bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void ActivateContents(TabContents* contents) {} 110bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void DeactivateContents(TabContents* contents) {} 111bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void LoadingStateChanged(TabContents* source) {} 112bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void CloseContents(TabContents* source) {} 113bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {} 11421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen virtual bool IsPopup(const TabContents* source) const; 115bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen virtual void UpdateTargetURL(TabContents* source, const GURL& url) {} 116bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 11772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Overridden from ImageLoadingTracker::Observer. 11872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual void OnImageLoaded(SkBitmap* image, 119ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const ExtensionResource& resource, 12072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int index); 12172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 12272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Returns an extension this sidebar belongs to. 12372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const Extension* GetExtension() const; 12472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 125bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Contents of the tab this sidebar is linked to. 126bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab_; 127bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 128bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Sidebar's content id. There might be more than one sidebar liked to each 129bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // particular tab and they are identified by their unique content id. 130bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string content_id_; 131bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 132bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Sidebar update notification listener. 133bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Delegate* delegate_; 134bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 135bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Sidebar contents. 136bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen scoped_ptr<TabContents> sidebar_contents_; 137bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 138bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Badge text displayed on the sidebar's mini tab. 139bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen string16 badge_text_; 140bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 141bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Icon displayed on the sidebar's mini tab. 142bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen scoped_ptr<SkBitmap> icon_; 143bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 144bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Sidebar's title, displayed as a tooltip for sidebar's mini tab. 145bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen string16 title_; 146bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 14772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // On the first expand sidebar will be automatically navigated to the default 14872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // page (specified in the extension manifest), but only if the extension has 14972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // not explicitly navigated it yet. This variable is set to false on the first 15072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // sidebar navigation. 15172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool navigate_to_default_page_on_expand_; 15272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Since the default icon (specified in the extension manifest) is loaded 15372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // asynchronously, sidebar icon can already be set by the extension 15472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // by the time it's loaded. This variable tracks whether the loaded default 15572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // icon should be used or discarded. 15672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool use_default_icon_; 15772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 15872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Helper to load icons from extension asynchronously. 15972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen scoped_ptr<ImageLoadingTracker> image_loading_tracker_; 16072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 161bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DISALLOW_COPY_AND_ASSIGN(SidebarContainer); 162bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen}; 163bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 164bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#endif // CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_ 165