| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/mac/bundle_locations.h" | |
| 7 #include "base/mac/mac_util.h" | 6 #include "base/mac/mac_util.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" | 7 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/infobars/confirm_infobar_delegate.h" | 8 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
| 10 #include "chrome/browser/infobars/infobar.h" | 9 #include "chrome/browser/infobars/infobar.h" |
| 10 #include "chrome/browser/infobars/infobar_container.h" |
| 11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
| 12 #import "chrome/browser/ui/cocoa/animatable_view.h" | |
| 13 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 13 #import "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h" |
| 14 #import "chrome/browser/ui/cocoa/infobars/infobar_container_cocoa.h" |
| 14 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 15 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" |
| 15 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | 16 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" |
| 17 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 16 #import "chrome/browser/ui/cocoa/view_id_util.h" | 18 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 17 #include "content/public/browser/notification_details.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "skia/ext/skia_utils_mac.h" | |
| 20 | 19 |
| 21 // C++ class that receives INFOBAR_ADDED and INFOBAR_REMOVED | 20 @interface InfoBarContainerController () |
| 22 // notifications and proxies them back to |controller|. | 21 // Removes |controller| from the list of controllers in this container and |
| 23 class InfoBarNotificationObserver : public content::NotificationObserver { | 22 // removes its view from the view hierarchy. This method is safe to call while |
| 24 public: | 23 // |controller| is still on the call stack. |
| 25 InfoBarNotificationObserver(InfoBarContainerController* controller) | 24 - (void)removeController:(InfoBarController*)controller; |
| 26 : controller_(controller) { | |
| 27 } | |
| 28 | |
| 29 private: | |
| 30 // NotificationObserver implementation | |
| 31 virtual void Observe( | |
| 32 int type, | |
| 33 const content::NotificationSource& source, | |
| 34 const content::NotificationDetails& details) OVERRIDE { | |
| 35 InfoBarService* infobar_service = | |
| 36 content::Source<InfoBarService>(source).ptr(); | |
| 37 switch (type) { | |
| 38 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | |
| 39 [controller_ addInfoBar:content::Details<InfoBarAddedDetails>(details)-> | |
| 40 CreateInfoBar(infobar_service) | |
| 41 animate:YES]; | |
| 42 break; | |
| 43 | |
| 44 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { | |
| 45 InfoBarRemovedDetails* removed_details = | |
| 46 content::Details<InfoBarRemovedDetails>(details).ptr(); | |
| 47 [controller_ | |
| 48 closeInfoBarsForDelegate:removed_details->first | |
| 49 animate:(removed_details->second ? YES : NO)]; | |
| 50 break; | |
| 51 } | |
| 52 | |
| 53 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { | |
| 54 InfoBarReplacedDetails* replaced_details = | |
| 55 content::Details<InfoBarReplacedDetails>(details).ptr(); | |
| 56 [controller_ closeInfoBarsForDelegate:replaced_details->first | |
| 57 animate:NO]; | |
| 58 [controller_ addInfoBar:replaced_details->second-> | |
| 59 CreateInfoBar(infobar_service) | |
| 60 animate:NO]; | |
| 61 break; | |
| 62 } | |
| 63 | |
| 64 default: | |
| 65 NOTREACHED(); // we don't ask for anything else! | |
| 66 break; | |
| 67 } | |
| 68 | |
| 69 [controller_ positionInfoBarsAndRedraw]; | |
| 70 } | |
| 71 | |
| 72 InfoBarContainerController* controller_; // weak, owns us. | |
| 73 }; | |
| 74 | |
| 75 | |
| 76 @interface InfoBarContainerController (PrivateMethods) | |
| 77 // Returns the desired height of the container view, computed by | |
| 78 // adding together the heights of all its subviews. | |
| 79 - (CGFloat)desiredHeight; | |
| 80 | |
| 81 @end | 25 @end |
| 82 | 26 |
| 83 | 27 |
| 84 @implementation InfoBarContainerController | 28 @implementation InfoBarContainerController |
| 85 | 29 |
| 86 @synthesize shouldSuppressTopInfoBarTip = shouldSuppressTopInfoBarTip_; | 30 @synthesize shouldSuppressTopInfoBarTip = shouldSuppressTopInfoBarTip_; |
| 87 | 31 |
| 88 - (id)initWithResizeDelegate:(id<ViewResizer>)resizeDelegate { | 32 - (id)initWithResizeDelegate:(id<ViewResizer>)resizeDelegate { |
| 89 DCHECK(resizeDelegate); | 33 DCHECK(resizeDelegate); |
| 90 if ((self = [super initWithNibName:@"InfoBarContainer" | 34 if ((self = [super initWithNibName:nil bundle:nil])) { |
| 91 bundle:base::mac::FrameworkBundle()])) { | 35 base::scoped_nsobject<NSView> view( |
| 36 [[NSView alloc] initWithFrame:NSZeroRect]); |
| 37 [view setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin]; |
| 38 view_id_util::SetID(view, VIEW_ID_INFO_BAR_CONTAINER); |
| 39 [self setView:view]; |
| 40 |
| 92 resizeDelegate_ = resizeDelegate; | 41 resizeDelegate_ = resizeDelegate; |
| 93 infoBarObserver_.reset(new InfoBarNotificationObserver(self)); | 42 containerCocoa_.reset(new InfoBarContainerCocoa(self)); |
| 94 | 43 infobarControllers_.reset([[NSMutableArray alloc] init]); |
| 95 // NSMutableArray needs an initial capacity, and we rarely ever see | |
| 96 // more than two infobars at a time, so that seems like a good choice. | |
| 97 infobarControllers_.reset([[NSMutableArray alloc] initWithCapacity:2]); | |
| 98 closingInfoBars_.reset([[NSMutableSet alloc] initWithCapacity:2]); | |
| 99 } | 44 } |
| 100 return self; | 45 return self; |
| 101 } | 46 } |
| 102 | 47 |
| 103 - (void)dealloc { | 48 - (void)dealloc { |
| 49 // Delete the container so that any remaining infobars are removed. |
| 50 containerCocoa_.reset(); |
| 104 DCHECK_EQ([infobarControllers_ count], 0U); | 51 DCHECK_EQ([infobarControllers_ count], 0U); |
| 105 DCHECK_EQ([closingInfoBars_ count], 0U); | |
| 106 view_id_util::UnsetID([self view]); | 52 view_id_util::UnsetID([self view]); |
| 107 [super dealloc]; | 53 [super dealloc]; |
| 108 } | 54 } |
| 109 | 55 |
| 110 - (void)awakeFromNib { | |
| 111 // The info bar container view is an ordinary NSView object, so we set its | |
| 112 // ViewID here. | |
| 113 view_id_util::SetID([self view], VIEW_ID_INFO_BAR_CONTAINER); | |
| 114 } | |
| 115 | |
| 116 - (void)willRemoveController:(InfoBarController*)controller { | |
| 117 [closingInfoBars_ addObject:controller]; | |
| 118 } | |
| 119 | |
| 120 - (void)removeController:(InfoBarController*)controller { | |
| 121 if (![infobarControllers_ containsObject:controller]) | |
| 122 return; | |
| 123 | |
| 124 // This code can be executed while InfoBarController is still on the stack, so | |
| 125 // we retain and autorelease the controller to prevent it from being | |
| 126 // dealloc'ed too early. | |
| 127 [[controller retain] autorelease]; | |
| 128 [[controller view] removeFromSuperview]; | |
| 129 [infobarControllers_ removeObject:controller]; | |
| 130 [closingInfoBars_ removeObject:controller]; | |
| 131 [self positionInfoBarsAndRedraw]; | |
| 132 } | |
| 133 | |
| 134 - (BrowserWindowController*)browserWindowController { | 56 - (BrowserWindowController*)browserWindowController { |
| 135 id controller = [[[self view] window] windowController]; | 57 id controller = [[[self view] window] windowController]; |
| 136 if (![controller isKindOfClass:[BrowserWindowController class]]) | 58 if (![controller isKindOfClass:[BrowserWindowController class]]) |
| 137 return nil; | 59 return nil; |
| 138 return controller; | 60 return controller; |
| 139 } | 61 } |
| 140 | 62 |
| 63 - (CGFloat)infobarArrowX { |
| 64 LocationBarViewMac* locationBar = |
| 65 [[self browserWindowController] locationBarBridge]; |
| 66 return locationBar->GetPageInfoBubblePoint().x; |
| 67 } |
| 68 |
| 141 - (void)changeWebContents:(content::WebContents*)contents { | 69 - (void)changeWebContents:(content::WebContents*)contents { |
| 142 registrar_.RemoveAll(); | 70 if (contents) { |
| 143 [self removeAllInfoBars]; | 71 containerCocoa_->ChangeInfoBarService( |
| 144 | 72 InfoBarService::FromWebContents(contents)); |
| 145 currentWebContents_ = contents; | 73 } else { |
| 146 if (currentWebContents_) { | 74 containerCocoa_->ChangeInfoBarService(NULL); |
| 147 InfoBarService* infobarService = | |
| 148 InfoBarService::FromWebContents(currentWebContents_); | |
| 149 for (size_t i = 0; i < infobarService->infobar_count(); ++i) { | |
| 150 InfoBar* infobar = | |
| 151 infobarService->infobar_at(i)->CreateInfoBar(infobarService); | |
| 152 [self addInfoBar:infobar animate:NO]; | |
| 153 } | |
| 154 | |
| 155 content::Source<InfoBarService> source(infobarService); | |
| 156 registrar_.Add(infoBarObserver_.get(), | |
| 157 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, source); | |
| 158 registrar_.Add(infoBarObserver_.get(), | |
| 159 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, source); | |
| 160 registrar_.Add(infoBarObserver_.get(), | |
| 161 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, source); | |
| 162 } | 75 } |
| 163 | |
| 164 [self positionInfoBarsAndRedraw]; | |
| 165 } | 76 } |
| 166 | 77 |
| 167 - (void)tabDetachedWithContents:(content::WebContents*)contents { | 78 - (void)tabDetachedWithContents:(content::WebContents*)contents { |
| 168 if (currentWebContents_ == contents) | 79 if (currentWebContents_ == contents) |
| 169 [self changeWebContents:NULL]; | 80 [self changeWebContents:NULL]; |
| 170 } | 81 } |
| 171 | 82 |
| 172 - (NSUInteger)infobarCount { | 83 - (CGFloat)overlappingTipHeight { |
| 173 return [infobarControllers_ count] - [closingInfoBars_ count]; | 84 return containerCocoa_->GetVerticalOverlap(NULL); |
| 174 } | 85 } |
| 175 | 86 |
| 176 - (CGFloat)overlappingTipHeight { | 87 - (void)addInfoBar:(InfoBarCocoa*)infobar |
| 177 return [self infobarCount] ? infobars::kTipHeight : 0; | 88 position:(NSUInteger)position { |
| 89 InfoBarController* controller = infobar->controller(); |
| 90 [controller setContainerController:self]; |
| 91 [infobarControllers_ insertObject:controller atIndex:position]; |
| 92 |
| 93 NSView* relativeView = nil; |
| 94 if (position > 0) |
| 95 relativeView = [[infobarControllers_ objectAtIndex:position - 1] view]; |
| 96 [[self view] addSubview:[controller view] |
| 97 positioned:NSWindowAbove |
| 98 relativeTo:relativeView]; |
| 178 } | 99 } |
| 179 | 100 |
| 180 - (void)resizeView:(NSView*)view newHeight:(CGFloat)height { | 101 - (void)removeInfoBar:(InfoBarCocoa*)infobar { |
| 181 NSRect frame = [view frame]; | 102 [infobar->controller() infobarWillClose]; |
| 182 frame.size.height = height; | 103 [self removeController:infobar->controller()]; |
| 183 [view setFrame:frame]; | 104 base::MessageLoop::current()->DeleteSoon(FROM_HERE, infobar); |
| 184 [self positionInfoBarsAndRedraw]; | |
| 185 } | 105 } |
| 186 | 106 |
| 187 - (void)setAnimationInProgress:(BOOL)inProgress { | 107 - (void)positionInfoBarsAndRedraw:(BOOL)isAnimating { |
| 188 if ([resizeDelegate_ respondsToSelector:@selector(setAnimationInProgress:)]) | 108 if (isAnimating_ != isAnimating) { |
| 189 [resizeDelegate_ setAnimationInProgress:inProgress]; | 109 isAnimating_ = isAnimating; |
| 190 } | 110 if ([resizeDelegate_ respondsToSelector:@selector(setAnimationInProgress:)]) |
| 111 [resizeDelegate_ setAnimationInProgress:isAnimating_]; |
| 112 } |
| 191 | 113 |
| 192 - (void)setShouldSuppressTopInfoBarTip:(BOOL)flag { | |
| 193 if (shouldSuppressTopInfoBarTip_ == flag) | |
| 194 return; | |
| 195 shouldSuppressTopInfoBarTip_ = flag; | |
| 196 [self positionInfoBarsAndRedraw]; | |
| 197 } | |
| 198 | |
| 199 @end | |
| 200 | |
| 201 @implementation InfoBarContainerController (PrivateMethods) | |
| 202 | |
| 203 - (CGFloat)desiredHeight { | |
| 204 CGFloat height = 0; | |
| 205 | |
| 206 // Take out the height of the tip from the total size of the infobar so that | |
| 207 // the tip overlaps the preceding infobar when there is more than one infobar. | |
| 208 for (InfoBarController* controller in infobarControllers_.get()) | |
| 209 height += NSHeight([[controller view] frame]) - infobars::kTipHeight; | |
| 210 | |
| 211 // If there are any infobars, add a little extra room for the tip of the first | |
| 212 // infobar. | |
| 213 if (height) | |
| 214 height += infobars::kTipHeight; | |
| 215 | |
| 216 return height; | |
| 217 } | |
| 218 | |
| 219 - (void)addInfoBar:(InfoBar*)infobar animate:(BOOL)animate { | |
| 220 InfoBarController* controller = infobar->controller(); | |
| 221 [controller setContainerController:self]; | |
| 222 [[controller animatableView] setResizeDelegate:self]; | |
| 223 [[self view] addSubview:[controller view]]; | |
| 224 [infobarControllers_ addObject:[controller autorelease]]; | |
| 225 | |
| 226 if (animate) | |
| 227 [controller animateOpen]; | |
| 228 else | |
| 229 [controller open]; | |
| 230 | |
| 231 delete infobar; | |
| 232 } | |
| 233 | |
| 234 - (void)closeInfoBarsForDelegate:(InfoBarDelegate*)delegate | |
| 235 animate:(BOOL)animate { | |
| 236 for (InfoBarController* controller in | |
| 237 [NSArray arrayWithArray:infobarControllers_.get()]) { | |
| 238 if ([controller delegate] == delegate) { | |
| 239 [controller infobarWillClose]; | |
| 240 if (animate) | |
| 241 [controller animateClosed]; | |
| 242 else | |
| 243 [controller close]; | |
| 244 } | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 - (void)removeAllInfoBars { | |
| 249 // stopAnimation can remove the infobar from infobarControllers_ if it was in | |
| 250 // the midst of closing, so copy the array so mutations won't cause problems. | |
| 251 for (InfoBarController* controller in | |
| 252 [NSArray arrayWithArray:infobarControllers_.get()]) { | |
| 253 [[controller animatableView] stopAnimation]; | |
| 254 // This code can be executed while InfoBarController is still on the stack, | |
| 255 // so we retain and autorelease the controller to prevent it from being | |
| 256 // dealloc'ed too early. | |
| 257 [[controller retain] autorelease]; | |
| 258 [[controller view] removeFromSuperview]; | |
| 259 } | |
| 260 [infobarControllers_ removeAllObjects]; | |
| 261 [closingInfoBars_ removeAllObjects]; | |
| 262 } | |
| 263 | |
| 264 - (void)positionInfoBarsAndRedraw { | |
| 265 NSRect containerBounds = [[self view] bounds]; | 114 NSRect containerBounds = [[self view] bounds]; |
| 266 int minY = 0; | 115 int minY = 0; |
| 267 | 116 |
| 268 // Stack the infobars at the bottom of the view, starting with the | 117 // Stack the infobars at the bottom of the view, starting with the |
| 269 // last infobar and working our way to the front of the array. This | 118 // last infobar and working our way to the front of the array. This |
| 270 // way we ensure that the first infobar added shows up on top, with | 119 // way we ensure that the first infobar added shows up on top, with |
| 271 // the others below. | 120 // the others below. |
| 272 for (InfoBarController* controller in | 121 for (InfoBarController* controller in |
| 273 [infobarControllers_ reverseObjectEnumerator]) { | 122 [infobarControllers_ reverseObjectEnumerator]) { |
| 274 NSView* view = [controller view]; | 123 NSRect frame; |
| 275 NSRect frame = [view frame]; | |
| 276 frame.origin.x = NSMinX(containerBounds); | 124 frame.origin.x = NSMinX(containerBounds); |
| 277 frame.origin.y = minY; | 125 frame.origin.y = minY; |
| 278 frame.size.width = NSWidth(containerBounds); | 126 frame.size.width = NSWidth(containerBounds); |
| 279 [view setFrame:frame]; | 127 frame.size.height = [controller infobar]->total_height(); |
| 128 [[controller view] setFrame:frame]; |
| 280 | 129 |
| 281 minY += NSHeight(frame) - infobars::kTipHeight; | 130 minY += NSHeight(frame) - [controller infobar]->arrow_height(); |
| 282 | 131 [controller layoutArrow]; |
| 283 BOOL isTop = [controller isEqual:[infobarControllers_ objectAtIndex:0]]; | |
| 284 [controller setHasTip:!shouldSuppressTopInfoBarTip_ || !isTop]; | |
| 285 } | 132 } |
| 286 | 133 |
| 287 [resizeDelegate_ resizeView:[self view] newHeight:[self desiredHeight]]; | 134 int totalHeight = 0; |
| 135 int overlap = containerCocoa_->GetVerticalOverlap(&totalHeight); |
| 136 |
| 137 if (NSHeight([[self view] frame]) != totalHeight) { |
| 138 [resizeDelegate_ resizeView:[self view] newHeight:totalHeight]; |
| 139 } else if (oldOverlappingTipHeight_ != overlap) { |
| 140 // If the infobar overlap changed but the height didn't change then |
| 141 // explicitly ask for a layout. |
| 142 [[self browserWindowController] layoutInfoBar]; |
| 143 } |
| 144 oldOverlappingTipHeight_ = overlap; |
| 145 } |
| 146 |
| 147 - (void)setShouldSuppressTopInfoBarTip:(BOOL)flag { |
| 148 if (shouldSuppressTopInfoBarTip_ == flag) |
| 149 return; |
| 150 shouldSuppressTopInfoBarTip_ = flag; |
| 151 [self positionInfoBarsAndRedraw:isAnimating_]; |
| 152 } |
| 153 |
| 154 - (void)removeController:(InfoBarController*)controller { |
| 155 if (![infobarControllers_ containsObject:controller]) |
| 156 return; |
| 157 |
| 158 // This code can be executed while InfoBarController is still on the stack, so |
| 159 // we retain and autorelease the controller to prevent it from being |
| 160 // dealloc'ed too early. |
| 161 [[controller retain] autorelease]; |
| 162 [[controller view] removeFromSuperview]; |
| 163 [infobarControllers_ removeObject:controller]; |
| 288 } | 164 } |
| 289 | 165 |
| 290 @end | 166 @end |
| OLD | NEW |