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 | |
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" | |
12 #include "content/browser/web_contents/aura/slidable_wrapper.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" | 18 #include "ui/aura_extra/image_window_delegate.h" |
15 #include "ui/base/layout.h" | 19 #include "ui/base/layout.h" |
16 #include "ui/compositor/layer.h" | 20 #include "ui/compositor/layer.h" |
17 #include "ui/compositor/layer_animation_observer.h" | 21 #include "ui/compositor/layer_animation_observer.h" |
18 #include "ui/compositor/scoped_layer_animation_settings.h" | 22 #include "ui/compositor/scoped_layer_animation_settings.h" |
19 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/image/image_png_rep.h" | 24 #include "ui/gfx/image/image_png_rep.h" |
21 #include "ui/gfx/image/image_skia.h" | |
22 | 25 |
23 namespace content { | 26 namespace content { |
24 namespace { | 27 namespace { |
25 | 28 |
26 // 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 |
27 // match |url|. | 30 // match |url|. |
28 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) { | 31 bool DoesEntryMatchURL(NavigationEntry* entry, const GURL& url) { |
29 if (!entry) | 32 if (!entry) |
30 return false; | 33 return false; |
31 if (entry->GetURL() == url) | 34 if (entry->GetURL() == url) |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 | 126 |
124 private: | 127 private: |
125 ~OverlayDismissAnimator() override {} | 128 ~OverlayDismissAnimator() override {} |
126 | 129 |
127 scoped_ptr<ui::Layer> layer_; | 130 scoped_ptr<ui::Layer> layer_; |
128 | 131 |
129 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); | 132 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); |
130 }; | 133 }; |
131 | 134 |
132 OverscrollNavigationOverlay::OverscrollNavigationOverlay( | 135 OverscrollNavigationOverlay::OverscrollNavigationOverlay( |
133 WebContentsImpl* web_contents) | 136 WebContentsImpl* web_contents, |
134 : web_contents_(web_contents), | 137 aura::Window* web_contents_window) |
135 image_delegate_(NULL), | 138 : direction_(NONE), |
139 web_contents_(web_contents), | |
140 window_(nullptr), | |
141 image_delegate_(nullptr), | |
136 loading_complete_(false), | 142 loading_complete_(false), |
137 received_paint_update_(false), | 143 received_paint_update_(false), |
138 slide_direction_(SLIDE_UNKNOWN) { | 144 owa_(new OverscrollWindowAnimation(this)), |
145 web_contents_window_(web_contents_window) { | |
139 } | 146 } |
140 | 147 |
141 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { | 148 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { |
142 } | 149 } |
143 | 150 |
144 void OverscrollNavigationOverlay::StartObserving() { | 151 void OverscrollNavigationOverlay::StartObserving() { |
145 loading_complete_ = false; | 152 loading_complete_ = false; |
146 received_paint_update_ = false; | 153 received_paint_update_ = false; |
147 overlay_dismiss_layer_.reset(); | |
148 Observe(web_contents_); | 154 Observe(web_contents_); |
149 | 155 |
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. | 156 // Assumes the navigation has been initiated. |
155 NavigationEntry* pending_entry = | 157 NavigationEntry* pending_entry = |
156 web_contents_->GetController().GetPendingEntry(); | 158 web_contents_->GetController().GetPendingEntry(); |
157 // 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. |
158 // Under some circumstances navigation can leave a null pending entry - | 160 // Under some circumstances navigation can leave a null pending entry - |
159 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). | 161 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). |
160 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); | 162 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); |
161 } | 163 } |
162 | 164 |
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() { | 165 void OverscrollNavigationOverlay::StopObservingIfDone() { |
182 // Normally we dismiss the overlay once we receive a paint update, however | 166 // Normally we dismiss the overlay once we receive a paint update, however |
183 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get | 167 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get |
184 // called, and we rely on loading_complete_ for those cases. | 168 // called, and we rely on loading_complete_ for those cases. |
185 if (!received_paint_update_ && !loading_complete_) | 169 // If a slide is in progress, then do not destroy the window or the slide. |
170 if (!window_ || | |
171 !(loading_complete_ || received_paint_update_) || | |
172 owa_->is_active()) { | |
186 return; | 173 return; |
187 | 174 } |
188 // If a slide is in progress, then do not destroy the window or the slide. | 175 // OverlayDismissAnimator deletes the window layer and itself when the |
189 if (window_slider_.get() && window_slider_->IsSlideInProgress()) | 176 // animation completes. |
190 return; | 177 (new OverlayDismissAnimator(window_->AcquireLayer().Pass()))->Animate(); |
191 | 178 window_.reset(); |
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); | 179 Observe(NULL); |
199 window_slider_.reset(); | 180 received_paint_update_ = false; |
200 window_.reset(); | 181 loading_complete_ = false; |
201 image_delegate_ = NULL; | |
202 if (overlay_dismiss_layer.get()) { | |
203 // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the | |
204 // animation completes. | |
205 (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate(); | |
206 } | |
207 } | 182 } |
208 | 183 |
209 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) { | 184 scoped_ptr<SlidableWrapper> OverscrollNavigationOverlay::CreateLayerWrapper() { |
185 if (window_) { | |
mfomitchev
2015/03/16 22:28:01
Add some comments for the two cases
Nina
2015/03/17 14:27:07
Done.
| |
186 if (!layer_delegate_) | |
187 layer_delegate_.reset(new ImageLayerDelegate()); | |
188 layer_delegate_->SetImage(GetImageForDirection(direction_)); | |
189 scoped_ptr<ui::Layer> layer(new ui::Layer(ui::LAYER_TEXTURED)); | |
190 layer->set_delegate(layer_delegate_.get()); | |
191 ui::Layer* parent = window_->layer()->parent(); | |
192 parent->Add(layer.get()); | |
193 if (direction_ == FORWARD) | |
194 parent->StackAbove(layer.get(), window_->layer()); | |
195 else | |
196 parent->StackBelow(layer.get(), window_->layer()); | |
197 return scoped_ptr<SlidableWrapper>( | |
198 new SlidableWrapper(layer.Pass())); | |
199 } | |
200 image_delegate_ = | |
201 new OverscrollWindowDelegate(owa_.get(), | |
202 GetImageForDirection(direction_)); | |
203 scoped_ptr<aura::Window> window(new aura::Window(image_delegate_)); | |
204 window->SetTransparent(true); | |
205 window->Init(aura::WINDOW_LAYER_TEXTURED); | |
206 window->layer()->SetMasksToBounds(false); | |
207 window->SetName("OverscrollOverlay"); | |
208 web_contents_window_->AddChild(window.get()); | |
209 window->Show(); | |
210 if (direction_ == FORWARD) { | |
211 web_contents_window_->StackChildAbove( | |
212 window.get(), web_contents_->GetContentNativeView()); | |
213 } else { | |
214 web_contents_window_->StackChildBelow( | |
215 window.get(), web_contents_->GetContentNativeView()); | |
216 } | |
217 return scoped_ptr<SlidableWrapper>( | |
mfomitchev
2015/03/16 22:28:01
So we don't need ONO to own the overlay window (wi
Nina
2015/03/17 14:27:07
Right, we don't need that anymore with this design
| |
218 new SlidableWrapper(window.Pass())); | |
219 } | |
220 | |
221 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection( | |
222 NavigationDirection direction) const { | |
210 const NavigationControllerImpl& controller = web_contents_->GetController(); | 223 const NavigationControllerImpl& controller = web_contents_->GetController(); |
211 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); | 224 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry( |
225 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1)); | |
212 | 226 |
213 gfx::Image image; | |
214 if (entry && entry->screenshot().get()) { | 227 if (entry && entry->screenshot().get()) { |
215 std::vector<gfx::ImagePNGRep> image_reps; | 228 std::vector<gfx::ImagePNGRep> image_reps; |
216 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); | 229 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); |
217 image = gfx::Image(image_reps); | 230 return gfx::Image(image_reps); |
218 } | 231 } |
219 if (!layer_delegate_) | 232 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 } | 233 } |
227 | 234 |
228 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { | 235 scoped_ptr<SlidableWrapper> OverscrollNavigationOverlay::CreateFrontWrapper() { |
229 if (!web_contents_->GetController().CanGoBack()) | 236 if (!web_contents_->GetController().CanGoForward()) |
230 return NULL; | 237 return nullptr; |
231 slide_direction_ = SLIDE_BACK; | 238 direction_ = FORWARD; |
232 return CreateSlideLayer(-1); | 239 return CreateLayerWrapper(); |
233 } | 240 } |
234 | 241 |
235 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { | 242 scoped_ptr<SlidableWrapper> OverscrollNavigationOverlay::CreateBackWrapper() { |
236 if (!web_contents_->GetController().CanGoForward()) | 243 if (!web_contents_->GetController().CanGoBack()) |
237 return NULL; | 244 return nullptr; |
238 slide_direction_ = SLIDE_FRONT; | 245 direction_ = BACK; |
239 return CreateSlideLayer(1); | 246 return CreateLayerWrapper(); |
240 } | 247 } |
241 | 248 |
242 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { | 249 aura::Window* OverscrollNavigationOverlay::GetLiveWindow() const { |
243 if (slide_direction_ == SLIDE_UNKNOWN) | 250 if (window_) |
244 return; | 251 return window_.get(); |
252 return web_contents_->GetContentNativeView(); | |
253 } | |
245 | 254 |
246 // Perform the navigation. | 255 void OverscrollNavigationOverlay::OnOverscrollCompleting() { |
247 if (slide_direction_ == SLIDE_BACK) | 256 // We start the navigation as soon as we know the overscroll gesture is |
257 // completing. | |
258 DCHECK(direction_ != NONE); | |
259 LOG(ERROR) << "OnOverscrollCompleting, navigating and observing"; | |
260 if (direction_ == FORWARD && web_contents_->GetController().CanGoForward()) | |
mfomitchev
2015/03/16 22:28:01
What happens if we do GoForward()/Back without che
Nina
2015/03/17 14:27:07
BOOM. There's a DCHECK in place to prevent that fr
| |
261 web_contents_->GetController().GoForward(); | |
262 if (direction_ == BACK && web_contents_->GetController().CanGoBack()) | |
248 web_contents_->GetController().GoBack(); | 263 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(); | 264 StartObserving(); |
257 } | 265 } |
258 | 266 |
259 void OverscrollNavigationOverlay::OnWindowSlideCompleted( | 267 void OverscrollNavigationOverlay::OnOverscrollCompleted( |
260 scoped_ptr<ui::Layer> layer) { | 268 scoped_ptr<SlidableWrapper> wrapper) { |
261 if (slide_direction_ == SLIDE_UNKNOWN) { | 269 if (!window_) { |
262 window_slider_.reset(); | 270 window_ = wrapper->AcquireWindow(); |
mfomitchev
2015/03/16 22:28:01
DCHECK wrapper->has_window()
Nina
2015/03/17 14:27:07
Done.
| |
263 StopObservingIfDone(); | 271 // Make sure the overlay window is on top. |
264 return; | 272 web_contents_window_->StackChildAtTop(window_.get()); |
273 // Reset the position of the contents window. | |
274 web_contents_->GetContentNativeView()->SetTransform(gfx::Transform()); | |
275 } else { | |
276 // Change the image used for the overlay window. | |
277 image_delegate_->SetImage(layer_delegate_->image()); | |
278 // Reset the position of its layer. | |
279 window_->layer()->SetTransform(gfx::Transform()); | |
280 window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size())); | |
265 } | 281 } |
266 | 282 direction_ = NONE; |
267 // Change the image used for the overlay window. | 283 LOG(ERROR) << "OnOverscrollCompleted, stop observing if done"; |
268 image_delegate_->SetImage(layer_delegate_->image()); | |
269 window_->layer()->SetTransform(gfx::Transform()); | |
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(); | 284 StopObservingIfDone(); |
277 } | 285 } |
278 | 286 |
279 void OverscrollNavigationOverlay::OnWindowSlideAborted() { | 287 void OverscrollNavigationOverlay::OnOverscrollAborted() { |
288 direction_ = NONE; | |
280 StopObservingIfDone(); | 289 StopObservingIfDone(); |
281 } | 290 } |
282 | 291 |
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() { | 292 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() { |
298 NavigationEntry* visible_entry = | 293 NavigationEntry* visible_entry = |
299 web_contents_->GetController().GetVisibleEntry(); | 294 web_contents_->GetController().GetVisibleEntry(); |
300 if (pending_entry_url_.is_empty() || | 295 if (pending_entry_url_.is_empty() || |
301 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { | 296 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { |
302 received_paint_update_ = true; | 297 received_paint_update_ = true; |
303 StopObservingIfDone(); | 298 StopObservingIfDone(); |
304 } | 299 } |
305 } | 300 } |
306 | 301 |
307 void OverscrollNavigationOverlay::DidStopLoading(RenderViewHost* host) { | 302 void OverscrollNavigationOverlay::DidStopLoading(RenderViewHost* host) { |
308 // Don't compare URLs in this case - it's possible they won't match if | 303 // 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 | 304 // a gesture-nav initiated navigation was interrupted by some other in-site |
310 // navigation ((e.g., from a script, or from a bookmark). | 305 // navigation ((e.g., from a script, or from a bookmark). |
311 loading_complete_ = true; | 306 loading_complete_ = true; |
312 StopObservingIfDone(); | 307 StopObservingIfDone(); |
313 } | 308 } |
314 | 309 |
315 } // namespace content | 310 } // namespace content |
OLD | NEW |