| Index: content/browser/web_contents/aura/overscroll_navigation_overlay.cc
|
| diff --git a/content/browser/web_contents/aura/overscroll_navigation_overlay.cc b/content/browser/web_contents/aura/overscroll_navigation_overlay.cc
|
| index 8c0d1a999b05412b01a8a99ff5b530434e2a57d3..8948f9a8bfed397db4fc40ebb316dc79c1db6482 100644
|
| --- a/content/browser/web_contents/aura/overscroll_navigation_overlay.cc
|
| +++ b/content/browser/web_contents/aura/overscroll_navigation_overlay.cc
|
| @@ -4,17 +4,14 @@
|
|
|
| #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h"
|
|
|
| -#include <vector>
|
| -
|
| -#include "base/i18n/rtl.h"
|
| #include "content/browser/frame_host/navigation_entry_impl.h"
|
| #include "content/browser/renderer_host/render_view_host_impl.h"
|
| -#include "content/browser/web_contents/aura/overscroll_window_delegate.h"
|
| #include "content/browser/web_contents/web_contents_impl.h"
|
| #include "content/common/view_messages.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/render_widget_host_view.h"
|
| #include "ui/aura/window.h"
|
| +#include "ui/aura_extra/image_window_delegate.h"
|
| #include "ui/base/layout.h"
|
| #include "ui/compositor/layer.h"
|
| #include "ui/compositor/layer_animation_observer.h"
|
| @@ -22,6 +19,7 @@
|
| #include "ui/compositor/scoped_layer_animation_settings.h"
|
| #include "ui/gfx/canvas.h"
|
| #include "ui/gfx/image/image_png_rep.h"
|
| +#include "ui/gfx/image/image_skia.h"
|
|
|
| namespace content {
|
| namespace {
|
| @@ -45,20 +43,50 @@
|
|
|
| } // namespace
|
|
|
| -// A class that sets masks to bounds to false on a layer and restores the old
|
| -// value on destruction.
|
| -class OverscrollNavigationOverlay::ScopedLayerClippingSetting {
|
| +// A LayerDelegate that paints an image for the layer.
|
| +class ImageLayerDelegate : public ui::LayerDelegate {
|
| public:
|
| - explicit ScopedLayerClippingSetting(ui::Layer* layer)
|
| - : masks_to_bounds_(layer->GetMasksToBounds()), layer_(layer) {
|
| - layer_->SetMasksToBounds(false);
|
| - }
|
| -
|
| - ~ScopedLayerClippingSetting() { layer_->SetMasksToBounds(masks_to_bounds_); }
|
| + ImageLayerDelegate() {}
|
| +
|
| + ~ImageLayerDelegate() override {}
|
| +
|
| + void SetImage(const gfx::Image& image) {
|
| + image_ = image;
|
| + image_size_ = image.AsImageSkia().size();
|
| + }
|
| + const gfx::Image& image() const { return image_; }
|
|
|
| private:
|
| - bool masks_to_bounds_;
|
| - ui::Layer* layer_;
|
| + // Overridden from ui::LayerDelegate:
|
| + void OnPaintLayer(const ui::PaintContext& context) override {
|
| + ui::PaintRecorder recorder(context);
|
| + if (image_.IsEmpty()) {
|
| + recorder.canvas()->DrawColor(SK_ColorWHITE);
|
| + } else {
|
| + SkISize size = recorder.canvas()->sk_canvas()->getDeviceSize();
|
| + if (size.width() != image_size_.width() ||
|
| + size.height() != image_size_.height()) {
|
| + recorder.canvas()->DrawColor(SK_ColorWHITE);
|
| + }
|
| + recorder.canvas()->DrawImageInt(image_.AsImageSkia(), 0, 0);
|
| + }
|
| + }
|
| +
|
| + void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
|
| +
|
| + // Called when the layer's device scale factor has changed.
|
| + void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
|
| +
|
| + // Invoked prior to the bounds changing. The returned closured is run after
|
| + // the bounds change.
|
| + base::Closure PrepareForLayerBoundsChange() override {
|
| + return base::Closure();
|
| + }
|
| +
|
| + gfx::Image image_;
|
| + gfx::Size image_size_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ImageLayerDelegate);
|
| };
|
|
|
| // Responsible for fading out and deleting the layer of the overlay window.
|
| @@ -104,25 +132,26 @@
|
| };
|
|
|
| OverscrollNavigationOverlay::OverscrollNavigationOverlay(
|
| - WebContentsImpl* web_contents,
|
| - aura::Window* web_contents_window)
|
| - : direction_(NONE),
|
| - web_contents_(web_contents),
|
| + WebContentsImpl* web_contents)
|
| + : web_contents_(web_contents),
|
| + image_delegate_(NULL),
|
| loading_complete_(false),
|
| received_paint_update_(false),
|
| - owa_(new OverscrollWindowAnimation(this)),
|
| - web_contents_window_(web_contents_window) {
|
| + slide_direction_(SLIDE_UNKNOWN) {
|
| }
|
|
|
| OverscrollNavigationOverlay::~OverscrollNavigationOverlay() {
|
| - if (owa_->is_active())
|
| - GetMainWindow()->ReleaseCapture();
|
| }
|
|
|
| void OverscrollNavigationOverlay::StartObserving() {
|
| loading_complete_ = false;
|
| received_paint_update_ = false;
|
| + overlay_dismiss_layer_.reset();
|
| Observe(web_contents_);
|
| +
|
| + // Make sure the overlay window is on top.
|
| + if (window_.get() && window_->parent())
|
| + window_->parent()->StackChildAtTop(window_.get());
|
|
|
| // Assumes the navigation has been initiated.
|
| NavigationEntry* pending_entry =
|
| @@ -133,129 +162,138 @@
|
| pending_entry_url_ = pending_entry ? pending_entry->GetURL() : GURL();
|
| }
|
|
|
| +void OverscrollNavigationOverlay::SetOverlayWindow(
|
| + scoped_ptr<aura::Window> window,
|
| + aura_extra::ImageWindowDelegate* delegate) {
|
| + window_ = window.Pass();
|
| + if (window_.get() && window_->parent())
|
| + window_->parent()->StackChildAtTop(window_.get());
|
| + image_delegate_ = delegate;
|
| +
|
| + if (window_.get() && delegate->has_image()) {
|
| + window_slider_.reset(new WindowSlider(this,
|
| + window_->parent(),
|
| + window_.get()));
|
| + slide_direction_ = SLIDE_UNKNOWN;
|
| + } else {
|
| + window_slider_.reset();
|
| + }
|
| +}
|
| +
|
| void OverscrollNavigationOverlay::StopObservingIfDone() {
|
| // Normally we dismiss the overlay once we receive a paint update, however
|
| // for in-page navigations DidFirstVisuallyNonEmptyPaint() does not get
|
| // called, and we rely on loading_complete_ for those cases.
|
| - // If an overscroll gesture is in progress, then do not destroy the window.
|
| - if (!window_ || !(loading_complete_ || received_paint_update_) ||
|
| - owa_->is_active()) {
|
| + if (!received_paint_update_ && !loading_complete_)
|
| return;
|
| - }
|
| - // Restore layer clipping.
|
| - contents_layer_settings_.reset();
|
| - contents_layer_parent_settings_.reset();
|
| -
|
| - // OverlayDismissAnimator deletes the dismiss layer and itself when the
|
| - // animation completes.
|
| - scoped_ptr<ui::Layer> dismiss_layer = window_->AcquireLayer();
|
| +
|
| + // If a slide is in progress, then do not destroy the window or the slide.
|
| + if (window_slider_.get() && window_slider_->IsSlideInProgress())
|
| + return;
|
| +
|
| + // The layer to be animated by OverlayDismissAnimator
|
| + scoped_ptr<ui::Layer> overlay_dismiss_layer;
|
| + if (overlay_dismiss_layer_)
|
| + overlay_dismiss_layer = overlay_dismiss_layer_.Pass();
|
| + else if (window_.get())
|
| + overlay_dismiss_layer = window_->AcquireLayer();
|
| + Observe(NULL);
|
| + window_slider_.reset();
|
| window_.reset();
|
| - (new OverlayDismissAnimator(dismiss_layer.Pass()))->Animate();
|
| - Observe(nullptr);
|
| - received_paint_update_ = false;
|
| - loading_complete_ = false;
|
| -}
|
| -
|
| -scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateOverlayWindow(
|
| - const gfx::Rect& bounds) {
|
| - OverscrollWindowDelegate* overscroll_delegate = new OverscrollWindowDelegate(
|
| - owa_.get(), GetImageForDirection(direction_));
|
| - scoped_ptr<aura::Window> window(new aura::Window(overscroll_delegate));
|
| - window->SetTransparent(true);
|
| - window->Init(ui::LAYER_TEXTURED);
|
| - window->layer()->SetMasksToBounds(false);
|
| - window->SetName("OverscrollOverlay");
|
| - web_contents_window_->AddChild(window.get());
|
| - aura::Window* event_window = GetMainWindow();
|
| - if (direction_ == FORWARD)
|
| - web_contents_window_->StackChildAbove(window.get(), event_window);
|
| - else
|
| - web_contents_window_->StackChildBelow(window.get(), event_window);
|
| - window->SetBounds(bounds);
|
| - // Set capture on the window that is receiving the overscroll events so that
|
| - // trackpad scroll gestures keep targetting it even if the mouse pointer moves
|
| - // off its bounds.
|
| - event_window->SetCapture();
|
| - window->Show();
|
| - return window.Pass();
|
| -}
|
| -
|
| -const gfx::Image OverscrollNavigationOverlay::GetImageForDirection(
|
| - NavigationDirection direction) const {
|
| + image_delegate_ = NULL;
|
| + if (overlay_dismiss_layer.get()) {
|
| + // OverlayDismissAnimator deletes overlay_dismiss_layer and itself when the
|
| + // animation completes.
|
| + (new OverlayDismissAnimator(overlay_dismiss_layer.Pass()))->Animate();
|
| + }
|
| +}
|
| +
|
| +ui::Layer* OverscrollNavigationOverlay::CreateSlideLayer(int offset) {
|
| const NavigationControllerImpl& controller = web_contents_->GetController();
|
| - const NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
|
| - controller.GetEntryAtOffset(direction == FORWARD ? 1 : -1));
|
| -
|
| + const NavigationEntryImpl* entry = controller.GetEntryAtOffset(offset);
|
| +
|
| + gfx::Image image;
|
| if (entry && entry->screenshot().get()) {
|
| std::vector<gfx::ImagePNGRep> image_reps;
|
| image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
|
| - return gfx::Image(image_reps);
|
| - }
|
| - return gfx::Image();
|
| -}
|
| -
|
| -scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateFrontWindow(
|
| - const gfx::Rect& bounds) {
|
| + image = gfx::Image(image_reps);
|
| + }
|
| + if (!layer_delegate_)
|
| + layer_delegate_.reset(new ImageLayerDelegate());
|
| + layer_delegate_->SetImage(image);
|
| +
|
| + ui::Layer* layer = new ui::Layer(ui::LAYER_TEXTURED);
|
| + layer->set_delegate(layer_delegate_.get());
|
| + return layer;
|
| +}
|
| +
|
| +ui::Layer* OverscrollNavigationOverlay::CreateBackLayer() {
|
| + if (!web_contents_->GetController().CanGoBack())
|
| + return NULL;
|
| + slide_direction_ = SLIDE_BACK;
|
| + return CreateSlideLayer(-1);
|
| +}
|
| +
|
| +ui::Layer* OverscrollNavigationOverlay::CreateFrontLayer() {
|
| if (!web_contents_->GetController().CanGoForward())
|
| - return nullptr;
|
| - direction_ = FORWARD;
|
| - return CreateOverlayWindow(bounds);
|
| -}
|
| -
|
| -scoped_ptr<aura::Window> OverscrollNavigationOverlay::CreateBackWindow(
|
| - const gfx::Rect& bounds) {
|
| - if (!web_contents_->GetController().CanGoBack())
|
| - return nullptr;
|
| - direction_ = BACK;
|
| - return CreateOverlayWindow(bounds);
|
| -}
|
| -
|
| -aura::Window* OverscrollNavigationOverlay::GetMainWindow() const {
|
| - if (window_)
|
| - return window_.get();
|
| - return web_contents_->GetContentNativeView();
|
| -}
|
| -
|
| -void OverscrollNavigationOverlay::OnOverscrollCompleting() {
|
| - GetMainWindow()->ReleaseCapture();
|
| - // We start the navigation as soon as we know the overscroll gesture is
|
| - // completing.
|
| - DCHECK(direction_ != NONE);
|
| -
|
| - // Avoid clipping on the screenshot caused by the contents window being moved
|
| - // outside of the screen bounds.
|
| - contents_layer_settings_.reset(
|
| - new ScopedLayerClippingSetting(web_contents_->GetNativeView()->layer()));
|
| - contents_layer_parent_settings_.reset(new ScopedLayerClippingSetting(
|
| - web_contents_->GetNativeView()->layer()->parent()));
|
| -
|
| - // Make sure we can navigate first, as other factors can trigger a navigation
|
| - // during an overscroll gesture and navigating without history produces a
|
| - // crash.
|
| - if (direction_ == FORWARD && web_contents_->GetController().CanGoForward())
|
| + return NULL;
|
| + slide_direction_ = SLIDE_FRONT;
|
| + return CreateSlideLayer(1);
|
| +}
|
| +
|
| +void OverscrollNavigationOverlay::OnWindowSlideCompleting() {
|
| + if (slide_direction_ == SLIDE_UNKNOWN)
|
| + return;
|
| +
|
| + // Perform the navigation.
|
| + if (slide_direction_ == SLIDE_BACK)
|
| + web_contents_->GetController().GoBack();
|
| + else if (slide_direction_ == SLIDE_FRONT)
|
| web_contents_->GetController().GoForward();
|
| - if (direction_ == BACK && web_contents_->GetController().CanGoBack())
|
| - web_contents_->GetController().GoBack();
|
| + else
|
| + NOTREACHED();
|
| +
|
| + // Reset state and wait for the new navigation page to complete
|
| + // loading/painting.
|
| StartObserving();
|
| }
|
|
|
| -void OverscrollNavigationOverlay::OnOverscrollCompleted(
|
| - scoped_ptr<aura::Window> window) {
|
| - GetMainWindow()->SetTransform(gfx::Transform());
|
| - window_ = window.Pass();
|
| - // Make sure the window is in its default position.
|
| - window_->SetBounds(gfx::Rect(web_contents_window_->bounds().size()));
|
| - window_->SetTransform(gfx::Transform());
|
| - // Make sure the overlay window is on top.
|
| - web_contents_window_->StackChildAtTop(window_.get());
|
| - direction_ = NONE;
|
| +void OverscrollNavigationOverlay::OnWindowSlideCompleted(
|
| + scoped_ptr<ui::Layer> layer) {
|
| + if (slide_direction_ == SLIDE_UNKNOWN) {
|
| + window_slider_.reset();
|
| + StopObservingIfDone();
|
| + return;
|
| + }
|
| +
|
| + // Change the image used for the overlay window.
|
| + image_delegate_->SetImage(layer_delegate_->image());
|
| + window_->layer()->SetTransform(gfx::Transform());
|
| + window_->SchedulePaintInRect(gfx::Rect(window_->bounds().size()));
|
| + slide_direction_ = SLIDE_UNKNOWN;
|
| + // We may end up dismissing the overlay before it has a chance to repaint, so
|
| + // set the slider layer to be the one animated by OverlayDismissAnimator.
|
| + if (layer.get())
|
| + overlay_dismiss_layer_ = layer.Pass();
|
| StopObservingIfDone();
|
| }
|
|
|
| -void OverscrollNavigationOverlay::OnOverscrollCancelled() {
|
| - GetMainWindow()->ReleaseCapture();
|
| - direction_ = NONE;
|
| +void OverscrollNavigationOverlay::OnWindowSlideAborted() {
|
| StopObservingIfDone();
|
| +}
|
| +
|
| +void OverscrollNavigationOverlay::OnWindowSliderDestroyed() {
|
| + // We only want to take an action here if WindowSlider is being destroyed
|
| + // outside of OverscrollNavigationOverlay. If window_slider_.get() is NULL,
|
| + // then OverscrollNavigationOverlay is the one destroying WindowSlider, and
|
| + // we don't need to do anything.
|
| + // This check prevents StopObservingIfDone() being called multiple times
|
| + // (including recursively) for a single event.
|
| + if (window_slider_.get()) {
|
| + // The slider has just been destroyed. Release the ownership.
|
| + ignore_result(window_slider_.release());
|
| + StopObservingIfDone();
|
| + }
|
| }
|
|
|
| void OverscrollNavigationOverlay::DidFirstVisuallyNonEmptyPaint() {
|
| @@ -271,7 +309,7 @@
|
| void OverscrollNavigationOverlay::DidStopLoading() {
|
| // Don't compare URLs in this case - it's possible they won't match if
|
| // a gesture-nav initiated navigation was interrupted by some other in-site
|
| - // navigation (e.g., from a script, or from a bookmark).
|
| + // navigation ((e.g., from a script, or from a bookmark).
|
| loading_complete_ = true;
|
| StopObservingIfDone();
|
| }
|
|
|