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

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

Issue 895543005: Refactor GestureNavigation to eliminate code redundancy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nits 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
7 #include "content/browser/frame_host/navigation_entry_impl.h" 9 #include "content/browser/frame_host/navigation_entry_impl.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h" 10 #include "content/browser/renderer_host/render_view_host_impl.h"
11 #include "content/browser/web_contents/aura/overscroll_window_delegate.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 12 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/view_messages.h" 13 #include "content/common/view_messages.h"
11 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_widget_host_view.h" 15 #include "content/public/browser/render_widget_host_view.h"
13 #include "ui/aura/window.h" 16 #include "ui/aura/window.h"
14 #include "ui/aura_extra/image_window_delegate.h"
15 #include "ui/base/layout.h" 17 #include "ui/base/layout.h"
16 #include "ui/compositor/layer.h" 18 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animation_observer.h" 19 #include "ui/compositor/layer_animation_observer.h"
18 #include "ui/compositor/paint_context.h" 20 #include "ui/compositor/paint_context.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h" 21 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/gfx/canvas.h" 22 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image_png_rep.h" 23 #include "ui/gfx/image/image_png_rep.h"
22 #include "ui/gfx/image/image_skia.h"
23 24
24 namespace content { 25 namespace content {
25 namespace { 26 namespace {
26 27
27 // Returns true if the entry's URL or any of the URLs in entry's redirect chain 28 // Returns true if the entry's URL or any of the URLs in entry's redirect chain
28 // match |url|. 29 // match |url|.
29 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) { 30 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) {
30 if (!entry) 31 if (!entry)
31 return false; 32 return false;
32 if (entry->GetURL() == url) 33 if (entry->GetURL() == url)
33 return true; 34 return true;
34 const std::vector<GURL>& redirect_chain = entry->GetRedirectChain(); 35 const std::vector<GURL>& redirect_chain = entry->GetRedirectChain();
35 for (std::vector<GURL>::const_iterator it = redirect_chain.begin(); 36 for (std::vector<GURL>::const_iterator it = redirect_chain.begin();
36 it != redirect_chain.end(); 37 it != redirect_chain.end();
37 it++) { 38 it++) {
38 if (*it == url) 39 if (*it == url)
39 return true; 40 return true;
40 } 41 }
41 return false; 42 return false;
42 } 43 }
43 44
44 } // namespace 45 } // namespace
45 46
46 // A LayerDelegate that paints an image for the layer. 47 // A class that sets masks to bounds to false on a layer and restores the old
47 class ImageLayerDelegate : public ui::LayerDelegate { 48 // value on destruction.
49 class OverscrollNavigationOverlay::ScopedLayerClippingSetting {
48 public: 50 public:
49 ImageLayerDelegate() {} 51 explicit ScopedLayerClippingSetting(ui::Layer* layer)
52 : masks_to_bounds_(layer->GetMasksToBounds()), layer_(layer) {
53 layer_->SetMasksToBounds(false);
54 }
50 55
51 ~ImageLayerDelegate() override {} 56 ~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 57
59 private: 58 private:
60 // Overridden from ui::LayerDelegate: 59 bool masks_to_bounds_;
61 void OnPaintLayer(const ui::PaintContext& context) override { 60 ui::Layer* layer_;
62 gfx::Canvas* canvas = context.canvas();
63 if (image_.IsEmpty()) {
64 canvas->DrawColor(SK_ColorWHITE);
65 } else {
66 SkISize size = canvas->sk_canvas()->getDeviceSize();
67 if (size.width() != image_size_.width() ||
68 size.height() != image_size_.height()) {
69 canvas->DrawColor(SK_ColorWHITE);
70 }
71 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 }; 61 };
91 62
92 // Responsible for fading out and deleting the layer of the overlay window. 63 // Responsible for fading out and deleting the layer of the overlay window.
93 class OverlayDismissAnimator 64 class OverlayDismissAnimator
94 : public ui::LayerAnimationObserver { 65 : public ui::LayerAnimationObserver {
95 public: 66 public:
96 // Takes ownership of the layer. 67 // Takes ownership of the layer.
97 explicit OverlayDismissAnimator(scoped_ptr<ui::Layer> layer) 68 explicit OverlayDismissAnimator(scoped_ptr<ui::Layer> layer)
98 : layer_(layer.Pass()) { 69 : layer_(layer.Pass()) {
99 CHECK(layer_.get()); 70 CHECK(layer_.get());
(...skipping 25 matching lines...) Expand all
125 96
126 private: 97 private:
127 ~OverlayDismissAnimator() override {} 98 ~OverlayDismissAnimator() override {}
128 99
129 scoped_ptr<ui::Layer> layer_; 100 scoped_ptr<ui::Layer> layer_;
130 101
131 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); 102 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator);
132 }; 103 };
133 104
134 OverscrollNavigationOverlay::OverscrollNavigationOverlay( 105 OverscrollNavigationOverlay::OverscrollNavigationOverlay(
135 WebContentsImpl* web_contents) 106 WebContentsImpl* web_contents,
136 : web_contents_(web_contents), 107 aura::Window* web_contents_window)
137 image_delegate_(NULL), 108 : direction_(NONE),
109 web_contents_(web_contents),
138 loading_complete_(false), 110 loading_complete_(false),
139 received_paint_update_(false), 111 received_paint_update_(false),
140 slide_direction_(SLIDE_UNKNOWN) { 112 owa_(new OverscrollWindowAnimation(this)),
113 web_contents_window_(web_contents_window) {
141 } 114 }
142 115
143 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { 116 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() {
117 if (owa_->is_active())
118 GetMainWindow()->ReleaseCapture();
144 } 119 }
145 120
146 void OverscrollNavigationOverlay::StartObserving() { 121 void OverscrollNavigationOverlay::StartObserving() {
147 loading_complete_ = false; 122 loading_complete_ = false;
148 received_paint_update_ = false; 123 received_paint_update_ = false;
149 overlay_dismiss_layer_.reset();
150 Observe(web_contents_); 124 Observe(web_contents_);
151 125
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. 126 // Assumes the navigation has been initiated.
157 NavigationEntry* pending_entry = 127 NavigationEntry* pending_entry =
158 web_contents_->GetController().GetPendingEntry(); 128 web_contents_->GetController().GetPendingEntry();
159 // Save url of the pending entry to identify when it loads and paints later. 129 // 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 - 130 // Under some circumstances navigation can leave a null pending entry -
161 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). 131 // see comments in NavigationControllerImpl::NavigateToPendingEntry().
162 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); 132 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL();
163 } 133 }
164 134
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() { 135 void OverscrollNavigationOverlay::StopObservingIfDone() {
184 // Normally we dismiss the overlay once we receive a paint update, however 136 // Normally we dismiss the overlay once we receive a paint update, however
185 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get 137 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get
186 // called, and we rely on loading_complete_ for those cases. 138 // called, and we rely on loading_complete_ for those cases.
187 if (!received_paint_update_ && !loading_complete_) 139 // If an overscroll gesture is in progress, then do not destroy the window.
140 if (!window_ || !(loading_complete_ || received_paint_update_) ||
141 owa_->is_active()) {
188 return; 142 return;
143 }
144 // Restore layer clipping.
145 contents_layer_settings_.reset();
146 contents_layer_parent_settings_.reset();
189 147
190 // If a slide is in progress, then do not destroy the window or the slide. 148 // OverlayDismissAnimator deletes the dismiss layer and itself when the
191 if (window_slider_.get() && window_slider_->IsSlideInProgress()) 149 // animation completes.
192 return; 150 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(); 151 window_.reset();
203 image_delegate_ = NULL; 152 (new OverlayDismissAnimator(dismiss_layer.Pass()))->Animate();
204 if (overlay_dismiss_layer.get()) { 153 Observe(nullptr);
205 // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the 154 received_paint_update_ = false;
206 // animation completes. 155 loading_complete_ = false;
207 (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate();
208 }
209 } 156 }
210 157
211 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) { 158 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateOverlayWindow() {
159 OverscrollWindowDelegate* overscroll_delegate = new OverscrollWindowDelegate(
160 owa_.get(), GetImageForDirection(direction_));
161 scoped_ptr<aura::Window> window(new aura::Window(overscroll_delegate));
162 window->SetTransparent(true);
163 window->Init(ui::LAYER_TEXTURED);
164 window->layer()->SetMasksToBounds(false);
165 window->SetName("OverscrollOverlay");
166 web_contents_window_->AddChild(window.get());
167 aura::Window* event_window = GetMainWindow();
168 if (direction_ == FORWARD)
169 web_contents_window_->StackChildAbove(window.get(), event_window);
170 else
171 web_contents_window_->StackChildBelow(window.get(), event_window);
172 // Set capture on the window that is receiving the overscroll events so that
173 // trackpad scroll gestures keep targetting it even if the mouse pointer moves
174 // off its bounds.
175 event_window->SetCapture();
176 window->Show();
177 return window.Pass();
178 }
179
180 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
181 NavigationDirection direction) const {
212 const NavigationControllerImpl& controller = web_contents_->GetController(); 182 const NavigationControllerImpl& controller = web_contents_->GetController();
213 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); 183 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
184 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1));
214 185
215 gfx::Image image;
216 if (entry && entry->screenshot().get()) { 186 if (entry && entry->screenshot().get()) {
217 std::vector<gfx::ImagePNGRep> image_reps; 187 std::vector<gfx::ImagePNGRep> image_reps;
218 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); 188 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
219 image = gfx::Image(image_reps); 189 return gfx::Image(image_reps);
220 } 190 }
221 if (!layer_delegate_) 191 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 } 192 }
229 193
230 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { 194 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateFrontWindow() {
231 if (!web_contents_->GetController().CanGoBack()) 195 if (!web_contents_->GetController().CanGoForward())
232 return NULL; 196 return nullptr;
233 slide_direction_ = SLIDE_BACK; 197 direction_ = FORWARD;
234 return CreateSlideLayer(-1); 198 return CreateOverlayWindow();
235 } 199 }
236 200
237 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { 201 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateBackWindow() {
238 if (!web_contents_->GetController().CanGoForward()) 202 if (!web_contents_->GetController().CanGoBack())
239 return NULL; 203 return nullptr;
240 slide_direction_ = SLIDE_FRONT; 204 direction_ = BACK;
241 return CreateSlideLayer(1); 205 return CreateOverlayWindow();
242 } 206 }
243 207
244 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { 208 aura::Window* OverscrollNavigationOverlay::GetMainWindow() const {
245 if (slide_direction_ == SLIDE_UNKNOWN) 209 if (window_)
246 return; 210 return window_.get();
211 return web_contents_->GetContentNativeView();
212 }
247 213
248 // Perform the navigation. 214 void OverscrollNavigationOverlay::OnOverscrollCompleting() {
249 if (slide_direction_ == SLIDE_BACK) 215 GetMainWindow()->ReleaseCapture();
216 // We start the navigation as soon as we know the overscroll gesture is
217 // completing.
218 DCHECK(direction_ != NONE);
219
220 // Avoid clipping on the screenshot caused by the contents window being moved
221 // outside of the screen bounds.
222 contents_layer_settings_.reset(
223 new ScopedLayerClippingSetting(web_contents_->GetNativeView()->layer()));
224 contents_layer_parent_settings_.reset(new ScopedLayerClippingSetting(
225 web_contents_->GetNativeView()->layer()->parent()));
226
227 // Make sure we can navigate first, as other factors can trigger a navigation
228 // during an overscroll gesture and navigating without history produces a
229 // crash.
230 if (direction_ == FORWARD && web_contents_->GetController().CanGoForward())
231 web_contents_->GetController().GoForward();
232 if (direction_ == BACK && web_contents_->GetController().CanGoBack())
250 web_contents_->GetController().GoBack(); 233 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(); 234 StartObserving();
259 } 235 }
260 236
261 void OverscrollNavigationOverlay::OnWindowSlideCompleted( 237 void OverscrollNavigationOverlay::OnOverscrollCompleted(
262 scoped_ptr<ui::Layer> layer) { 238 scoped_ptr<aura::Window> window) {
263 if (slide_direction_ == SLIDE_UNKNOWN) { 239 GetMainWindow()->SetTransform(gfx::Transform());
264 window_slider_.reset(); 240 window_ = window.Pass();
265 StopObservingIfDone(); 241 // Make sure the overlay window is on top.
266 return; 242 web_contents_window_->StackChildAtTop(window_.get());
267 } 243 direction_ = NONE;
268
269 // Change the image used for the overlay window.
270 image_delegate_->SetImage(layer_delegate_->image());
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(); 244 StopObservingIfDone();
279 } 245 }
280 246
281 void OverscrollNavigationOverlay::OnWindowSlideAborted() { 247 void OverscrollNavigationOverlay::OnOverscrollCancelled() {
248 GetMainWindow()->ReleaseCapture();
249 direction_ = NONE;
282 StopObservingIfDone(); 250 StopObservingIfDone();
283 } 251 }
284 252
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() { 253 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() {
300 NavigationEntry* visible_entry = 254 NavigationEntry* visible_entry =
301 web_contents_->GetController().GetVisibleEntry(); 255 web_contents_->GetController().GetVisibleEntry();
302 if (pending_entry_url_.is_empty() || 256 if (pending_entry_url_.is_empty() ||
303 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { 257 DoesEntryMatchURL(visible_entry, pending_entry_url_)) {
304 received_paint_update_ = true; 258 received_paint_update_ = true;
305 StopObservingIfDone(); 259 StopObservingIfDone();
306 } 260 }
307 } 261 }
308 262
309 void OverscrollNavigationOverlay::DidStopLoading() { 263 void OverscrollNavigationOverlay::DidStopLoading() {
310 // Don't compare URLs in this case - it's possible they won't match if 264 // 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 265 // a gesture-nav initiated navigation was interrupted by some other in-site
312 // navigation ((e.g., from a script, or from a bookmark). 266 // navigation (e.g., from a script, or from a bookmark).
313 loading_complete_ = true; 267 loading_complete_ = true;
314 StopObservingIfDone(); 268 StopObservingIfDone();
315 } 269 }
316 270
317 } // namespace content 271 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698