blob: 3b72774d5149f4e8060704358e174341b506dd7d [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2013 The Chromium Authors
aurimas@chromium.org2d02a2002012-09-18 21:47:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
jaekyune4f9eed2015-02-24 02:06:585#ifndef UI_ANDROID_WINDOW_ANDROID_H_
6#define UI_ANDROID_WINDOW_ANDROID_H_
aurimas@chromium.org2d02a2002012-09-18 21:47:567
8#include <jni.h>
Khushal Sagar931bd7de2024-07-18 21:25:239
eseckler8c15fc32016-12-20 20:22:2010#include <memory>
tedchocc1718be2015-07-10 00:37:1111#include <string>
tfarina93bfa912014-12-05 14:23:1512
newt@chromium.org6a718ab2014-04-23 20:01:2013#include "base/android/jni_weak_ref.h"
aurimas@chromium.org2d02a2002012-09-18 21:47:5614#include "base/android/scoped_java_ref.h"
Avi Drissmanc149c162023-01-12 02:16:5915#include "base/functional/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5216#include "base/memory/raw_ptr.h"
powei@chromium.orgaca40ac2013-11-21 21:35:2817#include "base/observer_list.h"
sievers@chromium.orgf22e45842014-04-29 23:40:3218#include "base/time/time.h"
Aman Vermaf16361d2023-07-10 18:04:0419#include "third_party/blink/public/common/page/content_to_visible_time_reporter.h"
Khushal Sagar931bd7de2024-07-18 21:25:2320#include "ui/android/progress_bar_config.h"
jaekyune4f9eed2015-02-24 02:06:5821#include "ui/android/ui_android_export.h"
sievers481b7fe2016-07-21 17:03:5922#include "ui/android/view_android.h"
tfarina93bfa912014-12-05 14:23:1523#include "ui/gfx/geometry/vector2d_f.h"
sangheon77.kimdd441f92022-08-31 01:15:0824#include "ui/gfx/overlay_transform.h"
aurimas@chromium.org2d02a2002012-09-18 21:47:5625
timavfe53c2c62016-11-23 02:35:4626namespace display {
Bo Liu71ca79cb2019-02-26 18:01:2427class Display;
timavfe53c2c62016-11-23 02:35:4628class DisplayAndroidManager;
eseckler8c15fc32016-12-20 20:22:2029} // namespace display
timavfe53c2c62016-11-23 02:35:4630
aurimas@chromium.org2d02a2002012-09-18 21:47:5631namespace ui {
32
Jinsuk Kim7ba8f2402018-05-04 20:44:3633extern UI_ANDROID_EXPORT const float kDefaultMouseWheelTickMultiplier;
Jinsuk Kim00e46982018-03-07 07:54:3634
William Liu7e940dd2024-09-12 13:16:4435class ModalDialogManagerBridge;
powei@chromium.org0809f7e22014-03-20 00:05:4536class WindowAndroidCompositor;
powei@chromium.orgaca40ac2013-11-21 21:35:2837class WindowAndroidObserver;
38
aurimas@chromium.org2d02a2002012-09-18 21:47:5639// Android implementation of the activity window.
sievers481b7fe2016-07-21 17:03:5940// WindowAndroid is also the root of a ViewAndroid tree.
41class UI_ANDROID_EXPORT WindowAndroid : public ViewAndroid {
aurimas@chromium.org2d02a2002012-09-18 21:47:5642 public:
William Liu196e5aa2024-10-01 18:27:0943 // Intended for unittests only.
Michael Thiessenf42f9f32022-02-03 22:23:0644 class ScopedWindowAndroidForTesting {
45 public:
46 ScopedWindowAndroidForTesting(WindowAndroid* window);
47 ~ScopedWindowAndroidForTesting();
48
49 WindowAndroid* get() { return window_; }
50
Joel Hockey18f767a2024-08-20 03:23:4451 void SetModalDialogManager(
52 base::android::ScopedJavaLocalRef<jobject> modal_dialog_manager);
53
Michael Thiessenf42f9f32022-02-03 22:23:0654 private:
55 raw_ptr<WindowAndroid> window_;
56 };
57
Bo Liu15828fe42025-03-13 01:34:4458 struct AdaptiveRefreshRateInfo {
59 bool supports_adaptive_refresh_rate = false;
60 // Fields below are valid only if `supports_adaptive_refresh_rate` is true.
Bo Liu15828fe42025-03-13 01:34:4461 float suggested_frame_rate_high = 0.f;
Bo Liu15828fe42025-03-13 01:34:4462
63 AdaptiveRefreshRateInfo();
64 AdaptiveRefreshRateInfo(const AdaptiveRefreshRateInfo& other);
65 ~AdaptiveRefreshRateInfo();
66 AdaptiveRefreshRateInfo& operator=(const AdaptiveRefreshRateInfo& other);
67 };
68
Michael Thiessen4c05f2a92018-01-11 00:37:2669 static WindowAndroid* FromJavaWindowAndroid(
70 const base::android::JavaParamRef<jobject>& jwindow_android);
71
Khushal51874322019-04-18 23:55:4072 WindowAndroid(JNIEnv* env,
Andrew Grieve88b4c5fb2025-07-17 17:06:4773 const base::android::JavaRef<jobject>& obj,
Khushal51874322019-04-18 23:55:4074 int display_id,
75 float scroll_factor,
76 bool window_is_wide_color_gamut);
aurimas@chromium.org2d02a2002012-09-18 21:47:5677
Peter BostrÃļmc8c123532021-09-21 23:37:1578 WindowAndroid(const WindowAndroid&) = delete;
79 WindowAndroid& operator=(const WindowAndroid&) = delete;
80
Jinsuk Kimd5d29a22017-08-29 01:39:4481 ~WindowAndroid() override;
82
Andrew Grieve474183fd2025-07-02 15:47:1083 void Destroy(JNIEnv* env);
aurimas@chromium.org2d02a2002012-09-18 21:47:5684
85 base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
86
powei@chromium.org0809f7e22014-03-20 00:05:4587 void AttachCompositor(WindowAndroidCompositor* compositor);
powei@chromium.orgaca40ac2013-11-21 21:35:2888 void DetachCompositor();
89
90 void AddObserver(WindowAndroidObserver* observer);
91 void RemoveObserver(WindowAndroidObserver* observer);
92
powei@chromium.org0809f7e22014-03-20 00:05:4593 WindowAndroidCompositor* GetCompositor() { return compositor_; }
Khushala0424812019-03-13 10:05:4394 float GetRefreshRate();
sangheon77.kimdd441f92022-08-31 01:15:0895 gfx::OverlayTransform GetOverlayTransform();
Khushal51874322019-04-18 23:55:4096 std::vector<float> GetSupportedRefreshRates();
Bo Liu15828fe42025-03-13 01:34:4497 AdaptiveRefreshRateInfo adaptive_refresh_rate_info() const {
98 return adaptive_refresh_rate_info_;
99 }
Khushal51874322019-04-18 23:55:40100 void SetPreferredRefreshRate(float refresh_rate);
powei@chromium.org0809f7e22014-03-20 00:05:45101
sievers@chromium.org41256cd2014-05-27 22:45:34102 void SetNeedsAnimate();
jdduke566c0f32015-07-23 00:39:51103 void Animate(base::TimeTicks begin_frame_time);
tornebb13c8332015-12-07 12:49:14104 void OnVisibilityChanged(JNIEnv* env,
tornebb13c8332015-12-07 12:49:14105 bool visible);
Andrew Grieve474183fd2025-07-02 15:47:10106 void OnActivityStopped(JNIEnv* env);
107 void OnActivityStarted(JNIEnv* env);
Khushala0424812019-03-13 10:05:43108 void OnUpdateRefreshRate(JNIEnv* env,
Khushala0424812019-03-13 10:05:43109 float refresh_rate);
Khushal019a87ec2019-03-21 03:41:47110 void OnSupportedRefreshRatesUpdated(
111 JNIEnv* env,
Khushal019a87ec2019-03-21 03:41:47112 const base::android::JavaParamRef<jfloatArray>& supported_refresh_rates);
Bo Liuefb3dcf2025-04-26 04:35:27113 void OnAdaptiveRefreshRateInfoChanged(JNIEnv* env,
114 jboolean supports_adaptive_refresh_rate,
115 jfloat suggested_frame_rate_high);
Andrew Grieve474183fd2025-07-02 15:47:10116 void OnOverlayTransformUpdated(JNIEnv* env);
Aman Vermaf16361d2023-07-10 18:04:04117 void SendUnfoldLatencyBeginTimestamp(JNIEnv* env, jlong begin_time);
sievers@chromium.orgf22e45842014-04-29 23:40:32118
Abdelrahman Eed0c786212025-03-12 10:22:32119 void OnWindowPointerLockRelease(JNIEnv* env);
120
Daniil Hrybchuk5f326622025-09-12 07:42:09121 void OnWindowPositionChanged(JNIEnv* env);
122
Charles Hagerd3c47be2025-01-29 06:15:59123 void ShowToast(const std::string text);
124
tedchocc1718be2015-07-10 00:37:11125 // Return whether the specified Android permission is granted.
126 bool HasPermission(const std::string& permission);
127 // Return whether the specified Android permission can be requested by Chrome.
128 bool CanRequestPermission(const std::string& permission);
129
Khushal Sagar931bd7de2024-07-18 21:25:23130 void set_progress_bar_config_for_testing(ProgressBarConfig config) {
131 progress_bar_config_for_testing_ = config;
132 }
133 ProgressBarConfig GetProgressBarConfig();
134
William Liu7e940dd2024-09-12 13:16:44135 ModalDialogManagerBridge* GetModalDialogManagerBridge();
136
William Liu196e5aa2024-10-01 18:27:09137 // Intended for native browser tests.
138 void SetModalDialogManagerForTesting(
139 base::android::ScopedJavaLocalRef<jobject> java_modal_dialog_manager);
140
Jinsuk Kim4583ba52018-03-14 04:58:56141 float mouse_wheel_scroll_factor() const { return mouse_wheel_scroll_factor_; }
Jinsuk Kim00e46982018-03-07 07:54:36142
Michael Thiessenf42f9f32022-02-03 22:23:06143 static std::unique_ptr<ScopedWindowAndroidForTesting> CreateForTesting();
gsennton44b70442015-11-17 12:44:12144
Bo Liu71ca79cb2019-02-26 18:01:24145 // This should return the same Display as Screen::GetDisplayNearestWindow
146 // except the color space depends on the status of this particular window
147 // rather than the display itself.
148 // See comment on WindowAndroid.getWindowIsWideColorGamut for details.
149 display::Display GetDisplayWithWindowColorSpace();
150
Chris Blumea5846142020-04-22 07:38:12151 void SetWideColorEnabled(bool enabled);
152
Abdelrahman Eede1544332025-03-06 11:59:08153 bool RequestPointerLock(ViewAndroid& view_android);
154
155 bool HasPointerLock(ViewAndroid& view_android);
156
157 void ReleasePointerLock(ViewAndroid& view_android);
158
Nate Chapin789fc8c2025-08-01 17:31:32159 bool SetHasKeyboardCapture(bool keyboard_capture);
160
Bartosz Chominskiaf4915562025-08-25 18:31:43161 std::optional<gfx::Rect> GetBoundsInScreenCoordinates();
162
Khushalc98fcadd92019-04-22 18:47:29163 class TestHooks {
164 public:
165 virtual ~TestHooks() = default;
166 virtual std::vector<float> GetSupportedRates() = 0;
167 virtual void SetPreferredRate(float refresh_rate) = 0;
168 };
169 void SetTestHooks(TestHooks* hooks);
170
Khushal942ed342019-05-24 19:33:49171 class ScopedSelectionHandles {
172 public:
173 ScopedSelectionHandles(WindowAndroid* window);
174 ~ScopedSelectionHandles();
175
176 private:
Keishi Hattori0e45c022021-11-27 09:25:52177 raw_ptr<WindowAndroid> window_;
Khushal942ed342019-05-24 19:33:49178 };
179
aurimas@chromium.org2d02a2002012-09-18 21:47:56180 private:
eseckler8c15fc32016-12-20 20:22:20181 class WindowBeginFrameSource;
Bo Liuca28fc1c2018-11-02 23:03:50182 class ScopedOnBeginFrame;
timavfe53c2c62016-11-23 02:35:46183 friend class DisplayAndroidManager;
eseckler8c15fc32016-12-20 20:22:20184 friend class WindowBeginFrameSource;
timavfe53c2c62016-11-23 02:35:46185
Khushal019a87ec2019-03-21 03:41:47186 void Force60HzRefreshRateIfNeeded();
eseckler8c15fc32016-12-20 20:22:20187
sievers481b7fe2016-07-21 17:03:59188 // ViewAndroid overrides.
189 WindowAndroid* GetWindowAndroid() const override;
aurimas@chromium.org2d02a2002012-09-18 21:47:56190
timavfe53c2c62016-11-23 02:35:46191 // The ID of the display that this window belongs to.
192 int display_id() const { return display_id_; }
193
boliuf1c913e2015-03-25 14:49:10194 base::android::ScopedJavaGlobalRef<jobject> java_window_;
timavfe53c2c62016-11-23 02:35:46195 const int display_id_;
Bo Liu71ca79cb2019-02-26 18:01:24196 const bool window_is_wide_color_gamut_;
Keishi Hattori0e45c022021-11-27 09:25:52197 raw_ptr<WindowAndroidCompositor> compositor_;
aurimas@chromium.org2d02a2002012-09-18 21:47:56198
Khushal Sagar931bd7de2024-07-18 21:25:23199 std::optional<ProgressBarConfig> progress_bar_config_for_testing_;
200
Trent Apteda250ec3ab2018-08-19 08:52:19201 base::ObserverList<WindowAndroidObserver>::Unchecked observer_list_;
Aman Vermaf16361d2023-07-10 18:04:04202 blink::ContentToVisibleTimeReporter content_to_visible_time_recorder_;
powei@chromium.orgaca40ac2013-11-21 21:35:28203
Jinsuk Kim4583ba52018-03-14 04:58:56204 float mouse_wheel_scroll_factor_;
eseckler8c15fc32016-12-20 20:22:20205
Keishi Hattori0e45c022021-11-27 09:25:52206 raw_ptr<TestHooks> test_hooks_ = nullptr;
Khushal019a87ec2019-03-21 03:41:47207
Khushal942ed342019-05-24 19:33:49208 int selection_handles_active_count_ = 0;
Abdelrahman Eede1544332025-03-06 11:59:08209
210 raw_ptr<ViewAndroid> pointer_locking_view_ = nullptr;
Bo Liu15828fe42025-03-13 01:34:44211
212 AdaptiveRefreshRateInfo adaptive_refresh_rate_info_;
aurimas@chromium.org2d02a2002012-09-18 21:47:56213};
214
215} // namespace ui
216
jaekyune4f9eed2015-02-24 02:06:58217#endif // UI_ANDROID_WINDOW_ANDROID_H_