blob: 6efebbf148f84244fe86045241f1ffe30195f9d9 [file] [log] [blame]
[email protected]3a80ea332012-01-09 19:53:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7d791652010-12-01 16:34:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
6
[email protected]a932d9e2011-09-29 01:14:347#include "base/bind.h"
[email protected]7d791652010-12-01 16:34:498#include "base/command_line.h"
9#include "base/logging.h"
[email protected]d82a3c9f2012-02-22 04:46:0010#include "base/mac/mac_util.h"
[email protected]7d791652010-12-01 16:34:4911#include "base/message_loop.h"
12#include "base/sys_string_conversions.h"
13#include "chrome/app/chrome_command_ids.h"
[email protected]7d791652010-12-01 16:34:4914#include "chrome/browser/download/download_shelf.h"
[email protected]78ce3022012-09-24 01:48:4815#include "chrome/browser/extensions/tab_helper.h"
[email protected]7d791652010-12-01 16:34:4916#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3317#include "chrome/browser/profiles/profile.h"
[email protected]03020ff2012-11-29 12:08:1718#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
[email protected]7d791652010-12-01 16:34:4919#include "chrome/browser/ui/browser.h"
[email protected]5d98294912012-06-27 22:57:4020#include "chrome/browser/ui/browser_command_controller.h"
[email protected]3f32b9b2012-07-09 16:59:2821#include "chrome/browser/ui/browser_commands.h"
[email protected]7d791652010-12-01 16:34:4922#include "chrome/browser/ui/browser_list.h"
[email protected]52877dbc62012-06-29 22:22:0323#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]1f0b50b2012-06-22 20:37:1624#include "chrome/browser/ui/browser_window_state.h"
[email protected]7d2d08152011-10-25 22:58:4725#import "chrome/browser/ui/cocoa/browser/avatar_button_controller.h"
[email protected]2f733a02011-10-06 15:17:3426#import "chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.h"
[email protected]464f0012011-07-07 02:12:5127#import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h"
[email protected]7d791652010-12-01 16:34:4928#import "chrome/browser/ui/cocoa/browser_window_controller.h"
[email protected]3a1381d2011-08-26 18:12:1329#import "chrome/browser/ui/cocoa/browser_window_utils.h"
[email protected]7d791652010-12-01 16:34:4930#import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
[email protected]7d791652010-12-01 16:34:4931#import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
[email protected]632983f2011-08-08 22:51:2432#include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h"
[email protected]51152d6d2011-11-07 01:55:1233#import "chrome/browser/ui/cocoa/info_bubble_view.h"
[email protected]7d791652010-12-01 16:34:4934#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
[email protected]a7d83ca2011-03-06 14:41:0735#import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
[email protected]7d791652010-12-01 16:34:4936#include "chrome/browser/ui/cocoa/restart_browser.h"
37#include "chrome/browser/ui/cocoa/status_bubble_mac.h"
38#include "chrome/browser/ui/cocoa/task_manager_mac.h"
[email protected]8450c4f2011-01-19 22:16:2239#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
[email protected]684dace42012-07-01 14:30:4140#import "chrome/browser/ui/cocoa/web_dialog_window_controller.h"
[email protected]5436c8fd2012-07-02 11:30:4641#import "chrome/browser/ui/cocoa/website_settings_bubble_controller.h"
[email protected]7e2514c2011-11-28 20:04:4542#include "chrome/browser/ui/page_info_bubble.h"
[email protected]432115822011-07-10 15:52:2743#include "chrome/common/chrome_notification_types.h"
[email protected]7d791652010-12-01 16:34:4944#include "chrome/common/pref_names.h"
[email protected]8bc38d22011-10-14 20:39:2045#include "content/public/browser/native_web_keyboard_event.h"
[email protected]6c2381d2011-10-19 02:52:5346#include "content/public/browser/notification_details.h"
[email protected]684dace42012-07-01 14:30:4147#include "content/public/browser/notification_source.h"
[email protected]e8558cf22012-01-10 03:10:5148#include "content/public/browser/web_contents.h"
[email protected]7d791652010-12-01 16:34:4949#include "grit/chromium_strings.h"
50#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1751#include "ui/base/l10n/l10n_util_mac.h"
[email protected]08397d52011-02-05 01:53:3852#include "ui/gfx/rect.h"
[email protected]7d791652010-12-01 16:34:4953
[email protected]fe54b4a2012-03-28 16:11:0454#if defined(ENABLE_ONE_CLICK_SIGNIN)
55#import "chrome/browser/ui/cocoa/one_click_signin_bubble_controller.h"
56#endif
57
[email protected]b87ee522012-05-18 15:16:5458using content::NativeWebKeyboardEvent;
[email protected]d583e3f22011-12-27 21:38:1759using content::SSLStatus;
[email protected]2a6bc3e2011-12-28 23:51:3360using content::WebContents;
[email protected]d583e3f22011-12-27 21:38:1761
[email protected]400eaf82011-08-22 15:47:3962// Replicate specific 10.7 SDK declarations for building with prior SDKs.
63#if !defined(MAC_OS_X_VERSION_10_7) || \
64 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
65
66enum {
67 NSWindowAnimationBehaviorDefault = 0,
68 NSWindowAnimationBehaviorNone = 2,
69 NSWindowAnimationBehaviorDocumentWindow = 3,
70 NSWindowAnimationBehaviorUtilityWindow = 4,
71 NSWindowAnimationBehaviorAlertPanel = 5
72};
73typedef NSInteger NSWindowAnimationBehavior;
74
75@interface NSWindow (LionSDKDeclarations)
76- (NSWindowAnimationBehavior)animationBehavior;
77- (void)setAnimationBehavior:(NSWindowAnimationBehavior)newAnimationBehavior;
78@end
79
80#endif // MAC_OS_X_VERSION_10_7
81
[email protected]7d791652010-12-01 16:34:4982BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser,
[email protected]400eaf82011-08-22 15:47:3983 BrowserWindowController* controller)
[email protected]7d791652010-12-01 16:34:4984 : browser_(browser),
85 controller_(controller),
[email protected]ad996342012-01-14 00:29:5986 confirm_close_factory_(browser),
[email protected]6a9b53a22012-10-09 02:43:3487 initial_show_state_(ui::SHOW_STATE_DEFAULT),
[email protected]ad996342012-01-14 00:29:5988 attention_request_id_(0) {
[email protected]cfecf9432011-09-27 17:10:0889
[email protected]6a9b53a22012-10-09 02:43:3490 gfx::Rect bounds;
91 chrome::GetSavedWindowBoundsAndShowState(browser_,
92 &bounds,
93 &initial_show_state_);
[email protected]7d791652010-12-01 16:34:4994}
95
96BrowserWindowCocoa::~BrowserWindowCocoa() {
97}
98
99void BrowserWindowCocoa::Show() {
100 // The Browser associated with this browser window must become the active
101 // browser at the time |Show()| is called. This is the natural behaviour under
102 // Windows, but |-makeKeyAndOrderFront:| won't send |-windowDidBecomeMain:|
103 // until we return to the runloop. Therefore any calls to
[email protected]4d900252012-11-20 20:37:11104 // |chrome::FindLastActiveWithHostDesktopType| will return the previous
105 // browser instead if we don't explicitly set it here.
[email protected]7d791652010-12-01 16:34:49106 BrowserList::SetLastActive(browser_);
107
[email protected]8bc061f2011-08-31 22:46:23108 bool is_session_restore = browser_->is_session_restore();
[email protected]b5cc93c2011-09-02 02:33:42109 NSWindowAnimationBehavior saved_animation_behavior =
110 NSWindowAnimationBehaviorDefault;
[email protected]8bc061f2011-08-31 22:46:23111 bool did_save_animation_behavior = false;
112 // Turn off swishing when restoring windows.
113 if (is_session_restore &&
114 [window() respondsToSelector:@selector(animationBehavior)] &&
115 [window() respondsToSelector:@selector(setAnimationBehavior:)]) {
116 did_save_animation_behavior = true;
117 saved_animation_behavior = [window() animationBehavior];
118 [window() setAnimationBehavior:NSWindowAnimationBehaviorNone];
119 }
[email protected]400eaf82011-08-22 15:47:39120
[email protected]8bc061f2011-08-31 22:46:23121 [window() makeKeyAndOrderFront:controller_];
[email protected]400eaf82011-08-22 15:47:39122
[email protected]8bc061f2011-08-31 22:46:23123 // When creating windows from nibs it is necessary to |makeKeyAndOrderFront:|
124 // prior to |orderOut:| then |miniaturize:| when restoring windows in the
125 // minimized state.
[email protected]cfecf9432011-09-27 17:10:08126 if (initial_show_state_ == ui::SHOW_STATE_MINIMIZED) {
[email protected]400eaf82011-08-22 15:47:39127 [window() orderOut:controller_];
128 [window() miniaturize:controller_];
[email protected]85b0ac62012-02-09 01:57:43129 } else if (initial_show_state_ == ui::SHOW_STATE_FULLSCREEN) {
[email protected]3f32b9b2012-07-09 16:59:28130 chrome::ToggleFullscreenMode(browser_);
[email protected]400eaf82011-08-22 15:47:39131 }
[email protected]cfecf9432011-09-27 17:10:08132 initial_show_state_ = ui::SHOW_STATE_DEFAULT;
[email protected]8bc061f2011-08-31 22:46:23133
134 // Restore window animation behavior.
135 if (did_save_animation_behavior)
136 [window() setAnimationBehavior:saved_animation_behavior];
[email protected]2f516c792011-09-19 22:22:09137
138 browser_->OnWindowDidShow();
[email protected]7d791652010-12-01 16:34:49139}
140
[email protected]d4db6c702011-03-28 21:49:14141void BrowserWindowCocoa::ShowInactive() {
[email protected]400eaf82011-08-22 15:47:39142 [window() orderFront:controller_];
[email protected]d4db6c702011-03-28 21:49:14143}
144
[email protected]7d412f72012-10-25 04:59:15145void BrowserWindowCocoa::Hide() {
146 // Not implemented.
147}
148
[email protected]7d791652010-12-01 16:34:49149void BrowserWindowCocoa::SetBounds(const gfx::Rect& bounds) {
[email protected]ccb5895f2011-06-09 21:16:14150 gfx::Rect real_bounds = [controller_ enforceMinWindowSize:bounds];
151
[email protected]55c87fa2011-10-15 07:28:28152 ExitFullscreen();
[email protected]ccb5895f2011-06-09 21:16:14153 NSRect cocoa_bounds = NSMakeRect(real_bounds.x(), 0,
154 real_bounds.width(),
155 real_bounds.height());
[email protected]7d791652010-12-01 16:34:49156 // Flip coordinates based on the primary screen.
157 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
158 cocoa_bounds.origin.y =
[email protected]03e0f5f1e2012-06-07 21:35:39159 NSHeight([screen frame]) - real_bounds.height() - real_bounds.y();
[email protected]7d791652010-12-01 16:34:49160
161 [window() setFrame:cocoa_bounds display:YES];
162}
163
164// Callers assume that this doesn't immediately delete the Browser object.
165// The controller implementing the window delegate methods called from
166// |-performClose:| must take precautions to ensure that.
167void BrowserWindowCocoa::Close() {
168 // If there is an overlay window, we contain a tab being dragged between
169 // windows. Don't hide the window as it makes the UI extra confused. We can
170 // still close the window, as that will happen when the drag completes.
[email protected]60f94292012-03-12 22:24:27171 if ([controller_ overlayWindow]) {
[email protected]7d791652010-12-01 16:34:49172 [controller_ deferPerformClose];
[email protected]60f94292012-03-12 22:24:27173 } else {
[email protected]5ea51972012-10-15 16:45:47174 // Using |-performClose:| can prevent the window from actually closing if
175 // a JavaScript beforeunload handler opens an alert during shutdown, as
176 // documented at <http://crbug.com/118424>. Re-implement
177 // -[NSWindow performClose:] as closely as possible to how Apple documents
178 // it.
[email protected]d1136ca2012-10-18 23:35:55179 //
180 // Before calling |-close|, hide the window immediately. |-performClose:|
181 // would do something similar, and this ensures that the window is removed
182 // from AppKit's display list. Not doing so can lead to crashes like
183 // <http://crbug.com/156101>.
[email protected]5ea51972012-10-15 16:45:47184 id<NSWindowDelegate> delegate = [window() delegate];
185 SEL window_should_close = @selector(windowShouldClose:);
186 if ([delegate respondsToSelector:window_should_close]) {
[email protected]d1136ca2012-10-18 23:35:55187 if ([delegate windowShouldClose:window()]) {
188 [window() orderOut:nil];
[email protected]5ea51972012-10-15 16:45:47189 [window() close];
[email protected]d1136ca2012-10-18 23:35:55190 }
[email protected]5ea51972012-10-15 16:45:47191 } else if ([window() respondsToSelector:window_should_close]) {
[email protected]d1136ca2012-10-18 23:35:55192 if ([window() performSelector:window_should_close withObject:window()]) {
193 [window() orderOut:nil];
[email protected]5ea51972012-10-15 16:45:47194 [window() close];
[email protected]d1136ca2012-10-18 23:35:55195 }
[email protected]5ea51972012-10-15 16:45:47196 } else {
[email protected]d1136ca2012-10-18 23:35:55197 [window() orderOut:nil];
[email protected]5ea51972012-10-15 16:45:47198 [window() close];
199 }
[email protected]60f94292012-03-12 22:24:27200 }
[email protected]7d791652010-12-01 16:34:49201}
202
203void BrowserWindowCocoa::Activate() {
204 [controller_ activate];
205}
206
207void BrowserWindowCocoa::Deactivate() {
208 // TODO(jcivelli): http://crbug.com/51364 Implement me.
209 NOTIMPLEMENTED();
210}
211
[email protected]ad996342012-01-14 00:29:59212void BrowserWindowCocoa::FlashFrame(bool flash) {
213 if (flash) {
214 attention_request_id_ = [NSApp requestUserAttention:NSInformationalRequest];
215 } else {
216 [NSApp cancelUserAttentionRequest:attention_request_id_];
217 attention_request_id_ = 0;
218 }
[email protected]7d791652010-12-01 16:34:49219}
220
[email protected]d101b0c2012-03-16 00:30:57221bool BrowserWindowCocoa::IsAlwaysOnTop() const {
222 return false;
223}
224
[email protected]7d791652010-12-01 16:34:49225bool BrowserWindowCocoa::IsActive() const {
226 return [window() isKeyWindow];
227}
228
[email protected]90556dd2012-06-07 20:26:18229gfx::NativeWindow BrowserWindowCocoa::GetNativeWindow() {
[email protected]7d791652010-12-01 16:34:49230 return window();
231}
232
233BrowserWindowTesting* BrowserWindowCocoa::GetBrowserWindowTesting() {
234 return NULL;
235}
236
237StatusBubble* BrowserWindowCocoa::GetStatusBubble() {
238 return [controller_ statusBubble];
239}
240
[email protected]7d791652010-12-01 16:34:49241void BrowserWindowCocoa::UpdateTitleBar() {
242 NSString* newTitle =
243 base::SysUTF16ToNSString(browser_->GetWindowTitleForCurrentTab());
244
[email protected]d78e615e2011-09-06 21:46:03245 pending_window_title_.reset(
246 [BrowserWindowUtils scheduleReplaceOldTitle:pending_window_title_.get()
247 withNewTitle:newTitle
248 forWindow:window()]);
[email protected]7d791652010-12-01 16:34:49249}
250
[email protected]09b29342011-06-24 19:18:48251void BrowserWindowCocoa::BookmarkBarStateChanged(
252 BookmarkBar::AnimateChangeType change_type) {
[email protected]1eeccb92012-12-06 20:31:24253 [[controller_ bookmarkBarController]
254 updateState:browser_->bookmark_bar_state()
255 changeType:change_type];
[email protected]7d791652010-12-01 16:34:49256}
257
258void BrowserWindowCocoa::UpdateDevTools() {
259 [controller_ updateDevToolsForContents:
[email protected]52877dbc62012-06-29 22:22:03260 chrome::GetActiveWebContents(browser_)];
[email protected]7d791652010-12-01 16:34:49261}
262
263void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) {
264 // Do nothing on Mac.
265}
266
267void BrowserWindowCocoa::SetStarredState(bool is_starred) {
268 [controller_ setStarredState:is_starred ? YES : NO];
269}
270
[email protected]5423c372012-08-22 05:50:16271void BrowserWindowCocoa::ZoomChangedForActiveTab(bool can_show_bubble) {
272 [controller_ zoomChangedForActiveTab:can_show_bubble ? YES : NO];
[email protected]6f80e932012-06-04 19:00:07273}
274
[email protected]7d791652010-12-01 16:34:49275gfx::Rect BrowserWindowCocoa::GetRestoredBounds() const {
276 // Flip coordinates based on the primary screen.
277 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
278 NSRect frame = [controller_ regularWindowFrame];
[email protected]03e0f5f1e2012-06-07 21:35:39279 gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
280 bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
[email protected]7d791652010-12-01 16:34:49281 return bounds;
282}
283
[email protected]d479b8e22011-02-09 05:19:49284gfx::Rect BrowserWindowCocoa::GetBounds() const {
285 return GetRestoredBounds();
286}
287
[email protected]7d791652010-12-01 16:34:49288bool BrowserWindowCocoa::IsMaximized() const {
289 return [window() isZoomed];
290}
291
[email protected]400eaf82011-08-22 15:47:39292bool BrowserWindowCocoa::IsMinimized() const {
293 return [window() isMiniaturized];
294}
295
[email protected]541e3ee2011-11-22 01:31:10296void BrowserWindowCocoa::Maximize() {
297 // Zoom toggles so only call if not already maximized.
298 if (!IsMaximized())
299 [window() zoom:controller_];
300}
301
302void BrowserWindowCocoa::Minimize() {
303 [window() miniaturize:controller_];
304}
305
306void BrowserWindowCocoa::Restore() {
307 if (IsMaximized())
308 [window() zoom:controller_]; // Toggles zoom mode.
309 else if (IsMinimized())
310 [window() deminiaturize:controller_];
311}
312
[email protected]8d944b32011-10-17 06:11:53313void BrowserWindowCocoa::EnterFullscreen(
[email protected]22d74a6b2011-10-17 20:30:31314 const GURL& url, FullscreenExitBubbleType bubble_type) {
315 [controller_ enterFullscreenForURL:url
316 bubbleType:bubble_type];
[email protected]55c87fa2011-10-15 07:28:28317}
318
319void BrowserWindowCocoa::ExitFullscreen() {
[email protected]22d74a6b2011-10-17 20:30:31320 [controller_ exitFullscreen];
[email protected]8d944b32011-10-17 06:11:53321}
322
323void BrowserWindowCocoa::UpdateFullscreenExitBubbleContent(
324 const GURL& url,
325 FullscreenExitBubbleType bubble_type) {
[email protected]22d74a6b2011-10-17 20:30:31326 [controller_ updateFullscreenExitBubbleURL:url bubbleType:bubble_type];
[email protected]7d791652010-12-01 16:34:49327}
328
329bool BrowserWindowCocoa::IsFullscreen() const {
[email protected]8d944b32011-10-17 06:11:53330 return [controller_ isFullscreen];
[email protected]7d791652010-12-01 16:34:49331}
332
333bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const {
334 return false;
335}
336
337void BrowserWindowCocoa::ConfirmAddSearchProvider(
[email protected]3613347d2012-04-27 20:27:37338 TemplateURL* template_url,
[email protected]7d791652010-12-01 16:34:49339 Profile* profile) {
[email protected]464f0012011-07-07 02:12:51340 // The controller will release itself when the window closes.
341 EditSearchEngineCocoaController* editor =
342 [[EditSearchEngineCocoaController alloc] initWithProfile:profile
343 delegate:NULL
344 templateURL:template_url];
345 [NSApp beginSheet:[editor window]
346 modalForWindow:window()
347 modalDelegate:controller_
348 didEndSelector:@selector(sheetDidEnd:returnCode:context:)
349 contextInfo:NULL];
[email protected]7d791652010-12-01 16:34:49350}
351
352LocationBar* BrowserWindowCocoa::GetLocationBar() const {
353 return [controller_ locationBarBridge];
354}
355
356void BrowserWindowCocoa::SetFocusToLocationBar(bool select_all) {
357 [controller_ focusLocationBar:select_all ? YES : NO];
358}
359
360void BrowserWindowCocoa::UpdateReloadStopState(bool is_loading, bool force) {
361 [controller_ setIsLoading:is_loading force:force];
362}
363
[email protected]36ad4aa82012-11-21 19:20:48364void BrowserWindowCocoa::UpdateToolbar(content::WebContents* contents,
[email protected]7d791652010-12-01 16:34:49365 bool should_restore_state) {
[email protected]36ad4aa82012-11-21 19:20:48366 [controller_ updateToolbarWithContents:contents
[email protected]7d791652010-12-01 16:34:49367 shouldRestoreState:should_restore_state ? YES : NO];
368}
369
370void BrowserWindowCocoa::FocusToolbar() {
371 // Not needed on the Mac.
372}
373
374void BrowserWindowCocoa::FocusAppMenu() {
375 // Chrome uses the standard Mac OS X menu bar, so this isn't needed.
376}
377
378void BrowserWindowCocoa::RotatePaneFocus(bool forwards) {
379 // Not needed on the Mac.
380}
381
382void BrowserWindowCocoa::FocusBookmarksToolbar() {
383 // Not needed on the Mac.
384}
385
[email protected]7d791652010-12-01 16:34:49386
387bool BrowserWindowCocoa::IsBookmarkBarVisible() const {
[email protected]a007e732011-08-05 13:32:19388 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
[email protected]7d791652010-12-01 16:34:49389}
390
391bool BrowserWindowCocoa::IsBookmarkBarAnimating() const {
392 return [controller_ isBookmarkBarAnimating];
393}
394
[email protected]95bf8a5b2010-12-22 16:04:07395bool BrowserWindowCocoa::IsTabStripEditable() const {
396 return ![controller_ isDragSessionActive];
397}
398
[email protected]7d791652010-12-01 16:34:49399bool BrowserWindowCocoa::IsToolbarVisible() const {
400 return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
401 browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
402}
403
[email protected]b7a756d42012-01-23 18:08:17404gfx::Rect BrowserWindowCocoa::GetRootWindowResizerRect() const {
[email protected]9f8727092012-02-08 01:38:18405 if (IsDownloadShelfVisible())
406 return gfx::Rect();
[email protected]b7a756d42012-01-23 18:08:17407 NSRect tabRect = [controller_ selectedTabGrowBoxRect];
408 return gfx::Rect(NSRectToCGRect(tabRect));
409}
410
[email protected]5f58c2af2012-01-11 17:55:29411bool BrowserWindowCocoa::IsPanel() const {
412 return false;
413}
414
[email protected]7d791652010-12-01 16:34:49415// This is called from Browser, which in turn is called directly from
416// a menu option. All we do here is set a preference. The act of
417// setting the preference sends notifications to all windows who then
418// know what to do.
419void BrowserWindowCocoa::ToggleBookmarkBar() {
[email protected]03020ff2012-11-29 12:08:17420 chrome::ToggleBookmarkBarWhenVisible(browser_->profile());
[email protected]7d791652010-12-01 16:34:49421}
422
423void BrowserWindowCocoa::AddFindBar(
424 FindBarCocoaController* find_bar_cocoa_controller) {
[email protected]632983f2011-08-08 22:51:24425 [controller_ addFindBar:find_bar_cocoa_controller];
[email protected]7d791652010-12-01 16:34:49426}
427
[email protected]7d791652010-12-01 16:34:49428void BrowserWindowCocoa::ShowUpdateChromeDialog() {
[email protected]678dae82011-02-11 20:03:08429 restart_browser::RequestRestart(window());
[email protected]7d791652010-12-01 16:34:49430}
431
432void BrowserWindowCocoa::ShowTaskManager() {
[email protected]898febec2012-05-31 18:15:48433 TaskManagerMac::Show(false);
[email protected]adb6a84d2011-02-07 16:58:40434}
435
436void BrowserWindowCocoa::ShowBackgroundPages() {
[email protected]898febec2012-05-31 18:15:48437 TaskManagerMac::Show(true);
[email protected]7d791652010-12-01 16:34:49438}
439
440void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url,
441 bool already_bookmarked) {
442 [controller_ showBookmarkBubbleForURL:url
443 alreadyBookmarked:(already_bookmarked ? YES : NO)];
444}
445
[email protected]b106ca52012-03-13 06:08:46446void BrowserWindowCocoa::ShowChromeToMobileBubble() {
[email protected]24bbcdd2012-03-27 03:14:17447 [controller_ showChromeToMobileBubble];
[email protected]b106ca52012-03-13 06:08:46448}
449
[email protected]235a0562012-03-26 22:22:35450#if defined(ENABLE_ONE_CLICK_SIGNIN)
[email protected]fe54b4a2012-03-28 16:11:04451void BrowserWindowCocoa::ShowOneClickSigninBubble(
[email protected]e4b66bf2012-05-29 20:39:51452 const StartSyncCallback& start_sync_callback) {
[email protected]fe54b4a2012-03-28 16:11:04453 OneClickSigninBubbleController* bubble_controller =
454 [[OneClickSigninBubbleController alloc]
455 initWithBrowserWindowController:cocoa_controller()
[email protected]e4b66bf2012-05-29 20:39:51456 start_sync_callback:start_sync_callback];
[email protected]fe54b4a2012-03-28 16:11:04457 [bubble_controller showWindow:nil];
[email protected]235a0562012-03-26 22:22:35458}
459#endif
460
[email protected]7d791652010-12-01 16:34:49461bool BrowserWindowCocoa::IsDownloadShelfVisible() const {
462 return [controller_ isDownloadShelfVisible] != NO;
463}
464
465DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() {
466 DownloadShelfController* shelfController = [controller_ downloadShelf];
467 return [shelfController bridge];
468}
469
[email protected]7d791652010-12-01 16:34:49470// We allow closing the window here since the real quit decision on Mac is made
471// in [AppController quit:].
472void BrowserWindowCocoa::ConfirmBrowserCloseWithPendingDownloads() {
473 // Call InProgressDownloadResponse asynchronously to avoid a crash when the
474 // browser window is closed here (http://crbug.com/44454).
[email protected]a932d9e2011-09-29 01:14:34475 MessageLoop::current()->PostTask(FROM_HERE,
476 base::Bind(&Browser::InProgressDownloadResponse,
477 confirm_close_factory_.GetWeakPtr(), true));
[email protected]7d791652010-12-01 16:34:49478}
479
[email protected]7d791652010-12-01 16:34:49480void BrowserWindowCocoa::UserChangedTheme() {
481 [controller_ userChangedTheme];
482}
483
484int BrowserWindowCocoa::GetExtraRenderViewHeight() const {
485 // Currently this is only used on linux.
486 return 0;
487}
488
[email protected]2a6bc3e2011-12-28 23:51:33489void BrowserWindowCocoa::WebContentsFocused(WebContents* contents) {
[email protected]7d791652010-12-01 16:34:49490 NOTIMPLEMENTED();
491}
492
[email protected]4eb4ec02012-06-19 15:54:21493void BrowserWindowCocoa::ShowPageInfo(WebContents* web_contents,
[email protected]7d791652010-12-01 16:34:49494 const GURL& url,
[email protected]d583e3f22011-12-27 21:38:17495 const SSLStatus& ssl,
[email protected]7d791652010-12-01 16:34:49496 bool show_history) {
[email protected]684dace42012-07-01 14:30:41497 chrome::ShowPageInfoBubble(window(), web_contents, url, ssl, show_history,
498 browser_);
[email protected]7d791652010-12-01 16:34:49499}
500
[email protected]b8e681e82012-02-20 10:18:47501void BrowserWindowCocoa::ShowWebsiteSettings(
502 Profile* profile,
[email protected]36ad4aa82012-11-21 19:20:48503 content::WebContents* web_contents,
[email protected]b8e681e82012-02-20 10:18:47504 const GURL& url,
505 const content::SSLStatus& ssl,
506 bool show_history) {
[email protected]5436c8fd2012-07-02 11:30:46507 WebsiteSettingsUIBridge::Show(
[email protected]36ad4aa82012-11-21 19:20:48508 window(), profile, web_contents, url, ssl);
[email protected]b8e681e82012-02-20 10:18:47509}
510
[email protected]7d791652010-12-01 16:34:49511void BrowserWindowCocoa::ShowAppMenu() {
512 // No-op. Mac doesn't support showing the menus via alt keys.
513}
514
515bool BrowserWindowCocoa::PreHandleKeyboardEvent(
516 const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) {
[email protected]3a1381d2011-08-26 18:12:13517 if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
[email protected]7d791652010-12-01 16:34:49518 return false;
519
[email protected]d2885ba2012-08-17 10:23:12520 if (event.type == WebKit::WebInputEvent::RawKeyDown &&
521 [controller_ handledByExtensionCommand:event.os_event])
522 return true;
523
[email protected]3a1381d2011-08-26 18:12:13524 int id = [BrowserWindowUtils getCommandId:event];
[email protected]7d791652010-12-01 16:34:49525 if (id == -1)
526 return false;
527
[email protected]5d98294912012-06-27 22:57:40528 if (browser_->command_controller()->IsReservedCommandOrKey(id, event)) {
[email protected]3a1381d2011-08-26 18:12:13529 return [BrowserWindowUtils handleKeyboardEvent:event.os_event
530 inWindow:window()];
531 }
[email protected]7d791652010-12-01 16:34:49532
[email protected]3a1381d2011-08-26 18:12:13533 DCHECK(is_keyboard_shortcut);
[email protected]7d791652010-12-01 16:34:49534 *is_keyboard_shortcut = true;
[email protected]7d791652010-12-01 16:34:49535 return false;
536}
537
538void BrowserWindowCocoa::HandleKeyboardEvent(
539 const NativeWebKeyboardEvent& event) {
[email protected]3a1381d2011-08-26 18:12:13540 if ([BrowserWindowUtils shouldHandleKeyboardEvent:event])
541 [BrowserWindowUtils handleKeyboardEvent:event.os_event inWindow:window()];
[email protected]7d791652010-12-01 16:34:49542}
543
[email protected]7d791652010-12-01 16:34:49544void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog(
[email protected]1c321ee2012-05-21 03:02:34545 Profile* profile, const extensions::Extension* app) {
[email protected]7d791652010-12-01 16:34:49546 NOTIMPLEMENTED();
547}
548
549void BrowserWindowCocoa::Cut() {
550 [NSApp sendAction:@selector(cut:) to:nil from:nil];
551}
552
553void BrowserWindowCocoa::Copy() {
554 [NSApp sendAction:@selector(copy:) to:nil from:nil];
555}
556
557void BrowserWindowCocoa::Paste() {
558 [NSApp sendAction:@selector(paste:) to:nil from:nil];
559}
560
[email protected]7d791652010-12-01 16:34:49561void BrowserWindowCocoa::OpenTabpose() {
562 [controller_ openTabpose];
563}
564
[email protected]8d944b32011-10-17 06:11:53565void BrowserWindowCocoa::EnterPresentationMode(
566 const GURL& url,
567 FullscreenExitBubbleType bubble_type) {
[email protected]22d74a6b2011-10-17 20:30:31568 [controller_ enterPresentationModeForURL:url
569 bubbleType:bubble_type];
[email protected]8d944b32011-10-17 06:11:53570}
571
572void BrowserWindowCocoa::ExitPresentationMode() {
[email protected]22d74a6b2011-10-17 20:30:31573 [controller_ exitPresentationMode];
[email protected]d3766932011-08-04 22:18:23574}
575
576bool BrowserWindowCocoa::InPresentationMode() {
577 return [controller_ inPresentationMode];
578}
579
[email protected]7d791652010-12-01 16:34:49580gfx::Rect BrowserWindowCocoa::GetInstantBounds() {
581 // Flip coordinates based on the primary screen.
582 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
583 NSRect monitorFrame = [screen frame];
584 NSRect frame = [controller_ instantFrame];
585 gfx::Rect bounds(NSRectToCGRect(frame));
586 bounds.set_y(NSHeight(monitorFrame) - bounds.y() - bounds.height());
587 return bounds;
588}
589
[email protected]85fd03e2012-09-04 22:50:14590bool BrowserWindowCocoa::IsInstantTabShowing() {
591 return [controller_ isInstantTabShowing];
592}
593
[email protected]588300d2011-04-28 21:06:35594WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds(
595 const gfx::Rect& bounds) {
[email protected]d82a3c9f2012-02-22 04:46:00596 // In Lion fullscreen mode, convert popups into tabs.
597 if (base::mac::IsOSLionOrLater() && IsFullscreen())
598 return NEW_FOREGROUND_TAB;
[email protected]588300d2011-04-28 21:06:35599 return NEW_POPUP;
600}
601
[email protected]632983f2011-08-08 22:51:24602FindBar* BrowserWindowCocoa::CreateFindBar() {
603 // We could push the AddFindBar() call into the FindBarBridge
604 // constructor or the FindBarCocoaController init, but that makes
605 // unit testing difficult, since we would also require a
606 // BrowserWindow object.
607 FindBarBridge* bridge = new FindBarBridge();
608 AddFindBar(bridge->find_bar_cocoa_controller());
609 return bridge;
610}
611
[email protected]d3afce92012-10-07 05:47:36612bool BrowserWindowCocoa::GetConstrainedWindowTopY(int* top_y) {
613 return false;
[email protected]6a151762012-10-03 22:33:19614}
615
[email protected]78ce3022012-09-24 01:48:48616extensions::ActiveTabPermissionGranter*
617 BrowserWindowCocoa::GetActiveTabPermissionGranter() {
[email protected]852934842012-12-10 16:50:13618 WebContents* web_contents =
619 browser_->tab_strip_model()->GetActiveWebContents();
620 if (!web_contents)
[email protected]78ce3022012-09-24 01:48:48621 return NULL;
622 extensions::TabHelper* tab_helper =
[email protected]852934842012-12-10 16:50:13623 extensions::TabHelper::FromWebContents(web_contents);
[email protected]78ce3022012-09-24 01:48:48624 return tab_helper ? tab_helper->active_tab_permission_granter() : NULL;
625}
626
[email protected]7d791652010-12-01 16:34:49627void BrowserWindowCocoa::DestroyBrowser() {
628 [controller_ destroyBrowser];
629
630 // at this point the controller is dead (autoreleased), so
631 // make sure we don't try to reference it any more.
632}
633
634NSWindow* BrowserWindowCocoa::window() const {
635 return [controller_ window];
636}
637
[email protected]0ec4898e2011-12-30 21:09:24638void BrowserWindowCocoa::ShowAvatarBubble(WebContents* web_contents,
[email protected]2f733a02011-10-06 15:17:34639 const gfx::Rect& rect) {
[email protected]0ec4898e2011-12-30 21:09:24640 NSView* view = web_contents->GetNativeView();
[email protected]79ac3cb2011-10-12 17:11:21641 NSRect bounds = [view bounds];
642 NSPoint point;
643 point.x = NSMinX(bounds) + rect.right();
644 // The view's origin is at the bottom but |rect|'s origin is at the top.
645 point.y = NSMaxY(bounds) - rect.bottom();
[email protected]2f733a02011-10-06 15:17:34646 point = [view convertPoint:point toView:nil];
647 point = [[view window] convertBaseToScreen:point];
648
649 // |menu| will automatically release itself on close.
650 AvatarMenuBubbleController* menu =
651 [[AvatarMenuBubbleController alloc] initWithBrowser:browser_
652 anchoredAt:point];
[email protected]51152d6d2011-11-07 01:55:12653 [[menu bubble] setAlignment:info_bubble::kAlignEdgeToAnchorEdge];
[email protected]2f733a02011-10-06 15:17:34654 [menu showWindow:nil];
655}
[email protected]7d2d08152011-10-25 22:58:47656
657void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton() {
658 [[controller_ avatarButtonController] showAvatarBubble];
659}