1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_ 7 #pragma once 8 9 // A class acting as the Objective-C controller for the Browser 10 // object. Handles interactions between Cocoa and the cross-platform 11 // code. Each window has a single toolbar and, by virtue of being a 12 // TabWindowController, a tab strip along the top. 13 14 #import <Cocoa/Cocoa.h> 15 16 #include "base/memory/scoped_nsobject.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "chrome/browser/sync/sync_ui_util.h" 19 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" 20 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bubble_controller.h" 21 #import "chrome/browser/ui/cocoa/browser_command_executor.h" 22 #import "chrome/browser/ui/cocoa/tab_contents/tab_contents_controller.h" 23 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 24 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h" 25 #import "chrome/browser/ui/cocoa/themed_window.h" 26 #import "chrome/browser/ui/cocoa/url_drop_target.h" 27 #import "chrome/browser/ui/cocoa/view_resizer.h" 28 29 30 class Browser; 31 class BrowserWindow; 32 class BrowserWindowCocoa; 33 class ConstrainedWindowMac; 34 @class DevToolsController; 35 @class DownloadShelfController; 36 @class FindBarCocoaController; 37 @class FullscreenController; 38 @class GTMWindowSheetController; 39 @class IncognitoImageView; 40 @class InfoBarContainerController; 41 class LocationBarViewMac; 42 @class PreviewableContentsController; 43 @class SidebarController; 44 class StatusBubbleMac; 45 class TabContents; 46 @class TabStripController; 47 @class TabStripView; 48 @class ToolbarController; 49 50 51 @interface BrowserWindowController : 52 TabWindowController<NSUserInterfaceValidations, 53 BookmarkBarControllerDelegate, 54 BrowserCommandExecutor, 55 ViewResizer, 56 TabContentsControllerDelegate, 57 TabStripControllerDelegate> { 58 @private 59 // The ordering of these members is important as it determines the order in 60 // which they are destroyed. |browser_| needs to be destroyed last as most of 61 // the other objects hold weak references to it or things it owns 62 // (tab/toolbar/bookmark models, profiles, etc). 63 scoped_ptr<Browser> browser_; 64 NSWindow* savedRegularWindow_; 65 scoped_ptr<BrowserWindowCocoa> windowShim_; 66 scoped_nsobject<ToolbarController> toolbarController_; 67 scoped_nsobject<TabStripController> tabStripController_; 68 scoped_nsobject<FindBarCocoaController> findBarCocoaController_; 69 scoped_nsobject<InfoBarContainerController> infoBarContainerController_; 70 scoped_nsobject<DownloadShelfController> downloadShelfController_; 71 scoped_nsobject<BookmarkBarController> bookmarkBarController_; 72 scoped_nsobject<DevToolsController> devToolsController_; 73 scoped_nsobject<SidebarController> sidebarController_; 74 scoped_nsobject<PreviewableContentsController> previewableContentsController_; 75 scoped_nsobject<FullscreenController> fullscreenController_; 76 77 // Strong. StatusBubble is a special case of a strong reference that 78 // we don't wrap in a scoped_ptr because it is acting the same 79 // as an NSWindowController in that it wraps a window that must 80 // be shut down before our destructors are called. 81 StatusBubbleMac* statusBubble_; 82 83 BookmarkBubbleController* bookmarkBubbleController_; // Weak. 84 BOOL initializing_; // YES while we are currently in initWithBrowser: 85 BOOL ownsBrowser_; // Only ever NO when testing 86 87 // The total amount by which we've grown the window up or down (to display a 88 // bookmark bar and/or download shelf), respectively; reset to 0 when moved 89 // away from the bottom/top or resized (or zoomed). 90 CGFloat windowTopGrowth_; 91 CGFloat windowBottomGrowth_; 92 93 // YES only if we're shrinking the window from an apparent zoomed state (which 94 // we'll only do if we grew it to the zoomed state); needed since we'll then 95 // restrict the amount of shrinking by the amounts specified above. Reset to 96 // NO on growth. 97 BOOL isShrinkingFromZoomed_; 98 99 // The raw accumulated zoom value and the actual zoom increments made for an 100 // an in-progress pinch gesture. 101 CGFloat totalMagnifyGestureAmount_; 102 NSInteger currentZoomStepDelta_; 103 104 // The view which shows the incognito badge (NULL if not an incognito window). 105 // Needed to access the view to move it to/from the fullscreen window. 106 scoped_nsobject<IncognitoImageView> incognitoBadge_; 107 108 // Lazily created view which draws the background for the floating set of bars 109 // in fullscreen mode (for window types having a floating bar; it remains nil 110 // for those which don't). 111 scoped_nsobject<NSView> floatingBarBackingView_; 112 113 // Tracks whether the floating bar is above or below the bookmark bar, in 114 // terms of z-order. 115 BOOL floatingBarAboveBookmarkBar_; 116 117 // The proportion of the floating bar which is shown (in fullscreen mode). 118 CGFloat floatingBarShownFraction_; 119 120 // Various UI elements/events may want to ensure that the floating bar is 121 // visible (in fullscreen mode), e.g., because of where the mouse is or where 122 // keyboard focus is. Whenever an object requires bar visibility, it has 123 // itself added to |barVisibilityLocks_|. When it no longer requires bar 124 // visibility, it has itself removed. 125 scoped_nsobject<NSMutableSet> barVisibilityLocks_; 126 127 // Bar visibility locks and releases only result (when appropriate) in changes 128 // in visible state when the following is |YES|. 129 BOOL barVisibilityUpdatesEnabled_; 130 } 131 132 // A convenience class method which gets the |BrowserWindowController| for a 133 // given window. This method returns nil if no window in the chain has a BWC. 134 + (BrowserWindowController*)browserWindowControllerForWindow:(NSWindow*)window; 135 136 // A convenience class method which gets the |BrowserWindowController| for a 137 // given view. This is the controller for the window containing |view|, if it 138 // is a BWC, or the first controller in the parent-window chain that is a 139 // BWC. This method returns nil if no window in the chain has a BWC. 140 + (BrowserWindowController*)browserWindowControllerForView:(NSView*)view; 141 142 // Load the browser window nib and do any Cocoa-specific initialization. 143 // Takes ownership of |browser|. 144 - (id)initWithBrowser:(Browser*)browser; 145 146 // Call to make the browser go away from other places in the cross-platform 147 // code. 148 - (void)destroyBrowser; 149 150 // Access the C++ bridge between the NSWindow and the rest of Chromium. 151 - (BrowserWindow*)browserWindow; 152 153 // Return a weak pointer to the toolbar controller. 154 - (ToolbarController*)toolbarController; 155 156 // Return a weak pointer to the tab strip controller. 157 - (TabStripController*)tabStripController; 158 159 // Access the ObjC controller that contains the infobars. 160 - (InfoBarContainerController*)infoBarContainerController; 161 162 // Access the C++ bridge object representing the status bubble for the window. 163 - (StatusBubbleMac*)statusBubble; 164 165 // Access the C++ bridge object representing the location bar. 166 - (LocationBarViewMac*)locationBarBridge; 167 168 // Updates the toolbar (and transitively the location bar) with the states of 169 // the specified |tab|. If |shouldRestore| is true, we're switching 170 // (back?) to this tab and should restore any previous location bar state 171 // (such as user editing) as well. 172 - (void)updateToolbarWithContents:(TabContents*)tab 173 shouldRestoreState:(BOOL)shouldRestore; 174 175 // Sets whether or not the current page in the frontmost tab is bookmarked. 176 - (void)setStarredState:(BOOL)isStarred; 177 178 // Called to tell the selected tab to update its loading state. 179 // |force| is set if the update is due to changing tabs, as opposed to 180 // the page-load finishing. See comment in reload_button.h. 181 - (void)setIsLoading:(BOOL)isLoading force:(BOOL)force; 182 183 // Brings this controller's window to the front. 184 - (void)activate; 185 186 // Make the location bar the first responder, if possible. 187 - (void)focusLocationBar:(BOOL)selectAll; 188 189 // Make the (currently-selected) tab contents the first responder, if possible. 190 - (void)focusTabContents; 191 192 // Returns the frame of the regular (non-fullscreened) window (even if the 193 // window is currently in fullscreen mode). The frame is returned in Cocoa 194 // coordinates (origin in bottom-left). 195 - (NSRect)regularWindowFrame; 196 197 - (BOOL)isBookmarkBarVisible; 198 199 // Returns YES if the bookmark bar is currently animating. 200 - (BOOL)isBookmarkBarAnimating; 201 202 // Called after bookmark bar visibility changes (due to pref change or change in 203 // tab/tab contents). 204 - (void)updateBookmarkBarVisibilityWithAnimation:(BOOL)animate; 205 206 - (BOOL)isDownloadShelfVisible; 207 208 // Lazily creates the download shelf in visible state if it doesn't exist yet. 209 - (DownloadShelfController*)downloadShelf; 210 211 // Retains the given FindBarCocoaController and adds its view to this 212 // browser window. Must only be called once per 213 // BrowserWindowController. 214 - (void)addFindBar:(FindBarCocoaController*)findBarCocoaController; 215 216 // The user changed the theme. 217 - (void)userChangedTheme; 218 219 // Executes the command in the context of the current browser. 220 // |command| is an integer value containing one of the constants defined in the 221 // "chrome/app/chrome_command_ids.h" file. 222 - (void)executeCommand:(int)command; 223 224 // Delegate method for the status bubble to query its base frame. 225 - (NSRect)statusBubbleBaseFrame; 226 227 // Show the bookmark bubble (e.g. user just clicked on the STAR) 228 - (void)showBookmarkBubbleForURL:(const GURL&)url 229 alreadyBookmarked:(BOOL)alreadyBookmarked; 230 231 // Returns the (lazily created) window sheet controller of this window. Used 232 // for the per-tab sheets. 233 - (GTMWindowSheetController*)sheetController; 234 235 // Requests that |window| is opened as a per-tab sheet to the current tab. 236 - (void)attachConstrainedWindow:(ConstrainedWindowMac*)window; 237 // Closes the tab sheet |window| and potentially shows the next sheet in the 238 // tab's sheet queue. 239 - (void)removeConstrainedWindow:(ConstrainedWindowMac*)window; 240 // Returns NO if constrained windows cannot be attached to this window. 241 - (BOOL)canAttachConstrainedWindow; 242 243 // Shows or hides the docked web inspector depending on |contents|'s state. 244 - (void)updateDevToolsForContents:(TabContents*)contents; 245 246 // Displays the active sidebar linked to the |contents| or hides sidebar UI, 247 // if there's no such sidebar. 248 - (void)updateSidebarForContents:(TabContents*)contents; 249 250 // Gets the current theme provider. 251 - (ui::ThemeProvider*)themeProvider; 252 253 // Gets the window style. 254 - (ThemedWindowStyle)themedWindowStyle; 255 256 // Gets the pattern phase for the window. 257 - (NSPoint)themePatternPhase; 258 259 // Return the point to which a bubble window's arrow should point. 260 - (NSPoint)bookmarkBubblePoint; 261 262 // Call when the user changes the tab strip display mode, enabling or 263 // disabling vertical tabs for this browser. Re-flows the contents of the 264 // browser. 265 - (void)toggleTabStripDisplayMode; 266 267 // Shows or hides the Instant preview contents. 268 - (void)showInstant:(TabContents*)previewContents; 269 - (void)hideInstant; 270 - (void)commitInstant; 271 272 // Returns the frame, in Cocoa (unflipped) screen coordinates, of the area where 273 // Instant results are. If Instant is not showing, returns the frame of where 274 // it would be. 275 - (NSRect)instantFrame; 276 277 // Called when the Add Search Engine dialog is closed. 278 - (void)sheetDidEnd:(NSWindow*)sheet 279 returnCode:(NSInteger)code 280 context:(void*)context; 281 282 @end // @interface BrowserWindowController 283 284 285 // Methods having to do with the window type (normal/popup/app, and whether the 286 // window has various features; fullscreen methods are separate). 287 @interface BrowserWindowController(WindowType) 288 289 // Determines whether this controller's window supports a given feature (i.e., 290 // whether a given feature is or can be shown in the window). 291 // TODO(viettrungluu): |feature| is really should be |Browser::Feature|, but I 292 // don't want to include browser.h (and you can't forward declare enums). 293 - (BOOL)supportsWindowFeature:(int)feature; 294 295 // Called to check whether or not this window has a normal title bar (YES if it 296 // does, NO otherwise). (E.g., normal browser windows do not, pop-ups do.) 297 - (BOOL)hasTitleBar; 298 299 // Called to check whether or not this window has a toolbar (YES if it does, NO 300 // otherwise). (E.g., normal browser windows do, pop-ups do not.) 301 - (BOOL)hasToolbar; 302 303 // Called to check whether or not this window has a location bar (YES if it 304 // does, NO otherwise). (E.g., normal browser windows do, pop-ups may or may 305 // not.) 306 - (BOOL)hasLocationBar; 307 308 // Called to check whether or not this window can have bookmark bar (YES if it 309 // does, NO otherwise). (E.g., normal browser windows may, pop-ups may not.) 310 - (BOOL)supportsBookmarkBar; 311 312 // Called to check if this controller's window is a normal window (e.g., not a 313 // pop-up window). Returns YES if it is, NO otherwise. 314 // Note: The |-has...| methods are usually preferred, so this method is largely 315 // deprecated. 316 - (BOOL)isNormalWindow; 317 318 @end // @interface BrowserWindowController(WindowType) 319 320 321 // Methods having to do with fullscreen mode. 322 @interface BrowserWindowController(Fullscreen) 323 324 // Enters fullscreen mode. 325 - (IBAction)enterFullscreen:(id)sender; 326 327 // Enters (or exits) fullscreen mode. 328 - (void)setFullscreen:(BOOL)fullscreen; 329 330 // Returns fullscreen state. 331 - (BOOL)isFullscreen; 332 333 // Resizes the fullscreen window to fit the screen it's currently on. Called by 334 // the FullscreenController when there is a change in monitor placement or 335 // resolution. 336 - (void)resizeFullscreenWindow; 337 338 // Gets or sets the fraction of the floating bar (fullscreen overlay) that is 339 // shown. 0 is completely hidden, 1 is fully shown. 340 - (CGFloat)floatingBarShownFraction; 341 - (void)setFloatingBarShownFraction:(CGFloat)fraction; 342 343 // Query/lock/release the requirement that the tab strip/toolbar/attached 344 // bookmark bar bar cluster is visible (e.g., when one of its elements has 345 // focus). This is required for the floating bar in fullscreen mode, but should 346 // also be called when not in fullscreen mode; see the comments for 347 // |barVisibilityLocks_| for more details. Double locks/releases by the same 348 // owner are ignored. If |animate:| is YES, then an animation may be performed, 349 // possibly after a small delay if |delay:| is YES. If |animate:| is NO, 350 // |delay:| will be ignored. In the case of multiple calls, later calls have 351 // precedence with the rule that |animate:NO| has precedence over |animate:YES|, 352 // and |delay:NO| has precedence over |delay:YES|. 353 - (BOOL)isBarVisibilityLockedForOwner:(id)owner; 354 - (void)lockBarVisibilityForOwner:(id)owner 355 withAnimation:(BOOL)animate 356 delay:(BOOL)delay; 357 - (void)releaseBarVisibilityForOwner:(id)owner 358 withAnimation:(BOOL)animate 359 delay:(BOOL)delay; 360 361 // Returns YES if any of the views in the floating bar currently has focus. 362 - (BOOL)floatingBarHasFocus; 363 364 // Opens the tabpose window. 365 - (void)openTabpose; 366 367 @end // @interface BrowserWindowController(Fullscreen) 368 369 370 // Methods which are either only for testing, or only public for testing. 371 @interface BrowserWindowController(TestingAPI) 372 373 // Put the incognito badge on the browser and adjust the tab strip 374 // accordingly. 375 - (void)installIncognitoBadge; 376 377 // Allows us to initWithBrowser withOUT taking ownership of the browser. 378 - (id)initWithBrowser:(Browser*)browser takeOwnership:(BOOL)ownIt; 379 380 // Adjusts the window height by the given amount. If the window spans from the 381 // top of the current workspace to the bottom of the current workspace, the 382 // height is not adjusted. If growing the window by the requested amount would 383 // size the window to be taller than the current workspace, the window height is 384 // capped to be equal to the height of the current workspace. If the window is 385 // partially offscreen, its height is not adjusted at all. This function 386 // prefers to grow the window down, but will grow up if needed. Calls to this 387 // function should be followed by a call to |layoutSubviews|. 388 - (void)adjustWindowHeightBy:(CGFloat)deltaH; 389 390 // Return an autoreleased NSWindow suitable for fullscreen use. 391 - (NSWindow*)createFullscreenWindow; 392 393 // Resets any saved state about window growth (due to showing the bookmark bar 394 // or the download shelf), so that future shrinking will occur from the bottom. 395 - (void)resetWindowGrowthState; 396 397 // Computes by how far in each direction, horizontal and vertical, the 398 // |source| rect doesn't fit into |target|. 399 - (NSSize)overflowFrom:(NSRect)source 400 to:(NSRect)target; 401 @end // @interface BrowserWindowController(TestingAPI) 402 403 404 #endif // CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_ 405