overscroll_navigation_overlay.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 3// found in the LICENSE file. 4 5#ifndef CONTENT_BROWSER_WEB_CONTENTS_AURA_OVERSCROLL_NAVIGATION_OVERLAY_H_ 6#define CONTENT_BROWSER_WEB_CONTENTS_AURA_OVERSCROLL_NAVIGATION_OVERLAY_H_ 7 8#include "base/gtest_prod_util.h" 9#include "base/macros.h" 10#include "content/browser/web_contents/aura/window_slider.h" 11#include "content/common/content_export.h" 12#include "content/public/browser/web_contents_observer.h" 13 14struct ViewHostMsg_UpdateRect_Params; 15 16namespace content { 17 18class ImageLayerDelegate; 19class ImageWindowDelegate; 20class OverscrollNavigationOverlayTest; 21 22// When a history navigation is triggered at the end of an overscroll 23// navigation, it is necessary to show the history-screenshot until the page is 24// done navigating and painting. This class accomplishes this by showing the 25// screenshot window on top of the page until the page has completed loading and 26// painting. 27class CONTENT_EXPORT OverscrollNavigationOverlay 28 : public WebContentsObserver, 29 public WindowSlider::Delegate { 30 public: 31 explicit OverscrollNavigationOverlay(WebContentsImpl* web_contents); 32 virtual ~OverscrollNavigationOverlay(); 33 34 bool has_window() const { return !!window_.get(); } 35 36 // Resets state and starts observing |web_contents_| for page load/paint 37 // updates. This function makes sure that the screenshot window is stacked 38 // on top, so that it hides the content window behind it, and destroys the 39 // screenshot window when the page is done loading/painting. 40 // This should be called immediately after initiating the navigation, 41 // otherwise the overlay may be dismissed prematurely. 42 void StartObserving(); 43 44 // Sets the screenshot window and the delegate. This takes ownership of 45 // |window|. 46 // Note that ImageWindowDelegate manages its own lifetime, so this function 47 // does not take ownership of |delegate|. 48 void SetOverlayWindow(scoped_ptr<aura::Window> window, 49 ImageWindowDelegate* delegate); 50 51 // Sets up the overlay for tests. 52 void SetupForTesting(); 53 54 private: 55 friend class OverscrollNavigationOverlayTest; 56 FRIEND_TEST_ALL_PREFIXES(OverscrollNavigationOverlayTest, 57 FirstVisuallyNonEmptyPaint_NoImage); 58 FRIEND_TEST_ALL_PREFIXES(OverscrollNavigationOverlayTest, 59 FirstVisuallyNonEmptyPaint_WithImage); 60 FRIEND_TEST_ALL_PREFIXES(OverscrollNavigationOverlayTest, 61 PaintUpdateWithoutNonEmptyPaint); 62 FRIEND_TEST_ALL_PREFIXES(OverscrollNavigationOverlayTest, 63 MultiNavigation_LoadingUpdate); 64 FRIEND_TEST_ALL_PREFIXES(OverscrollNavigationOverlayTest, 65 MultiNavigation_PaintUpdate); 66 67 enum SlideDirection { 68 SLIDE_UNKNOWN, 69 SLIDE_BACK, 70 SLIDE_FRONT 71 }; 72 73 // Stop observing the page and start the final overlay fade-out animation 74 // if the page-load has completed and the page has been painted, and a 75 // window-slide isn't in progress. 76 void StopObservingIfDone(); 77 78 // Creates a layer to be used for window-slide. |offset| is the offset of the 79 // NavigationEntry for the screenshot image to display. 80 ui::Layer* CreateSlideLayer(int offset); 81 82 // IPC message callbacks. 83 void OnUpdateRect(const ViewHostMsg_UpdateRect_Params& params); 84 85 // Overridden from WindowSlider::Delegate: 86 virtual ui::Layer* CreateBackLayer() OVERRIDE; 87 virtual ui::Layer* CreateFrontLayer() OVERRIDE; 88 virtual void OnWindowSlideCompleting() OVERRIDE; 89 virtual void OnWindowSlideCompleted() OVERRIDE; 90 virtual void OnWindowSlideAborted() OVERRIDE; 91 virtual void OnWindowSliderDestroyed() OVERRIDE; 92 93 // Overridden from WebContentsObserver: 94 virtual void DocumentOnLoadCompletedInMainFrame(int32 page_id) OVERRIDE; 95 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id) OVERRIDE; 96 virtual void DidStopLoading(RenderViewHost* host) OVERRIDE; 97 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 98 99 // The WebContents which is being navigated. 100 WebContentsImpl* web_contents_; 101 102 // The screenshot overlay window. 103 scoped_ptr<aura::Window> window_; 104 105 // This is the WindowDelegate of |window_|. The delegate manages its own 106 // lifetime (destroys itself when |window_| is destroyed). 107 ImageWindowDelegate* image_delegate_; 108 109 bool loading_complete_; 110 bool received_paint_update_; 111 112 // Unique ID of the NavigationEntry we are navigating to. This is needed to 113 // filter on WebContentsObserver callbacks and is used to dismiss the overlay 114 // when the relevant page loads and paints. 115 int pending_entry_id_; 116 117 // The |WindowSlider| that allows sliding history layers while the page is 118 // being reloaded. 119 scoped_ptr<WindowSlider> window_slider_; 120 121 // The direction of the in-progress slide (if any). 122 SlideDirection slide_direction_; 123 124 // The LayerDelegate used for the back/front layers during a slide. 125 scoped_ptr<ImageLayerDelegate> layer_delegate_; 126 127 // During tests, the aura windows don't get any paint updates. So the overlay 128 // container keeps waiting for a paint update it never receives, causing a 129 // timeout. So during tests, disable the wait for paint updates. 130 bool need_paint_update_; 131 132 DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlay); 133}; 134 135} // namespace content 136 137#endif // CONTENT_BROWSER_WEB_CONTENTS_AURA_OVERSCROLL_NAVIGATION_OVERLAY_H_ 138