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

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: New design with window|wrapper in OWA 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)
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 // A LayerDelegate that paints an image for the layer.
47 // TODO move to overscroll_window.h???
46 class ImageLayerDelegate : public ui::LayerDelegate { 48 class ImageLayerDelegate : public ui::LayerDelegate {
47 public: 49 public:
48 ImageLayerDelegate() {} 50 ImageLayerDelegate() {}
49 51
50 ~ImageLayerDelegate() override {} 52 ~ImageLayerDelegate() override {}
51 53
52 void SetImage(const gfx::Image& image) { 54 void SetImage(const gfx::Image& image) {
53 image_ = image; 55 image_ = image;
54 image_size_ = image.AsImageSkia().size(); 56 image_size_ = image.AsImageSkia().size();
55 } 57 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 125
124 private: 126 private:
125 ~OverlayDismissAnimator() override {} 127 ~OverlayDismissAnimator() override {}
126 128
127 scoped_ptr<ui::Layer> layer_; 129 scoped_ptr<ui::Layer> layer_;
128 130
129 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator); 131 DISALLOW_COPY_AND_ASSIGN(OverlayDismissAnimator);
130 }; 132 };
131 133
132 OverscrollNavigationOverlay::OverscrollNavigationOverlay( 134 OverscrollNavigationOverlay::OverscrollNavigationOverlay(
133 WebContentsImpl* web_contents) 135 WebContentsImpl* web_contents,
134 : web_contents_(web_contents), 136 OverscrollWindowAnimation* owa,
135 image_delegate_(NULL), 137 aura::Window* web_contents_window)
138 : direction_(OverscrollWindowAnimation::NONE),
139 web_contents_(web_contents),
140 window_(nullptr),
136 loading_complete_(false), 141 loading_complete_(false),
137 received_paint_update_(false), 142 observing_(false),
138 slide_direction_(SLIDE_UNKNOWN) { 143 owa_(owa),
144 web_contents_window_(web_contents_window) {
139 } 145 }
140 146
141 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() { 147 OverscrollNavigationOverlay::~OverscrollNavigationOverlay() {
142 } 148 }
143 149
144 void OverscrollNavigationOverlay::StartObserving() { 150 void OverscrollNavigationOverlay::StartObserving() {
151 LOG(ERROR) << "Starting to observe";
145 loading_complete_ = false; 152 loading_complete_ = false;
146 received_paint_update_ = false; 153 observing_ = true;
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();
159
157 // Save url of the pending entry to identify when it loads and paints later. 160 // 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 - 161 // Under some circumstances navigation can leave a null pending entry -
159 // see comments in NavigationControllerImpl::NavigateToPendingEntry(). 162 // see comments in NavigationControllerImpl::NavigateToPendingEntry().
160 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL(); 163 pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL();
161 } 164 LOG(ERROR) << "URL: " << pending_entry_url_.GetContent();
162 165 }
163 void OverscrollNavigationOverlay::SetOverlayWindow( 166
164 scoped_ptr<aura::Window> window, 167 void OverscrollNavigationOverlay::SetupOverlayWindow() {
165 aura_extra::ImageWindowDelegate* delegate) { 168 LOG(ERROR) << "ONO: Setting overlay window";
166 window_ = window.Pass(); 169 window_.reset(new aura::Window(
167 if (window_.get() && window_->parent()) 170 new OverscrollWindowDelegate(owa_, GetImageForDirection(direction_))));
168 window_->parent()->StackChildAtTop(window_.get()); 171 window_->SetType(ui::wm::WINDOW_TYPE_CONTROL);
169 image_delegate_ = delegate; 172 window_->SetTransparent(true);
170 173 window_->Init(aura::WINDOW_LAYER_TEXTURED);
171 if (window_.get() && delegate->has_image()) { 174 window_->layer()->SetMasksToBounds(false);
172 window_slider_.reset(new WindowSlider(this, 175 window_->SetName("OverscrollOverlay");
173 window_->parent(), 176 window_->layer()->SetLayerBrightness(-0.1f);
mfomitchev 2015/03/06 01:36:43 It doesn't seem like we are animating the brightne
Nina 2015/03/09 15:54:52 Done.
174 window_.get())); 177 web_contents_window_->AddChild(window_.get());
175 slide_direction_ = SLIDE_UNKNOWN; 178 window_->Show();
176 } else { 179 }
177 window_slider_.reset(); 180
178 } 181 const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
179 } 182 OverscrollWindowAnimation::Direction direction) const {
180
181 void OverscrollNavigationOverlay::StopObservingIfDone() {
182 // Normally we dismiss the overlay once we receive a paint update, however
183 // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get
184 // called, and we rely on loading_complete_ for those cases.
185 if (!received_paint_update_ && !loading_complete_)
186 return;
187
188 // If a slide is in progress, then do not destroy the window or the slide.
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();
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 }
208
209 ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) {
210 const NavigationControllerImpl& controller = web_contents_->GetController(); 183 const NavigationControllerImpl& controller = web_contents_->GetController();
211 const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset); 184 const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
212 185 controller.GetEntryAtOffset(
213 gfx::Image image; 186 direction == OverscrollWindowAnimation::FORWARD ? 1 : -1));
187
188 LOG(ERROR) << "ONO: Image for entry - " << entry->GetTitle();
214 if (entry && entry->screenshot().get()) { 189 if (entry && entry->screenshot().get()) {
215 std::vector<gfx::ImagePNGRep> image_reps; 190 std::vector<gfx::ImagePNGRep> image_reps;
216 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f)); 191 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
217 image = gfx::Image(image_reps); 192 return gfx::Image(image_reps);
218 } 193 }
194 return gfx::Image();
195 }
196
197 scoped_ptr<ui::Layer> OverscrollNavigationOverlay::CreateSlideLayer() {
198 LOG(ERROR) << "ONO: Creating slide layer";
219 if (!layer_delegate_) 199 if (!layer_delegate_)
220 layer_delegate_.reset(new ImageLayerDelegate()); 200 layer_delegate_.reset(new ImageLayerDelegate());
221 layer_delegate_->SetImage(image); 201 layer_delegate_->SetImage(GetImageForDirection(direction_));
222 202 scoped_ptr<ui::Layer> layer(new ui::Layer(ui::LAYER_TEXTURED));
223 ui::Layer* layer = new ui::Layer(ui::LAYER_TEXTURED);
224 layer->set_delegate(layer_delegate_.get()); 203 layer->set_delegate(layer_delegate_.get());
225 return layer; 204 ui::Layer* parent = window_->layer()->parent();
226 } 205 parent->Add(layer.get());
227 206 if (direction_ == OverscrollWindowAnimation::FORWARD)
mfomitchev 2015/03/05 23:37:06 I think somewhere here we should have LTR/RTL logi
Nina 2015/03/09 15:54:52 FORWARD and BACKWARD refer to history direction, n
mfomitchev 2015/03/10 19:25:58 Ok, cool
228 ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() { 207 parent->StackAbove(layer.get(), window_->layer());
208 else
209 parent->StackBelow(layer.get(), window_->layer());
210 gfx::Rect bounds = gfx::Rect(layer->parent()->bounds().size());
211 layer->SetBounds(bounds);
212 return layer.Pass();
213 }
214
215 scoped_ptr<OverscrollLayerWrapper> OverscrollNavigationOverlay::CreateLayer() {
mfomitchev 2015/03/05 23:37:05 Method needs to be renamed - we are creating a wra
Nina 2015/03/09 15:54:52 Agreed, had a TODO on the .h file.
216 LOG(ERROR) << "ONO: CreateLayer";
217 if (window_) {
218 LOG(ERROR) << "We have a window, returning slide layer";
219 return scoped_ptr<OverscrollLayerWrapper>(
220 new OverscrollLayerWrapper(CreateSlideLayer()));
221 }
222 SetupOverlayWindow();
mfomitchev 2015/03/05 23:37:06 SetupOverlayWindow() should not inititalize window
Nina 2015/03/09 15:54:52 Done.
223 if (direction_ == OverscrollWindowAnimation::FORWARD) {
224 web_contents_window_->StackChildAbove(
225 window_.get(), web_contents_->GetContentNativeView());
226 } else {
227 web_contents_window_->StackChildBelow(
228 window_.get(), web_contents_->GetContentNativeView());
229 }
230 return scoped_ptr<OverscrollLayerWrapper>(
231 new OverscrollLayerWrapper(window_.Pass()));
232 }
233
234 scoped_ptr<OverscrollLayerWrapper>
235 OverscrollNavigationOverlay::CreateFrontLayer() {
236 LOG(ERROR) << "ONO: Create front layer";
237 if (!web_contents_->GetController().CanGoForward())
238 return nullptr;
239 direction_ = OverscrollWindowAnimation::FORWARD;
240 return CreateLayer();
241 }
242
243 scoped_ptr<OverscrollLayerWrapper>
244 OverscrollNavigationOverlay::CreateBackLayer() {
245 LOG(ERROR) << "ONO: Create back layer";
229 if (!web_contents_->GetController().CanGoBack()) 246 if (!web_contents_->GetController().CanGoBack())
230 return NULL; 247 return nullptr;
231 slide_direction_ = SLIDE_BACK; 248 direction_ = OverscrollWindowAnimation::BACKWARD;
232 return CreateSlideLayer(-1); 249 return CreateLayer();
233 } 250 }
234 251
235 ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() { 252 void OverscrollNavigationOverlay::OnOverscrollCompleted(
236 if (!web_contents_->GetController().CanGoForward()) 253 scoped_ptr<OverscrollLayerWrapper> layer_wrapper) {
237 return NULL; 254 LOG(ERROR) << "ONO: OnOverscrollCompleted";
238 slide_direction_ = SLIDE_FRONT; 255
239 return CreateSlideLayer(1); 256 // The overscroll gesture has been cancelled.
240 } 257 if (!observing_ && !loading_complete_) {
mfomitchev 2015/03/05 23:37:05 This seems like the only place we use observing_.
Nina 2015/03/09 15:54:52 Done.
241 258 LOG(ERROR) << "Not observing, the gesture has been cancelled";
242 void OverscrollNavigationOverlay::OnWindowSlideCompleting() { 259 LOG(ERROR) << "Observing: " << observing_;
243 if (slide_direction_ == SLIDE_UNKNOWN) 260 LOG(ERROR) << "Loading complete: " << loading_complete_;
261 window_.reset();
244 return; 262 return;
245 263 }
246 // Perform the navigation. 264
247 if (slide_direction_ == SLIDE_BACK) 265 // TODO if the first iteration captures a dismiss layer and the second a
248 web_contents_->GetController().GoBack(); 266 // window_, this code might not work.
249 else if (slide_direction_ == SLIDE_FRONT) 267 if (layer_wrapper->has_window())
268 window_ = layer_wrapper->AcquireWindow();
269 else
270 dismiss_layer_ = layer_wrapper->AcquireLayer();
271
272 // Make sure the overlay window is on top.
273 web_contents_window_->StackChildAtTop(window_.get());
274 //dismiss_layer_ = layer_wrapper->AcquireLayer();
275 owa_->set_live_window(window_.get());
276
277 // Reset the position of the contents window.
278 web_contents_->GetContentNativeView()->SetTransform(gfx::Transform());
mfomitchev 2015/03/06 01:36:43 These three instructions should only be done for t
Nina 2015/03/09 15:54:52 Done.
279
280 StopObservingIfDone();
281 }
282
283 void OverscrollNavigationOverlay::OnOverscrollCompleting() {
284 // We start the navigation as soon as we know the overscroll gesture is
285 // completing.
286 LOG(ERROR) << "ONO: Starting navigation";
287 if (direction_ == OverscrollWindowAnimation::FORWARD)
250 web_contents_->GetController().GoForward(); 288 web_contents_->GetController().GoForward();
251 else 289 else
252 NOTREACHED(); 290 web_contents_->GetController().GoBack();
253
254 // Reset state and wait for the new navigation page to complete
255 // loading/painting.
256 StartObserving(); 291 StartObserving();
257 } 292 }
258 293
259 void OverscrollNavigationOverlay::OnWindowSlideCompleted( 294 void OverscrollNavigationOverlay::FadeOutOverscrollWindow() {
260 scoped_ptr<ui::Layer> layer) { 295 LOG(ERROR) << "ONO: FadeOutOverscrollWindow";
261 if (slide_direction_ == SLIDE_UNKNOWN) { 296 // TODO maybe we do not need this check.
262 window_slider_.reset(); 297 if (!dismiss_layer_ && !window_)
263 StopObservingIfDone();
264 return; 298 return;
265 } 299 if (!dismiss_layer_)
266 300 dismiss_layer_ = window_->AcquireLayer();
267 // Change the image used for the overlay window. 301 window_.reset();
268 image_delegate_->SetImage(layer_delegate_->image()); 302 aura::Window* contents = web_contents_->GetContentNativeView();
269 window_->layer()->SetTransform(gfx::Transform()); 303 contents->layer()->SetLayerBrightness(1.f);
270 window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size())); 304 {
271 slide_direction_ = SLIDE_UNKNOWN; 305 ui::ScopedLayerAnimationSettings settings(contents->layer()->GetAnimator());
272 // We may end up dismissing the overlay before it has a chance to repaint, so 306 settings.SetPreemptionStrategy(
273 // set the slider layer to be the one animated by OverlayDismissAnimator. 307 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
274 if (layer.get()) 308 settings.SetTweenType(gfx::Tween::EASE_OUT);
275 overlay_dismiss_layer_ = layer.Pass(); 309 contents->layer()->SetLayerBrightness(0.f);
276 StopObservingIfDone(); 310 }
277 } 311 (new OverlayDismissAnimator(dismiss_layer_.Pass()))->Animate();
278 312 }
279 void OverscrollNavigationOverlay::OnWindowSlideAborted() { 313
280 StopObservingIfDone(); 314 void OverscrollNavigationOverlay::StopObservingIfDone() {
mfomitchev 2015/03/06 01:36:43 Order of the methods should be the same as in .h f
Nina 2015/03/09 15:54:52 Done. I also rearranged some of the methods in a w
281 } 315 LOG(ERROR) << "ONO: Stop observing if done";
282 316 if (!loading_complete_ || owa_->is_active()) {
283 void OverscrollNavigationOverlay::OnWindowSliderDestroyed() { 317 LOG(ERROR) << "Returning early";
284 // We only want to take an action here if WindowSlider is being destroyed 318 return;
285 // outside of OverscrollNavigationOverlay. If window_slider_.get() is NULL, 319 }
286 // then OverscrollNavigationOverlay is the one destroying WindowSlider, and 320 FadeOutOverscrollWindow();
287 // we don't need to do anything. 321 Observe(NULL);
288 // This check prevents StopObservingIfDone() being called multiple times 322 observing_ = false;
289 // (including recursively) for a single event. 323 loading_complete_ = false;
290 if (window_slider_.get()) { 324 owa_->set_live_window(web_contents_window_);
mfomitchev 2015/03/06 01:36:43 It's pretty unfortunate that ONO has to do this -
Nina 2015/03/09 15:54:52 Yes
291 // The slider has just been destroyed. Release the ownership.
292 ignore_result(window_slider_.release());
293 StopObservingIfDone();
294 }
295 } 325 }
296 326
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 loading_complete_ = 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