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 #ifndef CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_
8 // A class acting as the Objective-C controller for the Browser
9 // object. Handles interactions between Cocoa and the cross-platform
10 // code. Each window has a single toolbar and, by virtue of being a
11 // TabWindowController, a tab strip along the top.
13 #import <Cocoa/Cocoa.h>
15 #include "base/mac/scoped_nsobject.h"
16 #include "base/memory/scoped_ptr.h"
17 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
18 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bubble_controller.h"
19 #import "chrome/browser/ui/cocoa/browser_command_executor.h"
20 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h"
21 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
22 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
23 #import "chrome/browser/ui/cocoa/themed_window.h"
24 #import "chrome/browser/ui/cocoa/url_drop_target.h"
25 #import "chrome/browser/ui/cocoa/view_resizer.h"
26 #include "ui/gfx/rect.h"
28 @
class AvatarBaseController
;
31 class BrowserWindowCocoa
;
32 @
class DevToolsController
;
33 @
class DownloadShelfController
;
34 class ExtensionKeybindingRegistryCocoa
;
35 @
class FindBarCocoaController
;
36 @
class FullscreenModeController
;
37 @
class FullscreenWindow
;
38 @
class InfoBarContainerController
;
39 class LocationBarViewMac
;
40 @
class OverlayableContentsController
;
41 @
class PresentationModeController
;
42 class StatusBubbleMac
;
43 @
class TabStripController
;
45 @
class ToolbarController
;
51 @interface BrowserWindowController
:
52 TabWindowController
<NSUserInterfaceValidations
,
53 BookmarkBarControllerDelegate
,
54 BrowserCommandExecutor
,
56 TabStripControllerDelegate
> {
58 // The ordering of these members is important as it determines the order in
59 // which they are destroyed. |browser_| needs to be destroyed last as most of
60 // the other objects hold weak references to it or things it owns
61 // (tab/toolbar/bookmark models, profiles, etc).
62 scoped_ptr
<Browser
> browser_
;
63 NSWindow
* savedRegularWindow_
;
64 scoped_ptr
<BrowserWindowCocoa
> windowShim_
;
65 base::scoped_nsobject
<ToolbarController
> toolbarController_
;
66 base::scoped_nsobject
<TabStripController
> tabStripController_
;
67 base::scoped_nsobject
<FindBarCocoaController
> findBarCocoaController_
;
68 base::scoped_nsobject
<InfoBarContainerController
> infoBarContainerController_
;
69 base::scoped_nsobject
<DownloadShelfController
> downloadShelfController_
;
70 base::scoped_nsobject
<BookmarkBarController
> bookmarkBarController_
;
71 base::scoped_nsobject
<DevToolsController
> devToolsController_
;
72 base::scoped_nsobject
<OverlayableContentsController
>
73 overlayableContentsController_
;
74 base::scoped_nsobject
<PresentationModeController
> presentationModeController_
;
75 base::scoped_nsobject
<FullscreenModeController
> fullscreenModeController_
;
76 base::scoped_nsobject
<FullscreenExitBubbleController
>
77 fullscreenExitBubbleController_
;
79 // Strong. StatusBubble is a special case of a strong reference that
80 // we don't wrap in a scoped_ptr because it is acting the same
81 // as an NSWindowController in that it wraps a window that must
82 // be shut down before our destructors are called.
83 StatusBubbleMac
* statusBubble_
;
85 BookmarkBubbleController
* bookmarkBubbleController_
; // Weak.
86 BOOL initializing_
; // YES while we are currently in initWithBrowser:
87 BOOL ownsBrowser_
; // Only ever NO when testing
89 // The total amount by which we've grown the window up or down (to display a
90 // bookmark bar and/or download shelf), respectively; reset to 0 when moved
91 // away from the bottom/top or resized (or zoomed).
92 CGFloat windowTopGrowth_
;
93 CGFloat windowBottomGrowth_
;
95 // YES only if we're shrinking the window from an apparent zoomed state (which
96 // we'll only do if we grew it to the zoomed state); needed since we'll then
97 // restrict the amount of shrinking by the amounts specified above. Reset to
99 BOOL isShrinkingFromZoomed_
;
101 // The raw accumulated zoom value and the actual zoom increments made for an
102 // an in-progress pinch gesture.
103 CGFloat totalMagnifyGestureAmount_
;
104 NSInteger currentZoomStepDelta_
;
106 // The view controller that manages the incognito badge or the multi-profile
107 // avatar button. Depending on whether the --new-profile-management flag is
108 // used, the multi-profile button can either be the avatar's icon badge or a
109 // button with the profile's name. If the flag is used, the button is always
110 // shown, otherwise the view will always be in the view hierarchy but will
111 // be hidden unless it's appropriate to show it (i.e. if there's more than
113 base::scoped_nsobject
<AvatarBaseController
> avatarButtonController_
;
115 // Lazily created view which draws the background for the floating set of bars
116 // in presentation mode (for window types having a floating bar; it remains
117 // nil for those which don't).
118 base::scoped_nsobject
<NSView
> floatingBarBackingView_
;
120 // The borderless window used in fullscreen mode. Lion reuses the original
121 // window in fullscreen mode, so this is always nil on Lion.
122 base::scoped_nsobject
<NSWindow
> fullscreenWindow_
;
124 // Tracks whether presentation mode was entered from fullscreen mode or
125 // directly from normal windowed mode. Used to determine what to do when
126 // exiting presentation mode.
127 BOOL enteredPresentationModeFromFullscreen_
;
129 // True between -windowWillEnterFullScreen and -windowDidEnterFullScreen.
130 // Only used on Lion and higher.
131 BOOL enteringFullscreen_
;
133 // True between |-setPresentationMode:url:bubbleType:| and
134 // -windowDidEnterFullScreen. Only used on Lion and higher.
135 BOOL enteringPresentationMode_
;
137 // The size of the original (non-fullscreen) window. This is saved just
138 // before entering fullscreen mode and is only valid when |-isFullscreen|
140 NSRect savedRegularWindowFrame_
;
142 // The proportion of the floating bar which is shown (in presentation mode).
143 CGFloat floatingBarShownFraction_
;
145 // Various UI elements/events may want to ensure that the floating bar is
146 // visible (in presentation mode), e.g., because of where the mouse is or
147 // where keyboard focus is. Whenever an object requires bar visibility, it has
148 // itself added to |barVisibilityLocks_|. When it no longer requires bar
149 // visibility, it has itself removed.
150 base::scoped_nsobject
<NSMutableSet
> barVisibilityLocks_
;
152 // Bar visibility locks and releases only result (when appropriate) in changes
153 // in visible state when the following is |YES|.
154 BOOL barVisibilityUpdatesEnabled_
;
156 // When going fullscreen for a tab, we need to store the URL and the
157 // fullscreen type, since we can't show the bubble until
158 // -windowDidEnterFullScreen: gets called.
160 FullscreenExitBubbleType fullscreenBubbleType_
;
162 // The Extension Command Registry used to determine which keyboard events to
164 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
166 // The number of overlapped views being shown.
167 NSUInteger overlappedViewCount_
;
170 // A convenience class method which gets the |BrowserWindowController| for a
171 // given window. This method returns nil if no window in the chain has a BWC.
172 + (BrowserWindowController
*)browserWindowControllerForWindow
:(NSWindow
*)window
;
174 // A convenience class method which gets the |BrowserWindowController| for a
175 // given view. This is the controller for the window containing |view|, if it
176 // is a BWC, or the first controller in the parent-window chain that is a
177 // BWC. This method returns nil if no window in the chain has a BWC.
178 + (BrowserWindowController
*)browserWindowControllerForView
:(NSView
*)view
;
180 // Helper method used to update the "Signin" menu item to reflect the current
181 // signed in state. Class-level function as it's still required even when there
182 // are no open browser windows.
183 + (void)updateSigninItem
:(id
)signinItem
184 shouldShow
:(BOOL
)showSigninMenuItem
185 currentProfile
:(Profile
*)profile
;
187 // Load the browser window nib and do any Cocoa-specific initialization.
188 // Takes ownership of |browser|.
189 - (id
)initWithBrowser
:(Browser
*)browser
;
191 // Call to make the browser go away from other places in the cross-platform
193 - (void)destroyBrowser
;
195 // Ensure bounds for the window abide by the minimum window size.
196 - (gfx::Rect
)enforceMinWindowSize
:(gfx::Rect
)bounds
;
198 // Access the C++ bridge between the NSWindow and the rest of Chromium.
199 - (BrowserWindow
*)browserWindow
;
201 // Return a weak pointer to the toolbar controller.
202 - (ToolbarController
*)toolbarController
;
204 // Return a weak pointer to the tab strip controller.
205 - (TabStripController
*)tabStripController
;
207 // Return a weak pointer to the find bar controller.
208 - (FindBarCocoaController
*)findBarCocoaController
;
210 // Access the ObjC controller that contains the infobars.
211 - (InfoBarContainerController
*)infoBarContainerController
;
213 // Access the C++ bridge object representing the status bubble for the window.
214 - (StatusBubbleMac
*)statusBubble
;
216 // Access the C++ bridge object representing the location bar.
217 - (LocationBarViewMac
*)locationBarBridge
;
219 // Returns a weak pointer to the floating bar backing view;
220 - (NSView
*)floatingBarBackingView
;
222 // Returns a weak pointer to the overlayable contents controller.
223 - (OverlayableContentsController
*)overlayableContentsController
;
225 // Access the Profile object that backs this Browser.
228 // Access the avatar button controller.
229 - (AvatarBaseController
*)avatarButtonController
;
231 // Forces the toolbar (and transitively the location bar) to update its current
232 // state. If |tab| is non-NULL, we're switching (back?) to this tab and should
233 // restore any previous location bar state (such as user editing) as well.
234 - (void)updateToolbarWithContents
:(content::WebContents
*)tab
;
236 // Sets whether or not the current page in the frontmost tab is bookmarked.
237 - (void)setStarredState
:(BOOL
)isStarred
;
239 // Happens when the zoom level is changed in the active tab, the active tab is
240 // changed, or a new browser window or tab is created. |canShowBubble| denotes
241 // whether it would be appropriate to show a zoom bubble or not.
242 - (void)zoomChangedForActiveTab
:(BOOL
)canShowBubble
;
244 // Return the rect, in WebKit coordinates (flipped), of the window's grow box
245 // in the coordinate system of the content area of the currently selected tab.
246 - (NSRect
)selectedTabGrowBoxRect
;
248 // Called to tell the selected tab to update its loading state.
249 // |force| is set if the update is due to changing tabs, as opposed to
250 // the page-load finishing. See comment in reload_button.h.
251 - (void)setIsLoading
:(BOOL
)isLoading force
:(BOOL
)force
;
253 // Brings this controller's window to the front.
256 // Make the location bar the first responder, if possible.
257 - (void)focusLocationBar
:(BOOL
)selectAll
;
259 // Make the (currently-selected) tab contents the first responder, if possible.
260 - (void)focusTabContents
;
262 // Returns the frame of the regular (non-fullscreened) window (even if the
263 // window is currently in fullscreen mode). The frame is returned in Cocoa
264 // coordinates (origin in bottom-left).
265 - (NSRect
)regularWindowFrame
;
267 // Whether or not to show the avatar, which is either the incognito guy or the
268 // user's profile avatar.
269 - (BOOL
)shouldShowAvatar
;
271 // Whether or not to show the new avatar button used by --new-profile-maagement.
272 - (BOOL
)shouldUseNewAvatarButton
;
274 - (BOOL
)isBookmarkBarVisible
;
276 // Returns YES if the bookmark bar is currently animating.
277 - (BOOL
)isBookmarkBarAnimating
;
279 - (BookmarkBarController
*)bookmarkBarController
;
281 - (DevToolsController
*)devToolsController
;
283 - (BOOL
)isDownloadShelfVisible
;
285 // Lazily creates the download shelf in visible state if it doesn't exist yet.
286 - (DownloadShelfController
*)downloadShelf
;
288 // Retains the given FindBarCocoaController and adds its view to this
289 // browser window. Must only be called once per
290 // BrowserWindowController.
291 - (void)addFindBar
:(FindBarCocoaController
*)findBarCocoaController
;
293 // The user changed the theme.
294 - (void)userChangedTheme
;
296 // Executes the command in the context of the current browser.
297 // |command| is an integer value containing one of the constants defined in the
298 // "chrome/app/chrome_command_ids.h" file.
299 - (void)executeCommand
:(int)command
;
301 // Consults the Command Registry to see if this |event| needs to be handled as
302 // an extension command and returns YES if so (NO otherwise).
303 - (BOOL
)handledByExtensionCommand
:(NSEvent
*)event
;
305 // Delegate method for the status bubble to query its base frame.
306 - (NSRect
)statusBubbleBaseFrame
;
308 // Show the bookmark bubble (e.g. user just clicked on the STAR)
309 - (void)showBookmarkBubbleForURL
:(const GURL
&)url
310 alreadyBookmarked
:(BOOL
)alreadyBookmarked
;
312 // Shows or hides the docked web inspector depending on |contents|'s state.
313 - (void)updateDevToolsForContents
:(content::WebContents
*)contents
;
315 // Gets the current theme provider.
316 - (ui::ThemeProvider
*)themeProvider
;
318 // Gets the window style.
319 - (ThemedWindowStyle
)themedWindowStyle
;
321 // Returns the position in the coordinates of the root view
322 // ([[self contentView] superview]) that the top left of a theme image with
323 // |alignment| should be painted at. If the window does not have a tab strip,
324 // the offset for THEME_IMAGE_ALIGN_WITH_FRAME is always returned. The result of
325 // this method can be used in conjunction with
326 // [NSGraphicsContext cr_setPatternPhase:] to set the offset of pattern colors.
327 - (NSPoint
)themeImagePositionForAlignment
:(ThemeImageAlignment
)alignment
;
329 // Return the point to which a bubble window's arrow should point, in window
331 - (NSPoint
)bookmarkBubblePoint
;
333 // Called when the Add Search Engine dialog is closed.
334 - (void)sheetDidEnd
:(NSWindow
*)sheet
335 returnCode
:(NSInteger
)code
336 context
:(void*)context
;
338 // Called when the find bar visibility changes. This is used to update the
339 // allowOverlappingViews state.
340 - (void)onFindBarVisibilityChanged
;
342 // Called when an overlapped view is shown. This is used to update the
343 // allowOverlappingViews state. Currently used for history overlay and
345 - (void)onOverlappedViewShown
;
347 // Called when a history overlay is hidden. This is used to update the
348 // allowOverlappingViews state. Currently used for history overlay and
350 - (void)onOverlappedViewHidden
;
352 @end
// @interface BrowserWindowController
355 // Methods having to do with the window type (normal/popup/app, and whether the
356 // window has various features; fullscreen and presentation mode methods are
358 @interface
BrowserWindowController(WindowType
)
360 // Determines whether this controller's window supports a given feature (i.e.,
361 // whether a given feature is or can be shown in the window).
362 // TODO(viettrungluu): |feature| is really should be |Browser::Feature|, but I
363 // don't want to include browser.h (and you can't forward declare enums).
364 - (BOOL
)supportsWindowFeature
:(int)feature
;
366 // Called to check whether or not this window has a normal title bar (YES if it
367 // does, NO otherwise). (E.g., normal browser windows do not, pop-ups do.)
370 // Called to check whether or not this window has a toolbar (YES if it does, NO
371 // otherwise). (E.g., normal browser windows do, pop-ups do not.)
374 // Called to check whether or not this window has a location bar (YES if it
375 // does, NO otherwise). (E.g., normal browser windows do, pop-ups may or may
377 - (BOOL
)hasLocationBar
;
379 // Called to check whether or not this window can have bookmark bar (YES if it
380 // does, NO otherwise). (E.g., normal browser windows may, pop-ups may not.)
381 - (BOOL
)supportsBookmarkBar
;
383 // Called to check if this controller's window is a tabbed window (e.g., not a
384 // pop-up window). Returns YES if it is, NO otherwise.
385 // Note: The |-has...| methods are usually preferred, so this method is largely
387 - (BOOL
)isTabbedWindow
;
389 @end
// @interface BrowserWindowController(WindowType)
392 // Methods having to do with fullscreen and presentation mode.
393 @interface
BrowserWindowController(Fullscreen
)
395 // Toggles fullscreen mode. Meant to be called by Lion windows when they enter
396 // or exit Lion fullscreen mode. Must not be called on Snow Leopard or earlier.
397 - (void)handleLionToggleFullscreen
;
399 // Enters (or exits) fullscreen mode. This method is safe to call on all OS
401 - (void)enterFullscreen
;
402 - (void)exitFullscreen
;
404 // Updates the contents of the fullscreen exit bubble with |url| and
406 - (void)updateFullscreenExitBubbleURL
:(const GURL
&)url
407 bubbleType
:(FullscreenExitBubbleType
)bubbleType
;
409 // Returns fullscreen state. This method is safe to call on all OS versions.
410 - (BOOL
)isFullscreen
;
412 // Enters (or exits) presentation mode. Also enters fullscreen mode if this
413 // window is not already fullscreen. This method is safe to call on all OS
415 - (void)enterPresentationModeForURL
:(const GURL
&)url
416 bubbleType
:(FullscreenExitBubbleType
)bubbleType
;
417 - (void)exitPresentationMode
;
419 // For simplified fullscreen: Enters fullscreen for a tab at a URL. The |url|
420 // is guaranteed to be non-empty; see -enterFullscreen for the user-initiated
421 // fullscreen mode. Called on Snow Leopard and Lion+.
422 - (void)enterFullscreenForURL
:(const GURL
&)url
423 bubbleType
:(FullscreenExitBubbleType
)bubbleType
;
425 // Returns presentation mode state. This method is safe to call on all OS
427 - (BOOL
)inPresentationMode
;
429 // Resizes the fullscreen window to fit the screen it's currently on. Called by
430 // the PresentationModeController when there is a change in monitor placement or
432 - (void)resizeFullscreenWindow
;
434 // Gets or sets the fraction of the floating bar (presentation mode overlay)
435 // that is shown. 0 is completely hidden, 1 is fully shown.
436 - (CGFloat
)floatingBarShownFraction
;
437 - (void)setFloatingBarShownFraction
:(CGFloat
)fraction
;
439 // Query/lock/release the requirement that the tab strip/toolbar/attached
440 // bookmark bar bar cluster is visible (e.g., when one of its elements has
441 // focus). This is required for the floating bar in presentation mode, but
442 // should also be called when not in presentation mode; see the comments for
443 // |barVisibilityLocks_| for more details. Double locks/releases by the same
444 // owner are ignored. If |animate:| is YES, then an animation may be performed,
445 // possibly after a small delay if |delay:| is YES. If |animate:| is NO,
446 // |delay:| will be ignored. In the case of multiple calls, later calls have
447 // precedence with the rule that |animate:NO| has precedence over |animate:YES|,
448 // and |delay:NO| has precedence over |delay:YES|.
449 - (BOOL
)isBarVisibilityLockedForOwner
:(id
)owner
;
450 - (void)lockBarVisibilityForOwner
:(id
)owner
451 withAnimation
:(BOOL
)animate
453 - (void)releaseBarVisibilityForOwner
:(id
)owner
454 withAnimation
:(BOOL
)animate
457 // Returns YES if any of the views in the floating bar currently has focus.
458 - (BOOL
)floatingBarHasFocus
;
460 @end
// @interface BrowserWindowController(Fullscreen)
463 // Methods which are either only for testing, or only public for testing.
464 @interface
BrowserWindowController (TestingAPI
)
466 // Put the incognito badge or multi-profile avatar on the browser and adjust the
467 // tab strip accordingly.
468 - (void)installAvatar
;
470 // Allows us to initWithBrowser withOUT taking ownership of the browser.
471 - (id
)initWithBrowser
:(Browser
*)browser takeOwnership
:(BOOL
)ownIt
;
473 // Adjusts the window height by the given amount. If the window spans from the
474 // top of the current workspace to the bottom of the current workspace, the
475 // height is not adjusted. If growing the window by the requested amount would
476 // size the window to be taller than the current workspace, the window height is
477 // capped to be equal to the height of the current workspace. If the window is
478 // partially offscreen, its height is not adjusted at all. This function
479 // prefers to grow the window down, but will grow up if needed. Calls to this
480 // function should be followed by a call to |layoutSubviews|.
481 // Returns if the window height was changed.
482 - (BOOL
)adjustWindowHeightBy
:(CGFloat
)deltaH
;
484 // Return an autoreleased NSWindow suitable for fullscreen use.
485 - (NSWindow
*)createFullscreenWindow
;
487 // Resets any saved state about window growth (due to showing the bookmark bar
488 // or the download shelf), so that future shrinking will occur from the bottom.
489 - (void)resetWindowGrowthState
;
491 // Computes by how far in each direction, horizontal and vertical, the
492 // |source| rect doesn't fit into |target|.
493 - (NSSize
)overflowFrom
:(NSRect
)source
496 // The fullscreen exit bubble controller, or nil if the bubble isn't showing.
497 - (FullscreenExitBubbleController
*)fullscreenExitBubbleController
;
499 // Gets the rect, in window base coordinates, that the omnibox popup should be
500 // positioned relative to.
501 - (NSRect
)omniboxPopupAnchorRect
;
503 // Force a layout of info bars.
504 - (void)layoutInfoBars
;
506 @end
// @interface BrowserWindowController (TestingAPI)
509 #endif // CHROME_BROWSER_UI_COCOA_BROWSER_WINDOW_CONTROLLER_H_