diff options
author | rdevlin.cronin@chromium.org <rdevlin.cronin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 19:53:29 +0000 |
---|---|---|
committer | rdevlin.cronin@chromium.org <rdevlin.cronin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-09 19:53:29 +0000 |
commit | 3a80ea33a37398a081455f4ae8b70572c7bc80e8 (patch) | |
tree | c0a76df02501ee14bb3912d87bf88a3427a96749 /chrome/browser/sidebar | |
parent | dc08829586171f0d33750de889ff3ec88b63cf06 (diff) | |
download | chromium_src-3a80ea33a37398a081455f4ae8b70572c7bc80e8.zip chromium_src-3a80ea33a37398a081455f4ae8b70572c7bc80e8.tar.gz chromium_src-3a80ea33a37398a081455f4ae8b70572c7bc80e8.tar.bz2 |
Rip Out the Sidebar API
This fix removes the sidebar api and all its references from the code (primarily relating to chrome/common/extensions and chrome/browser/ui).
BUG=107646
TEST=Covered by existing tests.
Review URL: http://codereview.chromium.org/9006027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116901 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sidebar')
-rw-r--r-- | chrome/browser/sidebar/sidebar_browsertest.cc | 213 | ||||
-rw-r--r-- | chrome/browser/sidebar/sidebar_container.cc | 131 | ||||
-rw-r--r-- | chrome/browser/sidebar/sidebar_container.h | 144 | ||||
-rw-r--r-- | chrome/browser/sidebar/sidebar_manager.cc | 341 | ||||
-rw-r--r-- | chrome/browser/sidebar/sidebar_manager.h | 157 |
5 files changed, 0 insertions, 986 deletions
diff --git a/chrome/browser/sidebar/sidebar_browsertest.cc b/chrome/browser/sidebar/sidebar_browsertest.cc index d6b45c4..e69de29 100644 --- a/chrome/browser/sidebar/sidebar_browsertest.cc +++ b/chrome/browser/sidebar/sidebar_browsertest.cc @@ -1,213 +0,0 @@ -// Copyright (c) 2011 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. - -#include "base/command_line.h" -#include "base/file_path.h" -#include "base/memory/ref_counted.h" -#include "base/path_service.h" -#include "chrome/browser/extensions/extension_browsertest.h" -#include "chrome/browser/sidebar/sidebar_manager.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_window.h" -#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" -#include "chrome/browser/ui/views/frame/browser_view.h" -#include "chrome/common/chrome_paths.h" -#include "chrome/common/chrome_switches.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/test/base/in_process_browser_test.h" -#include "chrome/test/base/ui_test_utils.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "net/test/test_server.h" - -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/profiles/profile.h" - -using content::NavigationController; -using content::WebContents; - -namespace { - -const char kSimplePage[] = "files/sidebar/simple_page.html"; - -class SidebarTest : public ExtensionBrowserTest { - public: - SidebarTest() { - CommandLine::ForCurrentProcess()->AppendSwitch( - switches::kEnableExperimentalExtensionApis); - } - - protected: - // InProcessBrowserTest overrides. - virtual void SetUpOnMainThread() { - ExtensionBrowserTest::SetUpOnMainThread(); - - // Load test sidebar extension. - FilePath extension_path; - ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); - extension_path = extension_path.AppendASCII("sidebar"); - - ASSERT_TRUE(LoadExtension(extension_path)); - - // For now content_id == extension_id. - content_id_ = last_loaded_extension_id_; - } - - void ShowSidebarForCurrentTab() { - ShowSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); - } - - void ExpandSidebarForCurrentTab() { - ExpandSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); - } - - void CollapseSidebarForCurrentTab() { - CollapseSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); - } - - void HideSidebarForCurrentTab() { - HideSidebar(browser()->GetSelectedTabContentsWrapper()->web_contents()); - } - - void NavigateSidebarForCurrentTabTo(const std::string& test_page) { - GURL url = test_server()->GetURL(test_page); - - TabContents* tab = static_cast<TabContents*>( - browser()->GetSelectedTabContentsWrapper()->web_contents()); - - SidebarManager* sidebar_manager = SidebarManager::GetInstance(); - SidebarContainer* sidebar_container = - sidebar_manager->GetSidebarContainerFor(tab, content_id_); - TabContents* client_contents = sidebar_container->sidebar_contents(); - - ui_test_utils::WindowedNotificationObserver observer( - content::NOTIFICATION_LOAD_STOP, - content::Source<NavigationController>( - &client_contents->GetController())); - sidebar_manager->NavigateSidebar(tab, content_id_, url); - observer.Wait(); - } - - void ShowSidebar(WebContents* temp) { - TabContents* tab = static_cast<TabContents*>(temp); - SidebarManager* sidebar_manager = SidebarManager::GetInstance(); - sidebar_manager->ShowSidebar(tab, content_id_); - } - - void ExpandSidebar(WebContents* temp) { - TabContents* tab = static_cast<TabContents*>(temp); - SidebarManager* sidebar_manager = SidebarManager::GetInstance(); - sidebar_manager->ExpandSidebar(tab, content_id_); - if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) - EXPECT_GT(browser_view()->GetSidebarWidth(), 0); - } - - void CollapseSidebar(WebContents* temp) { - TabContents* tab = static_cast<TabContents*>(temp); - SidebarManager* sidebar_manager = SidebarManager::GetInstance(); - sidebar_manager->CollapseSidebar(tab, content_id_); - if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) - EXPECT_EQ(0, browser_view()->GetSidebarWidth()); - } - - void HideSidebar(WebContents* temp) { - TabContents* tab = static_cast<TabContents*>(temp); - SidebarManager* sidebar_manager = SidebarManager::GetInstance(); - sidebar_manager->HideSidebar(tab, content_id_); - if (browser()->GetSelectedTabContentsWrapper()->web_contents() == tab) - EXPECT_EQ(0, browser_view()->GetSidebarWidth()); - } - - TabContents* tab_contents(int i) { - return static_cast<TabContents*>( - browser()->GetTabContentsWrapperAt(i)->web_contents()); - } - - BrowserView* browser_view() const { - return static_cast<BrowserView*>(browser()->window()); - } - - private: - std::string content_id_; -}; - -IN_PROC_BROWSER_TEST_F(SidebarTest, OpenClose) { - ShowSidebarForCurrentTab(); - - ExpandSidebarForCurrentTab(); - CollapseSidebarForCurrentTab(); - - ExpandSidebarForCurrentTab(); - CollapseSidebarForCurrentTab(); - - ExpandSidebarForCurrentTab(); - CollapseSidebarForCurrentTab(); - - HideSidebarForCurrentTab(); - - ShowSidebarForCurrentTab(); - - ExpandSidebarForCurrentTab(); - CollapseSidebarForCurrentTab(); - - HideSidebarForCurrentTab(); -} - -IN_PROC_BROWSER_TEST_F(SidebarTest, SwitchingTabs) { - ShowSidebarForCurrentTab(); - ExpandSidebarForCurrentTab(); - - browser()->NewTab(); - - // Make sure sidebar is not visbile for the newly opened tab. - EXPECT_EQ(0, browser_view()->GetSidebarWidth()); - - // Switch back to the first tab. - browser()->SelectNumberedTab(0); - - // Make sure it is visible now. - EXPECT_GT(browser_view()->GetSidebarWidth(), 0); - - HideSidebarForCurrentTab(); -} - -IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarOnInactiveTab) { - ShowSidebarForCurrentTab(); - ExpandSidebarForCurrentTab(); - - browser()->NewTab(); - - // Hide sidebar on inactive (first) tab. - HideSidebar(tab_contents(0)); - - // Switch back to the first tab. - browser()->SelectNumberedTab(0); - - // Make sure sidebar is not visbile anymore. - EXPECT_EQ(0, browser_view()->GetSidebarWidth()); - - // Show sidebar on inactive (second) tab. - ShowSidebar(tab_contents(1)); - ExpandSidebar(tab_contents(1)); - // Make sure sidebar is not visible yet. - EXPECT_EQ(0, browser_view()->GetSidebarWidth()); - - // Switch back to the second tab. - browser()->SelectNumberedTab(1); - // Make sure sidebar is visible now. - EXPECT_GT(browser_view()->GetSidebarWidth(), 0); - - HideSidebarForCurrentTab(); -} - -IN_PROC_BROWSER_TEST_F(SidebarTest, SidebarNavigate) { - ASSERT_TRUE(test_server()->Start()); - - ShowSidebarForCurrentTab(); - - NavigateSidebarForCurrentTabTo(kSimplePage); - - HideSidebarForCurrentTab(); -} - -} // namespace diff --git a/chrome/browser/sidebar/sidebar_container.cc b/chrome/browser/sidebar/sidebar_container.cc index e1198e0..e69de29 100644 --- a/chrome/browser/sidebar/sidebar_container.cc +++ b/chrome/browser/sidebar/sidebar_container.cc @@ -1,131 +0,0 @@ -// Copyright (c) 2011 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. - -#include "chrome/browser/sidebar/sidebar_container.h" - -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_resource.h" -#include "chrome/common/extensions/extension_sidebar_defaults.h" -#include "chrome/common/extensions/extension_sidebar_utils.h" -#include "content/browser/renderer_host/render_view_host.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "content/browser/tab_contents/tab_contents_view.h" -#include "content/public/browser/navigation_controller.h" -#include "content/public/browser/render_process_host.h" -#include "googleurl/src/gurl.h" -#include "third_party/skia/include/core/SkBitmap.h" - -SidebarContainer::SidebarContainer(TabContents* tab, - const std::string& content_id, - Delegate* delegate) - : tab_(tab), - content_id_(content_id), - delegate_(delegate), - icon_(new SkBitmap), - navigate_to_default_page_on_expand_(true), - use_default_icon_(true) { - // Create TabContents for sidebar. - sidebar_contents_.reset( - new TabContents(Profile::FromBrowserContext(tab->GetBrowserContext()), - NULL, MSG_ROUTING_NONE, NULL, NULL)); - sidebar_contents_->SetDelegate(this); -} - -SidebarContainer::~SidebarContainer() { -} - -void SidebarContainer::SidebarClosing() { - delegate_->UpdateSidebar(this); -} - -void SidebarContainer::LoadDefaults() { - const Extension* extension = GetExtension(); - if (!extension) - return; // Can be NULL in tests. - const ExtensionSidebarDefaults* sidebar_defaults = - extension->sidebar_defaults(); - - title_ = sidebar_defaults->default_title(); - - if (!sidebar_defaults->default_icon_path().empty()) { - image_loading_tracker_.reset(new ImageLoadingTracker(this)); - image_loading_tracker_->LoadImage( - extension, - extension->GetResource(sidebar_defaults->default_icon_path()), - gfx::Size(Extension::kSidebarIconMaxSize, - Extension::kSidebarIconMaxSize), - ImageLoadingTracker::CACHE); - } -} - -void SidebarContainer::Show() { - delegate_->UpdateSidebar(this); -} - -void SidebarContainer::Expand() { - if (navigate_to_default_page_on_expand_) { - navigate_to_default_page_on_expand_ = false; - // Check whether a default page is specified for this sidebar. - const Extension* extension = GetExtension(); - if (extension) { // Can be NULL in tests. - if (extension->sidebar_defaults()->default_page().is_valid()) - Navigate(extension->sidebar_defaults()->default_page()); - } - } - - delegate_->UpdateSidebar(this); - sidebar_contents_->GetView()->SetInitialFocus(); -} - -void SidebarContainer::Collapse() { - delegate_->UpdateSidebar(this); -} - -void SidebarContainer::Navigate(const GURL& url) { - // TODO(alekseys): add a progress UI. - navigate_to_default_page_on_expand_ = false; - sidebar_contents_->GetController().LoadURL( - url, content::Referrer(), content::PAGE_TRANSITION_START_PAGE, - std::string()); -} - -void SidebarContainer::SetBadgeText(const string16& badge_text) { - badge_text_ = badge_text; -} - -void SidebarContainer::SetIcon(const SkBitmap& bitmap) { - use_default_icon_ = false; - *icon_ = bitmap; -} - -void SidebarContainer::SetTitle(const string16& title) { - title_ = title; -} - -content::JavaScriptDialogCreator* -SidebarContainer::GetJavaScriptDialogCreator() { - return GetJavaScriptDialogCreatorInstance(); -} - -void SidebarContainer::OnImageLoaded(SkBitmap* image, - const ExtensionResource& resource, - int index) { - if (image && use_default_icon_) { - *icon_ = *image; - delegate_->UpdateSidebar(this); - } -} - -const Extension* SidebarContainer::GetExtension() const { - Profile* profile = - Profile::FromBrowserContext(sidebar_contents_->GetBrowserContext()); - ExtensionService* service = profile->GetExtensionService(); - if (!service) - return NULL; - return service->GetExtensionById( - extension_sidebar_utils::GetExtensionIdByContentId(content_id_), false); -} diff --git a/chrome/browser/sidebar/sidebar_container.h b/chrome/browser/sidebar/sidebar_container.h index 917560a..e69de29 100644 --- a/chrome/browser/sidebar/sidebar_container.h +++ b/chrome/browser/sidebar/sidebar_container.h @@ -1,144 +0,0 @@ -// Copyright (c) 2011 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_CONTAINER_H_ -#define CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_ - -#include <string> - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" -#include "base/string16.h" -#include "chrome/browser/extensions/image_loading_tracker.h" -#include "content/public/browser/web_contents_delegate.h" - -class SkBitmap; -class TabContents; - -/////////////////////////////////////////////////////////////////////////////// -// SidebarContainer -// -// Stores one particular sidebar state: sidebar's content, its content id, -// tab it is linked to, mini tab icon, title etc. -// -class SidebarContainer : public content::WebContentsDelegate, - private ImageLoadingTracker::Observer { - public: - // Interface to implement to listen for sidebar update notification. - class Delegate { - public: - Delegate() {} - virtual ~Delegate() {} - virtual void UpdateSidebar(SidebarContainer* host) = 0; - private: - DISALLOW_COPY_AND_ASSIGN(Delegate); - }; - - SidebarContainer(TabContents* tab, - const std::string& content_id, - Delegate* delegate); - virtual ~SidebarContainer(); - - // Called right before destroying this sidebar. - // Does all the necessary cleanup. - void SidebarClosing(); - - // Sets default sidebar parameters, as specified in extension manifest. - void LoadDefaults(); - - // Returns sidebar's content id. - const std::string& content_id() const { return content_id_; } - - // Returns TabContents sidebar is linked to. - TabContents* tab_contents() const { return tab_; } - - // Returns sidebar's TabContents. - TabContents* sidebar_contents() const { return sidebar_contents_.get(); } - - // Accessor for the badge text. - const string16& badge_text() const { return badge_text_; } - - // Accessor for the icon. - const SkBitmap& icon() const { return *icon_; } - - // Accessor for the title. - const string16& title() const { return title_; } - - // Functions supporting chrome.experimental.sidebar API. - - // Notifies hosting window that this sidebar was expanded. - void Show(); - - // Notifies hosting window that this sidebar was expanded. - void Expand(); - - // Notifies hosting window that this sidebar was collapsed. - void Collapse(); - - // Navigates sidebar contents to the |url|. - void Navigate(const GURL& url); - - // Changes sidebar's badge text. - void SetBadgeText(const string16& badge_text); - - // Changes sidebar's icon. - void SetIcon(const SkBitmap& bitmap); - - // Changes sidebar's title. - void SetTitle(const string16& title); - - private: - // Overridden from content::WebContentsDelegate: - virtual content::JavaScriptDialogCreator* - GetJavaScriptDialogCreator() OVERRIDE; - - // Overridden from ImageLoadingTracker::Observer: - virtual void OnImageLoaded(SkBitmap* image, - const ExtensionResource& resource, - int index) OVERRIDE; - - // Returns an extension this sidebar belongs to. - const Extension* GetExtension() const; - - // Contents of the tab this sidebar is linked to. - TabContents* tab_; - - // Sidebar's content id. There might be more than one sidebar liked to each - // particular tab and they are identified by their unique content id. - const std::string content_id_; - - // Sidebar update notification listener. - Delegate* delegate_; - - // Sidebar contents. - scoped_ptr<TabContents> sidebar_contents_; - - // Badge text displayed on the sidebar's mini tab. - string16 badge_text_; - - // Icon displayed on the sidebar's mini tab. - scoped_ptr<SkBitmap> icon_; - - // Sidebar's title, displayed as a tooltip for sidebar's mini tab. - string16 title_; - - // On the first expand sidebar will be automatically navigated to the default - // page (specified in the extension manifest), but only if the extension has - // not explicitly navigated it yet. This variable is set to false on the first - // sidebar navigation. - bool navigate_to_default_page_on_expand_; - // Since the default icon (specified in the extension manifest) is loaded - // asynchronously, sidebar icon can already be set by the extension - // by the time it's loaded. This variable tracks whether the loaded default - // icon should be used or discarded. - bool use_default_icon_; - - // Helper to load icons from extension asynchronously. - scoped_ptr<ImageLoadingTracker> image_loading_tracker_; - - DISALLOW_COPY_AND_ASSIGN(SidebarContainer); -}; - -#endif // CHROME_BROWSER_SIDEBAR_SIDEBAR_CONTAINER_H_ diff --git a/chrome/browser/sidebar/sidebar_manager.cc b/chrome/browser/sidebar/sidebar_manager.cc index 07271d0..e69de29 100644 --- a/chrome/browser/sidebar/sidebar_manager.cc +++ b/chrome/browser/sidebar/sidebar_manager.cc @@ -1,341 +0,0 @@ -// Copyright (c) 2011 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. - -#include "chrome/browser/sidebar/sidebar_manager.h" - -#include <vector> - -#include "base/command_line.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/extensions/extension_sidebar_api.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/sidebar/sidebar_container.h" -#include "chrome/common/chrome_notification_types.h" -#include "chrome/common/chrome_switches.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "content/public/browser/notification_service.h" -#include "googleurl/src/gurl.h" - -using content::WebContents; - -struct SidebarManager::SidebarStateForTab { - // 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; -}; - -// static -SidebarManager* SidebarManager::GetInstance() { - return g_browser_process->sidebar_manager(); -} - -// static -bool SidebarManager::IsSidebarAllowed() { - return CommandLine::ForCurrentProcess()->HasSwitch( - switches::kEnableExperimentalExtensionApis); -} - -SidebarManager::SidebarManager() { -} - -SidebarContainer* SidebarManager::GetActiveSidebarContainerFor( - TabContents* tab) { - TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); - if (it == tab_to_sidebar_host_.end()) - return NULL; - if (it->second.active_content_id.empty()) - return NULL; - ContentIdToSidebarHostMap::iterator host_it = - it->second.content_id_to_sidebar_host.find(it->second.active_content_id); - DCHECK(host_it != it->second.content_id_to_sidebar_host.end()); - return host_it->second; -} - -SidebarContainer* SidebarManager::GetSidebarContainerFor( - WebContents* tab, const std::string& content_id) { - DCHECK(!content_id.empty()); - TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); - if (it == tab_to_sidebar_host_.end()) - return NULL; - ContentIdToSidebarHostMap::iterator host_it = - it->second.content_id_to_sidebar_host.find(content_id); - if (host_it == it->second.content_id_to_sidebar_host.end()) - return NULL; - return host_it->second; -} - -TabContents* SidebarManager::GetSidebarTabContents( - TabContents* tab, const std::string& content_id) { - DCHECK(!content_id.empty()); - SidebarContainer* sidebar_host = GetSidebarContainerFor(tab, content_id); - if (!sidebar_host) - return NULL; - return sidebar_host->sidebar_contents(); -} - -void SidebarManager::NotifyStateChanges( - TabContents* was_active_sidebar_contents, - TabContents* active_sidebar_contents) { - if (was_active_sidebar_contents == active_sidebar_contents) - return; - - SidebarContainer* was_active_host = - was_active_sidebar_contents == NULL ? NULL : - FindSidebarContainerFor(was_active_sidebar_contents); - SidebarContainer* active_host = - active_sidebar_contents == NULL ? NULL : - FindSidebarContainerFor(active_sidebar_contents); - - if (was_active_host != NULL) { - Profile* profile = Profile::FromBrowserContext( - was_active_sidebar_contents->GetBrowserContext()); - ExtensionSidebarEventRouter::OnStateChanged( - profile, - was_active_host->tab_contents(), - was_active_host->content_id(), - extension_sidebar_constants::kShownState); - } - - if (active_host != NULL) { - Profile* profile = Profile::FromBrowserContext( - active_sidebar_contents->GetBrowserContext()); - ExtensionSidebarEventRouter::OnStateChanged( - profile, - active_host->tab_contents(), - active_host->content_id(), - extension_sidebar_constants::kActiveState); - } -} - -void SidebarManager::ShowSidebar(TabContents* tab, - const std::string& content_id) { - DCHECK(!content_id.empty()); - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - if (!host) { - host = new SidebarContainer(tab, content_id, this); - RegisterSidebarContainerFor(tab, host); - // It might trigger UpdateSidebar notification, so load them after - // the registration. - host->LoadDefaults(); - } - - host->Show(); - - Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); - ExtensionSidebarEventRouter::OnStateChanged( - profile, tab, content_id, - extension_sidebar_constants::kShownState); -} - -void SidebarManager::ExpandSidebar(TabContents* tab, - const std::string& content_id) { - DCHECK(!content_id.empty()); - TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); - if (it == tab_to_sidebar_host_.end()) - return; - // If it's already active, bail out. - if (it->second.active_content_id == content_id) - return; - - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - DCHECK(host); - if (!host) - return; - it->second.active_content_id = content_id; - - host->Expand(); -} - -void SidebarManager::CollapseSidebar(TabContents* tab, - const std::string& content_id) { - DCHECK(!content_id.empty()); - TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); - if (it == tab_to_sidebar_host_.end()) - return; - // If it's not the one active now, bail out. - if (it->second.active_content_id != content_id) - return; - - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - DCHECK(host); - if (!host) - return; - it->second.active_content_id.clear(); - - host->Collapse(); -} - -void SidebarManager::HideSidebar(WebContents* tab, - const std::string& content_id) { - DCHECK(!content_id.empty()); - TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); - if (it == tab_to_sidebar_host_.end()) - return; - if (it->second.active_content_id == content_id) - it->second.active_content_id.clear(); - - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - DCHECK(host); - - UnregisterSidebarContainerFor(tab, content_id); - - Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); - ExtensionSidebarEventRouter::OnStateChanged( - profile, tab, content_id, - extension_sidebar_constants::kHiddenState); -} - -void SidebarManager::NavigateSidebar(TabContents* tab, - const std::string& content_id, - const GURL& url) { - DCHECK(!content_id.empty()); - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - if (!host) - return; - - host->Navigate(url); -} - -void SidebarManager::SetSidebarBadgeText( - TabContents* tab, const std::string& content_id, - const string16& badge_text) { - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - if (!host) - return; - host->SetBadgeText(badge_text); -} - -void SidebarManager::SetSidebarIcon( - TabContents* tab, const std::string& content_id, - const SkBitmap& bitmap) { - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - if (!host) - return; - host->SetIcon(bitmap); -} - -void SidebarManager::SetSidebarTitle( - TabContents* tab, const std::string& content_id, - const string16& title) { - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - if (!host) - return; - host->SetTitle(title); -} - -SidebarManager::~SidebarManager() { - DCHECK(tab_to_sidebar_host_.empty()); - DCHECK(sidebar_host_to_tab_.empty()); -} - -void SidebarManager::Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { - HideAllSidebars(content::Source<WebContents>(source).ptr()); - } else { - NOTREACHED() << "Got a notification we didn't register for!"; - } -} - -void SidebarManager::UpdateSidebar(SidebarContainer* host) { - content::NotificationService::current()->Notify( - chrome::NOTIFICATION_SIDEBAR_CHANGED, - content::Source<SidebarManager>(this), - content::Details<SidebarContainer>(host)); -} - -void SidebarManager::HideAllSidebars(WebContents* tab) { - TabToSidebarHostMap::iterator tab_it = tab_to_sidebar_host_.find(tab); - if (tab_it == tab_to_sidebar_host_.end()) - return; - const ContentIdToSidebarHostMap& hosts = - tab_it->second.content_id_to_sidebar_host; - - std::vector<std::string> content_ids; - for (ContentIdToSidebarHostMap::const_iterator it = hosts.begin(); - it != hosts.end(); ++it) { - content_ids.push_back(it->first); - } - - for (std::vector<std::string>::iterator it = content_ids.begin(); - it != content_ids.end(); ++it) { - HideSidebar(tab, *it); - } -} - -SidebarContainer* SidebarManager::FindSidebarContainerFor( - TabContents* sidebar_contents) { - for (SidebarHostToTabMap::iterator it = sidebar_host_to_tab_.begin(); - it != sidebar_host_to_tab_.end(); - ++it) { - if (sidebar_contents == it->first->sidebar_contents()) - return it->first; - } - return NULL; -} - -void SidebarManager::RegisterSidebarContainerFor( - WebContents* tab, SidebarContainer* sidebar_host) { - DCHECK(!GetSidebarContainerFor(tab, sidebar_host->content_id())); - - // If it's a first sidebar for this tab, register destroy notification. - if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { - registrar_.Add(this, - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - content::Source<WebContents>(tab)); - } - - BindSidebarHost(tab, sidebar_host); -} - -void SidebarManager::UnregisterSidebarContainerFor( - WebContents* tab, const std::string& content_id) { - SidebarContainer* host = GetSidebarContainerFor(tab, content_id); - DCHECK(host); - if (!host) - return; - - UnbindSidebarHost(tab, host); - - // If there's no more sidebars linked to this tab, unsubscribe. - if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { - registrar_.Remove(this, - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - content::Source<WebContents>(tab)); - } - - // Issue tab closing event post unbound. - host->SidebarClosing(); - // Destroy sidebar container. - delete host; -} - -void SidebarManager::BindSidebarHost(WebContents* tab, - SidebarContainer* sidebar_host) { - const std::string& content_id = sidebar_host->content_id(); - - DCHECK(GetSidebarContainerFor(tab, content_id) == NULL); - DCHECK(sidebar_host_to_tab_.find(sidebar_host) == - sidebar_host_to_tab_.end()); - - tab_to_sidebar_host_[tab].content_id_to_sidebar_host[content_id] = - sidebar_host; - sidebar_host_to_tab_[sidebar_host] = tab; -} - -void SidebarManager::UnbindSidebarHost(WebContents* tab, - SidebarContainer* sidebar_host) { - const std::string& content_id = sidebar_host->content_id(); - - DCHECK(GetSidebarContainerFor(tab, content_id) == sidebar_host); - DCHECK(sidebar_host_to_tab_.find(sidebar_host)->second == tab); - DCHECK(tab_to_sidebar_host_[tab].active_content_id != content_id); - - tab_to_sidebar_host_[tab].content_id_to_sidebar_host.erase(content_id); - if (tab_to_sidebar_host_[tab].content_id_to_sidebar_host.empty()) - tab_to_sidebar_host_.erase(tab); - sidebar_host_to_tab_.erase(sidebar_host); -} diff --git a/chrome/browser/sidebar/sidebar_manager.h b/chrome/browser/sidebar/sidebar_manager.h index f059347..e69de29 100644 --- a/chrome/browser/sidebar/sidebar_manager.h +++ b/chrome/browser/sidebar/sidebar_manager.h @@ -1,157 +0,0 @@ -// Copyright (c) 2011 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/memory/ref_counted.h" -#include "base/string16.h" -#include "chrome/browser/sidebar/sidebar_container.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" - -class GURL; -class SidebarContainer; -class SkBitmap; -class TabContents; - -namespace content { -class WebContents; -} - -/////////////////////////////////////////////////////////////////////////////// -// SidebarManager -// -// This class is a singleton that manages SidebarContainer instances and -// maintains a connection between tabs and sidebars. -// -class SidebarManager : public content::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(content::WebContents* 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(content::WebContents* 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 content::NotificationObserver. - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; - - // Overridden from SidebarContainer::Delegate. - virtual void UpdateSidebar(SidebarContainer* host) OVERRIDE; - - // Hides all sidebars registered for |tab|. - void HideAllSidebars(content::WebContents* 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(content::WebContents* tab, - SidebarContainer* container); - - // Unregisters SidebarContainer identified by |tab| and |content_id|. - void UnregisterSidebarContainerFor(content::WebContents* tab, - const std::string& content_id); - - // Records the link between |tab| and |sidebar_host|. - void BindSidebarHost(content::WebContents* tab, - SidebarContainer* sidebar_host); - - // Forgets the link between |tab| and |sidebar_host|. - void UnbindSidebarHost(content::WebContents* tab, - SidebarContainer* sidebar_host); - - content::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. - struct SidebarStateForTab; - typedef std::map<content::WebContents*, SidebarStateForTab> - TabToSidebarHostMap; - TabToSidebarHostMap tab_to_sidebar_host_; - - typedef std::map<SidebarContainer*, content::WebContents*> - SidebarHostToTabMap; - SidebarHostToTabMap sidebar_host_to_tab_; - - DISALLOW_COPY_AND_ASSIGN(SidebarManager); -}; - -#endif // CHROME_BROWSER_SIDEBAR_SIDEBAR_MANAGER_H_ |