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

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

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

Powered by Google App Engine
This is Rietveld 408576698