From 09c29db6b0635f4b7812e8ea6632daee12426117 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Wed, 28 Jan 2015 13:43:31 -0800 Subject: [PATCH] Plumb ViewportMetrics change notifications around the world and back. This fixes the race which is seen where mojo_shell will sometimes launch with the wrong viewport metrics since the DisplayManager will happily respond to GetViewportMetrics with default values before the NativeViewport has fully booted and told it what the actual values are. I considered making DisplayManager hang until the NativeViewport was ready, but I decided that it does make sense for the ViewportMetrics (device pixel ratio, mostly) to change for a view if it were to move between displays (as exists on desktop OSes today). R=abarth@chromium.org, sky@chromium.org Review URL: https://codereview.chromium.org/880743002 --- viewer/document_view.cc | 19 +++++++++++++++---- viewer/document_view.h | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/viewer/document_view.cc b/viewer/document_view.cc index 0c5f9a2a0cf..80ae7db9e62 100644 --- a/viewer/document_view.cc +++ b/viewer/document_view.cc @@ -118,10 +118,7 @@ void DocumentView::OnEmbed( Load(response_.Pass()); - auto& bounds = root_->bounds(); - float device_pixel_ratio = GetDevicePixelRatio(); - web_view_->resize(blink::WebSize(bounds.width / device_pixel_ratio, - bounds.height / device_pixel_ratio)); + UpdateRootSizeAndViewportMetrics(root_->bounds()); // TODO(abarth): We should ask the view whether it is focused instead of // assuming that we're focused. @@ -270,6 +267,20 @@ void DocumentView::OnViewBoundsChanged(mojo::View* view, const mojo::Rect& old_bounds, const mojo::Rect& new_bounds) { DCHECK_EQ(view, root_); + UpdateRootSizeAndViewportMetrics(new_bounds); +} + +void DocumentView::OnViewViewportMetricsChanged( + mojo::View* view, + const mojo::ViewportMetrics& old_metrics, + const mojo::ViewportMetrics& new_metrics) { + DCHECK_EQ(view, root_); + web_view_->setDeviceScaleFactor(GetDevicePixelRatio()); + UpdateRootSizeAndViewportMetrics(root_->bounds()); +} + +void DocumentView::UpdateRootSizeAndViewportMetrics( + const mojo::Rect& new_bounds) { float device_pixel_ratio = GetDevicePixelRatio(); web_view_->resize(blink::WebSize(new_bounds.width / device_pixel_ratio, new_bounds.height / device_pixel_ratio)); diff --git a/viewer/document_view.h b/viewer/document_view.h index 58e526d3040..3c57354cfa5 100644 --- a/viewer/document_view.h +++ b/viewer/document_view.h @@ -112,6 +112,10 @@ class DocumentView : public blink::ServiceProvider, void OnViewBoundsChanged(mojo::View* view, const mojo::Rect& old_bounds, const mojo::Rect& new_bounds) override; + void OnViewViewportMetricsChanged( + mojo::View* view, + const mojo::ViewportMetrics& old_metrics, + const mojo::ViewportMetrics& new_metrics) override; void OnViewFocusChanged(mojo::View* gained_focus, mojo::View* lost_focus) override; void OnViewDestroyed(mojo::View* view) override; @@ -121,6 +125,8 @@ class DocumentView : public blink::ServiceProvider, float GetDevicePixelRatio() const; scoped_ptr CreateRasterizer(); + void UpdateRootSizeAndViewportMetrics(const mojo::Rect& new_bounds); + mojo::URLResponsePtr response_; mojo::ServiceProviderImpl exported_services_; mojo::ServiceProviderPtr imported_services_;