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
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_ANDROID_H_
6 #define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_ANDROID_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "content/browser/renderer_host/render_view_host_delegate_view.h"
12 #include "content/browser/web_contents/web_contents_view.h"
13 #include "content/public/browser/web_contents_view_delegate.h"
14 #include "content/public/common/drop_data.h"
15 #include "ui/android/overscroll_refresh.h"
16 #include "ui/android/view_android.h"
17 #include "ui/android/view_android_observer.h"
18 #include "ui/events/android/event_handler_android.h"
19 #include "ui/gfx/geometry/rect_f.h"
20 
21 namespace content {
22 class ContentUiEventHandler;
23 class RenderWidgetHostViewAndroid;
24 class SelectPopup;
25 class SelectionPopupController;
26 class SynchronousCompositorClient;
27 class WebContentsImpl;
28 
29 // Android-specific implementation of the WebContentsView.
30 class WebContentsViewAndroid : public WebContentsView,
31                                public RenderViewHostDelegateView,
32                                public ui::EventHandlerAndroid {
33  public:
34   WebContentsViewAndroid(WebContentsImpl* web_contents,
35                          WebContentsViewDelegate* delegate);
36   ~WebContentsViewAndroid() override;
37 
38   void SetContentUiEventHandler(std::unique_ptr<ContentUiEventHandler> handler);
39 
set_synchronous_compositor_client(SynchronousCompositorClient * client)40   void set_synchronous_compositor_client(SynchronousCompositorClient* client) {
41     synchronous_compositor_client_ = client;
42   }
43 
set_selection_popup_controller(SelectionPopupController * controller)44   void set_selection_popup_controller(SelectionPopupController* controller) {
45     selection_popup_controller_ = controller;
46   }
47 
synchronous_compositor_client()48   SynchronousCompositorClient* synchronous_compositor_client() const {
49     return synchronous_compositor_client_;
50   }
51 
52   void SetOverscrollRefreshHandler(
53       std::unique_ptr<ui::OverscrollRefreshHandler> overscroll_refresh_handler);
54 
55   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
56 
57   // WebContentsView implementation --------------------------------------------
58   gfx::NativeView GetNativeView() const override;
59   gfx::NativeView GetContentNativeView() const override;
60   gfx::NativeWindow GetTopLevelNativeWindow() const override;
61   void GetContainerBounds(gfx::Rect* out) const override;
62   void SizeContents(const gfx::Size& size) override;
63   void Focus() override;
64   void SetInitialFocus() override;
65   void StoreFocus() override;
66   void RestoreFocus() override;
67   void FocusThroughTabTraversal(bool reverse) override;
68   DropData* GetDropData() const override;
69   gfx::Rect GetViewBounds() const override;
70   void CreateView(gfx::NativeView context) override;
71   RenderWidgetHostViewBase* CreateViewForWidget(
72       RenderWidgetHost* render_widget_host) override;
73   RenderWidgetHostViewBase* CreateViewForChildWidget(
74       RenderWidgetHost* render_widget_host) override;
75   void SetPageTitle(const base::string16& title) override;
76   void RenderViewReady() override;
77   void RenderViewHostChanged(RenderViewHost* old_host,
78                              RenderViewHost* new_host) override;
79   void SetOverscrollControllerEnabled(bool enabled) override;
80 
81   // Backend implementation of RenderViewHostDelegateView.
82   void ShowContextMenu(RenderFrameHost* render_frame_host,
83                        const ContextMenuParams& params) override;
84   void ShowPopupMenu(RenderFrameHost* render_frame_host,
85                      const gfx::Rect& bounds,
86                      int item_height,
87                      double item_font_size,
88                      int selected_item,
89                      const std::vector<MenuItem>& items,
90                      bool right_aligned,
91                      bool allow_multiple_selection) override;
92   void HidePopupMenu() override;
93   ui::OverscrollRefreshHandler* GetOverscrollRefreshHandler() const override;
94   void StartDragging(const DropData& drop_data,
95                      blink::WebDragOperationsMask allowed_ops,
96                      const gfx::ImageSkia& image,
97                      const gfx::Vector2d& image_offset,
98                      const DragEventSourceInfo& event_info,
99                      RenderWidgetHostImpl* source_rwh) override;
100   void UpdateDragCursor(blink::WebDragOperation operation) override;
101   void GotFocus(RenderWidgetHostImpl* render_widget_host) override;
102   void LostFocus(RenderWidgetHostImpl* render_widget_host) override;
103   void TakeFocus(bool reverse) override;
104   int GetTopControlsHeight() const override;
105   int GetTopControlsMinHeight() const override;
106   int GetBottomControlsHeight() const override;
107   int GetBottomControlsMinHeight() const override;
108   bool ShouldAnimateBrowserControlsHeightChanges() const override;
109   bool DoBrowserControlsShrinkRendererSize() const override;
110 
111   // ui::EventHandlerAndroid implementation.
112   bool OnTouchEvent(const ui::MotionEventAndroid& event) override;
113   bool OnMouseEvent(const ui::MotionEventAndroid& event) override;
114   bool OnDragEvent(const ui::DragEventAndroid& event) override;
115   bool OnGenericMotionEvent(const ui::MotionEventAndroid& event) override;
116   bool OnKeyUp(const ui::KeyEventAndroid& event) override;
117   bool DispatchKeyEvent(const ui::KeyEventAndroid& event) override;
118   bool ScrollBy(float delta_x, float delta_y) override;
119   bool ScrollTo(float x, float y) override;
120   void OnSizeChanged() override;
121   void OnPhysicalBackingSizeChanged() override;
122   void OnBrowserControlsHeightChanged() override;
123 
124   void SetFocus(bool focused);
set_device_orientation(int orientation)125   void set_device_orientation(int orientation) {
126     device_orientation_ = orientation;
127   }
128 
129  private:
130   void OnDragEntered(const std::vector<DropData::Metadata>& metadata,
131                      const gfx::PointF& location,
132                      const gfx::PointF& screen_location);
133   void OnDragUpdated(const gfx::PointF& location,
134                      const gfx::PointF& screen_location);
135   void OnDragExited();
136   void OnPerformDrop(DropData* drop_data,
137                      const gfx::PointF& location,
138                      const gfx::PointF& screen_location);
139   void OnDragEnded();
140   void OnSystemDragEnded();
141 
142   SelectPopup* GetSelectPopup();
143 
144   // The WebContents whose contents we display.
145   WebContentsImpl* web_contents_;
146 
147   // Handles UI events in Java layer when necessary.
148   std::unique_ptr<ContentUiEventHandler> content_ui_event_handler_;
149 
150   // Handles "overscroll to refresh" events
151   std::unique_ptr<ui::OverscrollRefreshHandler> overscroll_refresh_handler_;
152 
153   // Interface for extensions to WebContentsView. Used to show the context menu.
154   std::unique_ptr<WebContentsViewDelegate> delegate_;
155 
156   // The native view associated with the contents of the web.
157   ui::ViewAndroid view_;
158 
159   // Interface used to get notified of events from the synchronous compositor.
160   SynchronousCompositorClient* synchronous_compositor_client_;
161 
162   SelectionPopupController* selection_popup_controller_ = nullptr;
163 
164   int device_orientation_ = 0;
165 
166   // Show/hide popup UI for <select> tag.
167   std::unique_ptr<SelectPopup> select_popup_;
168 
169   gfx::PointF drag_location_;
170   gfx::PointF drag_screen_location_;
171 
172   DISALLOW_COPY_AND_ASSIGN(WebContentsViewAndroid);
173 };
174 
175 } // namespace content
176 
177 #endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_ANDROID_H_
178