1 // Copyright 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 #include "chrome/browser/ui/views/frame/browser_view_layout.h"
6 
7 #include <algorithm>
8 
9 #include "base/observer_list.h"
10 #include "base/stl_util.h"
11 #include "base/trace_event/common/trace_event_common.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/find_bar/find_bar.h"
18 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
19 #include "chrome/browser/ui/layout_constants.h"
20 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
21 #include "chrome/browser/ui/views/download/download_shelf_view.h"
22 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h"
23 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
24 #include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
25 #include "chrome/browser/ui/views/frame/contents_layout_manager.h"
26 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
27 #include "chrome/browser/ui/views/frame/tab_strip_region_view.h"
28 #include "chrome/browser/ui/views/frame/top_container_view.h"
29 #include "chrome/browser/ui/views/infobars/infobar_container_view.h"
30 #include "chrome/browser/ui/views/tabs/tab_strip.h"
31 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
32 #include "components/web_modal/web_contents_modal_dialog_host.h"
33 #include "ui/base/hit_test.h"
34 #include "ui/gfx/geometry/point.h"
35 #include "ui/gfx/geometry/rect.h"
36 #include "ui/gfx/geometry/size.h"
37 #include "ui/views/controls/webview/webview.h"
38 #include "ui/views/widget/widget.h"
39 #include "ui/views/window/client_view.h"
40 
41 using views::View;
42 using web_modal::WebContentsModalDialogHost;
43 using web_modal::ModalDialogHostObserver;
44 
45 namespace {
46 
47 // The visible height of the shadow above the tabs. Clicks in this area are
48 // treated as clicks to the frame, rather than clicks to the tab.
49 const int kTabShadowSize = 2;
50 // The number of pixels the constrained window should overlap the bottom
51 // of the omnibox.
52 const int kConstrainedWindowOverlap = 3;
53 
54 // Combines View::ConvertPointToTarget and View::HitTest for a given |point|.
55 // Converts |point| from |src| to |dst| and hit tests it against |dst|. The
56 // converted |point| can then be retrieved and used for additional tests.
ConvertedHitTest(views::View * src,views::View * dst,gfx::Point * point)57 bool ConvertedHitTest(views::View* src, views::View* dst, gfx::Point* point) {
58   DCHECK(src);
59   DCHECK(dst);
60   DCHECK(point);
61   views::View::ConvertPointToTarget(src, dst, point);
62   return dst->HitTestPoint(*point);
63 }
64 
65 }  // namespace
66 
67 constexpr int BrowserViewLayout::kMainBrowserContentsMinimumWidth;
68 
69 class BrowserViewLayout::WebContentsModalDialogHostViews
70     : public WebContentsModalDialogHost {
71  public:
WebContentsModalDialogHostViews(BrowserViewLayout * browser_view_layout)72   explicit WebContentsModalDialogHostViews(
73       BrowserViewLayout* browser_view_layout)
74           : browser_view_layout_(browser_view_layout) {
75   }
76 
~WebContentsModalDialogHostViews()77   ~WebContentsModalDialogHostViews() override {
78     for (ModalDialogHostObserver& observer : observer_list_)
79       observer.OnHostDestroying();
80   }
81 
NotifyPositionRequiresUpdate()82   void NotifyPositionRequiresUpdate() {
83     for (ModalDialogHostObserver& observer : observer_list_)
84       observer.OnPositionRequiresUpdate();
85   }
86 
GetDialogPosition(const gfx::Size & size)87   gfx::Point GetDialogPosition(const gfx::Size& size) override {
88     views::View* view = browser_view_layout_->contents_container_;
89     gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
90     const int middle_x = content_area.x() + content_area.width() / 2;
91     const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
92     return gfx::Point(middle_x - size.width() / 2, top);
93   }
94 
ShouldActivateDialog() const95   bool ShouldActivateDialog() const override {
96     // The browser Widget may be inactive if showing a bubble so instead check
97     // against the last active browser window when determining whether to
98     // activate the dialog.
99     return chrome::FindLastActive() ==
100            browser_view_layout_->browser_view_->browser();
101   }
102 
GetMaximumDialogSize()103   gfx::Size GetMaximumDialogSize() override {
104     views::View* view = browser_view_layout_->contents_container_;
105     gfx::Rect content_area = view->ConvertRectToWidget(view->GetLocalBounds());
106     const int top = browser_view_layout_->web_contents_modal_dialog_top_y_;
107     return gfx::Size(content_area.width(), content_area.bottom() - top);
108   }
109 
110  private:
GetHostView() const111   gfx::NativeView GetHostView() const override {
112     return browser_view_layout_->host_view_;
113   }
114 
115   // Add/remove observer.
AddObserver(ModalDialogHostObserver * observer)116   void AddObserver(ModalDialogHostObserver* observer) override {
117     observer_list_.AddObserver(observer);
118   }
RemoveObserver(ModalDialogHostObserver * observer)119   void RemoveObserver(ModalDialogHostObserver* observer) override {
120     observer_list_.RemoveObserver(observer);
121   }
122 
123   BrowserViewLayout* const browser_view_layout_;
124 
125   base::ObserverList<ModalDialogHostObserver>::Unchecked observer_list_;
126 
127   DISALLOW_COPY_AND_ASSIGN(WebContentsModalDialogHostViews);
128 };
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 // BrowserViewLayout, public:
132 
BrowserViewLayout(std::unique_ptr<BrowserViewLayoutDelegate> delegate,gfx::NativeView host_view,BrowserView * browser_view,views::View * top_container,TabStripRegionView * tab_strip_region_view,TabStrip * tab_strip,views::View * toolbar,InfoBarContainerView * infobar_container,views::View * contents_container,views::View * side_panel,ImmersiveModeController * immersive_mode_controller,views::View * web_footer_experiment,views::View * contents_separator)133 BrowserViewLayout::BrowserViewLayout(
134     std::unique_ptr<BrowserViewLayoutDelegate> delegate,
135     gfx::NativeView host_view,
136     BrowserView* browser_view,
137     views::View* top_container,
138     TabStripRegionView* tab_strip_region_view,
139     TabStrip* tab_strip,
140     views::View* toolbar,
141     InfoBarContainerView* infobar_container,
142     views::View* contents_container,
143     views::View* side_panel,
144     ImmersiveModeController* immersive_mode_controller,
145     views::View* web_footer_experiment,
146     views::View* contents_separator)
147     : delegate_(std::move(delegate)),
148       host_view_(host_view),
149       browser_view_(browser_view),
150       top_container_(top_container),
151       tab_strip_region_view_(tab_strip_region_view),
152       toolbar_(toolbar),
153       infobar_container_(infobar_container),
154       contents_container_(contents_container),
155       side_panel_(side_panel),
156       immersive_mode_controller_(immersive_mode_controller),
157       web_footer_experiment_(web_footer_experiment),
158       contents_separator_(contents_separator),
159       tab_strip_(tab_strip),
160       dialog_host_(std::make_unique<WebContentsModalDialogHostViews>(this)) {}
161 
162 BrowserViewLayout::~BrowserViewLayout() = default;
163 
164 WebContentsModalDialogHost*
GetWebContentsModalDialogHost()165     BrowserViewLayout::GetWebContentsModalDialogHost() {
166   return dialog_host_.get();
167 }
168 
GetMinimumSize(const views::View * host) const169 gfx::Size BrowserViewLayout::GetMinimumSize(const views::View* host) const {
170   // Prevent having a 0x0 sized-contents as this can allow the window to be
171   // resized down such that it's invisible and can no longer accept events.
172   // Use a very small 1x1 size to allow the user and the web contents to be able
173   // to resize the window as small as possible without introducing bugs.
174   // https://crbug.com/847179.
175   constexpr gfx::Size kContentsMinimumSize(1, 1);
176 
177   // The minimum height for the normal (tabbed) browser window's contents area.
178   constexpr int kMainBrowserContentsMinimumHeight = 1;
179 
180   const bool has_tabstrip =
181       delegate_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
182   const bool has_toolbar =
183       delegate_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR);
184   const bool has_location_bar =
185       delegate_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
186   const bool has_bookmarks_bar =
187       bookmark_bar_ && bookmark_bar_->GetVisible() &&
188       delegate_->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR);
189 
190   gfx::Size tabstrip_size(
191       has_tabstrip ? tab_strip_region_view_->GetMinimumSize() : gfx::Size());
192   gfx::Size toolbar_size((has_toolbar || has_location_bar)
193                              ? toolbar_->GetMinimumSize()
194                              : gfx::Size());
195   gfx::Size bookmark_bar_size;
196   if (has_bookmarks_bar)
197     bookmark_bar_size = bookmark_bar_->GetMinimumSize();
198   gfx::Size infobar_container_size(infobar_container_->GetMinimumSize());
199   // TODO(pkotwicz): Adjust the minimum height for the find bar.
200 
201   gfx::Size contents_size(contents_container_->GetMinimumSize());
202   contents_size.SetToMax(delegate_->BrowserIsTypeNormal()
203                              ? gfx::Size(kMainBrowserContentsMinimumWidth,
204                                          kMainBrowserContentsMinimumHeight)
205                              : kContentsMinimumSize);
206 
207   const int min_height =
208       delegate_->GetTopInsetInBrowserView() + tabstrip_size.height() +
209       toolbar_size.height() + bookmark_bar_size.height() +
210       infobar_container_size.height() + contents_size.height();
211 
212   const int min_width = std::max(
213       {tabstrip_size.width(), toolbar_size.width(), bookmark_bar_size.width(),
214        infobar_container_size.width(), contents_size.width()});
215 
216   return gfx::Size(min_width, min_height);
217 }
218 
GetHostView()219 gfx::NativeView BrowserViewLayout::GetHostView() {
220   return delegate_->GetHostView();
221 }
222 
NonClientHitTest(const gfx::Point & point)223 int BrowserViewLayout::NonClientHitTest(const gfx::Point& point) {
224   // Since the TabStrip only renders in some parts of the top of the window,
225   // the un-obscured area is considered to be part of the non-client caption
226   // area of the window. So we need to treat hit-tests in these regions as
227   // hit-tests of the titlebar.
228 
229   views::View* parent = browser_view_->parent();
230 
231   gfx::Point point_in_browser_view_coords(point);
232   views::View::ConvertPointToTarget(
233       parent, browser_view_, &point_in_browser_view_coords);
234 
235   // Determine if the TabStrip exists and is capable of being clicked on. We
236   // might be a popup window without a TabStrip.
237   if (delegate_->IsTabStripVisible()) {
238     // See if the mouse pointer is within the bounds of the TabStripRegionView.
239     gfx::Point test_point(point);
240     if (ConvertedHitTest(parent, tab_strip_region_view_, &test_point)) {
241       if (tab_strip_region_view_->IsPositionInWindowCaption(test_point))
242         return HTCAPTION;
243       return HTCLIENT;
244     }
245 
246     // The top few pixels of the TabStrip are a drop-shadow - as we're pretty
247     // starved of dragable area, let's give it to window dragging (this also
248     // makes sense visually).
249     // TODO(tluk): Investigate the impact removing this has on draggable area
250     // given the tab strip no longer uses shadows.
251     views::Widget* widget = browser_view_->GetWidget();
252     if (!(widget->IsMaximized() || widget->IsFullscreen()) &&
253         (point_in_browser_view_coords.y() <
254          (tab_strip_region_view_->y() + kTabShadowSize))) {
255       // We return HTNOWHERE as this is a signal to our containing
256       // NonClientView that it should figure out what the correct hit-test
257       // code is given the mouse position...
258       return HTNOWHERE;
259     }
260   }
261 
262   // If the point's y coordinate is below the top of the topmost view and
263   // otherwise within the bounds of this view, the point is considered to be
264   // within the client area.
265   gfx::Rect bounds_from_toolbar_top = browser_view_->bounds();
266   bounds_from_toolbar_top.Inset(0, GetClientAreaTop(), 0, 0);
267   if (bounds_from_toolbar_top.Contains(point))
268     return HTCLIENT;
269 
270   // If the point's y coordinate is above the top of the toolbar, but not
271   // over the tabstrip (per previous checking in this function), then we
272   // consider it in the window caption (e.g. the area to the right of the
273   // tabstrip underneath the window controls). However, note that we DO NOT
274   // return HTCAPTION here, because when the window is maximized the window
275   // controls will fall into this space (since the BrowserView is sized to
276   // entire size of the window at that point), and the HTCAPTION value will
277   // cause the window controls not to work. So we return HTNOWHERE so that the
278   // caller will hit-test the window controls before finally falling back to
279   // HTCAPTION.
280   gfx::Rect tabstrip_background_bounds = browser_view_->bounds();
281   gfx::Point toolbar_origin = toolbar_->origin();
282   views::View::ConvertPointToTarget(top_container_, browser_view_,
283                                     &toolbar_origin);
284   tabstrip_background_bounds.set_height(toolbar_origin.y());
285   if (tabstrip_background_bounds.Contains(point))
286     return HTNOWHERE;
287 
288   // If the point is somewhere else, delegate to the default implementation.
289   return browser_view_->views::ClientView::NonClientHitTest(point);
290 }
291 
292 //////////////////////////////////////////////////////////////////////////////
293 // BrowserViewLayout, views::LayoutManager implementation:
294 
Layout(views::View * browser_view)295 void BrowserViewLayout::Layout(views::View* browser_view) {
296   TRACE_EVENT0("ui", "BrowserViewLayout::Layout");
297   vertical_layout_rect_ = browser_view->GetLocalBounds();
298   int top_inset = delegate_->GetTopInsetInBrowserView();
299   int top = LayoutTabStripRegion(top_inset);
300   if (delegate_->IsTabStripVisible()) {
301     tab_strip_->SetBackgroundOffset(tab_strip_region_view_->GetMirroredX() +
302                                     browser_view_->GetMirroredX() +
303                                     delegate_->GetThemeBackgroundXInset());
304   }
305   top = LayoutWebUITabStrip(top);
306   top = LayoutToolbar(top);
307 
308   top = LayoutBookmarkAndInfoBars(top, browser_view->y());
309 
310   // Top container requires updated toolbar and bookmark bar to compute bounds.
311   UpdateTopContainerBounds();
312 
313   // Layout items at the bottom of the view.
314   int bottom = LayoutWebFooterExperiment(browser_view->height());
315   bottom = LayoutDownloadShelf(bottom);
316 
317   // Layout the contents container in the remaining space.
318   const gfx::Rect old_contents_bounds = contents_container_->bounds();
319   LayoutContentsContainerView(top, bottom);
320 
321   if (contents_border_widget_ && contents_border_widget_->IsVisible()) {
322     gfx::Point contents_top_left;
323     views::View::ConvertPointToScreen(contents_container_, &contents_top_left);
324     contents_border_widget_->SetBounds(
325         gfx::Rect(contents_top_left.x(), contents_top_left.y(),
326                   contents_container_->width(), contents_container_->height()));
327   }
328 
329   // This must be done _after_ we lay out the WebContents since this
330   // code calls back into us to find the bounding box the find bar
331   // must be laid out within, and that code depends on the
332   // TabContentsContainer's bounds being up to date.
333   //
334   // Because Find Bar can be repositioned to keep from hiding find results, we
335   // don't want to reset its position on every layout, however - only if the
336   // geometry of the contents pane actually changes in a way that could affect
337   // the positioning of the bar.
338   if (delegate_->HasFindBarController() &&
339       (contents_container_->y() != old_contents_bounds.y() ||
340        contents_container_->width() != old_contents_bounds.width())) {
341     delegate_->MoveWindowForFindBarIfNecessary();
342   }
343 
344   // Adjust the fullscreen exit bubble bounds for |top_container_|'s new bounds.
345   // This makes the fullscreen exit bubble look like it animates with
346   // |top_container_| in immersive fullscreen.
347   ExclusiveAccessBubbleViews* exclusive_access_bubble =
348       delegate_->GetExclusiveAccessBubble();
349   if (exclusive_access_bubble)
350     exclusive_access_bubble->RepositionIfVisible();
351 
352   // Adjust any hosted dialogs if the browser's dialog hosting bounds changed.
353   const gfx::Rect dialog_bounds(dialog_host_->GetDialogPosition(gfx::Size()),
354                                 dialog_host_->GetMaximumDialogSize());
355   if (latest_dialog_bounds_ != dialog_bounds) {
356     latest_dialog_bounds_ = dialog_bounds;
357     dialog_host_->NotifyPositionRequiresUpdate();
358   }
359 }
360 
361 // Return the preferred size which is the size required to give each
362 // children their respective preferred size.
GetPreferredSize(const views::View * host) const363 gfx::Size BrowserViewLayout::GetPreferredSize(const views::View* host) const {
364   return gfx::Size();
365 }
366 
367 //////////////////////////////////////////////////////////////////////////////
368 // BrowserViewLayout, private:
369 
LayoutTabStripRegion(int top)370 int BrowserViewLayout::LayoutTabStripRegion(int top) {
371   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutTabStripRegion");
372   if (!delegate_->IsTabStripVisible()) {
373     SetViewVisibility(tab_strip_region_view_, false);
374     tab_strip_region_view_->SetBounds(0, 0, 0, 0);
375     return top;
376   }
377   // This retrieves the bounds for the tab strip based on whether or not we show
378   // anything to the left of it, like the incognito avatar.
379   gfx::Rect tab_strip_region_bounds(
380       delegate_->GetBoundsForTabStripRegionInBrowserView());
381 
382   SetViewVisibility(tab_strip_region_view_, true);
383   tab_strip_region_view_->SetBoundsRect(tab_strip_region_bounds);
384 
385   return tab_strip_region_bounds.bottom() -
386          GetLayoutConstant(TABSTRIP_TOOLBAR_OVERLAP);
387 }
388 
LayoutWebUITabStrip(int top)389 int BrowserViewLayout::LayoutWebUITabStrip(int top) {
390   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutWebUITabStrip");
391   if (!webui_tab_strip_)
392     return top;
393   if (!webui_tab_strip_->GetVisible()) {
394     webui_tab_strip_->SetBoundsRect(gfx::Rect());
395     return top;
396   }
397   webui_tab_strip_->SetBounds(
398       vertical_layout_rect_.x(), top, vertical_layout_rect_.width(),
399       webui_tab_strip_->GetHeightForWidth(vertical_layout_rect_.width()));
400   return webui_tab_strip_->bounds().bottom();
401 }
402 
LayoutToolbar(int top)403 int BrowserViewLayout::LayoutToolbar(int top) {
404   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutToolbar");
405   int browser_view_width = vertical_layout_rect_.width();
406   bool toolbar_visible = delegate_->IsToolbarVisible();
407   int height = toolbar_visible ? toolbar_->GetPreferredSize().height() : 0;
408   SetViewVisibility(toolbar_, toolbar_visible);
409   toolbar_->SetBounds(vertical_layout_rect_.x(), top, browser_view_width,
410                       height);
411   return toolbar_->bounds().bottom();
412 }
413 
LayoutBookmarkAndInfoBars(int top,int browser_view_y)414 int BrowserViewLayout::LayoutBookmarkAndInfoBars(int top, int browser_view_y) {
415   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutBookmarkAndInfoBars");
416   web_contents_modal_dialog_top_y_ =
417       top + browser_view_y - kConstrainedWindowOverlap;
418 
419   if (bookmark_bar_) {
420     top = std::max(toolbar_->bounds().bottom(), LayoutBookmarkBar(top));
421   }
422 
423   if (delegate_->IsContentsSeparatorEnabled() &&
424       (toolbar_->GetVisible() || bookmark_bar_) && top > 0) {
425     SetViewVisibility(contents_separator_, true);
426     const int separator_height =
427         contents_separator_->GetPreferredSize().height();
428     contents_separator_->SetBounds(vertical_layout_rect_.x(), top,
429                                    vertical_layout_rect_.width(),
430                                    separator_height);
431     if (loading_bar_) {
432       SetViewVisibility(loading_bar_, true);
433       loading_bar_->SetBounds(vertical_layout_rect_.x(), top - 2,
434                               vertical_layout_rect_.width(),
435                               separator_height + 2);
436       top_container_->ReorderChildView(loading_bar_, -1);
437     }
438     top += separator_height;
439   } else {
440     SetViewVisibility(contents_separator_, false);
441     if (loading_bar_)
442       SetViewVisibility(loading_bar_, false);
443   }
444 
445   return LayoutInfoBar(top);
446 }
447 
LayoutBookmarkBar(int top)448 int BrowserViewLayout::LayoutBookmarkBar(int top) {
449   if (!delegate_->IsBookmarkBarVisible()) {
450     SetViewVisibility(bookmark_bar_, false);
451     // TODO(jamescook): Don't change the bookmark bar height when it is
452     // invisible, so we can use its height for layout even in that state.
453     bookmark_bar_->SetBounds(0, top, browser_view_->width(), 0);
454     return top;
455   }
456 
457   bookmark_bar_->SetInfoBarVisible(IsInfobarVisible());
458   int bookmark_bar_height = bookmark_bar_->GetPreferredSize().height();
459   bookmark_bar_->SetBounds(vertical_layout_rect_.x(), top,
460                            vertical_layout_rect_.width(), bookmark_bar_height);
461   // Set visibility after setting bounds, as the visibility update uses the
462   // bounds to determine if the mouse is hovering over a button.
463   SetViewVisibility(bookmark_bar_, true);
464   return top + bookmark_bar_height;
465 }
466 
LayoutInfoBar(int top)467 int BrowserViewLayout::LayoutInfoBar(int top) {
468   // In immersive fullscreen or when top-chrome is fully hidden due to the page
469   // gesture scroll slide behavior, the infobar always starts near the top of
470   // the screen.
471   if (immersive_mode_controller_->IsEnabled() ||
472       (delegate_->IsTopControlsSlideBehaviorEnabled() &&
473        delegate_->GetTopControlsSlideBehaviorShownRatio() == 0.f)) {
474     // Can be null in tests.
475     top = browser_view_ ? browser_view_->y() : 0;
476   }
477 
478   SetViewVisibility(infobar_container_, IsInfobarVisible());
479   infobar_container_->SetBounds(
480       vertical_layout_rect_.x(), top, vertical_layout_rect_.width(),
481       infobar_container_->GetPreferredSize().height());
482   return top + infobar_container_->height();
483 }
484 
LayoutContentsContainerView(int top,int bottom)485 void BrowserViewLayout::LayoutContentsContainerView(int top, int bottom) {
486   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutContentsContainerView");
487   // |contents_container_| contains web page contents and devtools.
488   // See browser_view.h for details.
489   gfx::Rect contents_container_bounds(vertical_layout_rect_.x(),
490                                       top,
491                                       vertical_layout_rect_.width(),
492                                       std::max(0, bottom - top));
493   if (webui_tab_strip_ && webui_tab_strip_->GetVisible()) {
494     // The WebUI tab strip container should "push" the tab contents down without
495     // resizing it.
496     contents_container_bounds.Inset(0, 0, 0,
497                                     -webui_tab_strip_->size().height());
498   }
499 
500   if (side_panel_ && side_panel_->GetVisible()) {
501     // Side panel occupies some of the container's space.
502     gfx::Rect side_panel_bounds = contents_container_bounds;
503     side_panel_bounds.set_width(side_panel_->GetPreferredSize().width());
504 
505     // Shrink container bounds to fit the side panel.
506     contents_container_bounds.set_width(contents_container_bounds.width() -
507                                         side_panel_bounds.width());
508     // Place the side panel to the right of contents.
509     side_panel_bounds.set_x(contents_container_bounds.x() +
510                             contents_container_bounds.width());
511 
512     gfx::Rect separator_bounds = contents_separator_->bounds();
513     const int separator_height = separator_bounds.height();
514     // Raise the side panel bounds with the height of the separator to have it
515     // connected to the toolbar area (and not be spoofable by web content).
516     side_panel_bounds.set_y(side_panel_bounds.y() - separator_height);
517     side_panel_bounds.set_height(side_panel_bounds.height() + separator_height);
518     side_panel_->SetBoundsRect(side_panel_bounds);
519 
520     // Resize separator so that it separates the contents area only.
521     separator_bounds.set_width(contents_container_bounds.width() + 1);
522     contents_separator_->SetBoundsRect(separator_bounds);
523   }
524 
525   contents_container_->SetBoundsRect(contents_container_bounds);
526 }
527 
UpdateTopContainerBounds()528 void BrowserViewLayout::UpdateTopContainerBounds() {
529   // Set the bounds of the top container view such that it is tall enough to
530   // fully show all of its children. In particular, the bottom of the bookmark
531   // bar can be above the bottom of the toolbar while the bookmark bar is
532   // animating. The top container view is positioned relative to the top of the
533   // client view instead of relative to GetTopInsetInBrowserView() because the
534   // top container view paints parts of the frame (title, window controls)
535   // during an immersive fullscreen reveal.
536   int height = 0;
537   for (views::View* child : top_container_->children()) {
538     if (child->GetVisible())
539       height = std::max(height, child->bounds().bottom());
540   }
541 
542   // Ensure that the top container view reaches the topmost view in the
543   // ClientView because the bounds of the top container view are used in
544   // layout and we assume that this is the case.
545   height = std::max(height, delegate_->GetTopInsetInBrowserView());
546 
547   gfx::Rect top_container_bounds(vertical_layout_rect_.width(), height);
548 
549   if (delegate_->IsTopControlsSlideBehaviorEnabled()) {
550     // If the top controls are fully hidden, then it's positioned outside the
551     // views' bounds.
552     const float ratio = delegate_->GetTopControlsSlideBehaviorShownRatio();
553     top_container_bounds.set_y(ratio == 0 ? -height : 0);
554   } else {
555     // If the immersive mode controller is animating the top container, it may
556     // be partly offscreen.
557     top_container_bounds.set_y(
558         immersive_mode_controller_->GetTopContainerVerticalOffset(
559             top_container_bounds.size()));
560   }
561   top_container_->SetBoundsRect(top_container_bounds);
562 }
563 
LayoutDownloadShelf(int bottom)564 int BrowserViewLayout::LayoutDownloadShelf(int bottom) {
565   TRACE_EVENT0("ui", "BrowserViewLayout::LayoutDownloadShelf");
566   if (download_shelf_ && download_shelf_->GetVisible()) {
567     const int height = download_shelf_->GetPreferredSize().height();
568     download_shelf_->SetBounds(vertical_layout_rect_.x(), bottom - height,
569                                vertical_layout_rect_.width(), height);
570     bottom -= height;
571   }
572   return bottom;
573 }
574 
GetClientAreaTop()575 int BrowserViewLayout::GetClientAreaTop() {
576   // If webui_tab_strip is displayed, the client area starts at its top,
577   // otherwise at the top of the toolbar.
578   return webui_tab_strip_ && webui_tab_strip_->GetVisible()
579              ? webui_tab_strip_->y()
580              : toolbar_->y();
581 }
582 
LayoutWebFooterExperiment(int bottom)583 int BrowserViewLayout::LayoutWebFooterExperiment(int bottom) {
584   if (!web_footer_experiment_)
585     return bottom;
586   bottom -= 1;
587   web_footer_experiment_->SetBounds(vertical_layout_rect_.x(), bottom,
588                                     vertical_layout_rect_.width(), 1);
589   return bottom;
590 }
591 
IsInfobarVisible() const592 bool BrowserViewLayout::IsInfobarVisible() const {
593   // NOTE: Can't check if the size IsEmpty() since it's always 0-width.
594   return infobar_container_->GetPreferredSize().height() != 0;
595 }
596