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

Side by Side Diff: content/browser/web_contents/web_contents_view_aura.cc

Issue 895543005: Refactor GestureNavigation to eliminate code redundancy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nits Created 5 years, 8 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/web_contents_view_aura.h" 5 #include "content/browser/web_contents/web_contents_view_aura.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "content/browser/browser_plugin/browser_plugin_guest.h" 12 #include "content/browser/browser_plugin/browser_plugin_guest.h"
13 #include "content/browser/download/drag_download_util.h" 13 #include "content/browser/download/drag_download_util.h"
14 #include "content/browser/frame_host/interstitial_page_impl.h" 14 #include "content/browser/frame_host/interstitial_page_impl.h"
15 #include "content/browser/frame_host/navigation_entry_impl.h" 15 #include "content/browser/frame_host/navigation_entry_impl.h"
16 #include "content/browser/renderer_host/dip_util.h" 16 #include "content/browser/renderer_host/dip_util.h"
17 #include "content/browser/renderer_host/overscroll_controller.h" 17 #include "content/browser/renderer_host/overscroll_controller.h"
18 #include "content/browser/renderer_host/render_view_host_factory.h" 18 #include "content/browser/renderer_host/render_view_host_factory.h"
19 #include "content/browser/renderer_host/render_view_host_impl.h" 19 #include "content/browser/renderer_host/render_view_host_impl.h"
20 #include "content/browser/renderer_host/render_widget_host_impl.h" 20 #include "content/browser/renderer_host/render_widget_host_impl.h"
21 #include "content/browser/renderer_host/render_widget_host_view_aura.h" 21 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
22 #include "content/browser/renderer_host/web_input_event_aura.h" 22 #include "content/browser/renderer_host/web_input_event_aura.h"
23 #include "content/browser/web_contents/aura/gesture_nav_simple.h" 23 #include "content/browser/web_contents/aura/gesture_nav_simple.h"
24 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h" 24 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h"
25 #include "content/browser/web_contents/aura/shadow_layer_delegate.h" 25 #include "content/browser/web_contents/aura/overscroll_window_animation.h"
26 #include "content/browser/web_contents/aura/window_slider.h"
27 #include "content/browser/web_contents/touch_editable_impl_aura.h" 26 #include "content/browser/web_contents/touch_editable_impl_aura.h"
28 #include "content/browser/web_contents/web_contents_impl.h" 27 #include "content/browser/web_contents/web_contents_impl.h"
29 #include "content/public/browser/content_browser_client.h" 28 #include "content/public/browser/content_browser_client.h"
30 #include "content/public/browser/notification_observer.h" 29 #include "content/public/browser/notification_observer.h"
31 #include "content/public/browser/notification_registrar.h" 30 #include "content/public/browser/notification_registrar.h"
32 #include "content/public/browser/notification_source.h" 31 #include "content/public/browser/notification_source.h"
33 #include "content/public/browser/notification_types.h" 32 #include "content/public/browser/notification_types.h"
34 #include "content/public/browser/overscroll_configuration.h" 33 #include "content/public/browser/overscroll_configuration.h"
35 #include "content/public/browser/render_view_host.h" 34 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/render_widget_host.h" 35 #include "content/public/browser/render_widget_host.h"
37 #include "content/public/browser/render_widget_host_view.h" 36 #include "content/public/browser/render_widget_host_view.h"
38 #include "content/public/browser/web_contents_delegate.h" 37 #include "content/public/browser/web_contents_delegate.h"
39 #include "content/public/browser/web_contents_observer.h" 38 #include "content/public/browser/web_contents_observer.h"
40 #include "content/public/browser/web_contents_view_delegate.h" 39 #include "content/public/browser/web_contents_view_delegate.h"
41 #include "content/public/browser/web_drag_dest_delegate.h" 40 #include "content/public/browser/web_drag_dest_delegate.h"
42 #include "content/public/common/content_client.h" 41 #include "content/public/common/content_client.h"
43 #include "content/public/common/content_switches.h" 42 #include "content/public/common/content_switches.h"
44 #include "content/public/common/drop_data.h" 43 #include "content/public/common/drop_data.h"
45 #include "net/base/filename_util.h" 44 #include "net/base/filename_util.h"
46 #include "third_party/WebKit/public/web/WebInputEvent.h" 45 #include "third_party/WebKit/public/web/WebInputEvent.h"
47 #include "ui/aura/client/aura_constants.h" 46 #include "ui/aura/client/aura_constants.h"
48 #include "ui/aura/client/window_tree_client.h" 47 #include "ui/aura/client/window_tree_client.h"
49 #include "ui/aura/env.h" 48 #include "ui/aura/env.h"
50 #include "ui/aura/window.h" 49 #include "ui/aura/window.h"
51 #include "ui/aura/window_observer.h" 50 #include "ui/aura/window_observer.h"
52 #include "ui/aura/window_tree_host.h" 51 #include "ui/aura/window_tree_host.h"
53 #include "ui/aura/window_tree_host_observer.h" 52 #include "ui/aura/window_tree_host_observer.h"
54 #include "ui/aura_extra/image_window_delegate.h"
55 #include "ui/base/clipboard/clipboard.h" 53 #include "ui/base/clipboard/clipboard.h"
56 #include "ui/base/clipboard/custom_data_helper.h" 54 #include "ui/base/clipboard/custom_data_helper.h"
57 #include "ui/base/dragdrop/drag_drop_types.h" 55 #include "ui/base/dragdrop/drag_drop_types.h"
58 #include "ui/base/dragdrop/drag_utils.h" 56 #include "ui/base/dragdrop/drag_utils.h"
59 #include "ui/base/dragdrop/drop_target_event.h" 57 #include "ui/base/dragdrop/drop_target_event.h"
60 #include "ui/base/dragdrop/os_exchange_data.h" 58 #include "ui/base/dragdrop/os_exchange_data.h"
61 #include "ui/base/hit_test.h" 59 #include "ui/base/hit_test.h"
62 #include "ui/compositor/layer.h" 60 #include "ui/compositor/layer.h"
63 #include "ui/compositor/scoped_layer_animation_settings.h"
64 #include "ui/events/event.h" 61 #include "ui/events/event.h"
65 #include "ui/events/event_utils.h" 62 #include "ui/events/event_utils.h"
66 #include "ui/gfx/canvas.h" 63 #include "ui/gfx/canvas.h"
67 #include "ui/gfx/image/image.h" 64 #include "ui/gfx/image/image.h"
68 #include "ui/gfx/image/image_png_rep.h" 65 #include "ui/gfx/image/image_png_rep.h"
69 #include "ui/gfx/image/image_skia.h" 66 #include "ui/gfx/image/image_skia.h"
70 #include "ui/gfx/screen.h" 67 #include "ui/gfx/screen.h"
71 #include "ui/wm/public/drag_drop_client.h" 68 #include "ui/wm/public/drag_drop_client.h"
72 #include "ui/wm/public/drag_drop_delegate.h" 69 #include "ui/wm/public/drag_drop_delegate.h"
73 70
74 namespace content { 71 namespace content {
75 WebContentsView* CreateWebContentsView( 72 WebContentsView* CreateWebContentsView(
76 WebContentsImpl* web_contents, 73 WebContentsImpl* web_contents,
77 WebContentsViewDelegate* delegate, 74 WebContentsViewDelegate* delegate,
78 RenderViewHostDelegateView** render_view_host_delegate_view) { 75 RenderViewHostDelegateView** render_view_host_delegate_view) {
79 WebContentsViewAura* rv = new WebContentsViewAura(web_contents, delegate); 76 WebContentsViewAura* rv = new WebContentsViewAura(web_contents, delegate);
80 *render_view_host_delegate_view = rv; 77 *render_view_host_delegate_view = rv;
81 return rv; 78 return rv;
82 } 79 }
83 80
84 namespace { 81 namespace {
85 82
86 bool IsScrollEndEffectEnabled() { 83 bool IsScrollEndEffectEnabled() {
87 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 84 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
88 switches::kScrollEndEffect) == "1"; 85 switches::kScrollEndEffect) == "1";
89 } 86 }
90 87
91 bool ShouldNavigateForward(const NavigationController& controller,
92 OverscrollMode mode) {
93 return mode == (base::i18n::IsRTL() ? OVERSCROLL_EAST : OVERSCROLL_WEST) &&
94 controller.CanGoForward();
95 }
96
97 bool ShouldNavigateBack(const NavigationController& controller,
98 OverscrollMode mode) {
99 return mode == (base::i18n::IsRTL() ? OVERSCROLL_WEST : OVERSCROLL_EAST) &&
100 controller.CanGoBack();
101 }
102
103 RenderWidgetHostViewAura* ToRenderWidgetHostViewAura( 88 RenderWidgetHostViewAura* ToRenderWidgetHostViewAura(
104 RenderWidgetHostView* view) { 89 RenderWidgetHostView* view) {
105 if (!view || RenderViewHostFactory::has_factory()) 90 if (!view || RenderViewHostFactory::has_factory())
106 return NULL; // Can't cast to RenderWidgetHostViewAura in unit tests. 91 return NULL; // Can't cast to RenderWidgetHostViewAura in unit tests.
107 92
108 RenderViewHost* rvh = RenderViewHost::From(view->GetRenderWidgetHost()); 93 RenderViewHost* rvh = RenderViewHost::From(view->GetRenderWidgetHost());
109 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( 94 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
110 rvh ? WebContents::FromRenderViewHost(rvh) : NULL); 95 rvh ? WebContents::FromRenderViewHost(rvh) : NULL);
111 if (BrowserPluginGuest::IsGuest(web_contents)) 96 if (BrowserPluginGuest::IsGuest(web_contents))
112 return NULL; 97 return NULL;
113 return static_cast<RenderWidgetHostViewAura*>(view); 98 return static_cast<RenderWidgetHostViewAura*>(view);
114 } 99 }
115 100
116 // The window delegate for the overscroll window. This redirects trackpad events
117 // to the web-contents window. The delegate destroys itself when the window is
118 // destroyed.
119 class OverscrollWindowDelegate : public aura_extra::ImageWindowDelegate {
120 public:
121 OverscrollWindowDelegate(WebContentsImpl* web_contents,
122 OverscrollMode overscroll_mode)
123 : web_contents_(web_contents),
124 forward_events_(true) {
125 const NavigationControllerImpl& controller = web_contents->GetController();
126 const NavigationEntryImpl* entry = NULL;
127 if (ShouldNavigateForward(controller, overscroll_mode)) {
128 entry = controller.GetEntryAtOffset(1);
129 } else if (ShouldNavigateBack(controller, overscroll_mode)) {
130 entry = controller.GetEntryAtOffset(-1);
131 }
132
133 gfx::Image image;
134 if (entry && entry->screenshot().get()) {
135 std::vector<gfx::ImagePNGRep> image_reps;
136 image_reps.push_back(gfx::ImagePNGRep(entry->screenshot(), 1.0f));
137 image = gfx::Image(image_reps);
138 }
139 SetImage(image);
140 }
141
142 void stop_forwarding_events() { forward_events_ = false; }
143
144 private:
145 ~OverscrollWindowDelegate() override {}
146
147 aura::Window* web_contents_window() {
148 return web_contents_->GetView()->GetContentNativeView();
149 }
150
151 // Overridden from ui::EventHandler.
152 void OnScrollEvent(ui::ScrollEvent* event) override {
153 if (forward_events_ && web_contents_window())
154 web_contents_window()->delegate()->OnScrollEvent(event);
155 }
156
157 void OnGestureEvent(ui::GestureEvent* event) override {
158 if (forward_events_ && web_contents_window())
159 web_contents_window()->delegate()->OnGestureEvent(event);
160 }
161
162 WebContentsImpl* web_contents_;
163
164 // The window is displayed both during the gesture, and after the gesture
165 // while the navigation is in progress. During the gesture, it is necessary to
166 // forward input events to the content page (e.g. when the overscroll window
167 // slides under the cursor and starts receiving scroll events). However, once
168 // the gesture is complete, and the window is being displayed as an overlay
169 // window during navigation, events should not be forwarded anymore.
170 bool forward_events_;
171
172 DISALLOW_COPY_AND_ASSIGN(OverscrollWindowDelegate);
173 };
174
175 // Listens to all mouse drag events during a drag and drop and sends them to 101 // Listens to all mouse drag events during a drag and drop and sends them to
176 // the renderer. 102 // the renderer.
177 class WebDragSourceAura : public NotificationObserver { 103 class WebDragSourceAura : public NotificationObserver {
178 public: 104 public:
179 WebDragSourceAura(aura::Window* window, WebContentsImpl* contents) 105 WebDragSourceAura(aura::Window* window, WebContentsImpl* contents)
180 : window_(window), 106 : window_(window),
181 contents_(contents) { 107 contents_(contents) {
182 registrar_.Add(this, 108 registrar_.Add(this,
183 NOTIFICATION_WEB_CONTENTS_DISCONNECTED, 109 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
184 Source<WebContents>(contents)); 110 Source<WebContents>(contents));
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 // The parent window that hosts the constrained windows. We cache the old host 650 // The parent window that hosts the constrained windows. We cache the old host
725 // view so that we can unregister when it's not the parent anymore. 651 // view so that we can unregister when it's not the parent anymore.
726 aura::Window* host_window_; 652 aura::Window* host_window_;
727 653
728 DISALLOW_COPY_AND_ASSIGN(WindowObserver); 654 DISALLOW_COPY_AND_ASSIGN(WindowObserver);
729 }; 655 };
730 656
731 //////////////////////////////////////////////////////////////////////////////// 657 ////////////////////////////////////////////////////////////////////////////////
732 // WebContentsViewAura, public: 658 // WebContentsViewAura, public:
733 659
734 WebContentsViewAura::WebContentsViewAura( 660 WebContentsViewAura::WebContentsViewAura(WebContentsImpl* web_contents,
735 WebContentsImpl* web_contents, 661 WebContentsViewDelegate* delegate)
736 WebContentsViewDelegate* delegate)
737 : web_contents_(web_contents), 662 : web_contents_(web_contents),
738 delegate_(delegate), 663 delegate_(delegate),
739 current_drag_op_(blink::WebDragOperationNone), 664 current_drag_op_(blink::WebDragOperationNone),
740 drag_dest_delegate_(NULL), 665 drag_dest_delegate_(NULL),
741 current_rvh_for_drag_(NULL), 666 current_rvh_for_drag_(NULL),
742 overscroll_change_brightness_(false),
743 current_overscroll_gesture_(OVERSCROLL_NONE), 667 current_overscroll_gesture_(OVERSCROLL_NONE),
744 completed_overscroll_gesture_(OVERSCROLL_NONE), 668 completed_overscroll_gesture_(OVERSCROLL_NONE),
669 navigation_overlay_(nullptr),
670 overscroll_window_animation_(nullptr),
745 touch_editable_(TouchEditableImplAura::Create()), 671 touch_editable_(TouchEditableImplAura::Create()),
746 is_or_was_visible_(false) { 672 is_or_was_visible_(false) {
747 } 673 }
748 674
749 //////////////////////////////////////////////////////////////////////////////// 675 ////////////////////////////////////////////////////////////////////////////////
750 // WebContentsViewAura, private: 676 // WebContentsViewAura, private:
751 677
752 WebContentsViewAura::~WebContentsViewAura() { 678 WebContentsViewAura::~WebContentsViewAura() {
753 if (!window_) 679 if (!window_)
754 return; 680 return;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 web_contents_->DragSourceEndedAt(client_loc.x(), client_loc.y(), 715 web_contents_->DragSourceEndedAt(client_loc.x(), client_loc.y(),
790 screen_loc.x(), screen_loc.y(), ops); 716 screen_loc.x(), screen_loc.y(), ops);
791 } 717 }
792 718
793 void WebContentsViewAura::InstallOverscrollControllerDelegate( 719 void WebContentsViewAura::InstallOverscrollControllerDelegate(
794 RenderWidgetHostViewAura* view) { 720 RenderWidgetHostViewAura* view) {
795 const std::string value = base::CommandLine::ForCurrentProcess()-> 721 const std::string value = base::CommandLine::ForCurrentProcess()->
796 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation); 722 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation);
797 if (value == "0") { 723 if (value == "0") {
798 navigation_overlay_.reset(); 724 navigation_overlay_.reset();
725 overscroll_window_animation_ = nullptr;
799 return; 726 return;
800 } 727 }
801 if (value == "2") { 728 if (value == "2") {
802 navigation_overlay_.reset(); 729 navigation_overlay_.reset();
803 if (!gesture_nav_simple_) 730 if (!gesture_nav_simple_)
804 gesture_nav_simple_.reset(new GestureNavSimple(web_contents_)); 731 gesture_nav_simple_.reset(new GestureNavSimple(web_contents_));
805 view->overscroll_controller()->set_delegate(gesture_nav_simple_.get()); 732 view->overscroll_controller()->set_delegate(gesture_nav_simple_.get());
806 return; 733 return;
807 } 734 }
808 view->overscroll_controller()->set_delegate(this); 735 view->overscroll_controller()->set_delegate(this);
809 if (!navigation_overlay_) 736 if (!navigation_overlay_) {
810 navigation_overlay_.reset(new OverscrollNavigationOverlay(web_contents_)); 737 navigation_overlay_.reset(
811 } 738 new OverscrollNavigationOverlay(web_contents_, window_.get()));
812 739 overscroll_window_animation_ = navigation_overlay_->owa();
813 void WebContentsViewAura::PrepareOverscrollWindow() {
814 // If there is an existing |overscroll_window_| which is in the middle of an
815 // animation, then destroying the window here causes the animation to be
816 // completed immediately, which triggers |OnImplicitAnimationsCompleted()|
817 // callback, and that tries to reset |overscroll_window_| again, causing a
818 // double-free. So use a temporary variable here.
819 if (overscroll_window_) {
820 base::AutoReset<OverscrollMode> reset_state(&current_overscroll_gesture_,
821 current_overscroll_gesture_);
822 scoped_ptr<aura::Window> reset_window(overscroll_window_.release());
823 }
824
825 OverscrollWindowDelegate* overscroll_delegate = new OverscrollWindowDelegate(
826 web_contents_,
827 current_overscroll_gesture_);
828 overscroll_window_.reset(new aura::Window(overscroll_delegate));
829 overscroll_window_->SetType(ui::wm::WINDOW_TYPE_CONTROL);
830 overscroll_window_->SetTransparent(true);
831 overscroll_window_->Init(ui::LAYER_TEXTURED);
832 overscroll_window_->layer()->SetMasksToBounds(false);
833 overscroll_window_->SetName("OverscrollOverlay");
834
835 overscroll_change_brightness_ = overscroll_delegate->has_image();
836 window_->AddChild(overscroll_window_.get());
837
838 gfx::Rect bounds = gfx::Rect(window_->bounds().size());
839 if (ShouldNavigateForward(web_contents_->GetController(),
840 current_overscroll_gesture_)) {
841 // The overlay will be sliding in from the right edge towards the left in
842 // non-RTL, or sliding in from the left edge towards the right in RTL.
843 // So position the overlay window accordingly.
844 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0);
845 }
846
847 aura::Window* animate_window = GetWindowToAnimateForOverscroll();
848 if (animate_window == overscroll_window_)
849 window_->StackChildAbove(overscroll_window_.get(), GetContentNativeView());
850 else
851 window_->StackChildBelow(overscroll_window_.get(), GetContentNativeView());
852
853 UpdateOverscrollWindowBrightness(0.f);
854
855 overscroll_window_->SetBounds(bounds);
856 overscroll_window_->Show();
857
858 overscroll_shadow_.reset(new ShadowLayerDelegate(animate_window->layer()));
859 }
860
861 void WebContentsViewAura::PrepareContentWindowForOverscroll() {
862 StopObservingImplicitAnimations();
863 aura::Window* content = GetContentNativeView();
864 content->layer()->GetAnimator()->AbortAllAnimations();
865 content->SetTransform(gfx::Transform());
866 content->layer()->SetLayerBrightness(0.f);
867 }
868
869 void WebContentsViewAura::ResetOverscrollTransform() {
870 if (!web_contents_->GetRenderWidgetHostView())
871 return;
872 aura::Window* target = GetWindowToAnimateForOverscroll();
873 if (!target)
874 return;
875 {
876 ui::ScopedLayerAnimationSettings settings(target->layer()->GetAnimator());
877 settings.SetPreemptionStrategy(
878 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
879 settings.SetTweenType(gfx::Tween::EASE_OUT);
880 settings.AddObserver(this);
881 target->SetTransform(gfx::Transform());
882 }
883 {
884 ui::ScopedLayerAnimationSettings settings(target->layer()->GetAnimator());
885 settings.SetPreemptionStrategy(
886 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
887 settings.SetTweenType(gfx::Tween::EASE_OUT);
888 UpdateOverscrollWindowBrightness(0.f);
889 } 740 }
890 } 741 }
891 742
892 void WebContentsViewAura::CompleteOverscrollNavigation(OverscrollMode mode) { 743 void WebContentsViewAura::CompleteOverscrollNavigation(OverscrollMode mode) {
893 if (!web_contents_->GetRenderWidgetHostView()) 744 if (!web_contents_->GetRenderWidgetHostView())
894 return; 745 return;
895 746
896 // Animate out the current view first. Navigate to the requested history at
897 // the end of the animation.
898 if (current_overscroll_gesture_ == OVERSCROLL_NONE)
899 return;
900
901 UMA_HISTOGRAM_ENUMERATION("Overscroll.Navigated", 747 UMA_HISTOGRAM_ENUMERATION("Overscroll.Navigated",
902 current_overscroll_gesture_, OVERSCROLL_COUNT); 748 current_overscroll_gesture_, OVERSCROLL_COUNT);
903 OverscrollWindowDelegate* delegate = static_cast<OverscrollWindowDelegate*>(
904 overscroll_window_->delegate());
905 delegate->stop_forwarding_events();
906 749
907 completed_overscroll_gesture_ = mode; 750 overscroll_window_animation_->OnOverscrollComplete(mode);
908 aura::Window* target = GetWindowToAnimateForOverscroll();
909 ui::ScopedLayerAnimationSettings settings(target->layer()->GetAnimator());
910 settings.SetPreemptionStrategy(
911 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
912 settings.SetTweenType(gfx::Tween::EASE_OUT);
913 settings.AddObserver(this);
914 gfx::Transform transform;
915 int content_width =
916 web_contents_->GetRenderWidgetHostView()->GetViewBounds().width();
917 float translate_x = static_cast<float>(mode == OVERSCROLL_WEST ?
918 -content_width : content_width);
919 transform.Translate(translate_x, 0);
920 target->SetTransform(transform);
921 UpdateOverscrollWindowBrightness(translate_x);
922 }
923
924 aura::Window* WebContentsViewAura::GetWindowToAnimateForOverscroll() {
925 if (current_overscroll_gesture_ == OVERSCROLL_NONE)
926 return NULL;
927
928 return ShouldNavigateForward(web_contents_->GetController(),
929 current_overscroll_gesture_) ?
930 overscroll_window_.get() : GetContentNativeView();
931 }
932
933 gfx::Vector2dF WebContentsViewAura::GetTranslationForOverscroll(float delta_x,
934 float delta_y) {
935 if (current_overscroll_gesture_ == OVERSCROLL_NORTH ||
936 current_overscroll_gesture_ == OVERSCROLL_SOUTH) {
937 return gfx::Vector2dF(0, delta_y);
938 }
939 // For horizontal overscroll, scroll freely if a navigation is possible. Do a
940 // resistive scroll otherwise.
941 const NavigationControllerImpl& controller = web_contents_->GetController();
942 const gfx::Rect& bounds = GetViewBounds();
943 const float bounds_width = static_cast<float>(bounds.width());
944 if (ShouldNavigateForward(controller, current_overscroll_gesture_))
945 return gfx::Vector2dF(std::max(-bounds_width, delta_x), 0);
946 else if (ShouldNavigateBack(controller, current_overscroll_gesture_))
947 return gfx::Vector2dF(std::min(bounds_width, delta_x), 0);
948 return gfx::Vector2dF();
949 }
950
951 void WebContentsViewAura::PrepareOverscrollNavigationOverlay() {
952 OverscrollWindowDelegate* delegate = static_cast<OverscrollWindowDelegate*>(
953 overscroll_window_->delegate());
954 overscroll_window_->SchedulePaintInRect(
955 gfx::Rect(overscroll_window_->bounds().size()));
956 overscroll_window_->SetBounds(gfx::Rect(window_->bounds().size()));
957 overscroll_window_->SetTransform(gfx::Transform());
958 navigation_overlay_->SetOverlayWindow(overscroll_window_.Pass(),
959 delegate);
960 navigation_overlay_->StartObserving();
961 }
962
963 void WebContentsViewAura::UpdateOverscrollWindowBrightness(float delta_x) {
964 if (!overscroll_change_brightness_)
965 return;
966
967 const float kBrightnessMin = -.1f;
968 const float kBrightnessMax = -.01f;
969
970 float ratio = fabs(delta_x) / GetViewBounds().width();
971 ratio = std::min(1.f, ratio);
972 if (base::i18n::IsRTL())
973 ratio = 1.f - ratio;
974 float brightness = current_overscroll_gesture_ == OVERSCROLL_WEST ?
975 kBrightnessMin + ratio * (kBrightnessMax - kBrightnessMin) :
976 kBrightnessMax - ratio * (kBrightnessMax - kBrightnessMin);
977 brightness = std::max(kBrightnessMin, brightness);
978 brightness = std::min(kBrightnessMax, brightness);
979 aura::Window* window = GetWindowToAnimateForOverscroll();
980 window->layer()->SetLayerBrightness(brightness);
981 } 751 }
982 752
983 void WebContentsViewAura::AttachTouchEditableToRenderView() { 753 void WebContentsViewAura::AttachTouchEditableToRenderView() {
984 if (!touch_editable_) 754 if (!touch_editable_)
985 return; 755 return;
986 RenderWidgetHostViewAura* rwhva = ToRenderWidgetHostViewAura( 756 RenderWidgetHostViewAura* rwhva = ToRenderWidgetHostViewAura(
987 web_contents_->GetRenderWidgetHostView()); 757 web_contents_->GetRenderWidgetHostView());
988 touch_editable_->AttachToView(rwhva); 758 touch_editable_->AttachToView(rwhva);
989 } 759 }
990 760
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 DCHECK(RenderViewHostFactory::has_factory()); 899 DCHECK(RenderViewHostFactory::has_factory());
1130 return static_cast<RenderWidgetHostViewBase*>( 900 return static_cast<RenderWidgetHostViewBase*>(
1131 render_widget_host->GetView()); 901 render_widget_host->GetView());
1132 } 902 }
1133 903
1134 RenderWidgetHostViewAura* view = 904 RenderWidgetHostViewAura* view =
1135 new RenderWidgetHostViewAura(render_widget_host, is_guest_view_hack); 905 new RenderWidgetHostViewAura(render_widget_host, is_guest_view_hack);
1136 view->InitAsChild(NULL); 906 view->InitAsChild(NULL);
1137 GetNativeView()->AddChild(view->GetNativeView()); 907 GetNativeView()->AddChild(view->GetNativeView());
1138 908
1139 if (navigation_overlay_.get() && navigation_overlay_->has_window()) {
1140 navigation_overlay_->StartObserving();
1141 }
1142
1143 RenderWidgetHostImpl* host_impl = 909 RenderWidgetHostImpl* host_impl =
1144 RenderWidgetHostImpl::From(render_widget_host); 910 RenderWidgetHostImpl::From(render_widget_host);
1145 911
1146 if (!host_impl->is_hidden()) 912 if (!host_impl->is_hidden())
1147 view->Show(); 913 view->Show();
1148 914
1149 // We listen to drag drop events in the newly created view's window. 915 // We listen to drag drop events in the newly created view's window.
1150 aura::client::SetDragDropDelegate(view->GetNativeView(), this); 916 aura::client::SetDragDropDelegate(view->GetNativeView(), this);
1151 917
1152 if (view->overscroll_controller() && 918 if (view->overscroll_controller() &&
(...skipping 12 matching lines...) Expand all
1165 } 931 }
1166 932
1167 void WebContentsViewAura::SetPageTitle(const base::string16& title) { 933 void WebContentsViewAura::SetPageTitle(const base::string16& title) {
1168 window_->SetTitle(title); 934 window_->SetTitle(title);
1169 } 935 }
1170 936
1171 void WebContentsViewAura::RenderViewCreated(RenderViewHost* host) { 937 void WebContentsViewAura::RenderViewCreated(RenderViewHost* host) {
1172 } 938 }
1173 939
1174 void WebContentsViewAura::RenderViewSwappedIn(RenderViewHost* host) { 940 void WebContentsViewAura::RenderViewSwappedIn(RenderViewHost* host) {
1175 if (navigation_overlay_.get() && navigation_overlay_->has_window())
1176 navigation_overlay_->StartObserving();
1177 AttachTouchEditableToRenderView(); 941 AttachTouchEditableToRenderView();
1178 } 942 }
1179 943
1180 void WebContentsViewAura::SetOverscrollControllerEnabled(bool enabled) { 944 void WebContentsViewAura::SetOverscrollControllerEnabled(bool enabled) {
1181 RenderWidgetHostViewAura* view = 945 RenderWidgetHostViewAura* view =
1182 ToRenderWidgetHostViewAura(web_contents_->GetRenderWidgetHostView()); 946 ToRenderWidgetHostViewAura(web_contents_->GetRenderWidgetHostView());
1183 if (view) { 947 if (view) {
1184 view->SetOverscrollControllerEnabled(enabled); 948 view->SetOverscrollControllerEnabled(enabled);
1185 if (enabled) 949 if (enabled)
1186 InstallOverscrollControllerDelegate(view); 950 InstallOverscrollControllerDelegate(view);
1187 } 951 }
1188 952
1189 if (!enabled) 953 if (!enabled) {
1190 navigation_overlay_.reset(); 954 navigation_overlay_.reset();
1191 else if (!navigation_overlay_) 955 overscroll_window_animation_ = nullptr;
1192 navigation_overlay_.reset(new OverscrollNavigationOverlay(web_contents_)); 956 }
957 if (!navigation_overlay_) {
958 navigation_overlay_.reset(
959 new OverscrollNavigationOverlay(web_contents_, window_.get()));
960 overscroll_window_animation_ = navigation_overlay_->owa();
961 }
1193 } 962 }
1194 963
1195 //////////////////////////////////////////////////////////////////////////////// 964 ////////////////////////////////////////////////////////////////////////////////
1196 // WebContentsViewAura, RenderViewHostDelegateView implementation: 965 // WebContentsViewAura, RenderViewHostDelegateView implementation:
1197 966
1198 void WebContentsViewAura::ShowContextMenu(RenderFrameHost* render_frame_host, 967 void WebContentsViewAura::ShowContextMenu(RenderFrameHost* render_frame_host,
1199 const ContextMenuParams& params) { 968 const ContextMenuParams& params) {
1200 if (touch_editable_) { 969 if (touch_editable_) {
1201 touch_editable_->EndTouchEditing(false); 970 touch_editable_->EndTouchEditing(false);
1202 } 971 }
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 if (!rwhv || !rwhv->IsShowing()) 1077 if (!rwhv || !rwhv->IsShowing())
1309 return gfx::Rect(); 1078 return gfx::Rect();
1310 1079
1311 return rwhv->GetViewBounds(); 1080 return rwhv->GetViewBounds();
1312 } 1081 }
1313 1082
1314 bool WebContentsViewAura::OnOverscrollUpdate(float delta_x, float delta_y) { 1083 bool WebContentsViewAura::OnOverscrollUpdate(float delta_x, float delta_y) {
1315 if (current_overscroll_gesture_ == OVERSCROLL_NONE) 1084 if (current_overscroll_gesture_ == OVERSCROLL_NONE)
1316 return false; 1085 return false;
1317 1086
1318 aura::Window* target = GetWindowToAnimateForOverscroll();
1319 gfx::Vector2dF translate = GetTranslationForOverscroll(delta_x, delta_y);
1320 gfx::Transform transform;
1321
1322 if (current_overscroll_gesture_ == OVERSCROLL_NORTH || 1087 if (current_overscroll_gesture_ == OVERSCROLL_NORTH ||
1323 current_overscroll_gesture_ == OVERSCROLL_SOUTH) { 1088 current_overscroll_gesture_ == OVERSCROLL_SOUTH) {
1324 OverscrollUpdateForWebContentsDelegate(translate.y()); 1089 OverscrollUpdateForWebContentsDelegate(delta_y);
1325 } else { 1090 return delta_y != 0;
1326 // Only horizontal overscrolls participate in the navigation gesture.
1327 transform.Translate(translate.x(), translate.y());
1328 target->SetTransform(transform);
1329 UpdateOverscrollWindowBrightness(delta_x);
1330 } 1091 }
1331 1092 return overscroll_window_animation_->OnOverscrollUpdate(delta_x, delta_y);
1332 return !translate.IsZero();
1333 } 1093 }
1334 1094
1335 void WebContentsViewAura::OnOverscrollComplete(OverscrollMode mode) { 1095 void WebContentsViewAura::OnOverscrollComplete(OverscrollMode mode) {
1336 UMA_HISTOGRAM_ENUMERATION("Overscroll.Completed", mode, OVERSCROLL_COUNT); 1096 UMA_HISTOGRAM_ENUMERATION("Overscroll.Completed", mode, OVERSCROLL_COUNT);
1337 if (web_contents_->GetDelegate() && 1097 if (web_contents_->GetDelegate() &&
1338 IsScrollEndEffectEnabled() && 1098 IsScrollEndEffectEnabled() &&
1339 (mode == OVERSCROLL_NORTH || mode == OVERSCROLL_SOUTH)) { 1099 (mode == OVERSCROLL_NORTH || mode == OVERSCROLL_SOUTH)) {
1340 web_contents_->GetDelegate()->OverscrollComplete(); 1100 web_contents_->GetDelegate()->OverscrollComplete();
1341 } 1101 }
1342 NavigationControllerImpl& controller = web_contents_->GetController(); 1102 CompleteOverscrollNavigation(mode);
1343 if (ShouldNavigateForward(controller, mode) ||
1344 ShouldNavigateBack(controller, mode)) {
1345 CompleteOverscrollNavigation(mode);
1346 return;
1347 }
1348
1349 ResetOverscrollTransform();
1350 } 1103 }
1351 1104
1352 void WebContentsViewAura::OnOverscrollModeChange(OverscrollMode old_mode, 1105 void WebContentsViewAura::OnOverscrollModeChange(OverscrollMode old_mode,
1353 OverscrollMode new_mode) { 1106 OverscrollMode new_mode) {
1354 // Reset any in-progress overscroll animation first.
1355 ResetOverscrollTransform();
1356
1357 if (old_mode == OVERSCROLL_NORTH || old_mode == OVERSCROLL_SOUTH) 1107 if (old_mode == OVERSCROLL_NORTH || old_mode == OVERSCROLL_SOUTH)
1358 OverscrollUpdateForWebContentsDelegate(0); 1108 OverscrollUpdateForWebContentsDelegate(0);
1359 1109
1360 if (new_mode != OVERSCROLL_NONE && touch_editable_) 1110 if (new_mode != OVERSCROLL_NONE && touch_editable_)
1361 touch_editable_->OverscrollStarted(); 1111 touch_editable_->OverscrollStarted();
1362 1112
1363 if (new_mode == OVERSCROLL_NONE || 1113 if (current_overscroll_gesture_ == OVERSCROLL_NONE &&
1364 !GetContentNativeView() || 1114 new_mode != OVERSCROLL_NONE) {
1365 ((new_mode == OVERSCROLL_EAST || new_mode == OVERSCROLL_WEST) &&
1366 navigation_overlay_.get() && navigation_overlay_->has_window())) {
1367 current_overscroll_gesture_ = OVERSCROLL_NONE;
1368 } else {
1369 aura::Window* target = GetWindowToAnimateForOverscroll();
1370 if (target) {
1371 StopObservingImplicitAnimations();
1372 target->layer()->GetAnimator()->AbortAllAnimations();
1373 }
1374 // Cleanup state of the content window first, because that can reset the
1375 // value of |current_overscroll_gesture_|.
1376 PrepareContentWindowForOverscroll();
1377
1378 current_overscroll_gesture_ = new_mode;
1379 if (current_overscroll_gesture_ == OVERSCROLL_EAST ||
1380 current_overscroll_gesture_ == OVERSCROLL_WEST)
1381 PrepareOverscrollWindow();
1382
1383 UMA_HISTOGRAM_ENUMERATION("Overscroll.Started", new_mode, OVERSCROLL_COUNT); 1115 UMA_HISTOGRAM_ENUMERATION("Overscroll.Started", new_mode, OVERSCROLL_COUNT);
1384 } 1116 }
1117 current_overscroll_gesture_ = new_mode;
1118 overscroll_window_animation_->OnOverscrollModeChange(old_mode, new_mode);
1385 completed_overscroll_gesture_ = OVERSCROLL_NONE; 1119 completed_overscroll_gesture_ = OVERSCROLL_NONE;
1386 } 1120 }
1387 1121
1388 //////////////////////////////////////////////////////////////////////////////// 1122 ////////////////////////////////////////////////////////////////////////////////
1389 // WebContentsViewAura, ui::ImplicitAnimationObserver implementation:
1390
1391 void WebContentsViewAura::OnImplicitAnimationsCompleted() {
1392 overscroll_shadow_.reset();
1393
1394 if (ShouldNavigateForward(web_contents_->GetController(),
1395 completed_overscroll_gesture_)) {
1396 web_contents_->GetController().GoForward();
1397 PrepareOverscrollNavigationOverlay();
1398 } else if (ShouldNavigateBack(web_contents_->GetController(),
1399 completed_overscroll_gesture_)) {
1400 web_contents_->GetController().GoBack();
1401 PrepareOverscrollNavigationOverlay();
1402 } else {
1403 if (touch_editable_)
1404 touch_editable_->OverscrollCompleted();
1405 }
1406
1407 aura::Window* content = GetContentNativeView();
1408 if (content) {
1409 content->SetTransform(gfx::Transform());
1410 content->layer()->SetLayerBrightness(0.f);
1411 }
1412 current_overscroll_gesture_ = OVERSCROLL_NONE;
1413 completed_overscroll_gesture_ = OVERSCROLL_NONE;
1414 overscroll_window_.reset();
1415 }
1416
1417 ////////////////////////////////////////////////////////////////////////////////
1418 // WebContentsViewAura, aura::WindowDelegate implementation: 1123 // WebContentsViewAura, aura::WindowDelegate implementation:
1419 1124
1420 gfx::Size WebContentsViewAura::GetMinimumSize() const { 1125 gfx::Size WebContentsViewAura::GetMinimumSize() const {
1421 return gfx::Size(); 1126 return gfx::Size();
1422 } 1127 }
1423 1128
1424 gfx::Size WebContentsViewAura::GetMaximumSize() const { 1129 gfx::Size WebContentsViewAura::GetMaximumSize() const {
1425 return gfx::Size(); 1130 return gfx::Size();
1426 } 1131 }
1427 1132
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1484 float device_scale_factor) { 1189 float device_scale_factor) {
1485 } 1190 }
1486 1191
1487 void WebContentsViewAura::OnWindowDestroying(aura::Window* window) { 1192 void WebContentsViewAura::OnWindowDestroying(aura::Window* window) {
1488 // This means the destructor is going to be called soon. If there is an 1193 // This means the destructor is going to be called soon. If there is an
1489 // overscroll gesture in progress (i.e. |overscroll_window_| is not NULL), 1194 // overscroll gesture in progress (i.e. |overscroll_window_| is not NULL),
1490 // then destroying it in the WebContentsViewAura destructor can trigger other 1195 // then destroying it in the WebContentsViewAura destructor can trigger other
1491 // virtual functions to be called (e.g. OnImplicitAnimationsCompleted()). So 1196 // virtual functions to be called (e.g. OnImplicitAnimationsCompleted()). So
1492 // destroy the overscroll window here. 1197 // destroy the overscroll window here.
1493 navigation_overlay_.reset(); 1198 navigation_overlay_.reset();
1494 overscroll_window_.reset(); 1199 overscroll_window_animation_ = nullptr;
sadrul 2015/04/07 17:10:08 Instead of keeping this pointer |overscroll_window
Nina 2015/04/07 19:08:56 Done.
1495 } 1200 }
1496 1201
1497 void WebContentsViewAura::OnWindowDestroyed(aura::Window* window) { 1202 void WebContentsViewAura::OnWindowDestroyed(aura::Window* window) {
1498 } 1203 }
1499 1204
1500 void WebContentsViewAura::OnWindowTargetVisibilityChanged(bool visible) { 1205 void WebContentsViewAura::OnWindowTargetVisibilityChanged(bool visible) {
1501 } 1206 }
1502 1207
1503 bool WebContentsViewAura::HasHitTestMask() const { 1208 bool WebContentsViewAura::HasHitTestMask() const {
1504 return false; 1209 return false;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
1646 if (visible) { 1351 if (visible) {
1647 if (!web_contents_->should_normally_be_visible()) 1352 if (!web_contents_->should_normally_be_visible())
1648 web_contents_->WasShown(); 1353 web_contents_->WasShown();
1649 } else { 1354 } else {
1650 if (web_contents_->should_normally_be_visible()) 1355 if (web_contents_->should_normally_be_visible())
1651 web_contents_->WasHidden(); 1356 web_contents_->WasHidden();
1652 } 1357 }
1653 } 1358 }
1654 1359
1655 } // namespace content 1360 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698