OLD | NEW |
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 Loading... |
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 if (owa_->is_active()) |
| 119 GetMainWindow()->ReleaseCapture(); |
144 } | 120 } |
145 | 121 |
146 void OverscrollNavigationOverlay::StartObserving() { | 122 void OverscrollNavigationOverlay::StartObserving() { |
147 loading_complete_ = false; | 123 loading_complete_ = false; |
148 received_paint_update_ = false; | 124 received_paint_update_ = false; |
149 overlay_dismiss_layer_.reset(); | |
150 Observe(web_contents_); | 125 Observe(web_contents_); |
151 | 126 |
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. | 127 // Assumes the navigation has been initiated. |
157 NavigationEntry* pending_entry = | 128 NavigationEntry* pending_entry = |
158 web_contents_->GetController().GetPendingEntry(); | 129 web_contents_->GetController().GetPendingEntry(); |
159 // Save url of the pending entry to identify when it loads and paints later. | 130 // 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 - | 131 // Under some circumstances navigation can leave a null pending entry - |
161 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). | 132 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). |
162 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); | 133 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); |
163 } | 134 } |
164 | 135 |
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() { | 136 void OverscrollNavigationOverlay::StopObservingIfDone() { |
184 // Normally we dismiss the overlay once we receive a paint update, however | 137 // Normally we dismiss the overlay once we receive a paint update, however |
185 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get | 138 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get |
186 // called, and we rely on loading_complete_ for those cases. | 139 // called, and we rely on loading_complete_ for those cases. |
187 if (!received_paint_update_ && !loading_complete_) | 140 // If an overscroll gesture is in progress, then do not destroy the window. |
| 141 if (!window_ || !(loading_complete_ || received_paint_update_) || |
| 142 owa_->is_active()) { |
188 return; | 143 return; |
| 144 } |
| 145 // Restore layer clipping. |
| 146 contents_layer_settings_.reset(); |
| 147 contents_layer_parent_settings_.reset(); |
189 | 148 |
190 // If a slide is in progress, then do not destroy the window or the slide. | 149 // OverlayDismissAnimator deletes the dismiss layer and itself when the |
191 if (window_slider_.get() && window_slider_->IsSlideInProgress()) | 150 // animation completes. |
192 return; | 151 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(); | 152 window_.reset(); |
203 image_delegate_ = NULL; | 153 (new OverlayDismissAnimator(dismiss_layer.Pass()))->Animate(); |
204 if (overlay_dismiss_layer.get()) { | 154 Observe(nullptr); |
205 // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the | 155 received_paint_update_ = false; |
206 // animation completes. | 156 loading_complete_ = false; |
207 (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate(); | |
208 } | |
209 } | 157 } |
210 | 158 |
211 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) { | 159 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateOverlayWindow( |
| 160 const gfx::Rect& bounds) { |
| 161 OverscrollWindowDelegate* overscroll_delegate = new OverscrollWindowDelegate( |
| 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 |
| 183 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection( |
| 184 NavigationDirection direction) const { |
212 const NavigationControllerImpl& controller = web_contents_->GetController(); | 185 const NavigationControllerImpl& controller = web_contents_->GetController(); |
213 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); | 186 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
| 187 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1)); |
214 | 188 |
215 gfx::Image image; | |
216 if (entry && entry->screenshot().get()) { | 189 if (entry && entry->screenshot().get()) { |
217 std::vector<gfx::ImagePNGRep> image_reps; | 190 std::vector<gfx::ImagePNGRep> image_reps; |
218 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); | 191 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); |
219 image = gfx::Image(image_reps); | 192 return gfx::Image(image_reps); |
220 } | 193 } |
221 if (!layer_delegate_) | 194 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 } | 195 } |
229 | 196 |
230 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { | 197 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateFrontWindow( |
231 if (!web_contents_->GetController().CanGoBack()) | 198 const gfx::Rect& bounds) { |
232 return NULL; | 199 if (!web_contents_->GetController().CanGoForward()) |
233 slide_direction_ = SLIDE_BACK; | 200 return nullptr; |
234 return CreateSlideLayer(-1); | 201 direction_ = FORWARD; |
| 202 return CreateOverlayWindow(bounds); |
235 } | 203 } |
236 | 204 |
237 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { | 205 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateBackWindow( |
238 if (!web_contents_->GetController().CanGoForward()) | 206 const gfx::Rect& bounds) { |
239 return NULL; | 207 if (!web_contents_->GetController().CanGoBack()) |
240 slide_direction_ = SLIDE_FRONT; | 208 return nullptr; |
241 return CreateSlideLayer(1); | 209 direction_ = BACK; |
| 210 return CreateOverlayWindow(bounds); |
242 } | 211 } |
243 | 212 |
244 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { | 213 aura::Window* OverscrollNavigationOverlay::GetMainWindow() const { |
245 if (slide_direction_ == SLIDE_UNKNOWN) | 214 if (window_) |
246 return; | 215 return window_.get(); |
| 216 return web_contents_->GetContentNativeView(); |
| 217 } |
247 | 218 |
248 // Perform the navigation. | 219 void OverscrollNavigationOverlay::OnOverscrollCompleting() { |
249 if (slide_direction_ == SLIDE_BACK) | 220 GetMainWindow()->ReleaseCapture(); |
| 221 // We start the navigation as soon as we know the overscroll gesture is |
| 222 // completing. |
| 223 DCHECK(direction_ != NONE); |
| 224 |
| 225 // Avoid clipping on the screenshot caused by the contents window being moved |
| 226 // outside of the screen bounds. |
| 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()) |
250 web_contents_->GetController().GoBack(); | 238 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(); | 239 StartObserving(); |
259 } | 240 } |
260 | 241 |
261 void OverscrollNavigationOverlay::OnWindowSlideCompleted( | 242 void OverscrollNavigationOverlay::OnOverscrollCompleted( |
262 scoped_ptr<ui::Layer> layer) { | 243 scoped_ptr<aura::Window> window) { |
263 if (slide_direction_ == SLIDE_UNKNOWN) { | 244 GetMainWindow()->SetTransform(gfx::Transform()); |
264 window_slider_.reset(); | 245 window_ = window.Pass(); |
265 StopObservingIfDone(); | 246 // Make sure the window is in its default position. |
266 return; | 247 window_->SetBounds(gfx::Rect(web_contents_window_->bounds().size())); |
267 } | 248 window_->SetTransform(gfx::Transform()); |
268 | 249 // Make sure the overlay window is on top. |
269 // Change the image used for the overlay window. | 250 web_contents_window_->StackChildAtTop(window_.get()); |
270 image_delegate_->SetImage(layer_delegate_->image()); | 251 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(); | 252 StopObservingIfDone(); |
279 } | 253 } |
280 | 254 |
281 void OverscrollNavigationOverlay::OnWindowSlideAborted() { | 255 void OverscrollNavigationOverlay::OnOverscrollCancelled() { |
| 256 GetMainWindow()->ReleaseCapture(); |
| 257 direction_ = NONE; |
282 StopObservingIfDone(); | 258 StopObservingIfDone(); |
283 } | 259 } |
284 | 260 |
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() { | 261 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() { |
300 NavigationEntry* visible_entry = | 262 NavigationEntry* visible_entry = |
301 web_contents_->GetController().GetVisibleEntry(); | 263 web_contents_->GetController().GetVisibleEntry(); |
302 if (pending_entry_url_.is_empty() || | 264 if (pending_entry_url_.is_empty() || |
303 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { | 265 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { |
304 received_paint_update_ = true; | 266 received_paint_update_ = true; |
305 StopObservingIfDone(); | 267 StopObservingIfDone(); |
306 } | 268 } |
307 } | 269 } |
308 | 270 |
309 void OverscrollNavigationOverlay::DidStopLoading() { | 271 void OverscrollNavigationOverlay::DidStopLoading() { |
310 // Don't compare URLs in this case - it's possible they won't match if | 272 // 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 | 273 // a gesture-nav initiated navigation was interrupted by some other in-site |
312 // navigation ((e.g., from a script, or from a bookmark). | 274 // navigation (e.g., from a script, or from a bookmark). |
313 loading_complete_ = true; | 275 loading_complete_ = true; |
314 StopObservingIfDone(); | 276 StopObservingIfDone(); |
315 } | 277 } |
316 | 278 |
317 } // namespace content | 279 } // namespace content |
OLD | NEW |