Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: content/browser/web_contents/aura/overscroll_navigation_overlay.cc

Issue 1072123002: Refactor GestureNavigation to eliminate code redundancy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed window ownership Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h" 5 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h"
6 6
7 #include <vector>
8
9 #include "base/i18n/rtl.h"
7 #include "content/browser/frame_host/navigation_entry_impl.h" 10 #include "content/browser/frame_host/navigation_entry_impl.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h" 11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/browser/web_contents/aura/overscroll_window_delegate.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 13 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/view_messages.h" 14 #include "content/common/view_messages.h"
11 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_widget_host_view.h" 16 #include "content/public/browser/render_widget_host_view.h"
13 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
14 #include "ui/aura_extra/image_window_delegate.h"
15 #include "ui/base/layout.h" 18 #include "ui/base/layout.h"
16 #include "ui/compositor/layer.h" 19 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animation_observer.h" 20 #include "ui/compositor/layer_animation_observer.h"
18 #include "ui/compositor/paint_recorder.h" 21 #include "ui/compositor/paint_recorder.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h" 22 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/gfx/canvas.h" 23 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image_png_rep.h" 24 #include "ui/gfx/image/image_png_rep.h"
22 #include "ui/gfx/image/image_skia.h"
23 25
24 namespace content { 26 namespace content {
25 namespace { 27 namespace {
26 28
27 // Returns true if the entry's URL or any of the URLs in entry's redirect chain 29 // Returns true if the entry's URL or any of the URLs in entry's redirect chain
28 // match |url|. 30 // match |url|.
29 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) { 31 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) {
30 if (!entry) 32 if (!entry)
31 return false; 33 return false;
32 if (entry->GetURL() == url) 34 if (entry->GetURL() == url)
33 return true; 35 return true;
34 const std::vector<GURL>& redirect_chain = entry->GetRedirectChain(); 36 const std::vector<GURL>& redirect_chain = entry->GetRedirectChain();
35 for (std::vector<GURL>::const_iterator it = redirect_chain.begin(); 37 for (std::vector<GURL>::const_iterator it = redirect_chain.begin();
36 it != redirect_chain.end(); 38 it != redirect_chain.end();
37 it++) { 39 it++) {
38 if (*it == url) 40 if (*it == url)
39 return true; 41 return true;
40 } 42 }
41 return false; 43 return false;
42 } 44 }
43 45
44 } // namespace 46 } // namespace
45 47
46 // A LayerDelegate that paints an image for the layer. 48 // A class that sets masks to bounds to false on a layer and restores the old
47 class ImageLayerDelegate : public ui::LayerDelegate { 49 // value on destruction.
50 class OverscrollNavigationOverlay::ScopedLayerClippingSetting {
48 public: 51 public:
49 ImageLayerDelegate() {} 52 explicit ScopedLayerClippingSetting(ui::Layer* layer)
53 : masks_to_bounds_(layer->GetMasksToBounds()), layer_(layer) {
54 layer_->SetMasksToBounds(false);
55 }
50 56
51 ~ImageLayerDelegate() override {} 57 ~ScopedLayerClippingSetting() { layer_->SetMasksToBounds(masks_to_bounds_); }
52
53 void SetImage(const gfx::Image& image) {
54 image_ = image;
55 image_size_ = image.AsImageSkia().size();
56 }
57 const gfx::Image& image() const { return image_; }
58 58
59 private: 59 private:
60 // Overridden from ui::LayerDelegate: 60 bool masks_to_bounds_;
61 void OnPaintLayer(const ui::PaintContext& context) override { 61 ui::Layer* layer_;
62 ui::PaintRecorder recorder(context);
63 if (image_.IsEmpty()) {
64 recorder.canvas()->DrawColor(SK_ColorWHITE);
65 } else {
66 SkISize size = recorder.canvas()->sk_canvas()->getDeviceSize();
67 if (size.width() != image_size_.width() ||
68 size.height() != image_size_.height()) {
69 recorder.canvas()->DrawColor(SK_ColorWHITE);
70 }
71 recorder.canvas()->DrawImageInt(image_.AsImageSkia(), 0, 0);
72 }
73 }
74
75 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
76
77 // Called when the layer's device scale factor has changed.
78 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
79
80 // Invoked prior to the bounds changing. The returned closured is run after
81 // the bounds change.
82 base::Closure PrepareForLayerBoundsChange() override {
83 return base::Closure();
84 }
85
86 gfx::Image image_;
87 gfx::Size image_size_;
88
89 DISALLOW_COPY_AND_ASSIGN(ImageLayerDelegate);
90 }; 62 };
91 63
92 // Responsible for fading out and deleting the layer of the overlay window. 64 // Responsible for fading out and deleting the layer of the overlay window.
93 class OverlayDismissAnimator 65 class OverlayDismissAnimator
94 : public ui::LayerAnimationObserver { 66 : public ui::LayerAnimationObserver {
95 public: 67 public:
96 // Takes ownership of the layer. 68 // Takes ownership of the layer.
97 explicit OverlayDismissAnimator(scoped_ptr<ui::Layer> layer) 69 explicit OverlayDismissAnimator(scoped_ptr<ui::Layer> layer)
98 : layer_(layer.Pass()) { 70 : layer_(layer.Pass()) {
99 CHECK(layer_.get()); 71 CHECK(layer_.get());
(...skipping 25 matching lines...) Expand all
125 97
126 private: 98 private:
127 ~OverlayDismissAnimator() override {} 99 ~OverlayDismissAnimator() override {}
128 100
129 scoped_ptr<ui::Layer> layer_; 101 scoped_ptr<ui::Layer> layer_;
130 102
131 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); 103 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator);
132 }; 104 };
133 105
134 OverscrollNavigationOverlay::OverscrollNavigationOverlay( 106 OverscrollNavigationOverlay::OverscrollNavigationOverlay(
135 WebContentsImpl* web_contents) 107 WebContentsImpl* web_contents,
136 : web_contents_(web_contents), 108 aura::Window* web_contents_window)
137 image_delegate_(NULL), 109 : direction_(NONE),
110 web_contents_(web_contents),
138 loading_complete_(false), 111 loading_complete_(false),
139 received_paint_update_(false), 112 received_paint_update_(false),
140 slide_direction_(SLIDE_UNKNOWN) { 113 owa_(new OverscrollWindowAnimation(this)),
114 web_contents_window_(web_contents_window) {
141 } 115 }
142 116
143 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { 117 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() {
118 aura::Window* event_window = GetMainWindow();
119 if (owa_->is_active() && event_window)
120 event_window->ReleaseCapture();
144 } 121 }
145 122
146 void OverscrollNavigationOverlay::StartObserving() { 123 void OverscrollNavigationOverlay::StartObserving() {
147 loading_complete_ = false; 124 loading_complete_ = false;
148 received_paint_update_ = false; 125 received_paint_update_ = false;
149 overlay_dismiss_layer_.reset();
150 Observe(web_contents_); 126 Observe(web_contents_);
151 127
152 // Make sure the overlay window is on top.
153 if (window_.get() && window_->parent())
154 window_->parent()->StackChildAtTop(window_.get());
155
156 // Assumes the navigation has been initiated. 128 // Assumes the navigation has been initiated.
157 NavigationEntry* pending_entry = 129 NavigationEntry* pending_entry =
158 web_contents_->GetController().GetPendingEntry(); 130 web_contents_->GetController().GetPendingEntry();
159 // Save url of the pending entry to identify when it loads and paints later. 131 // Save url of the pending entry to identify when it loads and paints later.
160 // Under some circumstances navigation can leave a null pending entry - 132 // Under some circumstances navigation can leave a null pending entry -
161 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). 133 // see comments in NavigationControllerImpl::NavigateToPendingEntry().
162 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); 134 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL();
163 } 135 }
164 136
165 void OverscrollNavigationOverlay::SetOverlayWindow(
166 scoped_ptr<aura::Window> window,
167 aura_extra::ImageWindowDelegate* delegate) {
168 window_ = window.Pass();
169 if (window_.get() && window_->parent())
170 window_->parent()->StackChildAtTop(window_.get());
171 image_delegate_ = delegate;
172
173 if (window_.get() && delegate->has_image()) {
174 window_slider_.reset(new WindowSlider(this,
175 window_->parent(),
176 window_.get()));
177 slide_direction_ = SLIDE_UNKNOWN;
178 } else {
179 window_slider_.reset();
180 }
181 }
182
183 void OverscrollNavigationOverlay::StopObservingIfDone() { 137 void OverscrollNavigationOverlay::StopObservingIfDone() {
184 // Normally we dismiss the overlay once we receive a paint update, however 138 // Normally we dismiss the overlay once we receive a paint update, however
185 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get 139 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get
186 // called, and we rely on loading_complete_ for those cases. 140 // called, and we rely on loading_complete_ for those cases.
187 if (!received_paint_update_ && !loading_complete_) 141 // If an overscroll gesture is in progress, then do not destroy the window.
142 if (!window_ || !(loading_complete_ || received_paint_update_) ||
143 owa_->is_active()) {
188 return; 144 return;
145 }
146 // Restore layer clipping.
147 contents_layer_settings_.reset();
148 contents_layer_parent_settings_.reset();
189 149
190 // If a slide is in progress, then do not destroy the window or the slide. 150 // OverlayDismissAnimator deletes the dismiss layer and itself when the
191 if (window_slider_.get() && window_slider_->IsSlideInProgress()) 151 // animation completes.
192 return; 152 scoped_ptr<ui::Layer> dismiss_layer = window_->AcquireLayer();
193
194 // The layer to be animated by OverlayDismissAnimator
195 scoped_ptr<ui::Layer> overlay_dismiss_layer;
196 if (overlay_dismiss_layer_)
197 overlay_dismiss_layer = overlay_dismiss_layer_.Pass();
198 else if (window_.get())
199 overlay_dismiss_layer = window_->AcquireLayer();
200 Observe(NULL);
201 window_slider_.reset();
202 window_.reset(); 153 window_.reset();
203 image_delegate_ = NULL; 154 (new OverlayDismissAnimator(dismiss_layer.Pass()))->Animate();
204 if (overlay_dismiss_layer.get()) { 155 Observe(nullptr);
205 // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the 156 received_paint_update_ = false;
206 // animation completes. 157 loading_complete_ = false;
207 (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate();
208 }
209 } 158 }
210 159
211 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) { 160 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateOverlayWindow(
161 const gfx::Rect& bounds) {
162 OverscrollWindowDelegate* overscroll_delegate = new OverscrollWindowDelegate(
163 owa_.get(), GetImageForDirection(direction_));
164 scoped_ptr<aura::Window> window(new aura::Window(overscroll_delegate));
165 window->set_owned_by_parent(false);
166 window->SetTransparent(true);
167 window->Init(ui::LAYER_TEXTURED);
168 window->layer()->SetMasksToBounds(false);
169 window->SetName("OverscrollOverlay");
170 web_contents_window_->AddChild(window.get());
171 aura::Window* event_window = GetMainWindow();
172 if (direction_ == FORWARD)
173 web_contents_window_->StackChildAbove(window.get(), event_window);
174 else
175 web_contents_window_->StackChildBelow(window.get(), event_window);
176 window->SetBounds(bounds);
177 // Set capture on the window that is receiving the overscroll events so that
178 // trackpad scroll gestures keep targetting it even if the mouse pointer moves
179 // off its bounds.
180 event_window->SetCapture();
181 window->Show();
182 return window.Pass();
183 }
184
185 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
186 NavigationDirection direction) const {
212 const NavigationControllerImpl& controller = web_contents_->GetController(); 187 const NavigationControllerImpl& controller = web_contents_->GetController();
213 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); 188 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
189 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1));
214 190
215 gfx::Image image;
216 if (entry && entry->screenshot().get()) { 191 if (entry && entry->screenshot().get()) {
217 std::vector<gfx::ImagePNGRep> image_reps; 192 std::vector<gfx::ImagePNGRep> image_reps;
218 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); 193 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
219 image = gfx::Image(image_reps); 194 return gfx::Image(image_reps);
220 } 195 }
221 if (!layer_delegate_) 196 return gfx::Image();
222 layer_delegate_.reset(new ImageLayerDelegate());
223 layer_delegate_->SetImage(image);
224
225 ui::Layer* layer = new ui::Layer(ui::LAYER_TEXTURED);
226 layer->set_delegate(layer_delegate_.get());
227 return layer;
228 } 197 }
229 198
230 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { 199 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateFrontWindow(
231 if (!web_contents_->GetController().CanGoBack()) 200 const gfx::Rect& bounds) {
232 return NULL; 201 if (!web_contents_->GetController().CanGoForward())
233 slide_direction_ = SLIDE_BACK; 202 return nullptr;
234 return CreateSlideLayer(-1); 203 direction_ = FORWARD;
204 return CreateOverlayWindow(bounds);
235 } 205 }
236 206
237 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { 207 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateBackWindow(
238 if (!web_contents_->GetController().CanGoForward()) 208 const gfx::Rect& bounds) {
239 return NULL; 209 if (!web_contents_->GetController().CanGoBack())
240 slide_direction_ = SLIDE_FRONT; 210 return nullptr;
241 return CreateSlideLayer(1); 211 direction_ = BACK;
212 return CreateOverlayWindow(bounds);
242 } 213 }
243 214
244 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { 215 aura::Window* OverscrollNavigationOverlay::GetMainWindow() const {
245 if (slide_direction_ == SLIDE_UNKNOWN) 216 if (window_)
246 return; 217 return window_.get();
218 return web_contents_->IsBeingDestroyed()
219 ? nullptr
220 : web_contents_->GetContentNativeView();
221 }
247 222
248 // Perform the navigation. 223 void OverscrollNavigationOverlay::OnOverscrollCompleting() {
249 if (slide_direction_ == SLIDE_BACK) 224 GetMainWindow()->ReleaseCapture();
225 // We start the navigation as soon as we know the overscroll gesture is
226 // completing.
227 DCHECK(direction_ != NONE);
228
229 // Avoid clipping on the screenshot caused by the contents window being moved
230 // outside of the screen bounds.
231 contents_layer_settings_.reset(
232 new ScopedLayerClippingSetting(web_contents_->GetNativeView()->layer()));
233 contents_layer_parent_settings_.reset(new ScopedLayerClippingSetting(
234 web_contents_->GetNativeView()->layer()->parent()));
235
236 // Make sure we can navigate first, as other factors can trigger a navigation
237 // during an overscroll gesture and navigating without history produces a
238 // crash.
239 if (direction_ == FORWARD && web_contents_->GetController().CanGoForward())
240 web_contents_->GetController().GoForward();
241 if (direction_ == BACK && web_contents_->GetController().CanGoBack())
250 web_contents_->GetController().GoBack(); 242 web_contents_->GetController().GoBack();
251 else if (slide_direction_ == SLIDE_FRONT)
252 web_contents_->GetController().GoForward();
253 else
254 NOTREACHED();
255
256 // Reset state and wait for the new navigation page to complete
257 // loading/painting.
258 StartObserving(); 243 StartObserving();
259 } 244 }
260 245
261 void OverscrollNavigationOverlay::OnWindowSlideCompleted( 246 void OverscrollNavigationOverlay::OnOverscrollCompleted(
262 scoped_ptr<ui::Layer> layer) { 247 scoped_ptr<aura::Window> window) {
263 if (slide_direction_ == SLIDE_UNKNOWN) { 248 GetMainWindow()->SetTransform(gfx::Transform());
264 window_slider_.reset(); 249 window_ = window.Pass();
265 StopObservingIfDone(); 250 // Make sure the window is in its default position.
266 return; 251 window_->SetBounds(gfx::Rect(web_contents_window_->bounds().size()));
267 } 252 window_->SetTransform(gfx::Transform());
268 253 // Make sure the overlay window is on top.
269 // Change the image used for the overlay window. 254 web_contents_window_->StackChildAtTop(window_.get());
270 image_delegate_->SetImage(layer_delegate_->image()); 255 direction_ = NONE;
271 window_->layer()->SetTransform(gfx::Transform());
272 window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size()));
273 slide_direction_ = SLIDE_UNKNOWN;
274 // We may end up dismissing the overlay before it has a chance to repaint, so
275 // set the slider layer to be the one animated by OverlayDismissAnimator.
276 if (layer.get())
277 overlay_dismiss_layer_ = layer.Pass();
278 StopObservingIfDone(); 256 StopObservingIfDone();
279 } 257 }
280 258
281 void OverscrollNavigationOverlay::OnWindowSlideAborted() { 259 void OverscrollNavigationOverlay::OnOverscrollCancelled() {
260 GetMainWindow()->ReleaseCapture();
261 direction_ = NONE;
282 StopObservingIfDone(); 262 StopObservingIfDone();
283 } 263 }
284 264
285 void OverscrollNavigationOverlay::OnWindowSliderDestroyed() {
286 // We only want to take an action here if WindowSlider is being destroyed
287 // outside of OverscrollNavigationOverlay. If window_slider_.get() is NULL,
288 // then OverscrollNavigationOverlay is the one destroying WindowSlider, and
289 // we don't need to do anything.
290 // This check prevents StopObservingIfDone() being called multiple times
291 // (including recursively) for a single event.
292 if (window_slider_.get()) {
293 // The slider has just been destroyed. Release the ownership.
294 ignore_result(window_slider_.release());
295 StopObservingIfDone();
296 }
297 }
298
299 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() { 265 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() {
300 NavigationEntry* visible_entry = 266 NavigationEntry* visible_entry =
301 web_contents_->GetController().GetVisibleEntry(); 267 web_contents_->GetController().GetVisibleEntry();
302 if (pending_entry_url_.is_empty() || 268 if (pending_entry_url_.is_empty() ||
303 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { 269 DoesEntryMatchURL(visible_entry, pending_entry_url_)) {
304 received_paint_update_ = true; 270 received_paint_update_ = true;
305 StopObservingIfDone(); 271 StopObservingIfDone();
306 } 272 }
307 } 273 }
308 274
309 void OverscrollNavigationOverlay::DidStopLoading() { 275 void OverscrollNavigationOverlay::DidStopLoading() {
310 // Don't compare URLs in this case - it's possible they won't match if 276 // Don't compare URLs in this case - it's possible they won't match if
311 // a gesture-nav initiated navigation was interrupted by some other in-site 277 // a gesture-nav initiated navigation was interrupted by some other in-site
312 // navigation ((e.g., from a script, or from a bookmark). 278 // navigation (e.g., from a script, or from a bookmark).
313 loading_complete_ = true; 279 loading_complete_ = true;
314 StopObservingIfDone(); 280 StopObservingIfDone();
315 } 281 }
316 282
317 } // namespace content 283 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698