Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/ui/cocoa/infobars/extension_infobar_controller.mm

Issue 23338005: Mac InfoBar: Use cross platform infobar classes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/ui/cocoa/infobars/extension_infobar_controller.h" 5 #import "chrome/browser/ui/cocoa/infobars/extension_infobar_controller.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "chrome/browser/extensions/extension_host.h" 9 #include "chrome/browser/extensions/extension_host.h"
10 #include "chrome/browser/extensions/extension_infobar_delegate.h" 10 #include "chrome/browser/extensions/extension_infobar_delegate.h"
11 #include "chrome/browser/extensions/image_loader.h" 11 #include "chrome/browser/extensions/image_loader.h"
12 #include "chrome/browser/infobars/infobar_service.h" 12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/ui/browser_finder.h" 13 #include "chrome/browser/ui/browser_finder.h"
14 #import "chrome/browser/ui/cocoa/animatable_view.h" 14 #import "chrome/browser/ui/cocoa/animatable_view.h"
15 #import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_contro ller.h" 15 #import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_contro ller.h"
16 #include "chrome/browser/ui/cocoa/infobars/infobar.h" 16 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
17 #import "chrome/browser/ui/cocoa/menu_button.h" 17 #import "chrome/browser/ui/cocoa/menu_button.h"
18 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/extension_constants.h" 19 #include "chrome/common/extensions/extension_constants.h"
20 #include "chrome/common/extensions/extension_icon_set.h" 20 #include "chrome/common/extensions/extension_icon_set.h"
21 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" 21 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
22 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h" 23 #include "content/public/browser/web_contents_view.h"
24 #include "extensions/common/extension_resource.h" 24 #include "extensions/common/extension_resource.h"
25 #include "grit/theme_resources.h" 25 #include "grit/theme_resources.h"
26 #include "skia/ext/skia_utils_mac.h" 26 #include "skia/ext/skia_utils_mac.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 ExtensionInfoBarDelegate* delegate_; 134 ExtensionInfoBarDelegate* delegate_;
135 135
136 base::WeakPtrFactory<InfobarBridge> weak_ptr_factory_; 136 base::WeakPtrFactory<InfobarBridge> weak_ptr_factory_;
137 137
138 DISALLOW_COPY_AND_ASSIGN(InfobarBridge); 138 DISALLOW_COPY_AND_ASSIGN(InfobarBridge);
139 }; 139 };
140 140
141 141
142 @implementation ExtensionInfoBarController 142 @implementation ExtensionInfoBarController
143 143
144 - (id)initWithDelegate:(InfoBarDelegate*)delegate 144 - (id)initWithInfoBar:(InfoBarCocoa*)infobar
145 owner:(InfoBarService*)owner 145 window:(NSWindow*)window {
146 window:(NSWindow*)window { 146 if ((self = [super initWithInfoBar:infobar])) {
147 if ((self = [super initWithDelegate:delegate owner:owner])) {
148 window_ = window; 147 window_ = window;
149 dropdownButton_.reset([[MenuButton alloc] init]); 148 dropdownButton_.reset([[MenuButton alloc] init]);
150 [dropdownButton_ setOpenMenuOnClick:YES]; 149 [dropdownButton_ setOpenMenuOnClick:YES];
151 150
152 extensions::ExtensionHost* extensionHost = 151 extensions::ExtensionHost* extensionHost =
153 delegate_->AsExtensionInfoBarDelegate()->extension_host(); 152 [self delegate]->AsExtensionInfoBarDelegate()->extension_host();
154 Browser* browser = 153 Browser* browser = chrome::FindBrowserWithWebContents(
155 chrome::FindBrowserWithWebContents(owner->web_contents()); 154 [self infobar]->OwnerCocoa()->web_contents());
156 contextMenuController_.reset([[ExtensionActionContextMenuController alloc] 155 contextMenuController_.reset([[ExtensionActionContextMenuController alloc]
157 initWithExtension:extensionHost->extension() 156 initWithExtension:extensionHost->extension()
158 browser:browser 157 browser:browser
159 extensionAction:NULL]); 158 extensionAction:NULL]);
160 159
161 base::scoped_nsobject<NSMenu> contextMenu( 160 base::scoped_nsobject<NSMenu> contextMenu(
162 [[NSMenu alloc] initWithTitle:@""]); 161 [[NSMenu alloc] initWithTitle:@""]);
163 [contextMenu setDelegate:self]; 162 [contextMenu setDelegate:self];
164 // See menu_button.h for documentation on why this is needed. 163 // See menu_button.h for documentation on why this is needed.
165 [contextMenu addItemWithTitle:@"" action:NULL keyEquivalent:@""]; 164 [contextMenu addItemWithTitle:@"" action:NULL keyEquivalent:@""];
166 [dropdownButton_ setAttachedMenu:contextMenu]; 165 [dropdownButton_ setAttachedMenu:contextMenu];
167 166
168 bridge_.reset(new InfobarBridge(self)); 167 bridge_.reset(new InfobarBridge(self));
169 } 168 }
170 return self; 169 return self;
171 } 170 }
172 171
173 - (void)dealloc { 172 - (void)dealloc {
174 [[NSNotificationCenter defaultCenter] removeObserver:self]; 173 [[NSNotificationCenter defaultCenter] removeObserver:self];
175 [super dealloc]; 174 [super dealloc];
176 } 175 }
177 176
178 - (void)addAdditionalControls { 177 - (void)addAdditionalControls {
179 [self removeButtons]; 178 [self removeButtons];
180 179
181 extensionView_ = delegate_->AsExtensionInfoBarDelegate()->extension_host()-> 180 extensionView_ = [self delegate]->AsExtensionInfoBarDelegate()
182 view()->native_view(); 181 ->extension_host()->view()->native_view();
183 182
184 // Add the extension's RenderWidgetHostView to the view hierarchy of the 183 // Add the extension's RenderWidgetHostView to the view hierarchy of the
185 // InfoBar and make sure to place it below the Close button. 184 // InfoBar and make sure to place it below the Close button.
186 [infoBarView_ addSubview:extensionView_ 185 [infoBarView_ addSubview:extensionView_
187 positioned:NSWindowBelow 186 positioned:NSWindowBelow
188 relativeTo:(NSView*)closeButton_]; 187 relativeTo:(NSView*)closeButton_];
189 188
190 // Add the context menu button to the hierarchy. 189 // Add the context menu button to the hierarchy.
191 [dropdownButton_ setShowsBorderOnlyWhileMouseInside:YES]; 190 [dropdownButton_ setShowsBorderOnlyWhileMouseInside:YES];
192 CGFloat buttonY = 191 CGFloat buttonY =
(...skipping 10 matching lines...) Expand all
203 NSRect extensionFrame = [extensionView_ frame]; 202 NSRect extensionFrame = [extensionView_ frame];
204 extensionFrame.origin.y = kBottomBorderHeightPx; 203 extensionFrame.origin.y = kBottomBorderHeightPx;
205 204
206 [extensionView_ setFrame:extensionFrame]; 205 [extensionView_ setFrame:extensionFrame];
207 // The extension's native view will only have a height that is non-zero if it 206 // The extension's native view will only have a height that is non-zero if it
208 // already has been loaded and rendered, which is the case when you switch 207 // already has been loaded and rendered, which is the case when you switch
209 // back to a tab with an extension infobar within it. The reason this is 208 // back to a tab with an extension infobar within it. The reason this is
210 // needed is because the extension view's frame will not have changed in the 209 // needed is because the extension view's frame will not have changed in the
211 // above case, so the NSViewFrameDidChangeNotification registered below will 210 // above case, so the NSViewFrameDidChangeNotification registered below will
212 // never fire. 211 // never fire.
213 if (NSHeight(extensionFrame) > 0.0) { 212 if (NSHeight(extensionFrame) > 0.0)
214 NSSize infoBarSize = [[self view] frame].size; 213 [self infobar]->SetBarTargetHeight([self clampedExtensionViewHeight]);
215 infoBarSize.height = [self clampedExtensionViewHeight] +
216 kBottomBorderHeightPx;
217 [[self view] setFrameSize:infoBarSize];
218 [infoBarView_ setFrameSize:infoBarSize];
219 }
220 214
221 [self adjustExtensionViewSize]; 215 [self adjustExtensionViewSize];
222 216
223 // These two notification handlers are here to ensure the width of the 217 // These two notification handlers are here to ensure the width of the
224 // native extension view is the same as the browser window's width and that 218 // native extension view is the same as the browser window's width and that
225 // the parent infobar view matches the height of the extension's native view. 219 // the parent infobar view matches the height of the extension's native view.
226 [[NSNotificationCenter defaultCenter] 220 [[NSNotificationCenter defaultCenter]
227 addObserver:self 221 addObserver:self
228 selector:@selector(extensionViewFrameChanged) 222 selector:@selector(extensionViewFrameChanged)
229 name:NSViewFrameDidChangeNotification 223 name:NSViewFrameDidChangeNotification
230 object:extensionView_]; 224 object:extensionView_];
231 225
232 [[NSNotificationCenter defaultCenter] 226 [[NSNotificationCenter defaultCenter]
233 addObserver:self 227 addObserver:self
234 selector:@selector(adjustExtensionViewSize) 228 selector:@selector(adjustExtensionViewSize)
235 name:NSWindowDidResizeNotification 229 name:NSWindowDidResizeNotification
236 object:window_]; 230 object:window_];
237 } 231 }
238 232
239 - (void)infobarWillClose { 233 - (void)infobarWillClose {
240 [self disablePopUpMenu:[dropdownButton_ menu]]; 234 [self disablePopUpMenu:[dropdownButton_ menu]];
241 [super infobarWillClose]; 235 [super infobarWillClose];
242 } 236 }
243 237
244 - (void)extensionViewFrameChanged { 238 - (void)extensionViewFrameChanged {
245 [self adjustExtensionViewSize]; 239 [self adjustExtensionViewSize];
246 240 [self infobar]->SetBarTargetHeight([self clampedExtensionViewHeight]);
247 AnimatableView* view = [self animatableView];
248 NSRect infoBarFrame = [view frame];
249 CGFloat newHeight = [self clampedExtensionViewHeight] + kBottomBorderHeightPx;
250 [infoBarView_ setPostsFrameChangedNotifications:NO];
251 infoBarFrame.size.height = newHeight;
252 [infoBarView_ setFrame:infoBarFrame];
253 [infoBarView_ setPostsFrameChangedNotifications:YES];
254 [view animateToNewHeight:newHeight duration:kAnimationDuration];
255 } 241 }
256 242
257 - (CGFloat)clampedExtensionViewHeight { 243 - (CGFloat)clampedExtensionViewHeight {
258 CGFloat height = delegate_->AsExtensionInfoBarDelegate()->height(); 244 CGFloat height = [self delegate]->AsExtensionInfoBarDelegate()->height();
259 return std::max(kToolbarMinHeightPx, std::min(height, kToolbarMaxHeightPx)); 245 return std::max(kToolbarMinHeightPx, std::min(height, kToolbarMaxHeightPx));
260 } 246 }
261 247
262 - (void)adjustExtensionViewSize { 248 - (void)adjustExtensionViewSize {
263 [extensionView_ setPostsFrameChangedNotifications:NO]; 249 [extensionView_ setPostsFrameChangedNotifications:NO];
264 NSSize extensionViewSize = [extensionView_ frame].size; 250 NSSize extensionViewSize = [extensionView_ frame].size;
265 extensionViewSize.width = NSWidth([window_ frame]); 251 extensionViewSize.width = NSWidth([window_ frame]);
266 extensionViewSize.height = [self clampedExtensionViewHeight]; 252 extensionViewSize.height = [self clampedExtensionViewHeight];
267 [extensionView_ setFrameSize:extensionViewSize]; 253 [extensionView_ setFrameSize:extensionViewSize];
268 [extensionView_ setPostsFrameChangedNotifications:YES]; 254 [extensionView_ setPostsFrameChangedNotifications:YES];
269 } 255 }
270 256
271 - (void)setButtonImage:(NSImage*)image { 257 - (void)setButtonImage:(NSImage*)image {
272 [dropdownButton_ setImage:image]; 258 [dropdownButton_ setImage:image];
273 } 259 }
274 260
275 - (void)menuNeedsUpdate:(NSMenu*)menu { 261 - (void)menuNeedsUpdate:(NSMenu*)menu {
276 [menu removeAllItems]; 262 [menu removeAllItems];
277 [contextMenuController_ populateMenu:menu]; 263 [contextMenuController_ populateMenu:menu];
278 } 264 }
279 265
280 @end 266 @end
281 267
282 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { 268 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar(InfoBarService* owner) {
269 scoped_ptr<InfoBarCocoa> infobar(new InfoBarCocoa(owner, this));
283 NSWindow* window = 270 NSWindow* window =
284 [(NSView*)owner->web_contents()->GetView()->GetContentNativeView() 271 [(NSView*)owner->web_contents()->GetView()->GetContentNativeView()
285 window]; 272 window];
286 ExtensionInfoBarController* controller = 273 base::scoped_nsobject<ExtensionInfoBarController> controller(
287 [[ExtensionInfoBarController alloc] initWithDelegate:this 274 [[ExtensionInfoBarController alloc] initWithInfoBar:infobar.get()
288 owner:owner 275 window:window]);
289 window:window]; 276 infobar->set_controller(controller);
290 return new InfoBar(controller, this); 277 return infobar.release();
291 } 278 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698