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 #import "browser_actions_controller.h" | 5 #import "browser_actions_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/browser.h" | 10 #include "chrome/browser/browser.h" |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 } | 165 } |
166 | 166 |
167 - (void)createButtons { | 167 - (void)createButtons { |
168 // No extensions in incognito mode. | 168 // No extensions in incognito mode. |
169 if (!toolbarModel_) | 169 if (!toolbarModel_) |
170 return; | 170 return; |
171 | 171 |
172 NSUInteger i = 0; | 172 NSUInteger i = 0; |
173 for (ExtensionList::iterator iter = toolbarModel_->begin(); | 173 for (ExtensionList::iterator iter = toolbarModel_->begin(); |
174 iter != toolbarModel_->end(); ++iter) { | 174 iter != toolbarModel_->end(); ++iter) { |
| 175 if (![self shouldDisplayBrowserAction:*iter]) |
| 176 continue; |
| 177 |
175 [self createActionButtonForExtension:*iter withIndex:i++]; | 178 [self createActionButtonForExtension:*iter withIndex:i++]; |
176 } | 179 } |
177 | 180 |
178 CGFloat width = [self savedWidth]; | 181 CGFloat width = [self savedWidth]; |
179 // The width will never be 0 (due to the container's minimum size restriction) | 182 // The width will never be 0 (due to the container's minimum size restriction) |
180 // except when no width has been saved. In this case, set the width to be as | 183 // except when no width has been saved. In this case, set the width to be as |
181 // if all buttons are shown. | 184 // if all buttons are shown. |
182 if (width == 0) | 185 if (width == 0) |
183 width = [self containerWidthWithButtonCount:[self buttonCount]]; | 186 width = [self containerWidthWithButtonCount:[self buttonCount]]; |
184 | 187 |
185 [containerView_ resizeToWidth:width animate:NO]; | 188 [containerView_ resizeToWidth:width animate:NO]; |
186 } | 189 } |
187 | 190 |
188 - (void)createActionButtonForExtension:(Extension*)extension | 191 - (void)createActionButtonForExtension:(Extension*)extension |
189 withIndex:(NSUInteger)index { | 192 withIndex:(NSUInteger)index { |
190 if (!extension->browser_action()) | 193 if (!extension->browser_action()) |
191 return; | 194 return; |
192 | 195 |
193 if (![self shouldDisplayBrowserAction:extension]) | 196 if (![self shouldDisplayBrowserAction:extension]) |
194 return; | 197 return; |
195 | 198 |
| 199 if (profile_->IsOffTheRecord()) |
| 200 index = toolbarModel_->OriginalIndexToIncognito(index); |
| 201 |
196 // Show the container if it's the first button. Otherwise it will be shown | 202 // Show the container if it's the first button. Otherwise it will be shown |
197 // already. | 203 // already. |
198 if ([buttons_ count] == 0) | 204 if ([buttons_ count] == 0) |
199 [containerView_ setHidden:NO]; | 205 [containerView_ setHidden:NO]; |
200 | 206 |
201 BrowserActionButton* newButton = [[[BrowserActionButton alloc] | 207 BrowserActionButton* newButton = [[[BrowserActionButton alloc] |
202 initWithExtension:extension | 208 initWithExtension:extension |
203 profile:profile_ | 209 profile:profile_ |
204 tabId:[self currentTabId]] autorelease]; | 210 tabId:[self currentTabId]] autorelease]; |
205 [newButton setTarget:self]; | 211 [newButton setTarget:self]; |
(...skipping 12 matching lines...) Expand all Loading... |
218 | 224 |
219 - (void)removeActionButtonForExtension:(Extension*)extension { | 225 - (void)removeActionButtonForExtension:(Extension*)extension { |
220 if (!extension->browser_action()) | 226 if (!extension->browser_action()) |
221 return; | 227 return; |
222 | 228 |
223 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); | 229 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); |
224 if (!buttonKey) | 230 if (!buttonKey) |
225 return; | 231 return; |
226 | 232 |
227 BrowserActionButton* button = [buttons_ objectForKey:buttonKey]; | 233 BrowserActionButton* button = [buttons_ objectForKey:buttonKey]; |
228 if (!button) { | 234 // This could be the case in incognito, where only a subset of extensions are |
229 NOTREACHED(); | 235 // shown. |
| 236 if (!button) |
230 return; | 237 return; |
231 } | 238 |
232 [button removeFromSuperview]; | 239 [button removeFromSuperview]; |
233 [buttons_ removeObjectForKey:buttonKey]; | 240 [buttons_ removeObjectForKey:buttonKey]; |
234 if ([buttons_ count] == 0) { | 241 if ([buttons_ count] == 0) { |
235 // No more buttons? Hide the container. | 242 // No more buttons? Hide the container. |
236 [containerView_ setHidden:YES]; | 243 [containerView_ setHidden:YES]; |
237 } else { | 244 } else { |
238 [self repositionActionButtons]; | 245 [self repositionActionButtons]; |
239 } | 246 } |
240 [containerView_ setNeedsDisplay:YES]; | 247 [containerView_ setNeedsDisplay:YES]; |
241 } | 248 } |
242 | 249 |
243 - (void)repositionActionButtons { | 250 - (void)repositionActionButtons { |
244 NSUInteger i = 0; | 251 NSUInteger i = 0; |
245 for (ExtensionList::iterator iter = toolbarModel_->begin(); | 252 for (ExtensionList::iterator iter = toolbarModel_->begin(); |
246 iter != toolbarModel_->end(); ++iter) { | 253 iter != toolbarModel_->end(); ++iter) { |
| 254 if (![self shouldDisplayBrowserAction:*iter]) |
| 255 continue; |
| 256 |
247 CGFloat xOffset = kGrippyXOffset + | 257 CGFloat xOffset = kGrippyXOffset + |
248 (i * (kBrowserActionWidth + kBrowserActionButtonPadding)); | 258 (i * (kBrowserActionWidth + kBrowserActionButtonPadding)); |
249 NSString* extensionId = base::SysUTF8ToNSString((*iter)->id()); | 259 NSString* extensionId = base::SysUTF8ToNSString((*iter)->id()); |
250 DCHECK(extensionId); | 260 DCHECK(extensionId); |
251 if (!extensionId) | 261 if (!extensionId) |
252 continue; | 262 continue; |
253 BrowserActionButton* button = [buttons_ objectForKey:extensionId]; | 263 BrowserActionButton* button = [buttons_ objectForKey:extensionId]; |
254 NSRect buttonFrame = [button frame]; | 264 NSRect buttonFrame = [button frame]; |
255 buttonFrame.origin.x = xOffset; | 265 buttonFrame.origin.x = xOffset; |
256 [button setFrame:buttonFrame]; | 266 [button setFrame:buttonFrame]; |
(...skipping 10 matching lines...) Expand all Loading... |
267 return width; | 277 return width; |
268 } | 278 } |
269 | 279 |
270 // Resizes the container given the number of visible buttons in the container, | 280 // Resizes the container given the number of visible buttons in the container, |
271 // taking into account the size of the grippy. Also updates the persistent | 281 // taking into account the size of the grippy. Also updates the persistent |
272 // width preference. | 282 // width preference. |
273 - (void)resizeContainerWithAnimation:(BOOL)animate { | 283 - (void)resizeContainerWithAnimation:(BOOL)animate { |
274 CGFloat width = | 284 CGFloat width = |
275 [self containerWidthWithButtonCount:[self visibleButtonCount]]; | 285 [self containerWidthWithButtonCount:[self visibleButtonCount]]; |
276 [containerView_ resizeToWidth:width animate:animate]; | 286 [containerView_ resizeToWidth:width animate:animate]; |
277 profile_->GetPrefs()->SetReal(prefs::kBrowserActionContainerWidth, | 287 |
278 NSWidth([containerView_ frame])); | 288 if (!profile_->IsOffTheRecord()) |
| 289 profile_->GetPrefs()->SetReal(prefs::kBrowserActionContainerWidth, |
| 290 NSWidth([containerView_ frame])); |
279 | 291 |
280 [[NSNotificationCenter defaultCenter] | 292 [[NSNotificationCenter defaultCenter] |
281 postNotificationName:kBrowserActionVisibilityChangedNotification | 293 postNotificationName:kBrowserActionVisibilityChangedNotification |
282 object:self]; | 294 object:self]; |
283 } | 295 } |
284 | 296 |
285 - (void)updateButtonOpacityAndDragAbilities { | 297 - (void)updateButtonOpacityAndDragAbilities { |
286 for (BrowserActionButton* button in [buttons_ allValues]) { | 298 for (BrowserActionButton* button in [buttons_ allValues]) { |
287 NSRect buttonFrame = [button frame]; | 299 NSRect buttonFrame = [button frame]; |
288 buttonFrame.origin.x += kButtonOpacityLeadPadding; | 300 buttonFrame.origin.x += kButtonOpacityLeadPadding; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 iter != toolbarModel_->end(); ++iter) { | 417 iter != toolbarModel_->end(); ++iter) { |
406 if (i == index) | 418 if (i == index) |
407 return [buttons_ objectForKey:base::SysUTF8ToNSString((*iter)->id())]; | 419 return [buttons_ objectForKey:base::SysUTF8ToNSString((*iter)->id())]; |
408 | 420 |
409 ++i; | 421 ++i; |
410 } | 422 } |
411 return nil; | 423 return nil; |
412 } | 424 } |
413 | 425 |
414 - (bool)shouldDisplayBrowserAction:(Extension*)extension { | 426 - (bool)shouldDisplayBrowserAction:(Extension*)extension { |
| 427 // Only display incognito-enabled extensions while in incognito mode. |
415 return (!profile_->IsOffTheRecord() || | 428 return (!profile_->IsOffTheRecord() || |
416 profile_->GetExtensionsService()-> | 429 profile_->GetExtensionsService()-> |
417 IsIncognitoEnabled(extension->id())); | 430 IsIncognitoEnabled(extension->id())); |
418 } | 431 } |
419 | 432 |
420 - (CGFloat)savedWidth { | 433 - (CGFloat)savedWidth { |
| 434 // Don't use the standard saved width for incognito until a separate pref is |
| 435 // added. |
| 436 if (profile_->IsOffTheRecord()) |
| 437 return 0.0; |
| 438 |
421 return profile_->GetPrefs()->GetReal(prefs::kBrowserActionContainerWidth); | 439 return profile_->GetPrefs()->GetReal(prefs::kBrowserActionContainerWidth); |
422 } | 440 } |
423 | 441 |
424 + (void)registerUserPrefs:(PrefService*)prefs { | 442 + (void)registerUserPrefs:(PrefService*)prefs { |
425 prefs->RegisterRealPref(prefs::kBrowserActionContainerWidth, 0); | 443 prefs->RegisterRealPref(prefs::kBrowserActionContainerWidth, 0); |
426 } | 444 } |
427 | 445 |
428 @end | 446 @end |
OLD | NEW |