1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 18 19 #include <chrono> 20 #include <string> 21 #include <unordered_map> 22 23 #include "base/utils/macros.h" 24 #include "base/utils/noncopyable.h" 25 #include "core/event/touch_event.h" 26 #include "core/pipeline_ng/pipeline_context.h" 27 #include "base/geometry/offset.h" 28 #include "core/event/axis_event.h" 29 30 namespace OHOS::Ace { 31 namespace ResDefine { 32 constexpr int32_t LOAD_PAGE_START_EVENT = 0; 33 constexpr int32_t LOAD_PAGE_COMPLETE_EVENT = 1; 34 constexpr int32_t LOAD_PAGE_NO_REQUEST_FRAME_EVENT = 2; 35 constexpr double JUDGE_DISTANCE = 3.125; 36 constexpr int64_t INVALID_DATA = -1; 37 } 38 39 struct ResEventInfo { 40 TimeStamp timeStamp; 41 Offset offset; 42 SourceTool sourceTool = SourceTool::UNKNOWN; 43 }; 44 45 struct ReportConfig { 46 bool isReportTid = false; 47 uint64_t tid = 0; 48 }; 49 50 using ReportDataFunc = void (*)(uint32_t resType, int64_t value, 51 const std::unordered_map<std::string, std::string>& payload); 52 53 using ReportSyncEventFunc = int32_t (*)(const uint32_t resType, const int64_t value, 54 const std::unordered_map<std::string, std::string>& payload, std::unordered_map<std::string, std::string>& reply); 55 56 ReportDataFunc ACE_EXPORT LoadReportDataFunc(); 57 ReportSyncEventFunc ACE_EXPORT LoadReportSyncEventFunc(); 58 59 class ACE_EXPORT ResSchedReport final { 60 public: 61 static ResSchedReport& GetInstance(); 62 void ResSchedDataReport(const char* name, const std::unordered_map<std::string, std::string>& param = {}, 63 int64_t tid = ResDefine::INVALID_DATA); 64 void TriggerModuleSerializer(); 65 void ResSchedDataReport(uint32_t resType, int32_t value = 0, 66 const std::unordered_map<std::string, std::string>& payload = {}); 67 void ResScheSyncEventReport(const uint32_t resType, const int64_t value, 68 const std::unordered_map<std::string, std::string>& payload, 69 std::unordered_map<std::string, std::string>& reply); 70 bool AppWhiteListCheck(const std::unordered_map<std::string, std::string>& payload, 71 std::unordered_map<std::string, std::string>& reply); 72 void OnTouchEvent(const TouchEvent& touchEvent, const ReportConfig& config); 73 void OnKeyEvent(const KeyEvent& event); 74 void LoadPageEvent(int32_t value); 75 void OnAxisEvent(const AxisEvent& axisEvent); 76 void AxisEventReportEnd(); 77 void HandlePageTransition(const std::string& fromPage, const std::string& toPage, const std::string& mode); 78 static std::atomic<int32_t> createPageCount; // not consider multi-instances. 79 static bool triggerExecuted; // not consider multi-instances. 80 int64_t GetTid(); 81 int64_t GetPid(); 82 pthread_t GetPthreadSelf(); 83 84 private: 85 ResSchedReport(); ~ResSchedReport()86 ~ResSchedReport() {} 87 void HandleTouchDown(const TouchEvent& touchEvent, const ReportConfig& config); 88 void HandleTouchUp(const TouchEvent& touchEvent, const ReportConfig& config); 89 bool IsRateLimit(int64_t maxCount, std::chrono::seconds durTime, 90 int64_t& keyEventCount, std::chrono::steady_clock::time_point& startTime); 91 bool IsPerSecRateLimit(); 92 bool IsPerMinRateLimit(); 93 void HandleKeyDown(const KeyEvent& event); 94 void HandleKeyUp(const KeyEvent& event); 95 void HandleTouchMove(const TouchEvent& touchEvent, const ReportConfig& config); 96 void HandleTouchCancel(const TouchEvent& touchEvent, const ReportConfig& config); 97 void HandleTouchPullDown(const TouchEvent& touchEvent, const ReportConfig& config); 98 void HandleTouchPullUp(const TouchEvent& touchEvent, const ReportConfig& config); 99 void HandleTouchPullMove(const TouchEvent& touchEvent, const ReportConfig& config); 100 double GetUpVelocity(const ResEventInfo& lastMoveInfo, 101 const ResEventInfo& upEventInfo); 102 void RecordTouchEvent(const TouchEvent& touchEvent, bool enforce = false); 103 104 void HandleAxisBegin(const AxisEvent& axisEvent); 105 void HandleAxisUpdate(const AxisEvent& axisEvent); 106 void HandleAxisEnd(const AxisEvent& axisEvent); 107 108 void RecordAxisEvent(const AxisEvent& axisEvent, bool enforce = false); 109 double GetAxisUpVelocity(const ResEventInfo& lastAxisEvent, const ResEventInfo& curAxisEvent); 110 111 ReportDataFunc reportDataFunc_ = nullptr; 112 ReportSyncEventFunc reportSyncEventFunc_ = nullptr; 113 CancelableCallback<void()> delayTask_; 114 bool loadPageOn_ = false; 115 bool loadPageRequestFrameOn_ = false; 116 ResEventInfo curTouchEvent_; 117 ResEventInfo lastTouchEvent_; 118 ResEventInfo curAxisEvent_; 119 ResEventInfo lastAxisEvent_; 120 static thread_local Offset averageDistance_; 121 static thread_local bool isInSlide_; 122 static thread_local bool isInTouch_; 123 double dpi_ = PipelineBase::GetCurrentDensity(); 124 std::chrono::steady_clock::time_point startTimeMS = std::chrono::steady_clock::now(); 125 std::chrono::steady_clock::time_point startTimeS = std::chrono::steady_clock::now(); 126 int64_t keyEventCountMS = -1; 127 int64_t keyEventCountS = -1; 128 }; 129 130 class ACE_EXPORT ResSchedReportScope final { 131 public: 132 ACE_DISALLOW_COPY_AND_MOVE(ResSchedReportScope); 133 134 explicit ResSchedReportScope(const std::string& name, 135 const std::unordered_map<std::string, std::string>& param = {}); 136 ~ResSchedReportScope(); 137 138 private: 139 std::string name_; 140 std::unordered_map<std::string, std::string> payload_; 141 }; 142 } // namespace OHOS::Ace 143 144 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_RESSCHED_RESSCHED_REPORT_H 145