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

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

Powered by Google App Engine
This is Rietveld 408576698