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

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: Removed comment 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->SetTransparent(true);
166 window->Init(ui::LAYER_TEXTURED);
167 window->layer()->SetMasksToBounds(false);
168 window->SetName("OverscrollOverlay");
169 web_contents_window_->AddChild(window.get());
170 aura::Window* event_window = GetMainWindow();
171 if (direction_ == FORWARD)
172 web_contents_window_->StackChildAbove(window.get(), event_window);
173 else
174 web_contents_window_->StackChildBelow(window.get(), event_window);
175 window->SetBounds(bounds);
176 // Set capture on the window that is receiving the overscroll events so that
177 // trackpad scroll gestures keep targetting it even if the mouse pointer moves
178 // off its bounds.
179 event_window->SetCapture();
180 window->Show();
sadrul 2015/04/09 19:41:24 By default, aura Windows are owned by their parent
Nina 2015/04/09 20:18:54 Done.
181 return window.Pass();
182 }
183
184 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
185 NavigationDirection direction) const {
212 const NavigationControllerImpl& controller = web_contents_->GetController(); 186 const NavigationControllerImpl& controller = web_contents_->GetController();
213 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); 187 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
188 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1));
214 189
215 gfx::Image image;
216 if (entry && entry->screenshot().get()) { 190 if (entry && entry->screenshot().get()) {
217 std::vector<gfx::ImagePNGRep> image_reps; 191 std::vector<gfx::ImagePNGRep> image_reps;
218 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); 192 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
219 image = gfx::Image(image_reps); 193 return gfx::Image(image_reps);
220 } 194 }
221 if (!layer_delegate_) 195 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 } 196 }
229 197
230 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { 198 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateFrontWindow(
231 if (!web_contents_->GetController().CanGoBack()) 199 const gfx::Rect& bounds) {
232 return NULL; 200 if (!web_contents_->GetController().CanGoForward())
233 slide_direction_ = SLIDE_BACK; 201 return nullptr;
234 return CreateSlideLayer(-1); 202 direction_ = FORWARD;
203 return CreateOverlayWindow(bounds);
235 } 204 }
236 205
237 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { 206 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateBackWindow(
238 if (!web_contents_->GetController().CanGoForward()) 207 const gfx::Rect& bounds) {
239 return NULL; 208 if (!web_contents_->GetController().CanGoBack())
240 slide_direction_ = SLIDE_FRONT; 209 return nullptr;
241 return CreateSlideLayer(1); 210 direction_ = BACK;
211 return CreateOverlayWindow(bounds);
242 } 212 }
243 213
244 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { 214 aura::Window* OverscrollNavigationOverlay::GetMainWindow() const {
245 if (slide_direction_ == SLIDE_UNKNOWN) 215 if (window_)
246 return; 216 return window_.get();
217 return web_contents_->IsBeingDestroyed()
218 ? nullptr
219 : web_contents_->GetContentNativeView();
220 }
247 221
248 // Perform the navigation. 222 void OverscrollNavigationOverlay::OnOverscrollCompleting() {
249 if (slide_direction_ == SLIDE_BACK) 223 GetMainWindow()->ReleaseCapture();
224 // We start the navigation as soon as we know the overscroll gesture is
225 // completing.
226 DCHECK(direction_ != NONE);
227
228 // Avoid clipping on the screenshot caused by the contents window being moved
229 // outside of the screen bounds.
230 contents_layer_settings_.reset(
231 new ScopedLayerClippingSetting(web_contents_->GetNativeView()->layer()));
232 contents_layer_parent_settings_.reset(new ScopedLayerClippingSetting(
233 web_contents_->GetNativeView()->layer()->parent()));
234
235 // Make sure we can navigate first, as other factors can trigger a navigation
236 // during an overscroll gesture and navigating without history produces a
237 // crash.
238 if (direction_ == FORWARD && web_contents_->GetController().CanGoForward())
239 web_contents_->GetController().GoForward();
240 if (direction_ == BACK && web_contents_->GetController().CanGoBack())
250 web_contents_->GetController().GoBack(); 241 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(); 242 StartObserving();
259 } 243 }
260 244
261 void OverscrollNavigationOverlay::OnWindowSlideCompleted( 245 void OverscrollNavigationOverlay::OnOverscrollCompleted(
262 scoped_ptr<ui::Layer> layer) { 246 scoped_ptr<aura::Window> window) {
263 if (slide_direction_ == SLIDE_UNKNOWN) { 247 GetMainWindow()->SetTransform(gfx::Transform());
264 window_slider_.reset(); 248 window_ = window.Pass();
265 StopObservingIfDone(); 249 // Make sure the window is in its default position.
266 return; 250 window_->SetBounds(gfx::Rect(web_contents_window_->bounds().size()));
267 } 251 window_->SetTransform(gfx::Transform());
268 252 // Make sure the overlay window is on top.
269 // Change the image used for the overlay window. 253 web_contents_window_->StackChildAtTop(window_.get());
270 image_delegate_->SetImage(layer_delegate_->image()); 254 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(); 255 StopObservingIfDone();
279 } 256 }
280 257
281 void OverscrollNavigationOverlay::OnWindowSlideAborted() { 258 void OverscrollNavigationOverlay::OnOverscrollCancelled() {
259 GetMainWindow()->ReleaseCapture();
260 direction_ = NONE;
282 StopObservingIfDone(); 261 StopObservingIfDone();
283 } 262 }
284 263
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() { 264 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() {
300 NavigationEntry* visible_entry = 265 NavigationEntry* visible_entry =
301 web_contents_->GetController().GetVisibleEntry(); 266 web_contents_->GetController().GetVisibleEntry();
302 if (pending_entry_url_.is_empty() || 267 if (pending_entry_url_.is_empty() ||
303 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { 268 DoesEntryMatchURL(visible_entry, pending_entry_url_)) {
304 received_paint_update_ = true; 269 received_paint_update_ = true;
305 StopObservingIfDone(); 270 StopObservingIfDone();
306 } 271 }
307 } 272 }
308 273
309 void OverscrollNavigationOverlay::DidStopLoading() { 274 void OverscrollNavigationOverlay::DidStopLoading() {
310 // Don't compare URLs in this case - it's possible they won't match if 275 // 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 276 // a gesture-nav initiated navigation was interrupted by some other in-site
312 // navigation ((e.g., from a script, or from a bookmark). 277 // navigation (e.g., from a script, or from a bookmark).
313 loading_complete_ = true; 278 loading_complete_ = true;
314 StopObservingIfDone(); 279 StopObservingIfDone();
315 } 280 }
316 281
317 } // namespace content 282 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698