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 constexpr int32_t TOUCH_EVENT = 1;
31 constexpr int32_t CLICK_EVENT = 2;
32 constexpr int32_t SLIDE_OFF_EVENT = 0;
33 constexpr int32_t SLIDE_ON_EVENT = 1;
34 constexpr int32_t PUSH_PAGE_START_EVENT = 0;
35 constexpr int32_t PUSH_PAGE_COMPLETE_EVENT = 1;
36 constexpr int32_t POP_PAGE_EVENT = 0;
37 constexpr char NAME[] = "name";
38 constexpr char PID[] = "pid";
39 constexpr char UID[] = "uid";
40 constexpr char BUNDLE_NAME[] = "bundleName";
41 constexpr char ABILITY_NAME[] = "abilityName";
42 constexpr char CLICK[] = "click";
43 constexpr char PUSH_PAGE[] = "push_page";
44 constexpr char POP_PAGE[] = "pop_page";
45 constexpr char SLIDE_ON[] = "slide_on";
46 constexpr char SLIDE_OFF[] = "slide_off";
47 constexpr char TOUCH[] = "touch";
48 constexpr char WEB_GESTURE[] = "web_gesture";
49 constexpr char LOAD_PAGE[] = "load_page";
50
LoadAceApplicationContext(std::unordered_map<std::string,std::string> & payload)51 void LoadAceApplicationContext(std::unordered_map<std::string, std::string>& payload)
52 {
53 auto& aceApplicationInfo = AceApplicationInfo::GetInstance();
54 payload[PID] = std::to_string(aceApplicationInfo.GetPid());
55 payload[UID] = std::to_string(aceApplicationInfo.GetUid());
56 payload[BUNDLE_NAME] = aceApplicationInfo.GetPackageName();
57 payload[ABILITY_NAME] = aceApplicationInfo.GetAbilityName();
58 #ifdef ACE_DEBUG_LOG
59 for (auto& pair : payload) {
60 LOGD("DataReport: %{public}s : %{public}s", pair.first.c_str(), pair.second.c_str());
61 }
62 #endif
63 }
64 }
65
GetInstance()66 ResSchedReport& ResSchedReport::GetInstance()
67 {
68 static ResSchedReport instance;
69 return instance;
70 }
71
ResSchedDataReport(const char * name,const std::unordered_map<std::string,std::string> & param)72 void ResSchedReport::ResSchedDataReport(const char* name, const std::unordered_map<std::string, std::string>& param)
73 {
74 std::unordered_map<std::string, std::string> payload = param;
75 payload[NAME] = name;
76 if (!reportDataFunc_) {
77 reportDataFunc_ = LoadReportDataFunc();
78 }
79 if (!reportDataFunc_) {
80 return;
81 }
82 static std::unordered_map<std::string, std::function<void(std::unordered_map<std::string, std::string>&)>>
83 functionMap = {
84 { CLICK,
85 [this](std::unordered_map<std::string, std::string>& payload) {
86 reportDataFunc_(RES_TYPE_CLICK_RECOGNIZE, CLICK_EVENT, payload);
87 }
88 },
89 { SLIDE_ON,
90 [this](std::unordered_map<std::string, std::string>& payload) {
91 reportDataFunc_(RES_TYPE_SLIDE, SLIDE_ON_EVENT, payload);
92 }
93 },
94 { SLIDE_OFF,
95 [this](std::unordered_map<std::string, std::string>& payload) {
96 reportDataFunc_(RES_TYPE_SLIDE, SLIDE_OFF_EVENT, payload);
97 }
98 },
99 { POP_PAGE,
100 [this](std::unordered_map<std::string, std::string>& payload) {
101 LoadAceApplicationContext(payload);
102 reportDataFunc_(RES_TYPE_POP_PAGE, POP_PAGE_EVENT, payload);
103 }
104 },
105 { WEB_GESTURE,
106 [this](std::unordered_map<std::string, std::string>& payload) {
107 reportDataFunc_(RES_TYPE_WEB_GESTURE, 0, payload);
108 }
109 },
110 };
111 auto it = functionMap.find(name);
112 if (it == functionMap.end()) {
113 LOGE("ResSchedDataReport the name not found: %{public}s", name);
114 return;
115 }
116 it->second(payload);
117 }
118
ResSchedDataReport(uint32_t resType,int32_t value,const std::unordered_map<std::string,std::string> & payload)119 void ResSchedReport::ResSchedDataReport(uint32_t resType, int32_t value,
120 const std::unordered_map<std::string, std::string>& payload)
121 {
122 if (reportDataFunc_ == nullptr) {
123 reportDataFunc_ = LoadReportDataFunc();
124 }
125 if (reportDataFunc_ != nullptr) {
126 reportDataFunc_(resType, value, payload);
127 }
128 }
129
OnTouchEvent(const TouchType & touchType)130 void ResSchedReport::OnTouchEvent(const TouchType& touchType)
131 {
132 if (touchType == TouchType::DOWN || touchType == TouchType::UP) {
133 std::unordered_map<std::string, std::string> payload;
134 payload[NAME] = TOUCH;
135 ResSchedDataReport(RES_TYPE_CLICK_RECOGNIZE, TOUCH_EVENT, payload);
136 }
137 }
138
LoadPageEvent(int32_t value)139 void ResSchedReport::LoadPageEvent(int32_t value)
140 {
141 if (LIKELY(value == ResDefine::LOAD_PAGE_COMPLETE_EVENT && loadPageOn_ == false)) {
142 return;
143 } else if (value == ResDefine::LOAD_PAGE_COMPLETE_EVENT && loadPageOn_ == true) {
144 loadPageOn_ = false;
145 } else if (value == ResDefine::LOAD_PAGE_START_EVENT) {
146 loadPageOn_ = true;
147 }
148
149 std::unordered_map<std::string, std::string> payload;
150 payload[NAME] = LOAD_PAGE;
151 LoadAceApplicationContext(payload);
152 ResSchedDataReport(RES_TYPE_LOAD_PAGE, value, payload);
153 }
154
ResSchedReportScope(const std::string & name,const std::unordered_map<std::string,std::string> & param)155 ResSchedReportScope::ResSchedReportScope(const std::string& name,
156 const std::unordered_map<std::string, std::string>& param) : name_(name), payload_(param)
157 {
158 name_ = name;
159 payload_[NAME] = name;
160 LoadAceApplicationContext(payload_);
161 if (name_ == PUSH_PAGE) {
162 ResSchedReport::GetInstance().ResSchedDataReport(RES_TYPE_PUSH_PAGE, PUSH_PAGE_START_EVENT, payload_);
163 ResSchedReport::GetInstance().LoadPageEvent(ResDefine::LOAD_PAGE_START_EVENT);
164 }
165 }
166
~ResSchedReportScope()167 ResSchedReportScope::~ResSchedReportScope()
168 {
169 if (name_ == PUSH_PAGE) {
170 ResSchedReport::GetInstance().ResSchedDataReport(RES_TYPE_PUSH_PAGE, PUSH_PAGE_COMPLETE_EVENT, payload_);
171 }
172 }
173 } // namespace OHOS::Ace
174