sidebar_manager.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// Copyright (c) 2010 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#include "chrome/browser/sidebar/sidebar_manager.h" 6bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 7bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include <vector> 8bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 9bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "base/command_line.h" 10bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/browser/browser_process.h" 11bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/browser/extensions/extension_sidebar_api.h" 12bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/browser/prefs/pref_service.h" 1321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "chrome/browser/profiles/profile.h" 14bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/browser/tab_contents/tab_contents.h" 15bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/browser/sidebar/sidebar_container.h" 16bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/common/chrome_switches.h" 17bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/common/notification_service.h" 18bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "chrome/common/pref_names.h" 19bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen#include "googleurl/src/gurl.h" 20bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 21513209b27ff55e2841eac0e4120199c23acce758Ben Murdochstruct SidebarManager::SidebarStateForTab { 22513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch // Sidebars linked to this tab. 23513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch ContentIdToSidebarHostMap content_id_to_sidebar_host; 24513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch // Content id of the currently active (expanded and visible) sidebar. 25513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch std::string active_content_id; 26513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch}; 27513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 28bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// static 29bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarManager* SidebarManager::GetInstance() { 30bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return g_browser_process->sidebar_manager(); 31bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 32bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 33bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen// static 34bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenbool SidebarManager::IsSidebarAllowed() { 35bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return CommandLine::ForCurrentProcess()->HasSwitch( 36bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen switches::kEnableExperimentalExtensionApis); 37bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 38bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 39bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarManager::SidebarManager() { 40bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 41bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 42bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarContainer* SidebarManager::GetActiveSidebarContainerFor( 43bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab) { 44bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); 45bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it == tab_to_sidebar_host_.end()) 46bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 47bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it->second.active_content_id.empty()) 48bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 49bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ContentIdToSidebarHostMap::iterator host_it = 50bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it->second.content_id_to_sidebar_host.find(it->second.active_content_id); 51bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(host_it != it->second.content_id_to_sidebar_host.end()); 52bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return host_it->second; 53bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 54bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 55bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarContainer* SidebarManager::GetSidebarContainerFor( 56bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id) { 57bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 58bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); 59bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it == tab_to_sidebar_host_.end()) 60bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 61bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ContentIdToSidebarHostMap::iterator host_it = 62bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it->second.content_id_to_sidebar_host.find(content_id); 63bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (host_it == it->second.content_id_to_sidebar_host.end()) 64bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 65bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return host_it->second; 66bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 67bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 68bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenTabContents* SidebarManager::GetSidebarTabContents( 69bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id) { 70bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 71bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* sidebar_host = GetSidebarContainerFor(tab, content_id); 72bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!sidebar_host) 73bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 74bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return sidebar_host->sidebar_contents(); 75bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 76bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 77bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::NotifyStateChanges( 78bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* was_active_sidebar_contents, 79bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* active_sidebar_contents) { 80bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (was_active_sidebar_contents == active_sidebar_contents) 81bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 82bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 83bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* was_active_host = 84bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen was_active_sidebar_contents == NULL ? NULL : 85bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen FindSidebarContainerFor(was_active_sidebar_contents); 86bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* active_host = 87bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen active_sidebar_contents == NULL ? NULL : 88bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen FindSidebarContainerFor(active_sidebar_contents); 89bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 90bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (was_active_host != NULL) { 91bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ExtensionSidebarEventRouter::OnStateChanged( 92bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen was_active_sidebar_contents->profile(), 93bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen was_active_host->tab_contents(), was_active_host->content_id(), 94bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen extension_sidebar_constants::kShownState); 95bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 96bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 97bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (active_host != NULL) { 98bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ExtensionSidebarEventRouter::OnStateChanged( 99bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen active_sidebar_contents->profile(), 100bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen active_host->tab_contents(), active_host->content_id(), 101bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen extension_sidebar_constants::kActiveState); 102bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 103bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 104bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 105bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::ShowSidebar(TabContents* tab, 106bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id) { 107bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 108bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 109bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) { 110bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host = new SidebarContainer(tab, content_id, this); 111bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen RegisterSidebarContainerFor(tab, host); 112bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 113bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 114bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->Show(); 115bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 116bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ExtensionSidebarEventRouter::OnStateChanged( 117bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab->profile(), tab, content_id, 118bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen extension_sidebar_constants::kShownState); 119bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 120bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 121bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::ExpandSidebar(TabContents* tab, 122bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id) { 123bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 124bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); 125bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it == tab_to_sidebar_host_.end()) 126bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 127bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // If it's already active, bail out. 128bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it->second.active_content_id == content_id) 129bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 130bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 131bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 132bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(host); 133bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 134bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 135bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it->second.active_content_id = content_id; 136bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 137bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->Expand(); 138bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 139bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 140bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::CollapseSidebar(TabContents* tab, 141bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id) { 142bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 143bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); 144bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it == tab_to_sidebar_host_.end()) 145bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 146bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // If it's not the one active now, bail out. 147bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it->second.active_content_id != content_id) 148bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 149bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 150bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 151bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(host); 152bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 153bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 154bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it->second.active_content_id.clear(); 155bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 156bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->Collapse(); 157bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 158bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 159bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::HideSidebar(TabContents* tab, 160bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id) { 161bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 162bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); 163bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it == tab_to_sidebar_host_.end()) 164bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 165bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (it->second.active_content_id == content_id) 166bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it->second.active_content_id.clear(); 167bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 168bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 169bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(host); 170bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 171bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen UnregisterSidebarContainerFor(tab, content_id); 172bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 173bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ExtensionSidebarEventRouter::OnStateChanged( 174bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab->profile(), tab, content_id, 175bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen extension_sidebar_constants::kHiddenState); 176bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 177bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 178bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::NavigateSidebar(TabContents* tab, 179bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id, 180bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const GURL& url) { 181bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!content_id.empty()); 182bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 183bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 184bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 185bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 186bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->Navigate(url); 187bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 188bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 189bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::SetSidebarBadgeText( 190bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id, 191bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const string16& badge_text) { 192bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 193bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 194bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 195bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->SetBadgeText(badge_text); 196bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 197bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 198bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::SetSidebarIcon( 199bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id, 200bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const SkBitmap& bitmap) { 201bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 202bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 203bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 204bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->SetIcon(bitmap); 205bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 206bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 207bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::SetSidebarTitle( 208bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id, 209bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const string16& title) { 210bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 211bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 212bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 213bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->SetTitle(title); 214bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 215bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 216bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarManager::~SidebarManager() { 217bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(tab_to_sidebar_host_.empty()); 218bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(sidebar_host_to_tab_.empty()); 219bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 220bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 221bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::Observe(NotificationType type, 222bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const NotificationSource& source, 223bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const NotificationDetails& details) { 224bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (type == NotificationType::TAB_CONTENTS_DESTROYED) { 225bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen HideAllSidebars(Source<TabContents>(source).ptr()); 226bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } else { 227bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen NOTREACHED() << "Got a notification we didn't register for!"; 228bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 229bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 230bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 231bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::UpdateSidebar(SidebarContainer* host) { 232bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen NotificationService::current()->Notify( 233bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen NotificationType::SIDEBAR_CHANGED, 234bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Source<SidebarManager>(this), 235bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Details<SidebarContainer>(host)); 236bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 237bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 238bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::HideAllSidebars(TabContents* tab) { 239bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabToSidebarHostMap::iterator tab_it = tab_to_sidebar_host_.find(tab); 240bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (tab_it == tab_to_sidebar_host_.end()) 241bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 242bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const ContentIdToSidebarHostMap& hosts = 243bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab_it->second.content_id_to_sidebar_host; 244bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 245bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen std::vector<std::string> content_ids; 246bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen for (ContentIdToSidebarHostMap::const_iterator it = hosts.begin(); 247bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it != hosts.end(); ++it) { 248bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen content_ids.push_back(it->first); 249bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 250bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 251bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen for (std::vector<std::string>::iterator it = content_ids.begin(); 252bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it != content_ids.end(); ++it) { 253bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen HideSidebar(tab, *it); 254bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 255bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 256bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 257bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian MonsenSidebarContainer* SidebarManager::FindSidebarContainerFor( 258bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* sidebar_contents) { 259bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen for (SidebarHostToTabMap::iterator it = sidebar_host_to_tab_.begin(); 260bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen it != sidebar_host_to_tab_.end(); 261bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen ++it) { 262bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (sidebar_contents == it->first->sidebar_contents()) 263bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return it->first; 264bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 265bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return NULL; 266bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 267bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 268bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::RegisterSidebarContainerFor( 269bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, SidebarContainer* sidebar_host) { 270bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(!GetSidebarContainerFor(tab, sidebar_host->content_id())); 271bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 272bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // If it's a first sidebar for this tab, register destroy notification. 273bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { 274bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen registrar_.Add(this, 275bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen NotificationType::TAB_CONTENTS_DESTROYED, 276bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Source<TabContents>(tab)); 277bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 278bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 279bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen BindSidebarHost(tab, sidebar_host); 280bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 281bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 282bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::UnregisterSidebarContainerFor( 283bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen TabContents* tab, const std::string& content_id) { 284bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* host = GetSidebarContainerFor(tab, content_id); 285bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(host); 286bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (!host) 287bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen return; 288bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 289bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen UnbindSidebarHost(tab, host); 290bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 291bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // If there's no more sidebars linked to this tab, unsubscribe. 292bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { 293bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen registrar_.Remove(this, 294bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen NotificationType::TAB_CONTENTS_DESTROYED, 295bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen Source<TabContents>(tab)); 296bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen } 297bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 298bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Issue tab closing event post unbound. 299bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen host->SidebarClosing(); 300bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen // Destroy sidebar container. 301bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen delete host; 302bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 303bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 304bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::BindSidebarHost(TabContents* tab, 305bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* sidebar_host) { 306bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id = sidebar_host->content_id(); 307bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 308bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(GetSidebarContainerFor(tab, content_id) == NULL); 309bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(sidebar_host_to_tab_.find(sidebar_host) == 310bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen sidebar_host_to_tab_.end()); 311bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 312bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab_to_sidebar_host_[tab].content_id_to_sidebar_host[content_id] = 313bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen sidebar_host; 314bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen sidebar_host_to_tab_[sidebar_host] = tab; 315bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 316bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 317bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsenvoid SidebarManager::UnbindSidebarHost(TabContents* tab, 318bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen SidebarContainer* sidebar_host) { 319bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen const std::string& content_id = sidebar_host->content_id(); 320bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 321bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(GetSidebarContainerFor(tab, content_id) == sidebar_host); 322bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(sidebar_host_to_tab_.find(sidebar_host)->second == tab); 323bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen DCHECK(tab_to_sidebar_host_[tab].active_content_id != content_id); 324bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen 325bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab_to_sidebar_host_[tab].content_id_to_sidebar_host.erase(content_id); 326bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen if (tab_to_sidebar_host_[tab].content_id_to_sidebar_host.empty()) 327bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen tab_to_sidebar_host_.erase(tab); 328bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen sidebar_host_to_tab_.erase(sidebar_host); 329bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293Kristian Monsen} 330