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

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: Addressed Mikhail's comments Created 5 years, 9 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 "content/browser/frame_host/navigation_entry_impl.h" 7 #include "content/browser/frame_host/navigation_entry_impl.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h" 8 #include "content/browser/renderer_host/render_view_host_impl.h"
9 #include "content/browser/web_contents/aura/overscroll_layer_wrapper.h"
10 #include "content/browser/web_contents/aura/overscroll_window_delegate.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 11 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/view_messages.h" 12 #include "content/common/view_messages.h"
11 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_widget_host_view.h" 14 #include "content/public/browser/render_widget_host_view.h"
13 #include "ui/aura/window.h" 15 #include "ui/aura/window.h"
14 #include "ui/aura_extra/image_window_delegate.h" 16 #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)
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 124
124 private: 125 private:
125 ~OverlayDismissAnimator() override {} 126 ~OverlayDismissAnimator() override {}
126 127
127 scoped_ptr<ui::Layer> layer_; 128 scoped_ptr<ui::Layer> layer_;
128 129
129 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); 130 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator);
130 }; 131 };
131 132
132 OverscrollNavigationOverlay::OverscrollNavigationOverlay( 133 OverscrollNavigationOverlay::OverscrollNavigationOverlay(
133 WebContentsImpl* web_contents) 134 WebContentsImpl* web_contents,
134 : web_contents_(web_contents), 135 aura::Window* web_contents_window)
135 image_delegate_(NULL), 136 : direction_(NONE),
137 web_contents_(web_contents),
138 window_(nullptr),
139 image_delegate_(nullptr),
136 loading_complete_(false), 140 loading_complete_(false),
137 received_paint_update_(false), 141 received_paint_update_(false),
138 slide_direction_(SLIDE_UNKNOWN) { 142 owa_(new OverscrollWindowAnimation(this, nullptr)),
143 web_contents_window_(web_contents_window) {
139 } 144 }
140 145
141 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { 146 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() {
142 } 147 }
143 148
144 void OverscrollNavigationOverlay::StartObserving() { 149 void OverscrollNavigationOverlay::StartObserving() {
150 LOG(ERROR) << "Starting to observe";
145 loading_complete_ = false; 151 loading_complete_ = false;
146 received_paint_update_ = false; 152 received_paint_update_ = false;
mfomitchev 2015/03/10 19:25:59 overlay_dismiss_layer_.reset();
Nina 2015/03/12 22:21:28 Done.
147 overlay_dismiss_layer_.reset();
148 Observe(web_contents_); 153 Observe(web_contents_);
149 154
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. 155 // Assumes the navigation has been initiated.
155 NavigationEntry* pending_entry = 156 NavigationEntry* pending_entry =
156 web_contents_->GetController().GetPendingEntry(); 157 web_contents_->GetController().GetPendingEntry();
158
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 LOG(ERROR) << "URL: " << pending_entry_url_.GetContent();
162 164 }
163 void OverscrollNavigationOverlay::SetOverlayWindow( 165
164 scoped_ptr<aura::Window> window, 166 void OverscrollNavigationOverlay::StopObservingIfDone() {
165 aura_extra::ImageWindowDelegate* delegate) { 167 LOG(ERROR) << "ONO: Stop observing if done";
166 window_ = window.Pass(); 168 if (!window_ ||
167 if (window_.get() && window_->parent()) 169 !(loading_complete_ || received_paint_update_) ||
mfomitchev 2015/03/10 19:25:59 Can you please copy the comments for loading_compl
Nina 2015/03/12 22:21:28 Done.
168 window_->parent()->StackChildAtTop(window_.get()); 170 owa_->is_active()) {
169 image_delegate_ = delegate; 171 LOG(ERROR) << "Returning early";
170 172 return;
171 if (window_.get() && delegate->has_image()) { 173 }
172 window_slider_.reset(new WindowSlider(this, 174 FadeOutOverscrollWindow();
173 window_->parent(), 175 Observe(NULL);
174 window_.get())); 176 received_paint_update_ = false;
175 slide_direction_ = SLIDE_UNKNOWN; 177 loading_complete_ = false;
178 }
179
180 scoped_ptr<OverscrollLayerWrapper>
181 OverscrollNavigationOverlay::CreateLayerWrapper() {
182 LOG(ERROR) << "ONO: CreateLayerWrapper";
183 if (window_) {
184 LOG(ERROR) << "We have a window, returning slide layer";
185 return scoped_ptr<OverscrollLayerWrapper>(
186 new OverscrollLayerWrapper(CreateSlideLayer()));
187 }
188 scoped_ptr<aura::Window> window = CreateOverlayWindow();
189 if (direction_ == FORWARD) {
mfomitchev 2015/03/10 19:25:59 It's not very logical IMO that stacking is done in
Nina 2015/03/12 22:21:28 Done.
190 web_contents_window_->StackChildAbove(
191 window.get(), web_contents_->GetContentNativeView());
176 } else { 192 } else {
177 window_slider_.reset(); 193 web_contents_window_->StackChildBelow(
178 } 194 window.get(), web_contents_->GetContentNativeView());
179 } 195 }
180 196 return scoped_ptr<OverscrollLayerWrapper>(
181 void OverscrollNavigationOverlay::StopObservingIfDone() { 197 new OverscrollLayerWrapper(window.Pass()));
182 // Normally we dismiss the overlay once we receive a paint update, however 198 }
183 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get 199
184 // called, and we rely on loading_complete_ for those cases. 200 scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateOverlayWindow() {
185 if (!received_paint_update_ && !loading_complete_) 201 LOG(ERROR) << "ONO: Setting overlay window";
202 image_delegate_ =
203 new OverscrollWindowDelegate(owa_.get(),
204 GetImageForDirection(direction_));
205 scoped_ptr<aura::Window> window(new aura::Window(image_delegate_));
206 window->SetTransparent(true);
207 window->Init(aura::WINDOW_LAYER_TEXTURED);
208 window->layer()->SetMasksToBounds(false);
209 window->SetName("OverscrollOverlay");
210 web_contents_window_->AddChild(window.get());
211 window->Show();
212 owa_->set_overlay_window(window.get());
mfomitchev 2015/03/10 19:25:59 It would be better to do this in OnOverscrollCompl
Nina 2015/03/12 22:21:28 That function was deleted so now we handle it diff
213 return window.Pass();
214 }
215
216 scoped_ptr<ui::Layer> OverscrollNavigationOverlay::CreateSlideLayer() {
217 LOG(ERROR) << "ONO: Creating slide layer";
218 if (!layer_delegate_)
219 layer_delegate_.reset(new ImageLayerDelegate());
220 layer_delegate_->SetImage(GetImageForDirection(direction_));
221 scoped_ptr<ui::Layer> layer(new ui::Layer(ui::LAYER_TEXTURED));
222 layer->set_delegate(layer_delegate_.get());
223 ui::Layer* parent = window_->layer()->parent();
224 parent->Add(layer.get());
225 if (direction_ == FORWARD)
226 parent->StackAbove(layer.get(), window_->layer());
227 else
228 parent->StackBelow(layer.get(), window_->layer());
229 gfx::Rect bounds = gfx::Rect(layer->parent()->bounds().size());
230 layer->SetBounds(bounds);
231 return layer.Pass();
232 }
233
234 void OverscrollNavigationOverlay::FadeOutOverscrollWindow() {
235 LOG(ERROR) << "ONO: FadeOutOverscrollWindow";
236 if (!dismiss_layer_ && !window_)
186 return; 237 return;
mfomitchev 2015/03/10 19:25:59 I don't think you can get here if !window_
Nina 2015/03/12 22:21:28 Right!
187 238 if (!dismiss_layer_)
188 // If a slide is in progress, then do not destroy the window or the slide. 239 dismiss_layer_ = window_->AcquireLayer();
189 if (window_slider_.get() && window_slider_->IsSlideInProgress())
190 return;
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(); 240 window_.reset();
mfomitchev 2015/03/10 19:25:59 We should probably also reset the overlay window o
Nina 2015/03/12 22:21:28 Done.
201 image_delegate_ = NULL; 241 aura::Window* contents = web_contents_->GetContentNativeView();
202 if (overlay_dismiss_layer.get()) { 242 contents->layer()->SetLayerBrightness(1.f);
203 // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the 243 {
204 // animation completes. 244 ui::ScopedLayerAnimationSettings settings(contents->layer()->GetAnimator());
205 (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate(); 245 settings.SetPreemptionStrategy(
206 } 246 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
207 } 247 settings.SetTweenType(gfx::Tween::EASE_OUT);
208 248 contents->layer()->SetLayerBrightness(0.f);
mfomitchev 2015/03/10 19:25:59 Why are we fiddling with the brightness of web_con
Nina 2015/03/12 22:21:28 Got rid of this.
209 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) { 249 }
250 (new OverlayDismissAnimator(dismiss_layer_.Pass()))->Animate();
mfomitchev 2015/03/10 19:25:59 keep comment
Nina 2015/03/12 22:21:28 Done.
251 }
252
253 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
254 Direction direction) const {
210 const NavigationControllerImpl& controller = web_contents_->GetController(); 255 const NavigationControllerImpl& controller = web_contents_->GetController();
211 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); 256 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
212 257 controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1));
213 gfx::Image image; 258
259 LOG(ERROR) << "ONO: Image for entry - " << entry->GetTitle();
214 if (entry && entry->screenshot().get()) { 260 if (entry && entry->screenshot().get()) {
215 std::vector<gfx::ImagePNGRep> image_reps; 261 std::vector<gfx::ImagePNGRep> image_reps;
216 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); 262 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
217 image = gfx::Image(image_reps); 263 return gfx::Image(image_reps);
218 } 264 }
219 if (!layer_delegate_) 265 return gfx::Image();
220 layer_delegate_.reset(new ImageLayerDelegate()); 266 }
221 layer_delegate_->SetImage(image); 267
222 268 scoped_ptr<OverscrollLayerWrapper>
223 ui::Layer* layer = new ui::Layer(ui::LAYER_TEXTURED); 269 OverscrollNavigationOverlay::CreateFrontLayerWrapper() {
224 layer->set_delegate(layer_delegate_.get()); 270 LOG(ERROR) << "ONO: Create front layer";
225 return layer; 271 if (!web_contents_->GetController().CanGoForward())
226 } 272 return nullptr;
mfomitchev 2015/03/10 19:25:59 Not sure if the convention is to return a scoped_p
Nina 2015/03/12 22:21:28 I don't know what the convention usually is, but I
227 273 direction_ = FORWARD;
228 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { 274 return CreateLayerWrapper();
275 }
276
277 scoped_ptr<OverscrollLayerWrapper>
278 OverscrollNavigationOverlay::CreateBackLayerWrapper() {
279 LOG(ERROR) << "ONO: Create back layer";
229 if (!web_contents_->GetController().CanGoBack()) 280 if (!web_contents_->GetController().CanGoBack())
230 return NULL; 281 return nullptr;
231 slide_direction_ = SLIDE_BACK; 282 direction_ = BACK;
232 return CreateSlideLayer(-1); 283 return CreateLayerWrapper();
233 } 284 }
234 285
235 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { 286 void OverscrollNavigationOverlay::OnOverscrollCompleting() {
236 if (!web_contents_->GetController().CanGoForward()) 287 // We start the navigation as soon as we know the overscroll gesture is
237 return NULL; 288 // completing.
238 slide_direction_ = SLIDE_FRONT; 289 LOG(ERROR) << "ONO: Starting navigation";
239 return CreateSlideLayer(1); 290 if (direction_ == FORWARD)
240 }
241
242 void OverscrollNavigationOverlay::OnWindowSlideCompleting() {
243 if (slide_direction_ == SLIDE_UNKNOWN)
244 return;
245
246 // Perform the navigation.
247 if (slide_direction_ == SLIDE_BACK)
248 web_contents_->GetController().GoBack();
249 else if (slide_direction_ == SLIDE_FRONT)
250 web_contents_->GetController().GoForward(); 291 web_contents_->GetController().GoForward();
251 else 292 else
252 NOTREACHED(); 293 web_contents_->GetController().GoBack();
mfomitchev 2015/03/10 19:25:59 NOTREACHED() for direction == NONE
Nina 2015/03/12 22:21:28 Done.
253
254 // Reset state and wait for the new navigation page to complete
255 // loading/painting.
256 StartObserving(); 294 StartObserving();
257 } 295 }
258 296
259 void OverscrollNavigationOverlay::OnWindowSlideCompleted( 297 void OverscrollNavigationOverlay::OnOverscrollCompleted(
260 scoped_ptr<ui::Layer> layer) { 298 scoped_ptr<OverscrollLayerWrapper> layer_wrapper) {
261 if (slide_direction_ == SLIDE_UNKNOWN) { 299 LOG(ERROR) << "ONO: OnOverscrollCompleted";
262 window_slider_.reset(); 300 if (layer_wrapper->has_window()) {
mfomitchev 2015/03/10 19:26:00 I think checking if (!window_) would be better to
Nina 2015/03/12 22:21:29 Done, but I removed has_window()
263 StopObservingIfDone(); 301 LOG(ERROR) << "The layer wrapper has a window";
264 return; 302 window_ = layer_wrapper->AcquireWindow();
265 } 303 // Make sure the overlay window is on top.
266 304 web_contents_window_->StackChildAtTop(window_.get());
267 // Change the image used for the overlay window. 305 // Reset the position of the contents window.
268 image_delegate_->SetImage(layer_delegate_->image()); 306 web_contents_->GetContentNativeView()->SetTransform(gfx::Transform());
269 window_->layer()->SetTransform(gfx::Transform()); 307 } else {
270 window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size())); 308 LOG(ERROR) << "The layer wrapper has a layer";
271 slide_direction_ = SLIDE_UNKNOWN; 309 dismiss_layer_ = layer_wrapper->AcquireLayer();
mfomitchev 2015/03/10 19:25:59 Move this to after schedulepaint and add back the
Nina 2015/03/12 22:21:28 Done.
272 // We may end up dismissing the overlay before it has a chance to repaint, so 310 // If this overscroll was started too quickly, there is a chance we've
mfomitchev 2015/03/10 19:25:59 Huh? We shouldn't be dismissing the window while O
Nina 2015/03/12 22:21:29 Removed.
273 // set the slider layer to be the one animated by OverlayDismissAnimator. 311 // already dismissed the window_;
274 if (layer.get()) 312 if (window_) {
275 overlay_dismiss_layer_ = layer.Pass(); 313 // Change the image used for the overlay window.
314 image_delegate_->SetImage(layer_delegate_->image());
315 // Reset the position of its layer.
316 window_->layer()->SetTransform(gfx::Transform());
317 window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size()));
318 }
319 }
276 StopObservingIfDone(); 320 StopObservingIfDone();
mfomitchev 2015/03/10 19:25:59 We should be resetting direction_ to NONE when the
Nina 2015/03/12 22:21:28 Done.
277 } 321 }
278 322
279 void OverscrollNavigationOverlay::OnWindowSlideAborted() { 323 void OverscrollNavigationOverlay::OnOverscrollAborted() {
280 StopObservingIfDone(); 324 StopObservingIfDone();
281 } 325 }
282 326
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() { 327 void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() {
328 LOG(ERROR) << "Did first visually non empty paint";
298 NavigationEntry* visible_entry = 329 NavigationEntry* visible_entry =
299 web_contents_->GetController().GetVisibleEntry(); 330 web_contents_->GetController().GetVisibleEntry();
300 if (pending_entry_url_.is_empty() || 331 if (pending_entry_url_.is_empty() ||
301 DoesEntryMatchURL(visible_entry, pending_entry_url_)) { 332 DoesEntryMatchURL(visible_entry, pending_entry_url_)) {
302 received_paint_update_ = true; 333 received_paint_update_ = true;
303 StopObservingIfDone(); 334 StopObservingIfDone();
304 } 335 }
305 } 336 }
306 337
307 void OverscrollNavigationOverlay::DidStopLoading(RenderViewHost* host) { 338 void OverscrollNavigationOverlay::DidStopLoading(RenderViewHost* host) {
339 LOG(ERROR) << "Did stop loading";
308 // Don't compare URLs in this case - it's possible they won't match if 340 // 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 341 // a gesture-nav initiated navigation was interrupted by some other in-site
310 // navigation ((e.g., from a script, or from a bookmark). 342 // navigation ((e.g., from a script, or from a bookmark).
311 loading_complete_ = true; 343 loading_complete_ = true;
312 StopObservingIfDone(); 344 StopObservingIfDone();
313 } 345 }
314 346
315 } // namespace content 347 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698