[email protected] | 3a80ea33 | 2012-01-09 19:53:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 2 | // 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] | a932d9e | 2011-09-29 01:14:34 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 8 | #include "base/command_line.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/message_loop.h" |
| 11 | #include "base/sys_string_conversions.h" |
| 12 | #include "chrome/app/chrome_command_ids.h" |
| 13 | #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 14 | #include "chrome/browser/download/download_shelf.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 15 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser.h" |
| 18 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 7d2d0815 | 2011-10-25 22:58:47 | [diff] [blame] | 19 | #import "chrome/browser/ui/cocoa/browser/avatar_button_controller.h" |
[email protected] | 2f733a0 | 2011-10-06 15:17:34 | [diff] [blame] | 20 | #import "chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.h" |
[email protected] | 464f001 | 2011-07-07 02:12:51 | [diff] [blame] | 21 | #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 22 | #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 23 | #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 24 | #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
[email protected] | fa810229 | 2011-01-20 16:21:23 | [diff] [blame] | 25 | #import "chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 26 | #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame] | 27 | #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 28 | #import "chrome/browser/ui/cocoa/html_dialog_window_controller.h" |
[email protected] | 51152d6d | 2011-11-07 01:55:12 | [diff] [blame] | 29 | #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 30 | #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
[email protected] | a7d83ca | 2011-03-06 14:41:07 | [diff] [blame] | 31 | #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 32 | #include "chrome/browser/ui/cocoa/restart_browser.h" |
| 33 | #include "chrome/browser/ui/cocoa/status_bubble_mac.h" |
| 34 | #include "chrome/browser/ui/cocoa/task_manager_mac.h" |
[email protected] | 8450c4f | 2011-01-19 22:16:22 | [diff] [blame] | 35 | #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
[email protected] | 7e2514c | 2011-11-28 20:04:45 | [diff] [blame] | 36 | #include "chrome/browser/ui/page_info_bubble.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 37 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
[email protected] | 0bf4dc62 | 2011-12-19 06:59:44 | [diff] [blame] | 38 | #include "chrome/browser/ui/webui/chrome_web_ui.h" |
[email protected] | 7b93736 | 2011-10-05 08:28:07 | [diff] [blame] | 39 | #include "chrome/browser/ui/webui/task_manager_dialog.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 40 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 41 | #include "chrome/common/pref_names.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 42 | #include "content/public/browser/notification_source.h" |
[email protected] | 8bc38d2 | 2011-10-14 20:39:20 | [diff] [blame] | 43 | #include "content/public/browser/native_web_keyboard_event.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 44 | #include "content/public/browser/notification_details.h" |
[email protected] | e8558cf2 | 2012-01-10 03:10:51 | [diff] [blame^] | 45 | #include "content/public/browser/web_contents.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 46 | #include "grit/chromium_strings.h" |
| 47 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 48 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 49 | #include "ui/gfx/rect.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 50 | |
[email protected] | d583e3f2 | 2011-12-27 21:38:17 | [diff] [blame] | 51 | using content::SSLStatus; |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 52 | using content::WebContents; |
[email protected] | d583e3f2 | 2011-12-27 21:38:17 | [diff] [blame] | 53 | |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 54 | // Replicate specific 10.7 SDK declarations for building with prior SDKs. |
| 55 | #if !defined(MAC_OS_X_VERSION_10_7) || \ |
| 56 | MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
| 57 | |
| 58 | enum { |
| 59 | NSWindowAnimationBehaviorDefault = 0, |
| 60 | NSWindowAnimationBehaviorNone = 2, |
| 61 | NSWindowAnimationBehaviorDocumentWindow = 3, |
| 62 | NSWindowAnimationBehaviorUtilityWindow = 4, |
| 63 | NSWindowAnimationBehaviorAlertPanel = 5 |
| 64 | }; |
| 65 | typedef NSInteger NSWindowAnimationBehavior; |
| 66 | |
| 67 | @interface NSWindow (LionSDKDeclarations) |
| 68 | - (NSWindowAnimationBehavior)animationBehavior; |
| 69 | - (void)setAnimationBehavior:(NSWindowAnimationBehavior)newAnimationBehavior; |
| 70 | @end |
| 71 | |
| 72 | #endif // MAC_OS_X_VERSION_10_7 |
| 73 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 74 | BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser, |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 75 | BrowserWindowController* controller) |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 76 | : browser_(browser), |
| 77 | controller_(controller), |
| 78 | confirm_close_factory_(browser) { |
[email protected] | cfecf943 | 2011-09-27 17:10:08 | [diff] [blame] | 79 | |
[email protected] | 3710d000 | 2011-10-11 00:35:00 | [diff] [blame] | 80 | pref_change_registrar_.Init(browser_->profile()->GetPrefs()); |
| 81 | pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
| 82 | |
[email protected] | cfecf943 | 2011-09-27 17:10:08 | [diff] [blame] | 83 | initial_show_state_ = browser_->GetSavedWindowShowState(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | BrowserWindowCocoa::~BrowserWindowCocoa() { |
| 87 | } |
| 88 | |
| 89 | void BrowserWindowCocoa::Show() { |
| 90 | // The Browser associated with this browser window must become the active |
| 91 | // browser at the time |Show()| is called. This is the natural behaviour under |
| 92 | // Windows, but |-makeKeyAndOrderFront:| won't send |-windowDidBecomeMain:| |
| 93 | // until we return to the runloop. Therefore any calls to |
| 94 | // |BrowserList::GetLastActive()| (for example, in bookmark_util), will return |
| 95 | // the previous browser instead if we don't explicitly set it here. |
| 96 | BrowserList::SetLastActive(browser_); |
| 97 | |
[email protected] | 8bc061f | 2011-08-31 22:46:23 | [diff] [blame] | 98 | bool is_session_restore = browser_->is_session_restore(); |
[email protected] | b5cc93c | 2011-09-02 02:33:42 | [diff] [blame] | 99 | NSWindowAnimationBehavior saved_animation_behavior = |
| 100 | NSWindowAnimationBehaviorDefault; |
[email protected] | 8bc061f | 2011-08-31 22:46:23 | [diff] [blame] | 101 | bool did_save_animation_behavior = false; |
| 102 | // Turn off swishing when restoring windows. |
| 103 | if (is_session_restore && |
| 104 | [window() respondsToSelector:@selector(animationBehavior)] && |
| 105 | [window() respondsToSelector:@selector(setAnimationBehavior:)]) { |
| 106 | did_save_animation_behavior = true; |
| 107 | saved_animation_behavior = [window() animationBehavior]; |
| 108 | [window() setAnimationBehavior:NSWindowAnimationBehaviorNone]; |
| 109 | } |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 110 | |
[email protected] | 8bc061f | 2011-08-31 22:46:23 | [diff] [blame] | 111 | [window() makeKeyAndOrderFront:controller_]; |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 112 | |
[email protected] | 8bc061f | 2011-08-31 22:46:23 | [diff] [blame] | 113 | // When creating windows from nibs it is necessary to |makeKeyAndOrderFront:| |
| 114 | // prior to |orderOut:| then |miniaturize:| when restoring windows in the |
| 115 | // minimized state. |
[email protected] | cfecf943 | 2011-09-27 17:10:08 | [diff] [blame] | 116 | if (initial_show_state_ == ui::SHOW_STATE_MINIMIZED) { |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 117 | [window() orderOut:controller_]; |
| 118 | [window() miniaturize:controller_]; |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 119 | } |
[email protected] | cfecf943 | 2011-09-27 17:10:08 | [diff] [blame] | 120 | initial_show_state_ = ui::SHOW_STATE_DEFAULT; |
[email protected] | 8bc061f | 2011-08-31 22:46:23 | [diff] [blame] | 121 | |
| 122 | // Restore window animation behavior. |
| 123 | if (did_save_animation_behavior) |
| 124 | [window() setAnimationBehavior:saved_animation_behavior]; |
[email protected] | 2f516c79 | 2011-09-19 22:22:09 | [diff] [blame] | 125 | |
| 126 | browser_->OnWindowDidShow(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 129 | void BrowserWindowCocoa::ShowInactive() { |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 130 | [window() orderFront:controller_]; |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 133 | void BrowserWindowCocoa::SetBounds(const gfx::Rect& bounds) { |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 134 | gfx::Rect real_bounds = [controller_ enforceMinWindowSize:bounds]; |
| 135 | |
[email protected] | 55c87fa | 2011-10-15 07:28:28 | [diff] [blame] | 136 | ExitFullscreen(); |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 137 | NSRect cocoa_bounds = NSMakeRect(real_bounds.x(), 0, |
| 138 | real_bounds.width(), |
| 139 | real_bounds.height()); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 140 | // Flip coordinates based on the primary screen. |
| 141 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 142 | cocoa_bounds.origin.y = |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 143 | [screen frame].size.height - real_bounds.height() - real_bounds.y(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 144 | |
| 145 | [window() setFrame:cocoa_bounds display:YES]; |
| 146 | } |
| 147 | |
| 148 | // Callers assume that this doesn't immediately delete the Browser object. |
| 149 | // The controller implementing the window delegate methods called from |
| 150 | // |-performClose:| must take precautions to ensure that. |
| 151 | void BrowserWindowCocoa::Close() { |
| 152 | // If there is an overlay window, we contain a tab being dragged between |
| 153 | // windows. Don't hide the window as it makes the UI extra confused. We can |
| 154 | // still close the window, as that will happen when the drag completes. |
| 155 | if ([controller_ overlayWindow]) { |
| 156 | [controller_ deferPerformClose]; |
| 157 | } else { |
| 158 | // Make sure we hide the window immediately. Even though performClose: |
| 159 | // calls orderOut: eventually, it leaves the window on-screen long enough |
| 160 | // that we start to see tabs shutting down. http://crbug.com/23959 |
| 161 | // TODO(viettrungluu): This is kind of bad, since |-performClose:| calls |
| 162 | // |-windowShouldClose:| (on its delegate, which is probably the |
| 163 | // controller) which may return |NO| causing the window to not be closed, |
| 164 | // thereby leaving a hidden window. In fact, our window-closing procedure |
| 165 | // involves a (indirect) recursion on |-performClose:|, which is also bad. |
| 166 | [window() orderOut:controller_]; |
| 167 | [window() performClose:controller_]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void BrowserWindowCocoa::Activate() { |
| 172 | [controller_ activate]; |
| 173 | } |
| 174 | |
| 175 | void BrowserWindowCocoa::Deactivate() { |
| 176 | // TODO(jcivelli): http://crbug.com/51364 Implement me. |
| 177 | NOTIMPLEMENTED(); |
| 178 | } |
| 179 | |
| 180 | void BrowserWindowCocoa::FlashFrame() { |
| 181 | [NSApp requestUserAttention:NSInformationalRequest]; |
| 182 | } |
| 183 | |
| 184 | bool BrowserWindowCocoa::IsActive() const { |
| 185 | return [window() isKeyWindow]; |
| 186 | } |
| 187 | |
| 188 | gfx::NativeWindow BrowserWindowCocoa::GetNativeHandle() { |
| 189 | return window(); |
| 190 | } |
| 191 | |
| 192 | BrowserWindowTesting* BrowserWindowCocoa::GetBrowserWindowTesting() { |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | StatusBubble* BrowserWindowCocoa::GetStatusBubble() { |
| 197 | return [controller_ statusBubble]; |
| 198 | } |
| 199 | |
[email protected] | c9bd2e8 | 2011-04-15 23:28:19 | [diff] [blame] | 200 | void BrowserWindowCocoa::ToolbarSizeChanged(bool is_animating) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 201 | // According to beng, this is an ugly method that comes from the days when the |
[email protected] | e8558cf2 | 2012-01-10 03:10:51 | [diff] [blame^] | 202 | // download shelf was a ChromeView attached to the WebContents, and as its |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 203 | // size changed via animation it notified through TCD/etc to the browser view |
| 204 | // to relayout for each tick of the animation. We don't need anything of the |
| 205 | // sort on Mac. |
| 206 | } |
| 207 | |
| 208 | void BrowserWindowCocoa::UpdateTitleBar() { |
| 209 | NSString* newTitle = |
| 210 | base::SysUTF16ToNSString(browser_->GetWindowTitleForCurrentTab()); |
| 211 | |
[email protected] | d78e615e | 2011-09-06 21:46:03 | [diff] [blame] | 212 | pending_window_title_.reset( |
| 213 | [BrowserWindowUtils scheduleReplaceOldTitle:pending_window_title_.get() |
| 214 | withNewTitle:newTitle |
| 215 | forWindow:window()]); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 09b2934 | 2011-06-24 19:18:48 | [diff] [blame] | 218 | void BrowserWindowCocoa::BookmarkBarStateChanged( |
| 219 | BookmarkBar::AnimateChangeType change_type) { |
| 220 | // TODO: route changes to state through this. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void BrowserWindowCocoa::UpdateDevTools() { |
| 224 | [controller_ updateDevToolsForContents: |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 225 | browser_->GetSelectedWebContents()]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 853e16eb | 2011-12-29 16:58:19 | [diff] [blame] | 228 | void BrowserWindowCocoa::SetDevToolsDockSide(DevToolsDockSide side) { |
| 229 | [controller_ setDevToolsDockToRight:side == DEVTOOLS_DOCK_SIDE_RIGHT]; |
| 230 | } |
| 231 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 232 | void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) { |
| 233 | // Do nothing on Mac. |
| 234 | } |
| 235 | |
| 236 | void BrowserWindowCocoa::SetStarredState(bool is_starred) { |
| 237 | [controller_ setStarredState:is_starred ? YES : NO]; |
| 238 | } |
| 239 | |
| 240 | gfx::Rect BrowserWindowCocoa::GetRestoredBounds() const { |
| 241 | // Flip coordinates based on the primary screen. |
| 242 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 243 | NSRect frame = [controller_ regularWindowFrame]; |
| 244 | gfx::Rect bounds(frame.origin.x, 0, frame.size.width, frame.size.height); |
| 245 | bounds.set_y([screen frame].size.height - frame.origin.y - frame.size.height); |
| 246 | return bounds; |
| 247 | } |
| 248 | |
[email protected] | d479b8e2 | 2011-02-09 05:19:49 | [diff] [blame] | 249 | gfx::Rect BrowserWindowCocoa::GetBounds() const { |
| 250 | return GetRestoredBounds(); |
| 251 | } |
| 252 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 253 | bool BrowserWindowCocoa::IsMaximized() const { |
| 254 | return [window() isZoomed]; |
| 255 | } |
| 256 | |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 257 | bool BrowserWindowCocoa::IsMinimized() const { |
| 258 | return [window() isMiniaturized]; |
| 259 | } |
| 260 | |
[email protected] | 541e3ee | 2011-11-22 01:31:10 | [diff] [blame] | 261 | void BrowserWindowCocoa::Maximize() { |
| 262 | // Zoom toggles so only call if not already maximized. |
| 263 | if (!IsMaximized()) |
| 264 | [window() zoom:controller_]; |
| 265 | } |
| 266 | |
| 267 | void BrowserWindowCocoa::Minimize() { |
| 268 | [window() miniaturize:controller_]; |
| 269 | } |
| 270 | |
| 271 | void BrowserWindowCocoa::Restore() { |
| 272 | if (IsMaximized()) |
| 273 | [window() zoom:controller_]; // Toggles zoom mode. |
| 274 | else if (IsMinimized()) |
| 275 | [window() deminiaturize:controller_]; |
| 276 | } |
| 277 | |
[email protected] | 8d944b3 | 2011-10-17 06:11:53 | [diff] [blame] | 278 | void BrowserWindowCocoa::EnterFullscreen( |
[email protected] | 22d74a6b | 2011-10-17 20:30:31 | [diff] [blame] | 279 | const GURL& url, FullscreenExitBubbleType bubble_type) { |
| 280 | [controller_ enterFullscreenForURL:url |
| 281 | bubbleType:bubble_type]; |
[email protected] | 55c87fa | 2011-10-15 07:28:28 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void BrowserWindowCocoa::ExitFullscreen() { |
[email protected] | 22d74a6b | 2011-10-17 20:30:31 | [diff] [blame] | 285 | [controller_ exitFullscreen]; |
[email protected] | 8d944b3 | 2011-10-17 06:11:53 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void BrowserWindowCocoa::UpdateFullscreenExitBubbleContent( |
| 289 | const GURL& url, |
| 290 | FullscreenExitBubbleType bubble_type) { |
[email protected] | 22d74a6b | 2011-10-17 20:30:31 | [diff] [blame] | 291 | [controller_ updateFullscreenExitBubbleURL:url bubbleType:bubble_type]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | bool BrowserWindowCocoa::IsFullscreen() const { |
[email protected] | 8d944b3 | 2011-10-17 06:11:53 | [diff] [blame] | 295 | return [controller_ isFullscreen]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | void BrowserWindowCocoa::ConfirmAddSearchProvider( |
| 303 | const TemplateURL* template_url, |
| 304 | Profile* profile) { |
[email protected] | 464f001 | 2011-07-07 02:12:51 | [diff] [blame] | 305 | // The controller will release itself when the window closes. |
| 306 | EditSearchEngineCocoaController* editor = |
| 307 | [[EditSearchEngineCocoaController alloc] initWithProfile:profile |
| 308 | delegate:NULL |
| 309 | templateURL:template_url]; |
| 310 | [NSApp beginSheet:[editor window] |
| 311 | modalForWindow:window() |
| 312 | modalDelegate:controller_ |
| 313 | didEndSelector:@selector(sheetDidEnd:returnCode:context:) |
| 314 | contextInfo:NULL]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | LocationBar* BrowserWindowCocoa::GetLocationBar() const { |
| 318 | return [controller_ locationBarBridge]; |
| 319 | } |
| 320 | |
| 321 | void BrowserWindowCocoa::SetFocusToLocationBar(bool select_all) { |
| 322 | [controller_ focusLocationBar:select_all ? YES : NO]; |
| 323 | } |
| 324 | |
| 325 | void BrowserWindowCocoa::UpdateReloadStopState(bool is_loading, bool force) { |
| 326 | [controller_ setIsLoading:is_loading force:force]; |
| 327 | } |
| 328 | |
| 329 | void BrowserWindowCocoa::UpdateToolbar(TabContentsWrapper* contents, |
| 330 | bool should_restore_state) { |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 331 | [controller_ updateToolbarWithContents:contents->web_contents() |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 332 | shouldRestoreState:should_restore_state ? YES : NO]; |
| 333 | } |
| 334 | |
| 335 | void BrowserWindowCocoa::FocusToolbar() { |
| 336 | // Not needed on the Mac. |
| 337 | } |
| 338 | |
| 339 | void BrowserWindowCocoa::FocusAppMenu() { |
| 340 | // Chrome uses the standard Mac OS X menu bar, so this isn't needed. |
| 341 | } |
| 342 | |
| 343 | void BrowserWindowCocoa::RotatePaneFocus(bool forwards) { |
| 344 | // Not needed on the Mac. |
| 345 | } |
| 346 | |
| 347 | void BrowserWindowCocoa::FocusBookmarksToolbar() { |
| 348 | // Not needed on the Mac. |
| 349 | } |
| 350 | |
| 351 | void BrowserWindowCocoa::FocusChromeOSStatus() { |
| 352 | // Not needed on the Mac. |
| 353 | } |
| 354 | |
| 355 | bool BrowserWindowCocoa::IsBookmarkBarVisible() const { |
[email protected] | a007e73 | 2011-08-05 13:32:19 | [diff] [blame] | 356 | return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | bool BrowserWindowCocoa::IsBookmarkBarAnimating() const { |
| 360 | return [controller_ isBookmarkBarAnimating]; |
| 361 | } |
| 362 | |
[email protected] | 95bf8a5b | 2010-12-22 16:04:07 | [diff] [blame] | 363 | bool BrowserWindowCocoa::IsTabStripEditable() const { |
| 364 | return ![controller_ isDragSessionActive]; |
| 365 | } |
| 366 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 367 | bool BrowserWindowCocoa::IsToolbarVisible() const { |
| 368 | return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) || |
| 369 | browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR); |
| 370 | } |
| 371 | |
| 372 | // This is called from Browser, which in turn is called directly from |
| 373 | // a menu option. All we do here is set a preference. The act of |
| 374 | // setting the preference sends notifications to all windows who then |
| 375 | // know what to do. |
| 376 | void BrowserWindowCocoa::ToggleBookmarkBar() { |
| 377 | bookmark_utils::ToggleWhenVisible(browser_->profile()); |
| 378 | } |
| 379 | |
| 380 | void BrowserWindowCocoa::AddFindBar( |
| 381 | FindBarCocoaController* find_bar_cocoa_controller) { |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame] | 382 | [controller_ addFindBar:find_bar_cocoa_controller]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | 4c6b474b7 | 2011-02-24 21:35:21 | [diff] [blame] | 385 | void BrowserWindowCocoa::ShowAboutChromeDialog() { |
[email protected] | 287b9060 | 2011-03-02 22:43:27 | [diff] [blame] | 386 | // Go through AppController's implementation to bring up the branded panel. |
| 387 | [[NSApp delegate] orderFrontStandardAboutPanel:nil]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void BrowserWindowCocoa::ShowUpdateChromeDialog() { |
[email protected] | 678dae8 | 2011-02-11 20:03:08 | [diff] [blame] | 391 | restart_browser::RequestRestart(window()); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void BrowserWindowCocoa::ShowTaskManager() { |
[email protected] | 7b93736 | 2011-10-05 08:28:07 | [diff] [blame] | 395 | #if defined(WEBUI_TASK_MANAGER) |
| 396 | TaskManagerDialog::Show(); |
| 397 | #else |
| 398 | // Uses WebUI TaskManager when swiches is set. It is beta feature. |
[email protected] | 21e6caf8 | 2012-01-06 23:44:20 | [diff] [blame] | 399 | if (chrome_web_ui::IsMoreWebUI()) { |
[email protected] | 7b93736 | 2011-10-05 08:28:07 | [diff] [blame] | 400 | TaskManagerDialog::Show(); |
| 401 | } else { |
| 402 | TaskManagerMac::Show(false); |
| 403 | } |
| 404 | #endif // defined(WEBUI_TASK_MANAGER) |
[email protected] | adb6a84d | 2011-02-07 16:58:40 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void BrowserWindowCocoa::ShowBackgroundPages() { |
[email protected] | 7b93736 | 2011-10-05 08:28:07 | [diff] [blame] | 408 | #if defined(WEBUI_TASK_MANAGER) |
| 409 | TaskManagerDialog::ShowBackgroundPages(); |
| 410 | #else |
| 411 | // Uses WebUI TaskManager when swiches is set. It is beta feature. |
[email protected] | 21e6caf8 | 2012-01-06 23:44:20 | [diff] [blame] | 412 | if (chrome_web_ui::IsMoreWebUI()) { |
[email protected] | 7b93736 | 2011-10-05 08:28:07 | [diff] [blame] | 413 | TaskManagerDialog::ShowBackgroundPages(); |
| 414 | } else { |
| 415 | TaskManagerMac::Show(true); |
| 416 | } |
| 417 | #endif // defined(WEBUI_TASK_MANAGER) |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url, |
| 421 | bool already_bookmarked) { |
| 422 | [controller_ showBookmarkBubbleForURL:url |
| 423 | alreadyBookmarked:(already_bookmarked ? YES : NO)]; |
| 424 | } |
| 425 | |
| 426 | bool BrowserWindowCocoa::IsDownloadShelfVisible() const { |
| 427 | return [controller_ isDownloadShelfVisible] != NO; |
| 428 | } |
| 429 | |
| 430 | DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() { |
| 431 | DownloadShelfController* shelfController = [controller_ downloadShelf]; |
| 432 | return [shelfController bridge]; |
| 433 | } |
| 434 | |
[email protected] | a3627e6 | 2011-09-07 00:04:57 | [diff] [blame] | 435 | void BrowserWindowCocoa::ShowCollectedCookiesDialog( |
| 436 | TabContentsWrapper* wrapper) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 437 | // Deletes itself on close. |
[email protected] | a3627e6 | 2011-09-07 00:04:57 | [diff] [blame] | 438 | new CollectedCookiesMac(GetNativeHandle(), wrapper); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 441 | // We allow closing the window here since the real quit decision on Mac is made |
| 442 | // in [AppController quit:]. |
| 443 | void BrowserWindowCocoa::ConfirmBrowserCloseWithPendingDownloads() { |
| 444 | // Call InProgressDownloadResponse asynchronously to avoid a crash when the |
| 445 | // browser window is closed here (http://crbug.com/44454). |
[email protected] | a932d9e | 2011-09-29 01:14:34 | [diff] [blame] | 446 | MessageLoop::current()->PostTask(FROM_HERE, |
| 447 | base::Bind(&Browser::InProgressDownloadResponse, |
| 448 | confirm_close_factory_.GetWeakPtr(), true)); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 451 | void BrowserWindowCocoa::UserChangedTheme() { |
| 452 | [controller_ userChangedTheme]; |
| 453 | } |
| 454 | |
| 455 | int BrowserWindowCocoa::GetExtraRenderViewHeight() const { |
| 456 | // Currently this is only used on linux. |
| 457 | return 0; |
| 458 | } |
| 459 | |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 460 | void BrowserWindowCocoa::WebContentsFocused(WebContents* contents) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 461 | NOTIMPLEMENTED(); |
| 462 | } |
| 463 | |
| 464 | void BrowserWindowCocoa::ShowPageInfo(Profile* profile, |
| 465 | const GURL& url, |
[email protected] | d583e3f2 | 2011-12-27 21:38:17 | [diff] [blame] | 466 | const SSLStatus& ssl, |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 467 | bool show_history) { |
| 468 | browser::ShowPageInfoBubble(window(), profile, url, ssl, show_history); |
| 469 | } |
| 470 | |
| 471 | void BrowserWindowCocoa::ShowAppMenu() { |
| 472 | // No-op. Mac doesn't support showing the menus via alt keys. |
| 473 | } |
| 474 | |
| 475 | bool BrowserWindowCocoa::PreHandleKeyboardEvent( |
| 476 | const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 477 | if (![BrowserWindowUtils shouldHandleKeyboardEvent:event]) |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 478 | return false; |
| 479 | |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 480 | int id = [BrowserWindowUtils getCommandId:event]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 481 | if (id == -1) |
| 482 | return false; |
| 483 | |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 484 | if (browser_->IsReservedCommandOrKey(id, event)) { |
| 485 | return [BrowserWindowUtils handleKeyboardEvent:event.os_event |
| 486 | inWindow:window()]; |
| 487 | } |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 488 | |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 489 | DCHECK(is_keyboard_shortcut); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 490 | *is_keyboard_shortcut = true; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
| 494 | void BrowserWindowCocoa::HandleKeyboardEvent( |
| 495 | const NativeWebKeyboardEvent& event) { |
[email protected] | 3a1381d | 2011-08-26 18:12:13 | [diff] [blame] | 496 | if ([BrowserWindowUtils shouldHandleKeyboardEvent:event]) |
| 497 | [BrowserWindowUtils handleKeyboardEvent:event.os_event inWindow:window()]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void BrowserWindowCocoa::ShowCreateWebAppShortcutsDialog( |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 501 | TabContentsWrapper* tab_contents) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 502 | NOTIMPLEMENTED(); |
| 503 | } |
| 504 | |
| 505 | void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog( |
| 506 | Profile* profile, const Extension* app) { |
| 507 | NOTIMPLEMENTED(); |
| 508 | } |
| 509 | |
| 510 | void BrowserWindowCocoa::Cut() { |
| 511 | [NSApp sendAction:@selector(cut:) to:nil from:nil]; |
| 512 | } |
| 513 | |
| 514 | void BrowserWindowCocoa::Copy() { |
| 515 | [NSApp sendAction:@selector(copy:) to:nil from:nil]; |
| 516 | } |
| 517 | |
| 518 | void BrowserWindowCocoa::Paste() { |
| 519 | [NSApp sendAction:@selector(paste:) to:nil from:nil]; |
| 520 | } |
| 521 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 522 | void BrowserWindowCocoa::OpenTabpose() { |
| 523 | [controller_ openTabpose]; |
| 524 | } |
| 525 | |
[email protected] | 8d944b3 | 2011-10-17 06:11:53 | [diff] [blame] | 526 | void BrowserWindowCocoa::EnterPresentationMode( |
| 527 | const GURL& url, |
| 528 | FullscreenExitBubbleType bubble_type) { |
[email protected] | 22d74a6b | 2011-10-17 20:30:31 | [diff] [blame] | 529 | [controller_ enterPresentationModeForURL:url |
| 530 | bubbleType:bubble_type]; |
[email protected] | 8d944b3 | 2011-10-17 06:11:53 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void BrowserWindowCocoa::ExitPresentationMode() { |
[email protected] | 22d74a6b | 2011-10-17 20:30:31 | [diff] [blame] | 534 | [controller_ exitPresentationMode]; |
[email protected] | d376693 | 2011-08-04 22:18:23 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | bool BrowserWindowCocoa::InPresentationMode() { |
| 538 | return [controller_ inPresentationMode]; |
| 539 | } |
| 540 | |
[email protected] | e3690ed | 2011-03-25 20:25:14 | [diff] [blame] | 541 | void BrowserWindowCocoa::ShowInstant(TabContentsWrapper* preview) { |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 542 | [controller_ showInstant:preview->web_contents()]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 543 | } |
| 544 | |
[email protected] | 3e48128 | 2011-10-15 15:39:50 | [diff] [blame] | 545 | void BrowserWindowCocoa::HideInstant() { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 546 | [controller_ hideInstant]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | gfx::Rect BrowserWindowCocoa::GetInstantBounds() { |
| 550 | // Flip coordinates based on the primary screen. |
| 551 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 552 | NSRect monitorFrame = [screen frame]; |
| 553 | NSRect frame = [controller_ instantFrame]; |
| 554 | gfx::Rect bounds(NSRectToCGRect(frame)); |
| 555 | bounds.set_y(NSHeight(monitorFrame) - bounds.y() - bounds.height()); |
| 556 | return bounds; |
| 557 | } |
| 558 | |
[email protected] | 588300d | 2011-04-28 21:06:35 | [diff] [blame] | 559 | WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds( |
| 560 | const gfx::Rect& bounds) { |
| 561 | return NEW_POPUP; |
| 562 | } |
| 563 | |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame] | 564 | FindBar* BrowserWindowCocoa::CreateFindBar() { |
| 565 | // We could push the AddFindBar() call into the FindBarBridge |
| 566 | // constructor or the FindBarCocoaController init, but that makes |
| 567 | // unit testing difficult, since we would also require a |
| 568 | // BrowserWindow object. |
| 569 | FindBarBridge* bridge = new FindBarBridge(); |
| 570 | AddFindBar(bridge->find_bar_cocoa_controller()); |
| 571 | return bridge; |
| 572 | } |
| 573 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 574 | void BrowserWindowCocoa::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 575 | const content::NotificationSource& source, |
| 576 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 577 | switch (type) { |
[email protected] | 3710d000 | 2011-10-11 00:35:00 | [diff] [blame] | 578 | case chrome::NOTIFICATION_PREF_CHANGED: { |
[email protected] | 7ac4b87 | 2011-10-19 03:30:42 | [diff] [blame] | 579 | const std::string& pref_name = |
| 580 | *content::Details<std::string>(details).ptr(); |
[email protected] | 3710d000 | 2011-10-11 00:35:00 | [diff] [blame] | 581 | DCHECK(pref_name == prefs::kShowBookmarkBar); |
| 582 | [controller_ updateBookmarkBarVisibilityWithAnimation:YES]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 583 | break; |
[email protected] | 3710d000 | 2011-10-11 00:35:00 | [diff] [blame] | 584 | } |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 585 | default: |
| 586 | NOTREACHED(); // we don't ask for anything else! |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void BrowserWindowCocoa::DestroyBrowser() { |
| 592 | [controller_ destroyBrowser]; |
| 593 | |
| 594 | // at this point the controller is dead (autoreleased), so |
| 595 | // make sure we don't try to reference it any more. |
| 596 | } |
| 597 | |
| 598 | NSWindow* BrowserWindowCocoa::window() const { |
| 599 | return [controller_ window]; |
| 600 | } |
| 601 | |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 602 | void BrowserWindowCocoa::ShowAvatarBubble(WebContents* web_contents, |
[email protected] | 2f733a0 | 2011-10-06 15:17:34 | [diff] [blame] | 603 | const gfx::Rect& rect) { |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 604 | NSView* view = web_contents->GetNativeView(); |
[email protected] | 79ac3cb | 2011-10-12 17:11:21 | [diff] [blame] | 605 | NSRect bounds = [view bounds]; |
| 606 | NSPoint point; |
| 607 | point.x = NSMinX(bounds) + rect.right(); |
| 608 | // The view's origin is at the bottom but |rect|'s origin is at the top. |
| 609 | point.y = NSMaxY(bounds) - rect.bottom(); |
[email protected] | 2f733a0 | 2011-10-06 15:17:34 | [diff] [blame] | 610 | point = [view convertPoint:point toView:nil]; |
| 611 | point = [[view window] convertBaseToScreen:point]; |
| 612 | |
| 613 | // |menu| will automatically release itself on close. |
| 614 | AvatarMenuBubbleController* menu = |
| 615 | [[AvatarMenuBubbleController alloc] initWithBrowser:browser_ |
| 616 | anchoredAt:point]; |
[email protected] | 51152d6d | 2011-11-07 01:55:12 | [diff] [blame] | 617 | [[menu bubble] setAlignment:info_bubble::kAlignEdgeToAnchorEdge]; |
[email protected] | 2f733a0 | 2011-10-06 15:17:34 | [diff] [blame] | 618 | [menu showWindow:nil]; |
| 619 | } |
[email protected] | 7d2d0815 | 2011-10-25 22:58:47 | [diff] [blame] | 620 | |
| 621 | void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton() { |
| 622 | [[controller_ avatarButtonController] showAvatarBubble]; |
| 623 | } |