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