OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sidebar/sidebar_manager.h" | 5 #include "chrome/browser/sidebar/sidebar_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/extensions/extension_sidebar_api.h" | 11 #include "chrome/browser/extensions/extension_sidebar_api.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sidebar/sidebar_container.h" |
13 #include "chrome/browser/tab_contents/tab_contents.h" | 14 #include "chrome/browser/tab_contents/tab_contents.h" |
14 #include "chrome/browser/sidebar/sidebar_container.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/notification_service.h" | 17 #include "chrome/common/notification_service.h" |
17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
18 | 19 |
19 struct SidebarManager::SidebarStateForTab { | 20 struct SidebarManager::SidebarStateForTab { |
20 // Sidebars linked to this tab. | 21 // Sidebars linked to this tab. |
21 ContentIdToSidebarHostMap content_id_to_sidebar_host; | 22 ContentIdToSidebarHostMap content_id_to_sidebar_host; |
22 // Content id of the currently active (expanded and visible) sidebar. | 23 // Content id of the currently active (expanded and visible) sidebar. |
23 std::string active_content_id; | 24 std::string active_content_id; |
24 }; | 25 }; |
25 | 26 |
26 // static | 27 // static |
27 SidebarManager* SidebarManager::GetInstance() { | 28 SidebarManager* SidebarManager::GetInstance() { |
28 return g_browser_process->sidebar_manager(); | 29 return g_browser_process->sidebar_manager(); |
29 } | 30 } |
30 | 31 |
31 // static | 32 // static |
32 bool SidebarManager::IsSidebarAllowed() { | 33 bool SidebarManager::IsSidebarAllowed() { |
33 return CommandLine::ForCurrentProcess()->HasSwitch( | 34 return CommandLine::ForCurrentProcess()->HasSwitch( |
34 switches::kEnableExperimentalExtensionApis); | 35 switches::kEnableExperimentalExtensionApis); |
35 } | 36 } |
36 | 37 |
37 SidebarManager::SidebarManager() { | 38 SidebarManager::SidebarManager() { |
38 } | 39 } |
39 | 40 |
| 41 // SidebarModel overrides. |
| 42 |
| 43 std::vector<SidebarContainer*> SidebarManager::GetAllSidebarsFor( |
| 44 TabContentsWrapper* tab) { |
| 45 std::vector<SidebarContainer*> sidebars; |
| 46 TabToSidebarHostMap::const_iterator it = tab_to_sidebar_host_.find(tab); |
| 47 if (it != tab_to_sidebar_host_.end()) { |
| 48 const ContentIdToSidebarHostMap& hosts = |
| 49 it->second.content_id_to_sidebar_host; |
| 50 |
| 51 sidebars.reserve(hosts.size()); |
| 52 for (ContentIdToSidebarHostMap::const_iterator it = hosts.begin(); |
| 53 it != hosts.end(); ++it) { |
| 54 sidebars.push_back(it->second); |
| 55 } |
| 56 } |
| 57 return sidebars; |
| 58 } |
| 59 |
40 SidebarContainer* SidebarManager::GetActiveSidebarContainerFor( | 60 SidebarContainer* SidebarManager::GetActiveSidebarContainerFor( |
41 TabContents* tab) { | 61 TabContentsWrapper* tab) { |
42 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); | 62 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
43 if (it == tab_to_sidebar_host_.end()) | 63 if (it == tab_to_sidebar_host_.end()) |
44 return NULL; | 64 return NULL; |
45 if (it->second.active_content_id.empty()) | 65 if (it->second.active_content_id.empty()) |
46 return NULL; | 66 return NULL; |
47 ContentIdToSidebarHostMap::iterator host_it = | 67 ContentIdToSidebarHostMap::iterator host_it = |
48 it->second.content_id_to_sidebar_host.find(it->second.active_content_id); | 68 it->second.content_id_to_sidebar_host.find(it->second.active_content_id); |
49 DCHECK(host_it != it->second.content_id_to_sidebar_host.end()); | 69 DCHECK(host_it != it->second.content_id_to_sidebar_host.end()); |
50 return host_it->second; | 70 return host_it->second; |
51 } | 71 } |
52 | 72 |
53 SidebarContainer* SidebarManager::GetSidebarContainerFor( | 73 SidebarContainer* SidebarManager::GetSidebarContainerFor( |
54 TabContents* tab, const std::string& content_id) { | 74 TabContentsWrapper* tab, const std::string& content_id) { |
55 DCHECK(!content_id.empty()); | 75 DCHECK(!content_id.empty()); |
56 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); | 76 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
57 if (it == tab_to_sidebar_host_.end()) | 77 if (it == tab_to_sidebar_host_.end()) |
58 return NULL; | 78 return NULL; |
59 ContentIdToSidebarHostMap::iterator host_it = | 79 ContentIdToSidebarHostMap::iterator host_it = |
60 it->second.content_id_to_sidebar_host.find(content_id); | 80 it->second.content_id_to_sidebar_host.find(content_id); |
61 if (host_it == it->second.content_id_to_sidebar_host.end()) | 81 if (host_it == it->second.content_id_to_sidebar_host.end()) |
62 return NULL; | 82 return NULL; |
63 return host_it->second; | 83 return host_it->second; |
64 } | 84 } |
65 | 85 |
66 TabContents* SidebarManager::GetSidebarTabContents( | 86 TabContents* SidebarManager::GetSidebarTabContents( |
67 TabContents* tab, const std::string& content_id) { | 87 TabContentsWrapper* tab, const std::string& content_id) { |
68 DCHECK(!content_id.empty()); | 88 DCHECK(!content_id.empty()); |
69 SidebarContainer* sidebar_host = GetSidebarContainerFor(tab, content_id); | 89 SidebarContainer* sidebar_host = GetSidebarContainerFor(tab, content_id); |
70 if (!sidebar_host) | 90 if (!sidebar_host) |
71 return NULL; | 91 return NULL; |
72 return sidebar_host->sidebar_contents(); | 92 return sidebar_host->sidebar_contents(); |
73 } | 93 } |
74 | 94 |
75 void SidebarManager::NotifyStateChanges( | 95 void SidebarManager::NotifyStateChanges( |
76 TabContents* was_active_sidebar_contents, | 96 TabContents* was_active_sidebar_contents, |
77 TabContents* active_sidebar_contents) { | 97 TabContents* active_sidebar_contents) { |
78 if (was_active_sidebar_contents == active_sidebar_contents) | 98 if (was_active_sidebar_contents == active_sidebar_contents) |
79 return; | 99 return; |
80 | 100 |
81 SidebarContainer* was_active_host = | 101 SidebarContainer* was_active_host = |
82 was_active_sidebar_contents == NULL ? NULL : | 102 was_active_sidebar_contents == NULL ? NULL : |
83 FindSidebarContainerFor(was_active_sidebar_contents); | 103 FindSidebarContainerFor(was_active_sidebar_contents); |
84 SidebarContainer* active_host = | 104 SidebarContainer* active_host = |
85 active_sidebar_contents == NULL ? NULL : | 105 active_sidebar_contents == NULL ? NULL : |
86 FindSidebarContainerFor(active_sidebar_contents); | 106 FindSidebarContainerFor(active_sidebar_contents); |
87 | 107 |
88 if (was_active_host != NULL) { | 108 if (was_active_host != NULL) { |
89 ExtensionSidebarEventRouter::OnStateChanged( | 109 ExtensionSidebarEventRouter::OnStateChanged( |
90 was_active_sidebar_contents->profile(), | 110 was_active_sidebar_contents->profile(), |
91 was_active_host->tab_contents(), was_active_host->content_id(), | 111 was_active_host->tab(), was_active_host->content_id(), |
92 extension_sidebar_constants::kShownState); | 112 extension_sidebar_constants::kShownState); |
93 } | 113 } |
94 | 114 |
95 if (active_host != NULL) { | 115 if (active_host != NULL) { |
96 ExtensionSidebarEventRouter::OnStateChanged( | 116 ExtensionSidebarEventRouter::OnStateChanged( |
97 active_sidebar_contents->profile(), | 117 active_sidebar_contents->profile(), |
98 active_host->tab_contents(), active_host->content_id(), | 118 active_host->tab(), active_host->content_id(), |
99 extension_sidebar_constants::kActiveState); | 119 extension_sidebar_constants::kActiveState); |
100 } | 120 } |
101 } | 121 } |
102 | 122 |
103 void SidebarManager::ShowSidebar(TabContents* tab, | 123 void SidebarManager::ToggleSidebar(TabContentsWrapper* tab, |
| 124 const std::string& content_id) { |
| 125 DCHECK(!content_id.empty()); |
| 126 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
| 127 if (it == tab_to_sidebar_host_.end()) |
| 128 return; |
| 129 // If it's not active, expand it. |
| 130 if (it->second.active_content_id != content_id) |
| 131 ExpandSidebar(tab, content_id); |
| 132 else |
| 133 CollapseSidebar(tab, content_id); |
| 134 } |
| 135 |
| 136 void SidebarManager::ShowSidebar(TabContentsWrapper* tab, |
104 const std::string& content_id) { | 137 const std::string& content_id) { |
105 DCHECK(!content_id.empty()); | 138 DCHECK(!content_id.empty()); |
106 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 139 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
107 if (!host) { | 140 if (!host) { |
108 host = new SidebarContainer(tab, content_id, this); | 141 host = new SidebarContainer(tab, content_id, this); |
109 RegisterSidebarContainerFor(tab, host); | 142 RegisterSidebarContainerFor(tab, host); |
110 // It might trigger UpdateSidebar notification, so load them after | 143 // It might trigger UpdateSidebar notification, so load them after |
111 // the registration. | 144 // the registration. |
112 host->LoadDefaults(); | 145 host->LoadDefaults(); |
113 } | 146 } |
114 | 147 |
115 host->Show(); | 148 host->Show(); |
116 | 149 |
117 ExtensionSidebarEventRouter::OnStateChanged( | 150 ExtensionSidebarEventRouter::OnStateChanged( |
118 tab->profile(), tab, content_id, | 151 tab->profile(), tab, content_id, |
119 extension_sidebar_constants::kShownState); | 152 extension_sidebar_constants::kShownState); |
120 } | 153 } |
121 | 154 |
122 void SidebarManager::ExpandSidebar(TabContents* tab, | 155 void SidebarManager::ExpandSidebar(TabContentsWrapper* tab, |
123 const std::string& content_id) { | 156 const std::string& content_id) { |
124 DCHECK(!content_id.empty()); | 157 DCHECK(!content_id.empty()); |
125 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); | 158 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
126 if (it == tab_to_sidebar_host_.end()) | 159 if (it == tab_to_sidebar_host_.end()) |
127 return; | 160 return; |
128 // If it's already active, bail out. | 161 // If it's already active, bail out. |
129 if (it->second.active_content_id == content_id) | 162 if (it->second.active_content_id == content_id) |
130 return; | 163 return; |
131 | 164 |
132 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 165 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
133 DCHECK(host); | |
134 if (!host) | 166 if (!host) |
135 return; | 167 return; |
136 it->second.active_content_id = content_id; | 168 it->second.active_content_id = content_id; |
137 | 169 |
138 host->Expand(); | 170 host->Expand(); |
139 } | 171 } |
140 | 172 |
141 void SidebarManager::CollapseSidebar(TabContents* tab, | 173 void SidebarManager::CollapseSidebar(TabContentsWrapper* tab, |
142 const std::string& content_id) { | 174 const std::string& content_id) { |
143 DCHECK(!content_id.empty()); | 175 DCHECK(!content_id.empty()); |
144 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); | 176 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
145 if (it == tab_to_sidebar_host_.end()) | 177 if (it == tab_to_sidebar_host_.end()) |
146 return; | 178 return; |
147 // If it's not the one active now, bail out. | 179 // If it's not the one active now, bail out. |
148 if (it->second.active_content_id != content_id) | 180 if (it->second.active_content_id != content_id) |
149 return; | 181 return; |
150 | 182 |
151 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 183 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
152 DCHECK(host); | |
153 if (!host) | 184 if (!host) |
154 return; | 185 return; |
155 it->second.active_content_id.clear(); | 186 it->second.active_content_id.clear(); |
156 | 187 |
157 host->Collapse(); | 188 host->Collapse(); |
158 } | 189 } |
159 | 190 |
160 void SidebarManager::HideSidebar(TabContents* tab, | 191 void SidebarManager::HideSidebar(TabContentsWrapper* tab, |
161 const std::string& content_id) { | 192 const std::string& content_id) { |
162 DCHECK(!content_id.empty()); | 193 DCHECK(!content_id.empty()); |
163 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); | 194 TabToSidebarHostMap::iterator it = tab_to_sidebar_host_.find(tab); |
164 if (it == tab_to_sidebar_host_.end()) | 195 if (it == tab_to_sidebar_host_.end()) |
165 return; | 196 return; |
166 if (it->second.active_content_id == content_id) | 197 if (it->second.active_content_id == content_id) |
167 it->second.active_content_id.clear(); | 198 it->second.active_content_id.clear(); |
168 | 199 |
169 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 200 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
170 DCHECK(host); | 201 if (!host) |
| 202 return; |
171 | 203 |
172 UnregisterSidebarContainerFor(tab, content_id); | 204 UnregisterSidebarContainerFor(tab, content_id); |
173 | 205 |
174 ExtensionSidebarEventRouter::OnStateChanged( | 206 ExtensionSidebarEventRouter::OnStateChanged( |
175 tab->profile(), tab, content_id, | 207 tab->profile(), tab, content_id, |
176 extension_sidebar_constants::kHiddenState); | 208 extension_sidebar_constants::kHiddenState); |
177 } | 209 } |
178 | 210 |
179 void SidebarManager::NavigateSidebar(TabContents* tab, | 211 void SidebarManager::NavigateSidebar(TabContentsWrapper* tab, |
180 const std::string& content_id, | 212 const std::string& content_id, |
181 const GURL& url) { | 213 const GURL& url) { |
182 DCHECK(!content_id.empty()); | 214 DCHECK(!content_id.empty()); |
183 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 215 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
184 if (!host) | 216 if (!host) |
185 return; | 217 return; |
186 | 218 |
187 host->Navigate(url); | 219 host->Navigate(url); |
188 } | 220 } |
189 | 221 |
190 void SidebarManager::SetSidebarBadgeText( | 222 void SidebarManager::SetSidebarBadgeText( |
191 TabContents* tab, const std::string& content_id, | 223 TabContentsWrapper* tab, const std::string& content_id, |
192 const string16& badge_text) { | 224 const string16& badge_text) { |
193 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 225 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
194 if (!host) | 226 if (!host) |
195 return; | 227 return; |
196 host->SetBadgeText(badge_text); | 228 host->SetBadgeText(badge_text); |
197 } | 229 } |
198 | 230 |
199 void SidebarManager::SetSidebarIcon( | 231 void SidebarManager::SetSidebarIcon( |
200 TabContents* tab, const std::string& content_id, | 232 TabContentsWrapper* tab, const std::string& content_id, |
201 const SkBitmap& bitmap) { | 233 const SkBitmap& bitmap) { |
202 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 234 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
203 if (!host) | 235 if (!host) |
204 return; | 236 return; |
205 host->SetIcon(bitmap); | 237 host->SetIcon(bitmap); |
206 } | 238 } |
207 | 239 |
208 void SidebarManager::SetSidebarTitle( | 240 void SidebarManager::SetSidebarTitle( |
209 TabContents* tab, const std::string& content_id, | 241 TabContentsWrapper* tab, const std::string& content_id, |
210 const string16& title) { | 242 const string16& title) { |
211 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 243 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
212 if (!host) | 244 if (!host) |
213 return; | 245 return; |
214 host->SetTitle(title); | 246 host->SetTitle(title); |
215 } | 247 } |
216 | 248 |
| 249 // SidebarManager, public. |
| 250 |
| 251 bool SidebarManager::IsAnySidebarDefinedFor(TabContentsWrapper* tab) { |
| 252 TabToSidebarHostMap::const_iterator it = tab_to_sidebar_host_.find(tab); |
| 253 if (it == tab_to_sidebar_host_.end()) |
| 254 return false; |
| 255 return !it->second.content_id_to_sidebar_host.empty(); |
| 256 } |
| 257 |
217 SidebarManager::~SidebarManager() { | 258 SidebarManager::~SidebarManager() { |
218 DCHECK(tab_to_sidebar_host_.empty()); | 259 DCHECK(tab_to_sidebar_host_.empty()); |
219 DCHECK(sidebar_host_to_tab_.empty()); | 260 DCHECK(sidebar_host_to_tab_.empty()); |
220 } | 261 } |
221 | 262 |
| 263 // NotificationObserver overrides. |
| 264 |
222 void SidebarManager::Observe(NotificationType type, | 265 void SidebarManager::Observe(NotificationType type, |
223 const NotificationSource& source, | 266 const NotificationSource& source, |
224 const NotificationDetails& details) { | 267 const NotificationDetails& details) { |
225 if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | 268 if (type == NotificationType::TAB_CONTENTS_DESTROYED) { |
226 HideAllSidebars(Source<TabContents>(source).ptr()); | 269 TabContents* tab_contents = Source<TabContents>(source).ptr(); |
| 270 HideAllSidebars(FindTabContentsWrapperFor(tab_contents)); |
227 } else { | 271 } else { |
228 NOTREACHED() << "Got a notification we didn't register for!"; | 272 NOTREACHED() << "Got a notification we didn't register for!"; |
229 } | 273 } |
230 } | 274 } |
231 | 275 |
232 void SidebarManager::UpdateSidebar(SidebarContainer* host) { | 276 // SidebarContainer::Delegate overrides. |
| 277 |
| 278 void SidebarManager::UpdateSidebar(SidebarChangedDetails* details) { |
233 NotificationService::current()->Notify( | 279 NotificationService::current()->Notify( |
234 NotificationType::SIDEBAR_CHANGED, | 280 NotificationType::SIDEBAR_CHANGED, |
235 Source<SidebarManager>(this), | 281 Source<SidebarManager>(this), |
236 Details<SidebarContainer>(host)); | 282 Details<SidebarChangedDetails>(details)); |
237 } | 283 } |
238 | 284 |
239 void SidebarManager::HideAllSidebars(TabContents* tab) { | 285 // SidebarManager, private. |
| 286 |
| 287 void SidebarManager::HideAllSidebars(TabContentsWrapper* tab) { |
240 TabToSidebarHostMap::iterator tab_it = tab_to_sidebar_host_.find(tab); | 288 TabToSidebarHostMap::iterator tab_it = tab_to_sidebar_host_.find(tab); |
241 if (tab_it == tab_to_sidebar_host_.end()) | 289 if (tab_it == tab_to_sidebar_host_.end()) |
242 return; | 290 return; |
243 const ContentIdToSidebarHostMap& hosts = | 291 const ContentIdToSidebarHostMap& hosts = |
244 tab_it->second.content_id_to_sidebar_host; | 292 tab_it->second.content_id_to_sidebar_host; |
245 | 293 |
246 std::vector<std::string> content_ids; | 294 std::vector<std::string> content_ids; |
247 for (ContentIdToSidebarHostMap::const_iterator it = hosts.begin(); | 295 for (ContentIdToSidebarHostMap::const_iterator it = hosts.begin(); |
248 it != hosts.end(); ++it) { | 296 it != hosts.end(); ++it) { |
249 content_ids.push_back(it->first); | 297 content_ids.push_back(it->first); |
250 } | 298 } |
251 | 299 |
252 for (std::vector<std::string>::iterator it = content_ids.begin(); | 300 for (std::vector<std::string>::iterator it = content_ids.begin(); |
253 it != content_ids.end(); ++it) { | 301 it != content_ids.end(); ++it) { |
254 HideSidebar(tab, *it); | 302 HideSidebar(tab, *it); |
255 } | 303 } |
256 } | 304 } |
257 | 305 |
258 SidebarContainer* SidebarManager::FindSidebarContainerFor( | 306 SidebarContainer* SidebarManager::FindSidebarContainerFor( |
259 TabContents* sidebar_contents) { | 307 TabContents* sidebar_contents) { |
260 for (SidebarHostToTabMap::iterator it = sidebar_host_to_tab_.begin(); | 308 for (SidebarHostToTabMap::iterator it = sidebar_host_to_tab_.begin(); |
261 it != sidebar_host_to_tab_.end(); | 309 it != sidebar_host_to_tab_.end(); |
262 ++it) { | 310 ++it) { |
263 if (sidebar_contents == it->first->sidebar_contents()) | 311 if (sidebar_contents == it->first->sidebar_contents()) |
264 return it->first; | 312 return it->first; |
265 } | 313 } |
266 return NULL; | 314 return NULL; |
267 } | 315 } |
268 | 316 |
| 317 TabContentsWrapper* SidebarManager::FindTabContentsWrapperFor( |
| 318 TabContents* tab_contents) { |
| 319 for (TabToSidebarHostMap::const_iterator it = tab_to_sidebar_host_.begin(); |
| 320 it != tab_to_sidebar_host_.end(); ++it) { |
| 321 if (it->first->tab_contents() == tab_contents) |
| 322 return it->first; |
| 323 } |
| 324 return NULL; |
| 325 } |
| 326 |
269 void SidebarManager::RegisterSidebarContainerFor( | 327 void SidebarManager::RegisterSidebarContainerFor( |
270 TabContents* tab, SidebarContainer* sidebar_host) { | 328 TabContentsWrapper* tab, SidebarContainer* sidebar_host) { |
271 DCHECK(!GetSidebarContainerFor(tab, sidebar_host->content_id())); | 329 DCHECK(!GetSidebarContainerFor(tab, sidebar_host->content_id())); |
272 | 330 |
273 // If it's a first sidebar for this tab, register destroy notification. | 331 // If it's a first sidebar for this tab, register destroy notification. |
274 if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { | 332 if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { |
275 registrar_.Add(this, | 333 registrar_.Add(this, |
276 NotificationType::TAB_CONTENTS_DESTROYED, | 334 NotificationType::TAB_CONTENTS_DESTROYED, |
277 Source<TabContents>(tab)); | 335 Source<TabContents>(tab->tab_contents())); |
278 } | 336 } |
279 | 337 |
280 BindSidebarHost(tab, sidebar_host); | 338 BindSidebarHost(tab, sidebar_host); |
281 } | 339 } |
282 | 340 |
283 void SidebarManager::UnregisterSidebarContainerFor( | 341 void SidebarManager::UnregisterSidebarContainerFor( |
284 TabContents* tab, const std::string& content_id) { | 342 TabContentsWrapper* tab, const std::string& content_id) { |
285 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); | 343 SidebarContainer* host = GetSidebarContainerFor(tab, content_id); |
286 DCHECK(host); | 344 DCHECK(host); |
287 if (!host) | 345 if (!host) |
288 return; | 346 return; |
289 | 347 |
290 UnbindSidebarHost(tab, host); | 348 UnbindSidebarHost(tab, host); |
291 | 349 |
292 // If there's no more sidebars linked to this tab, unsubscribe. | 350 // If there's no more sidebars linked to this tab, unsubscribe. |
293 if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { | 351 if (tab_to_sidebar_host_.find(tab) == tab_to_sidebar_host_.end()) { |
294 registrar_.Remove(this, | 352 registrar_.Remove(this, |
295 NotificationType::TAB_CONTENTS_DESTROYED, | 353 NotificationType::TAB_CONTENTS_DESTROYED, |
296 Source<TabContents>(tab)); | 354 Source<TabContents>(tab->tab_contents())); |
297 } | 355 } |
298 | 356 |
299 // Issue tab closing event post unbound. | 357 // Issue tab closing event post unbound. |
300 host->SidebarClosing(); | 358 host->SidebarClosing(); |
301 // Destroy sidebar container. | 359 // Destroy sidebar container. |
302 delete host; | 360 delete host; |
303 } | 361 } |
304 | 362 |
305 void SidebarManager::BindSidebarHost(TabContents* tab, | 363 void SidebarManager::BindSidebarHost(TabContentsWrapper* tab, |
306 SidebarContainer* sidebar_host) { | 364 SidebarContainer* sidebar_host) { |
307 const std::string& content_id = sidebar_host->content_id(); | 365 const std::string& content_id = sidebar_host->content_id(); |
308 | 366 |
309 DCHECK(GetSidebarContainerFor(tab, content_id) == NULL); | 367 DCHECK(GetSidebarContainerFor(tab, content_id) == NULL); |
310 DCHECK(sidebar_host_to_tab_.find(sidebar_host) == | 368 DCHECK(sidebar_host_to_tab_.find(sidebar_host) == |
311 sidebar_host_to_tab_.end()); | 369 sidebar_host_to_tab_.end()); |
312 | 370 |
313 tab_to_sidebar_host_[tab].content_id_to_sidebar_host[content_id] = | 371 tab_to_sidebar_host_[tab].content_id_to_sidebar_host[content_id] = |
314 sidebar_host; | 372 sidebar_host; |
315 sidebar_host_to_tab_[sidebar_host] = tab; | 373 sidebar_host_to_tab_[sidebar_host] = tab; |
316 } | 374 } |
317 | 375 |
318 void SidebarManager::UnbindSidebarHost(TabContents* tab, | 376 void SidebarManager::UnbindSidebarHost(TabContentsWrapper* tab, |
319 SidebarContainer* sidebar_host) { | 377 SidebarContainer* sidebar_host) { |
320 const std::string& content_id = sidebar_host->content_id(); | 378 const std::string& content_id = sidebar_host->content_id(); |
321 | 379 |
322 DCHECK(GetSidebarContainerFor(tab, content_id) == sidebar_host); | 380 DCHECK(GetSidebarContainerFor(tab, content_id) == sidebar_host); |
323 DCHECK(sidebar_host_to_tab_.find(sidebar_host)->second == tab); | 381 DCHECK(sidebar_host_to_tab_.find(sidebar_host)->second == tab); |
324 DCHECK(tab_to_sidebar_host_[tab].active_content_id != content_id); | 382 DCHECK(tab_to_sidebar_host_[tab].active_content_id != content_id); |
325 | 383 |
326 tab_to_sidebar_host_[tab].content_id_to_sidebar_host.erase(content_id); | 384 tab_to_sidebar_host_[tab].content_id_to_sidebar_host.erase(content_id); |
327 if (tab_to_sidebar_host_[tab].content_id_to_sidebar_host.empty()) | 385 if (tab_to_sidebar_host_[tab].content_id_to_sidebar_host.empty()) |
328 tab_to_sidebar_host_.erase(tab); | 386 tab_to_sidebar_host_.erase(tab); |
329 sidebar_host_to_tab_.erase(sidebar_host); | 387 sidebar_host_to_tab_.erase(sidebar_host); |
330 } | 388 } |
OLD | NEW |