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 #include "core/common/ace_application_info.h"
17 #include "base/log/log.h"
18 #include "base/ressched/ressched_report.h"
19
20 #define LIKELY(x) __builtin_expect(!!(x), 1)
21
22 namespace OHOS::Ace {
23 namespace {
24 constexpr uint32_t RES_TYPE_CLICK_RECOGNIZE = 9;
25 constexpr uint32_t RES_TYPE_PUSH_PAGE = 10;
26 constexpr uint32_t RES_TYPE_SLIDE = 11;
27 constexpr uint32_t RES_TYPE_POP_PAGE = 28;
28 constexpr uint32_t RES_TYPE_WEB_GESTURE = 29;
29 constexpr uint32_t RES_TYPE_LOAD_PAGE = 34;
30 #ifdef FFRT_EXISTS
31 constexpr uint32_t RES_TYPE_LONG_FRAME = 71;
32 #endif
33 constexpr int32_t TOUCH_EVENT = 1;
34 constexpr int32_t CLICK_EVENT = 2;
35 constexpr int32_t SLIDE_OFF_EVENT = 0;
36 constexpr int32_t SLIDE_ON_EVENT = 1;
37 constexpr int32_t PUSH_PAGE_START_EVENT = 0;
38 constexpr int32_t PUSH_PAGE_COMPLETE_EVENT = 1;
39 constexpr int32_t POP_PAGE_EVENT = 0;
40 #ifdef FFRT_EXISTS
41 constexpr int32_t LONG_FRAME_START_EVENT = 0;
42 constexpr int32_t LONG_FRAME_END_EVENT = 1;
43 #endif
44 constexpr char NAME[] = "name";
45 constexpr char PID[] = "pid";
46 constexpr char UID[] = "uid";
47 constexpr char BUNDLE_NAME[] = "bundleName";
48 constexpr char ABILITY_NAME[] = "abilityName";
49 constexpr char CLICK[] = "click";
50 constexpr char PUSH_PAGE[] = "push_page";
51 constexpr char POP_PAGE[] = "pop_page";
52 constexpr char SLIDE_ON[] = "slide_on";
53 constexpr char SLIDE_OFF[] = "slide_off";
54 constexpr char TOUCH[] = "touch";
55 constexpr char WEB_GESTURE[] = "web_gesture";
56 constexpr char LOAD_PAGE[] = "load_page";
57 #ifdef FFRT_EXISTS
58 constexpr char LONG_FRAME_START[] = "long_frame_start";
59 constexpr char LONG_FRAME_END[] = "long_frame_end";
60 #endif
61
LoadAceApplicationContext(std::unordered_map<std::string,std::string> & payload)62 void LoadAceApplicationContext(std::unordered_map<std::string, std::string>& payload)
63 {
64 auto& aceApplicationInfo = AceApplicationInfo::GetInstance();
65 payload[PID] = std::to_string(aceApplicationInfo.GetPid());
66 payload[UID] = std::to_string(aceApplicationInfo.GetUid());
67 payload[BUNDLE_NAME] = aceApplicationInfo.GetPackageName();
68 payload[ABILITY_NAME] = aceApplicationInfo.GetAbilityName();
69 }
70 }
71
GetInstance()72 ResSchedReport& ResSchedReport::GetInstance()
73 {
74 static ResSchedReport instance;
75 return instance;
76 }
77
ResSchedDataReport(const char * name,const std::unordered_map<std::string,std::string> & param)78 void ResSchedReport::ResSchedDataReport(const char* name, const std::unordered_map<std::string, std::string>& param)
79 {
80 std::unordered_map<std::string, std::string> payload = param;
81 payload[NAME] = name;
82 if (!reportDataFunc_) {
83 reportDataFunc_ = LoadReportDataFunc();
84 }
85 if (!reportDataFunc_) {
86 return;
87 }
88 static std::unordered_map<std::string, std::function<void(std::unordered_map<std::string, std::string>&)>>
89 functionMap = {
90 { CLICK,
91 [this](std::unordered_map<std::string, std::string>& payload) {
92 reportDataFunc_(RES_TYPE_CLICK_RECOGNIZE, CLICK_EVENT, payload);
93 }
94 },
95 { SLIDE_ON,
96 [this](std::unordered_map<std::string, std::string>& payload) {
97 reportDataFunc_(RES_TYPE_SLIDE, SLIDE_ON_EVENT, payload);
98 }
99 },
100 { SLIDE_OFF,
101 [this](std::unordered_map<std::string, std::string>& payload) {
102 reportDataFunc_(RES_TYPE_SLIDE, SLIDE_OFF_EVENT, payload);
103 }
104 },
105 { POP_PAGE,
106 [this](std::unordered_map<std::string, std::string>& payload) {
107 LoadAceApplicationContext(payload);
108 reportDataFunc_(RES_TYPE_POP_PAGE, POP_PAGE_EVENT, payload);
109 }
110 },
111 { WEB_GESTURE,
112 [this](std::unordered_map<std::string, std::string>& payload) {
113 reportDataFunc_(RES_TYPE_WEB_GESTURE, 0, payload);
114 }
115 },
116 #ifdef FFRT_EXISTS
117 { LONG_FRAME_START,
118 [this](std::unordered_map<std::string, std::string>& payload) {
119 LoadAceApplicationContext(payload);
120 reportDataFunc_(RES_TYPE_LONG_FRAME, LONG_FRAME_START_EVENT, payload);
121 }
122 },
123 { LONG_FRAME_END,
124 [this](std::unordered_map<std::string, std::string>& payload) {
125 LoadAceApplicationContext(payload);
126 reportDataFunc_(RES_TYPE_LONG_FRAME, LONG_FRAME_END_EVENT, payload);
127 }
128 },
129 #endif
130 };
131 auto it = functionMap.find(name);
132 if (it == functionMap.end()) {
133 return;
134 }
135 it->second(payload);
136 }
137
ResSchedDataReport(uint32_t resType,int32_t value,const std::unordered_map<std::string,std::string> & payload)138 void ResSchedReport::ResSchedDataReport(uint32_t resType, int32_t value,
139 const std::unordered_map<std::string, std::string>& payload)
140 {
141 if (reportDataFunc_ == nullptr) {
142 reportDataFunc_ = LoadReportDataFunc();
143 }
144 if (reportDataFunc_ != nullptr) {
145 reportDataFunc_(resType, value, payload);
146 }
147 }
148
OnTouchEvent(const TouchType & touchType)149 void ResSchedReport::OnTouchEvent(const TouchType& touchType)
150 {
151 if (touchType == TouchType::DOWN || touchType == TouchType::UP) {
152 std::unordered_map<std::string, std::string> payload;
153 payload[NAME] = TOUCH;
154 ResSchedDataReport(RES_TYPE_CLICK_RECOGNIZE, TOUCH_EVENT, payload);
155 }
156 }
157
LoadPageEvent(int32_t value)158 void ResSchedReport::LoadPageEvent(int32_t value)
159 {
160 if (LIKELY(value == ResDefine::LOAD_PAGE_COMPLETE_EVENT && loadPageOn_ == false)) {
161 return;
162 } else if (value == ResDefine::LOAD_PAGE_COMPLETE_EVENT && loadPageOn_ == true) {
163 loadPageOn_ = false;
164 } else if (value == ResDefine::LOAD_PAGE_START_EVENT) {
165 loadPageOn_ = true;
166 }
167
168 std::unordered_map<std::string, std::string> payload;
169 payload[NAME] = LOAD_PAGE;
170 LoadAceApplicationContext(payload);
171 ResSchedDataReport(RES_TYPE_LOAD_PAGE, value, payload);
172 }
173
ResSchedReportScope(const std::string & name,const std::unordered_map<std::string,std::string> & param)174 ResSchedReportScope::ResSchedReportScope(const std::string& name,
175 const std::unordered_map<std::string, std::string>& param) : name_(name), payload_(param)
176 {
177 name_ = name;
178 payload_[NAME] = name;
179 LoadAceApplicationContext(payload_);
180 if (name_ == PUSH_PAGE) {
181 ResSchedReport::GetInstance().ResSchedDataReport(RES_TYPE_PUSH_PAGE, PUSH_PAGE_START_EVENT, payload_);
182 ResSchedReport::GetInstance().LoadPageEvent(ResDefine::LOAD_PAGE_START_EVENT);
183 }
184 }
185
~ResSchedReportScope()186 ResSchedReportScope::~ResSchedReportScope()
187 {
188 if (name_ == PUSH_PAGE) {
189 ResSchedReport::GetInstance().ResSchedDataReport(RES_TYPE_PUSH_PAGE, PUSH_PAGE_COMPLETE_EVENT, payload_);
190 }
191 }
192 } // namespace OHOS::Ace
193