Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / browser_window_cocoa.mm
blobc84172546fe62ede78d5ccb5ca8f5b03d066ee77
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #import "base/mac/sdk_forward_declarations.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/download/download_shelf.h"
18 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/fullscreen.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/shell_integration.h"
22 #include "chrome/browser/signin/signin_header_helper.h"
23 #include "chrome/browser/translate/chrome_translate_client.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_command_controller.h"
26 #include "chrome/browser/ui/browser_commands_mac.h"
27 #include "chrome/browser/ui/browser_list.h"
28 #include "chrome/browser/ui/browser_window_state.h"
29 #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h"
30 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
31 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
32 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
33 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
34 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
35 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h"
36 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
37 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h"
38 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
39 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
40 #import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h"
41 #import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h"
42 #include "chrome/browser/ui/cocoa/restart_browser.h"
43 #include "chrome/browser/ui/cocoa/status_bubble_mac.h"
44 #include "chrome/browser/ui/cocoa/task_manager_mac.h"
45 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
46 #import "chrome/browser/ui/cocoa/web_dialog_window_controller.h"
47 #import "chrome/browser/ui/cocoa/website_settings/website_settings_bubble_controller.h"
48 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
49 #include "chrome/browser/ui/search/search_model.h"
50 #include "chrome/browser/ui/tabs/tab_strip_model.h"
51 #include "chrome/browser/web_applications/web_app.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/pref_names.h"
54 #include "chrome/grit/generated_resources.h"
55 #include "components/translate/core/browser/language_state.h"
56 #include "content/public/browser/native_web_keyboard_event.h"
57 #include "content/public/browser/notification_details.h"
58 #include "content/public/browser/notification_service.h"
59 #include "content/public/browser/notification_source.h"
60 #include "content/public/browser/web_contents.h"
61 #include "extensions/browser/extension_registry.h"
62 #include "extensions/browser/extension_system.h"
63 #include "extensions/common/constants.h"
64 #include "ui/base/l10n/l10n_util_mac.h"
65 #include "ui/events/keycodes/keyboard_codes.h"
66 #include "ui/gfx/geometry/rect.h"
68 #if defined(ENABLE_ONE_CLICK_SIGNIN)
69 #import "chrome/browser/ui/cocoa/one_click_signin_bubble_controller.h"
70 #import "chrome/browser/ui/cocoa/one_click_signin_dialog_controller.h"
71 #endif
73 using content::NativeWebKeyboardEvent;
74 using content::SSLStatus;
75 using content::WebContents;
77 namespace {
79 // These UI constants are used in BrowserWindowCocoa::ShowBookmarkAppBubble.
80 // Used for defining the layout of the NSAlert and NSTextField within the
81 // accessory view.
82 const int kAppTextFieldVerticalSpacing = 2;
83 const int kAppTextFieldWidth = 200;
84 const int kAppTextFieldHeight = 22;
85 const int kBookmarkAppBubbleViewWidth = 200;
86 const int kBookmarkAppBubbleViewHeight = 46;
88 const int kIconPreviewTargetSize = 128;
90 base::string16 TrimText(NSString* controlText) {
91   base::string16 text = base::SysNSStringToUTF16(controlText);
92   base::TrimWhitespace(text, base::TRIM_ALL, &text);
93   return text;
96 }  // namespace
98 @interface TextRequiringDelegate : NSObject<NSTextFieldDelegate> {
99  @private
100   // Disable |control_| when text changes to just whitespace or empty string.
101   NSControl* control_;
103 - (id)initWithControl:(NSControl*)control text:(NSString*)text;
104 - (void)controlTextDidChange:(NSNotification*)notification;
105 @end
107 @interface TextRequiringDelegate ()
108 - (void)validateText:(NSString*)text;
109 @end
111 @implementation TextRequiringDelegate
112 - (id)initWithControl:(NSControl*)control text:(NSString*)text {
113   if ((self = [super init])) {
114     control_ = control;
115     [self validateText:text];
116   }
117   return self;
120 - (void)controlTextDidChange:(NSNotification*)notification {
121   [self validateText:[[notification object] stringValue]];
124 - (void)validateText:(NSString*)text {
125   [control_ setEnabled:TrimText(text).empty() ? NO : YES];
127 @end
129 BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser,
130                                        BrowserWindowController* controller)
131   : browser_(browser),
132     controller_(controller),
133     initial_show_state_(ui::SHOW_STATE_DEFAULT),
134     attention_request_id_(0) {
136   gfx::Rect bounds;
137   chrome::GetSavedWindowBoundsAndShowState(browser_,
138                                            &bounds,
139                                            &initial_show_state_);
141   browser_->search_model()->AddObserver(this);
144 BrowserWindowCocoa::~BrowserWindowCocoa() {
145   browser_->search_model()->RemoveObserver(this);
148 void BrowserWindowCocoa::Show() {
149   // The Browser associated with this browser window must become the active
150   // browser at the time |Show()| is called. This is the natural behaviour under
151   // Windows, but |-makeKeyAndOrderFront:| won't send |-windowDidBecomeMain:|
152   // until we return to the runloop. Therefore any calls to
153   // |chrome::FindLastActiveWithHostDesktopType| will return the previous
154   // browser instead if we don't explicitly set it here.
155   BrowserList::SetLastActive(browser_);
157   bool is_session_restore = browser_->is_session_restore();
158   NSWindowAnimationBehavior saved_animation_behavior =
159       NSWindowAnimationBehaviorDefault;
160   bool did_save_animation_behavior = false;
161   // Turn off swishing when restoring windows or showing an app.
162   if ((is_session_restore || browser_->is_app()) &&
163       [window() respondsToSelector:@selector(animationBehavior)] &&
164       [window() respondsToSelector:@selector(setAnimationBehavior:)]) {
165     did_save_animation_behavior = true;
166     saved_animation_behavior = [window() animationBehavior];
167     [window() setAnimationBehavior:NSWindowAnimationBehaviorNone];
168   }
170   {
171     TRACE_EVENT0("ui", "BrowserWindowCocoa::Show makeKeyAndOrderFront");
172     // This call takes up a substantial part of startup time, and an even more
173     // substantial part of startup time when any CALayers are part of the
174     // window's NSView heirarchy.
175     [window() makeKeyAndOrderFront:controller_];
176   }
178   // When creating windows from nibs it is necessary to |makeKeyAndOrderFront:|
179   // prior to |orderOut:| then |miniaturize:| when restoring windows in the
180   // minimized state.
181   if (initial_show_state_ == ui::SHOW_STATE_MINIMIZED) {
182     [window() orderOut:controller_];
183     [window() miniaturize:controller_];
184   } else if (initial_show_state_ == ui::SHOW_STATE_FULLSCREEN) {
185     chrome::ToggleFullscreenWithToolbarOrFallback(browser_);
186   }
187   initial_show_state_ = ui::SHOW_STATE_DEFAULT;
189   // Restore window animation behavior.
190   if (did_save_animation_behavior)
191     [window() setAnimationBehavior:saved_animation_behavior];
193   browser_->OnWindowDidShow();
196 void BrowserWindowCocoa::ShowInactive() {
197   [window() orderFront:controller_];
200 void BrowserWindowCocoa::Hide() {
201   [window() orderOut:controller_];
204 void BrowserWindowCocoa::SetBounds(const gfx::Rect& bounds) {
205   gfx::Rect real_bounds = [controller_ enforceMinWindowSize:bounds];
207   ExitFullscreen();
208   NSRect cocoa_bounds = NSMakeRect(real_bounds.x(), 0,
209                                    real_bounds.width(),
210                                    real_bounds.height());
211   // Flip coordinates based on the primary screen.
212   NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
213   cocoa_bounds.origin.y =
214       NSHeight([screen frame]) - real_bounds.height() - real_bounds.y();
216   [window() setFrame:cocoa_bounds display:YES];
219 // Callers assume that this doesn't immediately delete the Browser object.
220 // The controller implementing the window delegate methods called from
221 // |-performClose:| must take precautions to ensure that.
222 void BrowserWindowCocoa::Close() {
223   // If there is an overlay window, we contain a tab being dragged between
224   // windows. Don't hide the window as it makes the UI extra confused. We can
225   // still close the window, as that will happen when the drag completes.
226   if ([controller_ overlayWindow]) {
227     [controller_ deferPerformClose];
228   } else {
229     // Using |-performClose:| can prevent the window from actually closing if
230     // a JavaScript beforeunload handler opens an alert during shutdown, as
231     // documented at <http://crbug.com/118424>. Re-implement
232     // -[NSWindow performClose:] as closely as possible to how Apple documents
233     // it.
234     //
235     // Before calling |-close|, hide the window immediately. |-performClose:|
236     // would do something similar, and this ensures that the window is removed
237     // from AppKit's display list. Not doing so can lead to crashes like
238     // <http://crbug.com/156101>.
239     id<NSWindowDelegate> delegate = [window() delegate];
240     SEL window_should_close = @selector(windowShouldClose:);
241     if ([delegate respondsToSelector:window_should_close]) {
242       if ([delegate windowShouldClose:window()]) {
243         [window() orderOut:nil];
244         [window() close];
245       }
246     } else if ([window() respondsToSelector:window_should_close]) {
247       if ([window() performSelector:window_should_close withObject:window()]) {
248         [window() orderOut:nil];
249         [window() close];
250       }
251     } else {
252       [window() orderOut:nil];
253       [window() close];
254     }
255   }
258 void BrowserWindowCocoa::Activate() {
259   [controller_ activate];
262 void BrowserWindowCocoa::Deactivate() {
263   // TODO(jcivelli): http://crbug.com/51364 Implement me.
264   NOTIMPLEMENTED();
267 void BrowserWindowCocoa::FlashFrame(bool flash) {
268   if (flash) {
269     attention_request_id_ = [NSApp requestUserAttention:NSInformationalRequest];
270   } else {
271     [NSApp cancelUserAttentionRequest:attention_request_id_];
272     attention_request_id_ = 0;
273   }
276 bool BrowserWindowCocoa::IsAlwaysOnTop() const {
277   return false;
280 void BrowserWindowCocoa::SetAlwaysOnTop(bool always_on_top) {
281   // Not implemented for browser windows.
282   NOTIMPLEMENTED();
285 bool BrowserWindowCocoa::IsActive() const {
286   return [window() isKeyWindow];
289 gfx::NativeWindow BrowserWindowCocoa::GetNativeWindow() const {
290   return window();
293 StatusBubble* BrowserWindowCocoa::GetStatusBubble() {
294   return [controller_ statusBubble];
297 void BrowserWindowCocoa::UpdateTitleBar() {
298   NSString* newTitle =
299       base::SysUTF16ToNSString(browser_->GetWindowTitleForCurrentTab());
301   pending_window_title_.reset(
302       [BrowserWindowUtils scheduleReplaceOldTitle:pending_window_title_.get()
303                                      withNewTitle:newTitle
304                                         forWindow:window()]);
307 void BrowserWindowCocoa::BookmarkBarStateChanged(
308     BookmarkBar::AnimateChangeType change_type) {
309   [[controller_ bookmarkBarController]
310       updateState:browser_->bookmark_bar_state()
311        changeType:change_type];
314 void BrowserWindowCocoa::UpdateDevTools() {
315   [controller_ updateDevToolsForContents:
316       browser_->tab_strip_model()->GetActiveWebContents()];
319 void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) {
320   // Do nothing on Mac.
323 void BrowserWindowCocoa::SetStarredState(bool is_starred) {
324   [controller_ setStarredState:is_starred];
327 void BrowserWindowCocoa::SetTranslateIconToggled(bool is_lit) {
328   [controller_ setCurrentPageIsTranslated:is_lit];
331 void BrowserWindowCocoa::OnActiveTabChanged(content::WebContents* old_contents,
332                                             content::WebContents* new_contents,
333                                             int index,
334                                             int reason) {
335   [controller_ onActiveTabChanged:old_contents to:new_contents];
336   // TODO(pkasting): Perhaps the code in
337   // TabStripController::activateTabWithContents should move here?  Or this
338   // should call that (instead of TabStripModelObserverBridge doing so)?  It's
339   // not obvious to me why Mac doesn't handle tab changes in BrowserWindow the
340   // way views and GTK do.
341   // See http://crbug.com/340720 for discussion.
344 void BrowserWindowCocoa::ZoomChangedForActiveTab(bool can_show_bubble) {
345   [controller_ zoomChangedForActiveTab:can_show_bubble ? YES : NO];
348 gfx::Rect BrowserWindowCocoa::GetRestoredBounds() const {
349   // Flip coordinates based on the primary screen.
350   NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
351   NSRect frame = [controller_ regularWindowFrame];
352   gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
353   bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
354   return bounds;
357 ui::WindowShowState BrowserWindowCocoa::GetRestoredState() const {
358   if (IsMaximized())
359     return ui::SHOW_STATE_MAXIMIZED;
360   if (IsMinimized())
361     return ui::SHOW_STATE_MINIMIZED;
362   return ui::SHOW_STATE_NORMAL;
365 gfx::Rect BrowserWindowCocoa::GetBounds() const {
366   return GetRestoredBounds();
369 bool BrowserWindowCocoa::IsMaximized() const {
370   // -isZoomed returns YES if the window's frame equals the rect returned by
371   // -windowWillUseStandardFrame:defaultFrame:, even if the window is in the
372   // dock, so have to explicitly check for miniaturization state first.
373   return ![window() isMiniaturized] && [window() isZoomed];
376 bool BrowserWindowCocoa::IsMinimized() const {
377   return [window() isMiniaturized];
380 void BrowserWindowCocoa::Maximize() {
381   // Zoom toggles so only call if not already maximized.
382   if (!IsMaximized())
383     [window() zoom:controller_];
386 void BrowserWindowCocoa::Minimize() {
387   [window() miniaturize:controller_];
390 void BrowserWindowCocoa::Restore() {
391   if (IsMaximized())
392     [window() zoom:controller_];  // Toggles zoom mode.
393   else if (IsMinimized())
394     [window() deminiaturize:controller_];
397 // See browser_window_controller.h for a detailed explanation of the logic in
398 // this method.
399 void BrowserWindowCocoa::EnterFullscreen(const GURL& url,
400                                          ExclusiveAccessBubbleType bubble_type,
401                                          bool with_toolbar) {
402   if (browser_->exclusive_access_manager()
403           ->fullscreen_controller()
404           ->IsWindowFullscreenForTabOrPending())
405     [controller_ enterWebContentFullscreenForURL:url bubbleType:bubble_type];
406   else if (!url.is_empty())
407     [controller_ enterExtensionFullscreenForURL:url bubbleType:bubble_type];
408   else
409     [controller_ enterBrowserFullscreenWithToolbar:with_toolbar];
412 void BrowserWindowCocoa::ExitFullscreen() {
413   [controller_ exitAnyFullscreen];
416 void BrowserWindowCocoa::UpdateExclusiveAccessExitBubbleContent(
417     const GURL& url,
418     ExclusiveAccessBubbleType bubble_type) {
419   [controller_ updateFullscreenExitBubbleURL:url bubbleType:bubble_type];
422 bool BrowserWindowCocoa::ShouldHideUIForFullscreen() const {
423   // On Mac, fullscreen mode has most normal things (in a slide-down panel).
424   return false;
427 bool BrowserWindowCocoa::IsFullscreen() const {
428   return [controller_ isInAnyFullscreenMode];
431 bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const {
432   return false;
435 bool BrowserWindowCocoa::SupportsFullscreenWithToolbar() const {
436   return chrome::mac::SupportsSystemFullscreen();
439 void BrowserWindowCocoa::UpdateFullscreenWithToolbar(bool with_toolbar) {
440   [controller_ updateFullscreenWithToolbar:with_toolbar];
443 bool BrowserWindowCocoa::IsFullscreenWithToolbar() const {
444   return IsFullscreen() && ![controller_ inPresentationMode];
447 void BrowserWindowCocoa::ConfirmAddSearchProvider(
448     TemplateURL* template_url,
449     Profile* profile) {
450   // The controller will release itself when the window closes.
451   EditSearchEngineCocoaController* editor =
452       [[EditSearchEngineCocoaController alloc] initWithProfile:profile
453                                                       delegate:NULL
454                                                    templateURL:template_url];
455   [NSApp beginSheet:[editor window]
456      modalForWindow:window()
457       modalDelegate:controller_
458      didEndSelector:@selector(sheetDidEnd:returnCode:context:)
459         contextInfo:NULL];
462 LocationBar* BrowserWindowCocoa::GetLocationBar() const {
463   return [controller_ locationBarBridge];
466 void BrowserWindowCocoa::SetFocusToLocationBar(bool select_all) {
467   [controller_ focusLocationBar:select_all ? YES : NO];
470 void BrowserWindowCocoa::UpdateReloadStopState(bool is_loading, bool force) {
471   [controller_ setIsLoading:is_loading force:force];
474 void BrowserWindowCocoa::UpdateToolbar(content::WebContents* contents) {
475   [controller_ updateToolbarWithContents:contents];
478 void BrowserWindowCocoa::ResetToolbarTabState(content::WebContents* contents) {
479   [controller_ resetTabState:contents];
482 void BrowserWindowCocoa::FocusToolbar() {
483   // Not needed on the Mac.
486 void BrowserWindowCocoa::ToolbarSizeChanged(bool is_animating) {
487   // Not needed on the Mac.
490 void BrowserWindowCocoa::FocusAppMenu() {
491   // Chrome uses the standard Mac OS X menu bar, so this isn't needed.
494 void BrowserWindowCocoa::RotatePaneFocus(bool forwards) {
495   // Not needed on the Mac.
498 void BrowserWindowCocoa::FocusBookmarksToolbar() {
499   // Not needed on the Mac.
502 void BrowserWindowCocoa::FocusInfobars() {
503   // Not needed on the Mac.
506 bool BrowserWindowCocoa::IsBookmarkBarVisible() const {
507   return browser_->profile()->GetPrefs()->GetBoolean(
508       bookmarks::prefs::kShowBookmarkBar);
511 bool BrowserWindowCocoa::IsBookmarkBarAnimating() const {
512   return [controller_ isBookmarkBarAnimating];
515 bool BrowserWindowCocoa::IsTabStripEditable() const {
516   return ![controller_ isDragSessionActive];
519 bool BrowserWindowCocoa::IsToolbarVisible() const {
520   return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
521          browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
524 gfx::Rect BrowserWindowCocoa::GetRootWindowResizerRect() const {
525   if (IsDownloadShelfVisible())
526     return gfx::Rect();
527   NSRect tabRect = [controller_ selectedTabGrowBoxRect];
528   return gfx::Rect(NSRectToCGRect(tabRect));
531 void BrowserWindowCocoa::AddFindBar(
532     FindBarCocoaController* find_bar_cocoa_controller) {
533   [controller_ addFindBar:find_bar_cocoa_controller];
536 void BrowserWindowCocoa::ShowUpdateChromeDialog() {
537   restart_browser::RequestRestart(window());
540 void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url,
541                                             bool already_bookmarked) {
542   [controller_ showBookmarkBubbleForURL:url
543                       alreadyBookmarked:(already_bookmarked ? YES : NO)];
546 void BrowserWindowCocoa::ShowBookmarkAppBubble(
547     const WebApplicationInfo& web_app_info,
548     const ShowBookmarkAppBubbleCallback& callback) {
549   base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
550   [alert setMessageText:l10n_util::GetNSString(
551       IDS_ADD_TO_APPLICATIONS_BUBBLE_TITLE)];
552   [alert setAlertStyle:NSInformationalAlertStyle];
554   NSButton* continue_button =
555       [alert addButtonWithTitle:l10n_util::GetNSString(IDS_OK)];
556   [continue_button setKeyEquivalent:kKeyEquivalentReturn];
557   NSButton* cancel_button =
558       [alert addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)];
559   [cancel_button setKeyEquivalent:kKeyEquivalentEscape];
561   base::scoped_nsobject<NSButton> open_as_window_checkbox(
562       [[NSButton alloc] initWithFrame:NSZeroRect]);
563   [open_as_window_checkbox setButtonType:NSSwitchButton];
564   [open_as_window_checkbox
565       setTitle:l10n_util::GetNSString(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_WINDOW)];
566   [open_as_window_checkbox setState:web_app_info.open_as_window];
567   [open_as_window_checkbox sizeToFit];
569   base::scoped_nsobject<NSTextField> app_title([[NSTextField alloc]
570       initWithFrame:NSMakeRect(0, kAppTextFieldHeight +
571                                       kAppTextFieldVerticalSpacing,
572                                kAppTextFieldWidth, kAppTextFieldHeight)]);
573   NSString* original_title = SysUTF16ToNSString(web_app_info.title);
574   [[app_title cell] setWraps:NO];
575   [[app_title cell] setScrollable:YES];
576   [app_title setStringValue:original_title];
577   base::scoped_nsobject<TextRequiringDelegate> delegate(
578       [[TextRequiringDelegate alloc] initWithControl:continue_button
579                                                 text:[app_title stringValue]]);
580   [app_title setDelegate:delegate];
582   base::scoped_nsobject<NSView> view([[NSView alloc]
583       initWithFrame:NSMakeRect(0, 0, kBookmarkAppBubbleViewWidth,
584                                kBookmarkAppBubbleViewHeight)]);
585   [view addSubview:open_as_window_checkbox];
586   [view addSubview:app_title];
587   [alert setAccessoryView:view];
589   // Find the image with target size.
590   // Assumes that the icons are sorted in ascending order of size.
591   if (!web_app_info.icons.empty()) {
592     for (const WebApplicationInfo::IconInfo& info : web_app_info.icons) {
593       if (info.width == kIconPreviewTargetSize &&
594           info.height == kIconPreviewTargetSize) {
595         gfx::Image icon_image = gfx::Image::CreateFrom1xBitmap(info.data);
596         [alert setIcon:icon_image.ToNSImage()];
597         break;
598       }
599     }
600   }
602   NSInteger response = [alert runModal];
604   // Prevent |app_title| from accessing |delegate| after it's destroyed.
605   [app_title setDelegate:nil];
607   if (response == NSAlertFirstButtonReturn) {
608     WebApplicationInfo updated_info = web_app_info;
609     updated_info.open_as_window = [open_as_window_checkbox state] == NSOnState;
610     updated_info.title = TrimText([app_title stringValue]);
612     callback.Run(true, updated_info);
613     return;
614   }
616   callback.Run(false, web_app_info);
619 void BrowserWindowCocoa::ShowTranslateBubble(
620     content::WebContents* contents,
621     translate::TranslateStep step,
622     translate::TranslateErrors::Type error_type,
623     bool is_user_gesture) {
624   ChromeTranslateClient* chrome_translate_client =
625       ChromeTranslateClient::FromWebContents(contents);
626   translate::LanguageState& language_state =
627       chrome_translate_client->GetLanguageState();
628   language_state.SetTranslateEnabled(true);
630   [controller_ showTranslateBubbleForWebContents:contents
631                                             step:step
632                                        errorType:error_type];
635 bool BrowserWindowCocoa::ShowSessionCrashedBubble() {
636   return false;
639 bool BrowserWindowCocoa::IsProfileResetBubbleSupported() const {
640   return false;
643 GlobalErrorBubbleViewBase* BrowserWindowCocoa::ShowProfileResetBubble(
644     const base::WeakPtr<ProfileResetGlobalError>& global_error) {
645   NOTREACHED();
646   return nullptr;
649 #if defined(ENABLE_ONE_CLICK_SIGNIN)
650 void BrowserWindowCocoa::ShowOneClickSigninBubble(
651     OneClickSigninBubbleType type,
652     const base::string16& email,
653     const base::string16& error_message,
654     const StartSyncCallback& start_sync_callback) {
655   WebContents* web_contents =
656         browser_->tab_strip_model()->GetActiveWebContents();
657   if (type == ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE) {
658     base::scoped_nsobject<OneClickSigninBubbleController> bubble_controller([
659             [OneClickSigninBubbleController alloc]
660         initWithBrowserWindowController:cocoa_controller()
661                             webContents:web_contents
662                            errorMessage:base::SysUTF16ToNSString(error_message)
663                                callback:start_sync_callback]);
664     [bubble_controller showWindow:nil];
665   } else {
666     // Deletes itself when the dialog closes.
667     new OneClickSigninDialogController(
668         web_contents, start_sync_callback, email);
669   }
671 #endif
673 bool BrowserWindowCocoa::IsDownloadShelfVisible() const {
674   return [controller_ isDownloadShelfVisible] != NO;
677 DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() {
678   [controller_ createAndAddDownloadShelf];
679   DownloadShelfController* shelfController = [controller_ downloadShelf];
680   return [shelfController bridge];
683 // We allow closing the window here since the real quit decision on Mac is made
684 // in [AppController quit:].
685 void BrowserWindowCocoa::ConfirmBrowserCloseWithPendingDownloads(
686       int download_count,
687       Browser::DownloadClosePreventionType dialog_type,
688       bool app_modal,
689       const base::Callback<void(bool)>& callback) {
690   callback.Run(true);
693 void BrowserWindowCocoa::UserChangedTheme() {
694   [controller_ userChangedTheme];
697 void BrowserWindowCocoa::ShowWebsiteSettings(
698     Profile* profile,
699     content::WebContents* web_contents,
700     const GURL& url,
701     const content::SSLStatus& ssl) {
702   WebsiteSettingsUIBridge::Show(window(), profile, web_contents, url, ssl);
705 void BrowserWindowCocoa::ShowAppMenu() {
706   // No-op. Mac doesn't support showing the menus via alt keys.
709 bool BrowserWindowCocoa::PreHandleKeyboardEvent(
710     const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) {
711   // Handle ESC to dismiss permission bubbles, but still forward it
712   // to the window afterwards.
713   if (event.windowsKeyCode == ui::VKEY_ESCAPE)
714     [controller_ dismissPermissionBubble];
716   if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
717     return false;
719   if (event.type == blink::WebInputEvent::RawKeyDown &&
720       [controller_
721           handledByExtensionCommand:event.os_event
722                            priority:ui::AcceleratorManager::kHighPriority])
723     return true;
725   int id = [BrowserWindowUtils getCommandId:event];
726   if (id == -1)
727     return false;
729   if (browser_->command_controller()->IsReservedCommandOrKey(id, event)) {
730       return [BrowserWindowUtils handleKeyboardEvent:event.os_event
731                                             inWindow:window()];
732   }
734   DCHECK(is_keyboard_shortcut);
735   *is_keyboard_shortcut = true;
736   return false;
739 void BrowserWindowCocoa::HandleKeyboardEvent(
740     const NativeWebKeyboardEvent& event) {
741   if ([BrowserWindowUtils shouldHandleKeyboardEvent:event])
742     [BrowserWindowUtils handleKeyboardEvent:event.os_event inWindow:window()];
745 void BrowserWindowCocoa::CutCopyPaste(int command_id) {
746   if (command_id == IDC_CUT)
747     [NSApp sendAction:@selector(cut:) to:nil from:nil];
748   else if (command_id == IDC_COPY)
749     [NSApp sendAction:@selector(copy:) to:nil from:nil];
750   else
751     [NSApp sendAction:@selector(paste:) to:nil from:nil];
754 WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds(
755     const gfx::Rect& bounds) {
756   // When using Cocoa's System Fullscreen mode, convert popups into tabs.
757   if ([controller_ isInAppKitFullscreen])
758     return NEW_FOREGROUND_TAB;
759   return NEW_POPUP;
762 FindBar* BrowserWindowCocoa::CreateFindBar() {
763   // We could push the AddFindBar() call into the FindBarBridge
764   // constructor or the FindBarCocoaController init, but that makes
765   // unit testing difficult, since we would also require a
766   // BrowserWindow object.
767   FindBarBridge* bridge = new FindBarBridge(browser_);
768   AddFindBar(bridge->find_bar_cocoa_controller());
769   return bridge;
772 web_modal::WebContentsModalDialogHost*
773     BrowserWindowCocoa::GetWebContentsModalDialogHost() {
774   DCHECK([controller_ window]);
775   ConstrainedWindowSheetController* sheet_controller =
776       [ConstrainedWindowSheetController
777           controllerForParentWindow:[controller_ window]];
778   DCHECK(sheet_controller);
779   return [sheet_controller dialogHost];
782 extensions::ActiveTabPermissionGranter*
783     BrowserWindowCocoa::GetActiveTabPermissionGranter() {
784   WebContents* web_contents =
785       browser_->tab_strip_model()->GetActiveWebContents();
786   if (!web_contents)
787     return NULL;
788   extensions::TabHelper* tab_helper =
789       extensions::TabHelper::FromWebContents(web_contents);
790   return tab_helper ? tab_helper->active_tab_permission_granter() : NULL;
793 void BrowserWindowCocoa::ModelChanged(const SearchModel::State& old_state,
794                                       const SearchModel::State& new_state) {
797 void BrowserWindowCocoa::DestroyBrowser() {
798   [controller_ destroyBrowser];
800   // at this point the controller is dead (autoreleased), so
801   // make sure we don't try to reference it any more.
804 NSWindow* BrowserWindowCocoa::window() const {
805   return [controller_ window];
808 void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton(
809     AvatarBubbleMode mode,
810     const signin::ManageAccountsParams& manage_accounts_params) {
811   AvatarBaseController* controller = [controller_ avatarButtonController];
812   NSView* anchor = [controller buttonView];
813   if ([anchor isHiddenOrHasHiddenAncestor])
814     anchor = [[controller_ toolbarController] wrenchButton];
815   [controller showAvatarBubbleAnchoredAt:anchor
816                                 withMode:mode
817                          withServiceType:manage_accounts_params.service_type];
820 void BrowserWindowCocoa::CloseAvatarBubbleFromAvatarButton() {
821   [[controller_ avatarButtonController] closeAvatarBubble];
825 BrowserWindowCocoa::GetRenderViewHeightInsetWithDetachedBookmarkBar() {
826   if (browser_->bookmark_bar_state() != BookmarkBar::DETACHED)
827     return 0;
828   return 40;
831 void BrowserWindowCocoa::ExecuteExtensionCommand(
832     const extensions::Extension* extension,
833     const extensions::Command& command) {
834   [cocoa_controller() executeExtensionCommand:extension->id() command:command];
837 ExclusiveAccessContext* BrowserWindowCocoa::GetExclusiveAccessContext() {
838   return this;
841 Profile* BrowserWindowCocoa::GetProfile() {
842   return browser_->profile();
845 WebContents* BrowserWindowCocoa::GetActiveWebContents() {
846   return browser_->tab_strip_model()->GetActiveWebContents();
849 void BrowserWindowCocoa::UnhideDownloadShelf() {
850   GetDownloadShelf()->Unhide();
853 void BrowserWindowCocoa::HideDownloadShelf() {
854   GetDownloadShelf()->Hide();
855   StatusBubble* statusBubble = GetStatusBubble();
856   if (statusBubble)
857     statusBubble->Hide();