Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 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 #include "chrome/browser/extensions/sidebar_manager.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/chrome_notification_types.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/sidebar_container.h" | |
| 14 #include "chrome/browser/extensions/sidebar_manager_observer.h" | |
| 15 #include "content/public/browser/notification_service.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "extensions/browser/extension_system.h" | |
| 18 #include "extensions/common/switches.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 using content::BrowserContext; | |
| 22 using content::WebContents; | |
| 23 | |
| 24 namespace extensions { | |
| 25 // static | |
| 26 SidebarManager* SidebarManager::GetFromContext(BrowserContext* context) { | |
| 27 return ExtensionSystem::Get(context)->sidebar_manager(); | |
| 28 } | |
| 29 | |
| 30 SidebarManager::SidebarManager() { | |
| 31 } | |
| 32 | |
| 33 SidebarContainer* SidebarManager::GetSidebarContainerFor( | |
| 34 content::WebContents* tab) { | |
| 35 TabToSidebarContainerMap::iterator it = tab_to_sidebar_container_.find(tab); | |
| 36 if (it == tab_to_sidebar_container_.end()) | |
| 37 return nullptr; | |
| 38 return it->second; | |
| 39 } | |
| 40 | |
| 41 void SidebarManager::NotifyStateChanges( | |
| 42 content::WebContents* was_active_sidebar_contents, | |
| 43 content::WebContents* active_sidebar_contents) { | |
| 44 if (was_active_sidebar_contents == active_sidebar_contents) | |
| 45 return; | |
| 46 | |
| 47 SidebarContainer* was_active_container = | |
| 48 was_active_sidebar_contents == nullptr | |
| 49 ? nullptr | |
| 50 : FindSidebarContainerFor(was_active_sidebar_contents); | |
| 51 SidebarContainer* active_container = | |
| 52 active_sidebar_contents == nullptr | |
| 53 ? nullptr | |
| 54 : FindSidebarContainerFor(active_sidebar_contents); | |
| 55 | |
| 56 content::WebContents* old_tab = was_active_container == nullptr | |
| 57 ? nullptr | |
| 58 : was_active_container->web_contents(); | |
| 59 content::WebContents* new_tab = | |
| 60 active_container == nullptr ? nullptr : active_container->web_contents(); | |
| 61 const std::string& old_content_id = | |
| 62 was_active_container == nullptr ? "" | |
| 63 : was_active_container->extension_id(); | |
| 64 const std::string& new_content_id = | |
| 65 active_container == nullptr ? "" : active_container->extension_id(); | |
| 66 | |
| 67 FOR_EACH_OBSERVER( | |
| 68 SidebarManagerObserver, observer_list_, | |
| 69 OnSidebarSwitched(old_tab, old_content_id, new_tab, new_content_id)); | |
| 70 } | |
| 71 | |
| 72 void SidebarManager::ShowSidebar(content::WebContents* tab, | |
| 73 const GURL& url, | |
| 74 Browser* browser) { | |
| 75 SidebarContainer* container = GetSidebarContainerFor(tab); | |
| 76 if (container) | |
| 77 HideSidebar(tab); | |
| 78 | |
| 79 container = new SidebarContainer(browser, tab, url); | |
| 80 BindSidebarContainer(tab, container); | |
| 81 | |
| 82 container->Show(); | |
| 83 container->Expand(); | |
| 84 | |
| 85 const std::string id = container->extension_id(); | |
| 86 FOR_EACH_OBSERVER(SidebarManagerObserver, observer_list_, | |
| 87 OnSidebarShown(tab, id)); | |
| 88 } | |
| 89 | |
| 90 void SidebarManager::HideSidebar(WebContents* tab) { | |
| 91 SidebarContainer* container = GetSidebarContainerFor(tab); | |
| 92 if (!container) | |
| 93 return; | |
| 94 | |
| 95 const std::string content_id = container->extension_id(); | |
| 96 UnbindSidebarContainer(tab, container); | |
| 97 delete container; | |
| 98 | |
| 99 FOR_EACH_OBSERVER(SidebarManagerObserver, observer_list_, | |
| 100 OnSidebarHidden(tab, content_id)); | |
| 101 } | |
| 102 | |
| 103 void SidebarManager::NavigateSidebar(content::WebContents* tab) { | |
|
Devlin
2015/06/10 17:25:05
Taken directly from https://codereview.chromium.or
| |
| 104 SidebarContainer* container = GetSidebarContainerFor(tab); | |
| 105 if (!container) | |
| 106 return; | |
| 107 | |
| 108 container->Navigate(); | |
| 109 } | |
| 110 | |
| 111 SidebarManager::~SidebarManager() { | |
| 112 DCHECK(tab_to_sidebar_container_.empty()); | |
| 113 DCHECK(sidebar_container_to_tab_.empty()); | |
| 114 } | |
| 115 | |
| 116 void SidebarManager::Observe(int type, | |
| 117 const content::NotificationSource& source, | |
| 118 const content::NotificationDetails& details) { | |
| 119 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | |
| 120 HideSidebar(content::Source<WebContents>(source).ptr()); | |
| 121 } else { | |
| 122 NOTREACHED() << "Got a notification we didn't register for!"; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 SidebarContainer* SidebarManager::FindSidebarContainerFor( | |
| 127 content::WebContents* sidebar_contents) { | |
| 128 for (SidebarContainerToTabMap::iterator it = | |
| 129 sidebar_container_to_tab_.begin(); | |
| 130 it != sidebar_container_to_tab_.end(); ++it) { | |
| 131 if (sidebar_contents == it->first->host_contents()) | |
| 132 return it->first; | |
| 133 } | |
| 134 return nullptr; | |
| 135 } | |
| 136 | |
| 137 void SidebarManager::BindSidebarContainer(WebContents* tab, | |
| 138 SidebarContainer* container) { | |
| 139 tab_to_sidebar_container_[tab] = container; | |
| 140 sidebar_container_to_tab_[container] = tab; | |
| 141 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 142 content::Source<WebContents>(tab)); | |
| 143 } | |
| 144 | |
| 145 void SidebarManager::UnbindSidebarContainer(WebContents* tab, | |
| 146 SidebarContainer* container) { | |
| 147 tab_to_sidebar_container_.erase(tab); | |
| 148 sidebar_container_to_tab_.erase(container); | |
| 149 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 150 content::Source<WebContents>(tab)); | |
| 151 } | |
| 152 | |
| 153 void SidebarManager::AddObserver(SidebarManagerObserver* observer) { | |
| 154 observer_list_.AddObserver(observer); | |
| 155 } | |
| 156 | |
| 157 void SidebarManager::RemoveObserver(SidebarManagerObserver* observer) { | |
| 158 observer_list_.RemoveObserver(observer); | |
| 159 } | |
| 160 | |
| 161 } // namespace extensions | |
| OLD | NEW |