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 #import "chrome/browser/ui/cocoa/tab_contents/tab_contents_controller.h"
9 #include "base/command_line.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "chrome/browser/devtools/devtools_window.h"
13 #import "chrome/browser/themes/theme_properties.h"
14 #import "chrome/browser/themes/theme_service.h"
15 #import "chrome/browser/ui/cocoa/themed_window.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/render_widget_host_view.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/browser/web_contents_view.h"
21 #include "ui/base/cocoa/animation_utils.h"
22 #include "ui/base/ui_base_switches.h"
23 #include "ui/gfx/geometry/rect.h"
25 using content::WebContents;
26 using content::WebContentsObserver;
28 // FullscreenObserver is used by TabContentsController to monitor for the
29 // showing/destruction of fullscreen render widgets. When notified,
30 // TabContentsController will alter its child view hierarchy to either embed a
31 // fullscreen render widget view or restore the normal WebContentsView render
32 // view. The embedded fullscreen render widget will fill the user's screen in
33 // the case where TabContentsController's NSView is a subview of a browser
34 // window that has been toggled into fullscreen mode (e.g., via
35 // FullscreenController).
36 class FullscreenObserver : public WebContentsObserver {
38 explicit FullscreenObserver(TabContentsController* controller)
39 : controller_(controller) {}
41 void Observe(content::WebContents* new_web_contents) {
42 WebContentsObserver::Observe(new_web_contents);
45 WebContents* web_contents() const {
46 return WebContentsObserver::web_contents();
49 virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE {
50 [controller_ toggleFullscreenWidget:YES];
53 virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE {
54 [controller_ toggleFullscreenWidget:NO];
57 virtual void DidToggleFullscreenModeForTab(bool entered_fullscreen) OVERRIDE {
58 [controller_ toggleFullscreenWidget:YES];
62 TabContentsController* const controller_;
64 DISALLOW_COPY_AND_ASSIGN(FullscreenObserver);
67 @interface TabContentsController (TabContentsContainerViewDelegate)
68 - (BOOL)contentsInFullscreenCaptureMode;
69 // Computes and returns the frame to use for the contents view within the
71 - (NSRect)frameForContentsView;
74 // An NSView with special-case handling for when the contents view does not
75 // expand to fill the entire tab contents area. See 'AutoEmbedFullscreen mode'
76 // in header file comments.
77 @interface TabContentsContainerView : NSView {
79 TabContentsController* delegate_; // weak
82 - (NSColor*)computeBackgroundColor;
85 @implementation TabContentsContainerView
87 - (id)initWithDelegate:(TabContentsController*)delegate {
88 if ((self = [super initWithFrame:NSZeroRect])) {
90 if (!CommandLine::ForCurrentProcess()->HasSwitch(
91 switches::kDisableCoreAnimation)) {
92 ScopedCAActionDisabler disabler;
93 base::scoped_nsobject<CALayer> layer([[CALayer alloc] init]);
94 [layer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
95 [self setLayer:layer];
96 [self setWantsLayer:YES];
102 // Called by the delegate during dealloc to invalidate the pointer held by this
104 - (void)delegateDestroyed {
108 - (NSColor*)computeBackgroundColor {
109 // This view is sometimes flashed into visibility (e.g, when closing
110 // windows), so ensure that the flash be white in those cases.
111 if (![delegate_ contentsInFullscreenCaptureMode])
112 return [NSColor whiteColor];
114 // Fill with a dark tint of the new tab page's background color. This is
115 // only seen when the subview is sized specially for fullscreen tab capture.
116 NSColor* bgColor = nil;
117 ThemeService* const theme =
118 static_cast<ThemeService*>([[self window] themeProvider]);
120 bgColor = theme->GetNSColor(ThemeProperties::COLOR_NTP_BACKGROUND);
122 bgColor = [[self window] backgroundColor];
123 const float kDarknessFraction = 0.80f;
124 return [bgColor blendedColorWithFraction:kDarknessFraction
125 ofColor:[NSColor blackColor]];
128 // Override -drawRect to fill the view with a solid color outside of the
130 - (void)drawRect:(NSRect)dirtyRect {
131 NSView* const contentsView =
132 [[self subviews] count] > 0 ? [[self subviews] objectAtIndex:0] : nil;
133 if (!contentsView || !NSContainsRect([contentsView frame], dirtyRect)) {
134 [[self computeBackgroundColor] setFill];
135 NSRectFill(dirtyRect);
137 [super drawRect:dirtyRect];
140 // Override auto-resizing logic to query the delegate for the exact frame to
141 // use for the contents view.
142 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
143 NSView* const contentsView =
144 [[self subviews] count] > 0 ? [[self subviews] objectAtIndex:0] : nil;
145 if (!contentsView || [contentsView autoresizingMask] == NSViewNotSizable ||
150 ScopedCAActionDisabler disabler;
151 [contentsView setFrame:[delegate_ frameForContentsView]];
154 // Update the background layer's color whenever the view needs to repaint.
155 - (void)setNeedsDisplayInRect:(NSRect)rect {
156 [super setNeedsDisplayInRect:rect];
158 // Convert from an NSColor to a CGColorRef.
159 NSColor* nsBackgroundColor = [self computeBackgroundColor];
160 NSColorSpace* nsColorSpace = [nsBackgroundColor colorSpace];
161 CGColorSpaceRef cgColorSpace = [nsColorSpace CGColorSpace];
162 const NSInteger numberOfComponents = [nsBackgroundColor numberOfComponents];
163 CGFloat components[numberOfComponents];
164 [nsBackgroundColor getComponents:components];
165 base::ScopedCFTypeRef<CGColorRef> cgBackgroundColor(
166 CGColorCreate(cgColorSpace, components));
168 ScopedCAActionDisabler disabler;
169 [[self layer] setBackgroundColor:cgBackgroundColor];
172 @end // @implementation TabContentsContainerView
174 @implementation TabContentsController
175 @synthesize webContents = contents_;
177 - (id)initWithContents:(WebContents*)contents
178 andAutoEmbedFullscreen:(BOOL)enableEmbeddedFullscreen {
179 if ((self = [super initWithNibName:nil bundle:nil])) {
180 if (enableEmbeddedFullscreen)
181 fullscreenObserver_.reset(new FullscreenObserver(self));
182 [self changeWebContents:contents];
188 [static_cast<TabContentsContainerView*>([self view]) delegateDestroyed];
189 // Make sure the contents view has been removed from the container view to
190 // allow objects to be released.
191 [[self view] removeFromSuperview];
196 base::scoped_nsobject<NSView> view(
197 [[TabContentsContainerView alloc] initWithDelegate:self]);
198 [view setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable];
202 - (void)ensureContentsSizeDoesNotChange {
203 NSView* contentsContainer = [self view];
204 NSArray* subviews = [contentsContainer subviews];
205 if ([subviews count] > 0) {
206 NSView* currentSubview = [subviews objectAtIndex:0];
207 [currentSubview setAutoresizingMask:NSViewNotSizable];
211 - (void)ensureContentsVisible {
214 ScopedCAActionDisabler disabler;
215 NSView* contentsContainer = [self view];
216 NSArray* subviews = [contentsContainer subviews];
217 NSView* contentsNativeView;
218 content::RenderWidgetHostView* const fullscreenView =
219 isEmbeddingFullscreenWidget_ ?
220 contents_->GetFullscreenRenderWidgetHostView() : NULL;
221 if (fullscreenView) {
222 contentsNativeView = fullscreenView->GetNativeView();
224 isEmbeddingFullscreenWidget_ = NO;
225 contentsNativeView = contents_->GetView()->GetNativeView();
227 [contentsNativeView setFrame:[self frameForContentsView]];
228 if ([subviews count] == 0) {
229 [contentsContainer addSubview:contentsNativeView];
230 } else if ([subviews objectAtIndex:0] != contentsNativeView) {
231 [contentsContainer replaceSubview:[subviews objectAtIndex:0]
232 with:contentsNativeView];
234 [contentsNativeView setAutoresizingMask:NSViewWidthSizable|
235 NSViewHeightSizable];
237 // TODO(miu): The following can be removed once we use a CALayer in
238 // TabContentsContainerView. http://crbug.com/354598
239 [contentsContainer setNeedsDisplay:YES];
241 // The rendering path with overlapping views disabled causes bugs when
242 // transitioning between composited and non-composited mode.
243 // http://crbug.com/279472
245 contents_->GetView()->SetAllowOverlappingViews(true);
248 - (void)changeWebContents:(WebContents*)newContents {
249 contents_ = newContents;
250 if (fullscreenObserver_) {
251 fullscreenObserver_->Observe(contents_);
252 isEmbeddingFullscreenWidget_ =
253 contents_ && contents_->GetFullscreenRenderWidgetHostView();
255 isEmbeddingFullscreenWidget_ = NO;
259 // Returns YES if the tab represented by this controller is the front-most.
260 - (BOOL)isCurrentTab {
261 // We're the current tab if we're in the view hierarchy, otherwise some other
263 return [[self view] superview] ? YES : NO;
266 - (void)willBecomeUnselectedTab {
267 // The RWHV is ripped out of the view hierarchy on tab switches, so it never
268 // formally resigns first responder status. Handle this by explicitly sending
269 // a Blur() message to the renderer, but only if the RWHV currently has focus.
270 content::RenderViewHost* rvh = [self webContents]->GetRenderViewHost();
272 if (rvh->GetView() && rvh->GetView()->HasFocus()) {
276 DevToolsWindow* devtoolsWindow =
277 DevToolsWindow::GetDockedInstanceForInspectedTab([self webContents]);
278 if (devtoolsWindow) {
279 content::RenderViewHost* devtoolsView =
280 devtoolsWindow->web_contents()->GetRenderViewHost();
281 if (devtoolsView && devtoolsView->GetView() &&
282 devtoolsView->GetView()->HasFocus()) {
283 devtoolsView->Blur();
289 - (void)willBecomeSelectedTab {
290 // Do not explicitly call Focus() here, as the RWHV may not actually have
291 // focus (for example, if the omnibox has focus instead). The WebContents
292 // logic will restore focus to the appropriate view.
295 - (void)tabDidChange:(WebContents*)updatedContents {
296 // Calling setContentView: here removes any first responder status
297 // the view may have, so avoid changing the view hierarchy unless
298 // the view is different.
299 if ([self webContents] != updatedContents) {
300 [self changeWebContents:updatedContents];
301 [self ensureContentsVisible];
305 - (void)toggleFullscreenWidget:(BOOL)enterFullscreen {
306 isEmbeddingFullscreenWidget_ = enterFullscreen &&
307 contents_ && contents_->GetFullscreenRenderWidgetHostView();
308 [self ensureContentsVisible];
311 - (BOOL)contentsInFullscreenCaptureMode {
312 if (!fullscreenObserver_)
314 // Note: Grab a known-valid WebContents pointer from |fullscreenObserver_|.
315 content::WebContents* const wc = fullscreenObserver_->web_contents();
317 wc->GetCapturerCount() == 0 ||
318 wc->GetPreferredSize().IsEmpty() ||
319 !(isEmbeddingFullscreenWidget_ ||
320 (wc->GetDelegate() &&
321 wc->GetDelegate()->IsFullscreenForTabOrPending(wc)))) {
327 - (NSRect)frameForContentsView {
328 const NSSize containerSize = [[self view] frame].size;
330 rect.set_width(containerSize.width);
331 rect.set_height(containerSize.height);
333 // In most cases, the contents view is simply sized to fill the container
334 // view's bounds. Only WebContentses that are in fullscreen mode and being
335 // screen-captured will engage the special layout/sizing behavior.
336 if (![self contentsInFullscreenCaptureMode])
337 return NSRectFromCGRect(rect.ToCGRect());
339 // Size the contents view to the capture video resolution and center it. If
340 // the container view is not large enough to fit it at the preferred size,
341 // scale down to fit (preserving aspect ratio).
342 content::WebContents* const wc = fullscreenObserver_->web_contents();
343 const gfx::Size captureSize = wc->GetPreferredSize();
344 if (captureSize.width() <= rect.width() &&
345 captureSize.height() <= rect.height()) {
346 // No scaling, just centering.
347 rect.ClampToCenteredSize(captureSize);
349 // Scale down, preserving aspect ratio, and center.
350 // TODO(miu): This is basically media::ComputeLetterboxRegion(), and it
351 // looks like others have written this code elsewhere. Let's consolidate
352 // into a shared function ui/gfx/geometry or around there.
353 const int64 x = static_cast<int64>(captureSize.width()) * rect.height();
354 const int64 y = static_cast<int64>(captureSize.height()) * rect.width();
356 rect.ClampToCenteredSize(gfx::Size(
357 rect.width(), static_cast<int>(y / captureSize.width())));
359 rect.ClampToCenteredSize(gfx::Size(
360 static_cast<int>(x / captureSize.height()), rect.height()));
364 return NSRectFromCGRect(rect.ToCGRect());