view_android.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2013 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. #ifndef UI_ANDROID_VIEW_ANDROID_H_
  5. #define UI_ANDROID_VIEW_ANDROID_H_
  6. #include <list>
  7. #include <memory>
  8. #include "base/android/jni_array.h"
  9. #include "base/android/jni_weak_ref.h"
  10. #include "base/bind.h"
  11. #include "base/callback.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/observer_list.h"
  16. #include "base/time/time.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. #include "ui/android/ui_android_export.h"
  19. #include "ui/android/view_android_observer.h"
  20. #include "ui/gfx/geometry/rect_f.h"
  21. namespace cc {
  22. class Layer;
  23. }
  24. namespace gfx {
  25. class Point;
  26. class Size;
  27. }
  28. namespace viz {
  29. class CopyOutputRequest;
  30. }
  31. namespace ui {
  32. class Cursor;
  33. class DragEventAndroid;
  34. class EventForwarder;
  35. class EventHandlerAndroid;
  36. class GestureEventAndroid;
  37. class KeyEventAndroid;
  38. class MotionEventAndroid;
  39. class WindowAndroid;
  40. class ViewAndroidObserver;
  41. // View-related parameters from frame updates.
  42. struct FrameInfo {
  43. gfx::SizeF viewport_size; // In dip.
  44. // Content offset from the top. Used to translate snapshots to
  45. // the correct part of the view. In dip.
  46. float content_offset;
  47. };
  48. // A simple container for a UI layer.
  49. // At the root of the hierarchy is a WindowAndroid, when attached.
  50. // Dispatches input/view events coming from Java layer. Hit testing against
  51. // those events is implemented so that the |ViewClient| will be invoked
  52. // when the event got hit on the area defined by |layout_params_|.
  53. // Hit testing is done in the order of parent -> child, and from top
  54. // of the stack to back among siblings.
  55. class UI_ANDROID_EXPORT ViewAndroid {
  56. public:
  57. using CopyViewCallback =
  58. base::RepeatingCallback<void(std::unique_ptr<viz::CopyOutputRequest>)>;
  59. // Stores an anchored view to delete itself at the end of its lifetime
  60. // automatically. This helps manage the lifecyle without the dependency
  61. // on |ViewAndroid|.
  62. class ScopedAnchorView {
  63. public:
  64. ScopedAnchorView(JNIEnv* env,
  65. const base::android::JavaRef<jobject>& jview,
  66. const base::android::JavaRef<jobject>& jdelegate);
  67. ScopedAnchorView();
  68. ScopedAnchorView(ScopedAnchorView&& other);
  69. ScopedAnchorView& operator=(ScopedAnchorView&& other);
  70. // Calls JNI removeView() on the delegate for cleanup.
  71. ~ScopedAnchorView();
  72. void Reset();
  73. const base::android::ScopedJavaLocalRef<jobject> view() const;
  74. private:
  75. // TODO(jinsukkim): Following weak refs can be cast to strong refs which
  76. // cannot be garbage-collected and leak memory. Rewrite not to use them.
  77. // see comments in crrev.com/2103243002.
  78. JavaObjectWeakGlobalRef view_;
  79. JavaObjectWeakGlobalRef delegate_;
  80. // Default copy/assign disabled by move constructor.
  81. };
  82. enum class LayoutType {
  83. // Can have its own size given by |OnSizeChanged| events.
  84. NORMAL,
  85. // Always follows its parent's size.
  86. MATCH_PARENT
  87. };
  88. explicit ViewAndroid(LayoutType layout_type);
  89. ViewAndroid();
  90. ViewAndroid(const ViewAndroid&) = delete;
  91. ViewAndroid& operator=(const ViewAndroid&) = delete;
  92. virtual ~ViewAndroid();
  93. void UpdateFrameInfo(const FrameInfo& frame_info);
  94. // content_offset is in dip.
  95. float content_offset() const { return frame_info_.content_offset; }
  96. gfx::SizeF viewport_size() const { return frame_info_.viewport_size; }
  97. // Returns the window at the root of this hierarchy, or |null|
  98. // if disconnected.
  99. virtual WindowAndroid* GetWindowAndroid() const;
  100. // Virtual for testing.
  101. virtual float GetDipScale();
  102. // Used to return and set the layer for this view. May be |null|.
  103. cc::Layer* GetLayer() const;
  104. void SetLayer(scoped_refptr<cc::Layer> layer);
  105. void SetDelegate(const base::android::JavaRef<jobject>& delegate);
  106. // Gets (creates one if not present) Java object of the EventForwarder
  107. // for a view tree in the view hierarchy including this node.
  108. // Only one instance per the view tree is allowed.
  109. base::android::ScopedJavaLocalRef<jobject> GetEventForwarder();
  110. // Adds a child to this view.
  111. void AddChild(ViewAndroid* child);
  112. // Moves the give child ViewAndroid to the front of the list so that it can be
  113. // the first responder of events.
  114. void MoveToFront(ViewAndroid* child);
  115. // Moves the given child ViewAndroid to the back of the list so that any other
  116. // view may respond to events first.
  117. void MoveToBack(ViewAndroid* child);
  118. // Detaches this view from its parent.
  119. void RemoveFromParent();
  120. bool HasFocus();
  121. void RequestFocus();
  122. // Pass necessary |jdrop_data| to build Android ClipData for drag and drop.
  123. // |jshadow_image| is a bitmap presentation of the shadow image to be used
  124. // for dragging.
  125. bool StartDragAndDrop(const base::android::JavaRef<jobject>& jshadow_image,
  126. const base::android::JavaRef<jobject>& jdrop_data);
  127. gfx::Size GetPhysicalBackingSize() const;
  128. gfx::Size GetSize() const;
  129. gfx::Rect bounds() const { return bounds_; }
  130. void OnSizeChanged(int width, int height);
  131. // |deadline_override| if not nullopt will be used as the cc::DeadlinePolicy
  132. // timeout for this resize.
  133. void OnPhysicalBackingSizeChanged(
  134. const gfx::Size& size,
  135. absl::optional<base::TimeDelta> deadline_override = absl::nullopt);
  136. void OnCursorChanged(const Cursor& cursor);
  137. void SetHoverActionStylusWritable(bool stylus_writable);
  138. void OnBackgroundColorChanged(unsigned int color);
  139. void OnTopControlsChanged(float top_controls_offset,
  140. float top_content_offset,
  141. float top_controls_min_height_offset);
  142. void OnBottomControlsChanged(float bottom_controls_offset,
  143. float bottom_controls_min_height_offset);
  144. void OnBrowserControlsHeightChanged();
  145. // |current_scroll_ratio| is the ratio of vertical scroll in [0, 1] range.
  146. // Scroll at top of page is 0, and bottom of page is 1. It is defined as 0
  147. // if page is not scrollable, though this should not be called in that case.
  148. void OnVerticalScrollDirectionChanged(bool direction_up,
  149. float current_scroll_ratio);
  150. void OnControlsResizeViewChanged(bool controls_resize_view);
  151. // Gets the Visual Viewport inset to apply in physical pixels.
  152. int GetViewportInsetBottom();
  153. ScopedAnchorView AcquireAnchorView();
  154. void SetAnchorRect(const base::android::JavaRef<jobject>& anchor,
  155. const gfx::RectF& bounds);
  156. // This may return null.
  157. base::android::ScopedJavaLocalRef<jobject> GetContainerView();
  158. // Return the location of the container view in physical pixels.
  159. gfx::Point GetLocationOfContainerViewInWindow();
  160. // Return the location of the point relative to screen coordinate in pixels.
  161. gfx::PointF GetLocationOnScreen(float x, float y);
  162. // ViewAndroid does not own |observer|s.
  163. void AddObserver(ViewAndroidObserver* observer);
  164. void RemoveObserver(ViewAndroidObserver* observer);
  165. void RequestDisallowInterceptTouchEvent();
  166. void RequestUnbufferedDispatch(const MotionEventAndroid& event);
  167. void SetCopyOutputCallback(CopyViewCallback callback);
  168. // Return the CopyOutputRequest back if view cannot perform readback.
  169. std::unique_ptr<viz::CopyOutputRequest> MaybeRequestCopyOfView(
  170. std::unique_ptr<viz::CopyOutputRequest> request);
  171. void set_event_handler(EventHandlerAndroid* handler) {
  172. event_handler_ = handler;
  173. }
  174. ViewAndroid* parent() const { return parent_; }
  175. absl::optional<gfx::Rect> GetDisplayFeature();
  176. bool OnTouchEventForTesting(const MotionEventAndroid& event) {
  177. return OnTouchEvent(event);
  178. }
  179. void NotifyVirtualKeyboardOverlayRect(const gfx::Rect& keyboard_rect);
  180. void SetLayoutForTesting(int x, int y, int width, int height);
  181. protected:
  182. void RemoveAllChildren(bool attached_to_window);
  183. raw_ptr<ViewAndroid> parent_;
  184. private:
  185. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest, MatchesViewInFront);
  186. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest, MatchesViewArea);
  187. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest, MatchesViewAfterMove);
  188. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest,
  189. MatchesViewSizeOfkMatchParent);
  190. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest, MatchesViewsWithOffset);
  191. FRIEND_TEST_ALL_PREFIXES(ViewAndroidBoundsTest, OnSizeChanged);
  192. friend class EventForwarder;
  193. friend class ViewAndroidBoundsTest;
  194. bool OnDragEvent(const DragEventAndroid& event);
  195. bool OnTouchEvent(const MotionEventAndroid& event);
  196. bool OnMouseEvent(const MotionEventAndroid& event);
  197. bool OnMouseWheelEvent(const MotionEventAndroid& event);
  198. bool OnGestureEvent(const GestureEventAndroid& event);
  199. bool OnGenericMotionEvent(const MotionEventAndroid& event);
  200. bool OnKeyUp(const KeyEventAndroid& event);
  201. bool DispatchKeyEvent(const KeyEventAndroid& event);
  202. bool ScrollBy(float delta_x, float delta_y);
  203. bool ScrollTo(float x, float y);
  204. void RemoveChild(ViewAndroid* child);
  205. void OnAttachedToWindow();
  206. void OnDetachedFromWindow();
  207. template <typename E>
  208. using EventHandlerCallback =
  209. const base::RepeatingCallback<bool(EventHandlerAndroid*, const E&)>;
  210. template <typename E>
  211. bool HitTest(EventHandlerCallback<E> handler_callback,
  212. const E& event,
  213. const gfx::PointF& point);
  214. static bool SendDragEventToHandler(EventHandlerAndroid* handler,
  215. const DragEventAndroid& event);
  216. static bool SendTouchEventToHandler(EventHandlerAndroid* handler,
  217. const MotionEventAndroid& event);
  218. static bool SendMouseEventToHandler(EventHandlerAndroid* handler,
  219. const MotionEventAndroid& event);
  220. static bool SendMouseWheelEventToHandler(EventHandlerAndroid* handler,
  221. const MotionEventAndroid& event);
  222. static bool SendGestureEventToHandler(EventHandlerAndroid* handler,
  223. const GestureEventAndroid& event);
  224. bool has_event_forwarder() const { return !!event_forwarder_; }
  225. bool match_parent() const { return layout_type_ == LayoutType::MATCH_PARENT; }
  226. // Checks if there is any event forwarder in any node up to root.
  227. static bool RootPathHasEventForwarder(ViewAndroid* view);
  228. // Checks if there is any event forwarder in the node paths down to
  229. // each leaf of subtree.
  230. static bool SubtreeHasEventForwarder(ViewAndroid* view);
  231. void OnSizeChangedInternal(const gfx::Size& size);
  232. void DispatchOnSizeChanged();
  233. // Returns the Java delegate for this view. This is used to delegate work
  234. // up to the embedding view (or the embedder that can deal with the
  235. // implementation details).
  236. const base::android::ScopedJavaLocalRef<jobject> GetViewAndroidDelegate()
  237. const;
  238. std::list<ViewAndroid*> children_;
  239. base::ObserverList<ViewAndroidObserver>::Unchecked observer_list_;
  240. scoped_refptr<cc::Layer> layer_;
  241. JavaObjectWeakGlobalRef delegate_;
  242. raw_ptr<EventHandlerAndroid> event_handler_ = nullptr; // Not owned
  243. // Basic view layout information. Used to do hit testing deciding whether
  244. // the passed events should be processed by the view. Unit in DIP.
  245. gfx::Rect bounds_;
  246. const LayoutType layout_type_;
  247. // In physical pixel.
  248. gfx::Size physical_size_;
  249. FrameInfo frame_info_;
  250. std::unique_ptr<EventForwarder> event_forwarder_;
  251. // Copy output of View rather than window.
  252. CopyViewCallback copy_view_callback_;
  253. bool controls_resize_view_ = false;
  254. };
  255. } // namespace ui
  256. #endif // UI_ANDROID_VIEW_ANDROID_H_