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