1 // Copyright 2024 The Chromium Authors 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 BASE_ANDROID_INPUT_HINT_CHECKER_H_ 6 #define BASE_ANDROID_INPUT_HINT_CHECKER_H_ 7 8 #include "base/android/jni_weak_ref.h" 9 #include "base/base_export.h" 10 #include "base/feature_list.h" 11 #include "base/memory/raw_ptr.h" 12 #include "base/no_destructor.h" 13 #include "base/threading/thread_checker.h" 14 #include "base/time/time.h" 15 16 namespace base::android { 17 18 BASE_DECLARE_FEATURE(kYieldWithInputHint); 19 20 // A class to track a single global root View object and ask it for presence of 21 // new unhandled input events. 22 // 23 // This class uses bits specific to Android V and does nothing on earlier 24 // releases. 25 // 26 // Must be constructed on UI thread. All public methods must be called on the UI 27 // thread. 28 class BASE_EXPORT InputHintChecker { 29 public: 30 InputHintChecker() = default; 31 virtual ~InputHintChecker() = default; 32 33 // Returns the singleton. 34 static InputHintChecker& GetInstance(); 35 36 // Initializes features for this class. See `base::features::Init()`. 37 static void InitializeFeatures(); 38 39 // Obtains a weak reference to |root_view| so that the following calls to 40 // HasInput() take the input hint for this View. Requirements for the View 41 // object are described in InputHintChecker.java. 42 void SetView(JNIEnv* env, jobject root_view); 43 44 // Fetches and returns the input hint from the Android Framework. Throttles 45 // the calls to one every few milliseconds. When a call is made before the 46 // minimal time interval passed since the previous call, returns false. 47 static bool HasInput(); 48 49 // RAII override of GetInstance() for testing. 50 struct ScopedOverrideInstance { 51 explicit ScopedOverrideInstance(InputHintChecker* checker); 52 ~ScopedOverrideInstance(); 53 }; 54 55 protected: 56 virtual bool HasInputImplWithThrottling(); 57 58 private: 59 friend class base::NoDestructor<InputHintChecker>; 60 61 JavaObjectWeakGlobalRef view_; 62 THREAD_CHECKER(thread_checker_); 63 base::TimeTicks last_checked_; 64 }; 65 66 } // namespace base::android 67 68 #endif // BASE_ANDROID_INPUT_HINT_CHECKER_H_ 69