• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 #define LOG_TAG "HiViewAdapter"
17 
18 #include "hiview_adapter.h"
19 
20 #include <thread>
21 
22 #include "def.h"
23 #include "pasteboard_hilog.h"
24 #include "pasteboard_error.h"
25 #include "time_service_client.h"
26 
27 namespace OHOS {
28 using namespace HiviewDFX;
29 namespace MiscServices {
30 namespace {
31 const std::map<int, std::string> EVENT_COVERT_TABLE = {
32     { DfxCodeConstant::PASTEBOARD_FAULT, "PASTEBOARD_FAULT" },
33     { DfxCodeConstant::TIME_CONSUMING_STATISTIC, "TIME_CONSUMING_STATISTIC" },
34     { DfxCodeConstant::PASTEBOARD_BEHAVIOUR, "PASTEBOARD_BEHAVIOUR" },
35     { DfxCodeConstant::USE_BEHAVIOUR, "USE_BEHAVIOUR" },
36 };
37 } // namespace
38 
39 bool HiViewAdapter::running_ = false;
40 std::mutex HiViewAdapter::runMutex_;
41 std::mutex HiViewAdapter::timeConsumingMutex_;
42 std::mutex HiViewAdapter::behaviourMutex_;
43 std::vector<std::map<int, int>> HiViewAdapter::copyTimeConsumingStat_;
44 std::vector<std::map<int, int>> HiViewAdapter::pasteTimeConsumingStat_;
45 std::vector<std::map<int, int>> HiViewAdapter::remotePasteTimeConsumingStat_;
46 
47 std::map<std::string, int> HiViewAdapter::copyPasteboardBehaviour_;
48 std::map<std::string, int> HiViewAdapter::pastePasteboardBehaviour_;
49 std::map<std::string, int> HiViewAdapter::remotePastePasteboardBehaviour_;
50 
51 std::map<int, int> HiViewAdapter::dataMap_ = HiViewAdapter::InitDataMap();
52 std::map<int, int> HiViewAdapter::timeMap_ = HiViewAdapter::InitTimeMap();
53 
InitDataMap()54 std::map<int, int> HiViewAdapter::InitDataMap()
55 {
56     std::map<int, int> dataMap;
57     dataMap.insert(std::pair<int, int>(
58         static_cast<int>(DataRange::DR_ZERO_TO_HUNDRED_KB), static_cast<int>(DataConsumingLevel::DATA_LEVEL_ONE)));
59     dataMap.insert(std::pair<int, int>(static_cast<int>(DataRange::DR_HUNDRED_TO_FIVE_HUNDREDS_KB),
60         static_cast<int>(DataConsumingLevel::DATA_LEVEL_TWO)));
61     dataMap.insert(std::pair<int, int>(static_cast<int>(DataRange::DR_FIVE_HUNDREDS_TO_THOUSAND_KB),
62         static_cast<int>(DataConsumingLevel::DATA_LEVEL_THREE)));
63     dataMap.insert(std::pair<int, int>(
64         static_cast<int>(DataRange::DR_ONE_TO_FIVE_MB), static_cast<int>(DataConsumingLevel::DATA_LEVEL_FOUR)));
65     dataMap.insert(std::pair<int, int>(
66         static_cast<int>(DataRange::DR_FIVE_TO_TEN_MB), static_cast<int>(DataConsumingLevel::DATA_LEVEL_FIVE)));
67     dataMap.insert(std::pair<int, int>(
68         static_cast<int>(DataRange::DR_TEN_TO_FIFTY_MB), static_cast<int>(DataConsumingLevel::DATA_LEVEL_SIX)));
69     dataMap.insert(std::pair<int, int>(
70         static_cast<int>(DataRange::DR_OVER_FIFTY_MB), static_cast<int>(DataConsumingLevel::DATA_LEVEL_SEVEN)));
71     return dataMap;
72 }
73 
InitTimeMap()74 std::map<int, int> HiViewAdapter::InitTimeMap()
75 {
76     std::map<int, int> timeMap;
77     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ONE),
78         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_ONE)));
79     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TWO),
80         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_TWO)));
81     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_THREE),
82         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_THREE)));
83     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FOUR),
84         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_FOUR)));
85     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FIVE),
86         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_FIVE)));
87     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SIX),
88         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_SIX)));
89     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SEVEN),
90         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_SEVEN)));
91     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_EIGHT),
92         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_EIGHT)));
93     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_NINE),
94         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_NINE)));
95     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TEN),
96         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_TEN)));
97     timeMap.insert(std::pair<int, int>(static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ELEVEN),
98         static_cast<int>(TimeConsumingLevel::TIME_LEVEL_ELEVEN)));
99     return timeMap;
100 }
101 
ReportPasteboardFault(int dfxCode,const PasteboardFaultMsg & msg)102 void HiViewAdapter::ReportPasteboardFault(int dfxCode, const PasteboardFaultMsg &msg)
103 {
104     HiSysEventParam params[] = {
105         {.name = "USER_ID", .t = HISYSEVENT_INT32, .v = { .i32 = msg.userId }, .arraySize = 0, },
106         {.name = "ERROR_TYPE", .t = HISYSEVENT_STRING, .v = { .s = (char *)msg.errorCode.c_str() }, .arraySize = 0, },
107     };
108     size_t len = sizeof(params) / sizeof(params[0]);
109     int ret = OH_HiSysEvent_Write(PASTEBOARD_DOMAIN, CoverEventID(dfxCode).c_str(), HISYSEVENT_FAULT,
110         params, len);
111     if (ret != 0) {
112         PASTEBOARD_HILOGD(
113             PASTEBOARD_MODULE_SERVICE, "hisysevent write failed! ret %{public}d. errCode %{public}d", ret, dfxCode);
114     }
115 }
116 
InitializeTimeConsuming(int initFlag)117 void HiViewAdapter::InitializeTimeConsuming(int initFlag)
118 {
119     constexpr const int DATA_LEVEL_NUMBERS = 7;
120     std::map<int, int> initTimeConsuming;
121     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_ONE), 0));
122     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_TWO), 0));
123     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_THREE), 0));
124     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_FOUR), 0));
125     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_FIVE), 0));
126     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_SIX), 0));
127     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_SEVEN), 0));
128     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_EIGHT), 0));
129     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_NINE), 0));
130     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_TEN), 0));
131     initTimeConsuming.insert(std::pair<int, int>(static_cast<int>(TimeConsumingLevel::TIME_LEVEL_ELEVEN), 0));
132 
133     if (initFlag == INIT_COPY_TIME_CONSUMING) {
134         for (int i = 0; i < DATA_LEVEL_NUMBERS; ++i) {
135             copyTimeConsumingStat_.push_back(initTimeConsuming);
136         }
137     } else if (initFlag == INIT_PASTE_TIME_CONSUMING) {
138         for (int i = 0; i < DATA_LEVEL_NUMBERS; ++i) {
139             pasteTimeConsumingStat_.push_back(initTimeConsuming);
140         }
141     } else {
142         for (int i = 0; i < DATA_LEVEL_NUMBERS; ++i) {
143             remotePasteTimeConsumingStat_.push_back(initTimeConsuming);
144         }
145     }
146 }
147 
ReportTimeConsumingStatistic(const TimeConsumingStat & stat)148 void HiViewAdapter::ReportTimeConsumingStatistic(const TimeConsumingStat &stat)
149 {
150     std::lock_guard<std::mutex> lock(timeConsumingMutex_);
151     if (copyTimeConsumingStat_.empty()) {
152         InitializeTimeConsuming(INIT_COPY_TIME_CONSUMING);
153     }
154     if (pasteTimeConsumingStat_.empty()) {
155         InitializeTimeConsuming(INIT_PASTE_TIME_CONSUMING);
156     }
157     if (remotePasteTimeConsumingStat_.empty()) {
158         InitializeTimeConsuming(INIT_REMOTE_PASTE_TIME_CONSUMING);
159     }
160 
161     if (stat.pasteboardState == static_cast<int>(StatisticPasteboardState::SPS_COPY_STATE)) {
162         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "hisysevent pasteboard state is %{public}d", stat.pasteboardState);
163         auto iter = dataMap_.find(stat.dataSize);
164         if (iter != dataMap_.end()) {
165             CopyTimeConsuming(stat, iter->second);
166         } else {
167             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "wrong data level");
168         }
169     } else if ((stat.pasteboardState == static_cast<int>(StatisticPasteboardState::SPS_PASTE_STATE)) ||
170                (stat.pasteboardState == static_cast<int>(StatisticPasteboardState::SPS_REMOTE_PASTE_STATE))) {
171         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "hisysevent pasteboard state is %{public}d", stat.pasteboardState);
172         auto iter = dataMap_.find(stat.dataSize);
173         if (iter != dataMap_.end()) {
174             PasteTimeConsuming(stat, iter->second);
175         } else {
176             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "wrong data level");
177         }
178     } else {
179         PASTEBOARD_HILOGE(
180             PASTEBOARD_MODULE_SERVICE, "hisysevent wrong pasteboard state! errCode %{public}d", stat.pasteboardState);
181     }
182 }
183 
CopyTimeConsumingCount(int dataLevel,int timeLevel)184 void HiViewAdapter::CopyTimeConsumingCount(int dataLevel, int timeLevel)
185 {
186     if (static_cast<int>(copyTimeConsumingStat_.size()) <= dataLevel) {
187         return;
188     }
189     auto it = copyTimeConsumingStat_[dataLevel].find(timeLevel);
190     if (it != copyTimeConsumingStat_[dataLevel].end()) {
191         (it->second)++;
192     } else {
193         PASTEBOARD_HILOGE(
194             PASTEBOARD_MODULE_SERVICE, "hisysevent wrong copy time level, time level:  %{public}d", timeLevel);
195     }
196 }
197 
PasteTimeConsumingCount(int dataLevel,int timeLevel)198 void HiViewAdapter::PasteTimeConsumingCount(int dataLevel, int timeLevel)
199 {
200     if (static_cast<int>(pasteTimeConsumingStat_.size()) <= dataLevel) {
201         return;
202     }
203     auto it = pasteTimeConsumingStat_[dataLevel].find(timeLevel);
204     if (it != pasteTimeConsumingStat_[dataLevel].end()) {
205         (it->second)++;
206     } else {
207         PASTEBOARD_HILOGE(
208             PASTEBOARD_MODULE_SERVICE, "hisysevent wrong copy time level, time level:  %{public}d", timeLevel);
209     }
210 }
211 
RemotePasteTimeConsumingCount(int dataLevel,int timeLevel)212 void HiViewAdapter::RemotePasteTimeConsumingCount(int dataLevel, int timeLevel)
213 {
214     if (static_cast<int>(remotePasteTimeConsumingStat_.size()) <= dataLevel) {
215         return;
216     }
217     auto it = remotePasteTimeConsumingStat_[dataLevel].find(timeLevel);
218     if (it != remotePasteTimeConsumingStat_[dataLevel].end()) {
219         (it->second)++;
220     } else {
221         PASTEBOARD_HILOGD(
222             PASTEBOARD_MODULE_SERVICE, "hisysevent wrong copy time level, time level:  %{public}d", timeLevel);
223     }
224 }
225 
CopyTimeConsuming(const TimeConsumingStat & stat,int level)226 void HiViewAdapter::CopyTimeConsuming(const TimeConsumingStat &stat, int level)
227 {
228     auto iter = timeMap_.find(stat.timeConsuming);
229     if (iter != timeMap_.end()) {
230         CopyTimeConsumingCount(level, iter->second);
231     } else {
232         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "wrong time level");
233     }
234 }
235 
PasteTimeConsuming(const TimeConsumingStat & stat,int level)236 void HiViewAdapter::PasteTimeConsuming(const TimeConsumingStat &stat, int level)
237 {
238     auto iter = timeMap_.find(stat.timeConsuming);
239     if (iter != timeMap_.end()) {
240         if (stat.pasteboardState == SPS_PASTE_STATE) {
241             PasteTimeConsumingCount(level, iter->second);
242         } else {
243             RemotePasteTimeConsumingCount(level, iter->second);
244         }
245     } else {
246         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "wrong time level");
247     }
248 }
249 
ReportPasteboardBehaviour(const PasteboardBehaviourMsg & msg)250 void HiViewAdapter::ReportPasteboardBehaviour(const PasteboardBehaviourMsg &msg)
251 {
252     std::lock_guard<std::mutex> lock(behaviourMutex_);
253 
254     if (msg.pasteboardState == static_cast<int>(BehaviourPasteboardState::BPS_COPY_STATE)) {
255         auto it = copyPasteboardBehaviour_.find(msg.bundleName);
256         if (it != copyPasteboardBehaviour_.end()) {
257             (it->second)++;
258         } else {
259             copyPasteboardBehaviour_.insert(std::pair<std::string, int>(msg.bundleName, 1));
260         }
261     } else if (msg.pasteboardState == static_cast<int>(BehaviourPasteboardState::BPS_PASTE_STATE)) {
262         auto it = pastePasteboardBehaviour_.find(msg.bundleName);
263         if (it != pastePasteboardBehaviour_.end()) {
264             (it->second)++;
265         } else {
266             pastePasteboardBehaviour_.insert(std::pair<std::string, int>(msg.bundleName, 1));
267         }
268     } else if (msg.pasteboardState == static_cast<int>(BehaviourPasteboardState::BPS_REMOTE_PASTE_STATE)) {
269         auto it = remotePastePasteboardBehaviour_.find(msg.bundleName);
270         if (it != remotePastePasteboardBehaviour_.end()) {
271             (it->second)++;
272         } else {
273             remotePastePasteboardBehaviour_.insert(std::pair<std::string, int>(msg.bundleName, 1));
274         }
275     } else {
276         PASTEBOARD_HILOGD(
277             PASTEBOARD_MODULE_SERVICE, "hisysevent wrong pasteboard state! errCode %{public}d", msg.pasteboardState);
278     }
279 }
280 
GetDataLevel(int dataLevel)281 const char *HiViewAdapter::GetDataLevel(int dataLevel)
282 {
283     constexpr const char *WRONG_LEVEL = "WRONG_LEVEL";
284     switch (dataLevel) {
285         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_ONE): {
286             return ZERO_TO_HUNDRED_KB;
287         }
288         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_TWO): {
289             return HUNDRED_TO_FIVE_HUNDREDS_KB;
290         }
291         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_THREE): {
292             return FIVE_HUNDREDS_TO_THOUSAND_KB;
293         }
294         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_FOUR): {
295             return ONE_TO_FIVE_MB;
296         }
297         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_FIVE): {
298             return FIVE_TO_TEN_MB;
299         }
300         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_SIX): {
301             return TEN_TO_FIFTY_MB;
302         }
303         case static_cast<int>(DataConsumingLevel::DATA_LEVEL_SEVEN): {
304             return OVER_FIFTY_MB;
305         }
306         default: {
307             return WRONG_LEVEL;
308         }
309     }
310 }
311 
InvokeTimeConsuming()312 void HiViewAdapter::InvokeTimeConsuming()
313 {
314     std::lock_guard<std::mutex> lock(timeConsumingMutex_);
315     ReportStatisticEvent(copyTimeConsumingStat_, COPY_STATE);
316     copyTimeConsumingStat_.clear();
317     ReportStatisticEvent(pasteTimeConsumingStat_, PASTE_STATE);
318     pasteTimeConsumingStat_.clear();
319     ReportStatisticEvent(remotePasteTimeConsumingStat_, REMOTE_PASTE_STATE);
320     remotePasteTimeConsumingStat_.clear();
321 }
322 
ReportStatisticEvent(const std::vector<std::map<int,int>> & timeConsumingStat,const std::string & pasteboardState)323 void HiViewAdapter::ReportStatisticEvent(
324     const std::vector<std::map<int, int>> &timeConsumingStat, const std::string &pasteboardState)
325 {
326     if (timeConsumingStat.empty()) {
327         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "hisysevent timeConsumingStat is empty.");
328         return;
329     }
330     for (std::int32_t i = 0; i < static_cast<int>(timeConsumingStat.size()); ++i) {
331         std::string buffMsg = ": [";
332         for (std::int32_t j = TimeConsumingLevel::TIME_LEVEL_ONE; j <= TimeConsumingLevel::TIME_LEVEL_ELEVEN; ++j) {
333             buffMsg = buffMsg + std::to_string(timeConsumingStat[i].at(j)) + ",";
334         }
335         buffMsg += "]";
336 
337         int ret = -1;
338         if (pasteboardState == REMOTE_PASTE_STATE) {
339             std::string netType = "WIFI";
340             HiSysEventParam params[] = {
341                 {.name = {"PASTEBOARD_STATE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)pasteboardState.c_str()},
342                     .arraySize = 0, },
343                 {.name = {"NET_TYPE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)netType.c_str()}, .arraySize = 0, },
344                 {.name = {"DATA_LEVEL"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)GetDataLevel(i)}, .arraySize = 0},
345                 {.name = {"CONSUMING_DATA"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)buffMsg.c_str()},
346                     .arraySize = 0, },
347             };
348             size_t len = sizeof(params) / sizeof(params[0]);
349             ret = OH_HiSysEvent_Write(PASTEBOARD_DOMAIN,
350                 CoverEventID(DfxCodeConstant::TIME_CONSUMING_STATISTIC).c_str(), HISYSEVENT_STATISTIC, params, len);
351         } else {
352             HiSysEventParam params[] = {
353                 {.name = {"PASTEBOARD_STATE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)pasteboardState.c_str()},
354                     .arraySize = 0, },
355                 {.name = {"DATA_LEVEL"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)GetDataLevel(i)}, .arraySize = 0},
356                 {.name = {"CONSUMING_DATA"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)buffMsg.c_str()},
357                     .arraySize = 0, },
358             };
359             size_t len = sizeof(params) / sizeof(params[0]);
360             ret = OH_HiSysEvent_Write(PASTEBOARD_DOMAIN,
361                 CoverEventID(DfxCodeConstant::TIME_CONSUMING_STATISTIC).c_str(), HISYSEVENT_STATISTIC, params, len);
362         }
363         if (ret != HiviewDFX::SUCCESS) {
364             PASTEBOARD_HILOGD(
365                 PASTEBOARD_MODULE_SERVICE, "hisysevent write failed! ret = %{public}d, i = %{public}d.", ret, i);
366         }
367     }
368 }
369 
ReportBehaviour(std::map<std::string,int> & behaviour,const char * pasteboardState)370 void HiViewAdapter::ReportBehaviour(std::map<std::string, int> &behaviour, const char *pasteboardState)
371 {
372     if (behaviour.empty()) {
373         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "behaviour is empty!");
374         return;
375     }
376     std::vector<std::pair<std::string, int>> vec;
377     constexpr const int TOTAL_APP_NUMBERS = 10;
378     for (auto it = behaviour.begin(); it != behaviour.end(); ++it) {
379         vec.push_back(std::pair<std::string, int>(it->first, it->second));
380     }
381     sort(vec.begin(), vec.end(),
382          [](std::pair<std::string, int> a, std::pair<std::string, int> b) { return a.second > b.second; });
383     std::vector<std::string> appPackName;
384     for (int i = 0; i < TOTAL_APP_NUMBERS; ++i) {
385         appPackName.push_back("default");
386     }
387     int index = 0;
388     for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
389         appPackName[index] = iter->first + " :" + std::to_string(iter->second);
390         ++index;
391         if (index >= TOTAL_APP_NUMBERS) {
392             break;
393         }
394     }
395     HiSysEventParam params[] = {
396         {.name = {"PASTEBOARD_STATE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)pasteboardState}, .arraySize = 0},
397         {.name = {"TOP_ONE_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[0].c_str()}, .arraySize = 0},
398         {.name = {"TOP_TOW_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[1].c_str()}, .arraySize = 0},
399         {.name = {"TOP_THREE_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[2].c_str()},
400             .arraySize = 0},
401         {.name = {"TOP_FOUR_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[3].c_str()}, .arraySize = 0},
402         {.name = {"TOP_FIVE_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[4].c_str()}, .arraySize = 0},
403         {.name = {"TOP_SIX_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[5].c_str()}, .arraySize = 0},
404         {.name = {"TOP_SEVEN_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[6].c_str()},
405             .arraySize = 0},
406         {.name = {"TOP_EIGHT_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[7].c_str()},
407             .arraySize = 0},
408         {.name = {"TOP_NINE_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[8].c_str()}, .arraySize = 0},
409         {.name = {"TOP_TEN_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appPackName[9].c_str()}, .arraySize = 0},
410     };
411     int ret = OH_HiSysEvent_Write(PASTEBOARD_DOMAIN, CoverEventID(DfxCodeConstant::PASTEBOARD_BEHAVIOUR).c_str(),
412         HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
413     if (ret != HiviewDFX::SUCCESS) {
414         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "hisysevent write failed! ret %{public}d.", ret);
415     }
416 }
417 
InvokePasteBoardBehaviour()418 void HiViewAdapter::InvokePasteBoardBehaviour()
419 {
420     std::lock_guard<std::mutex> lock(behaviourMutex_);
421     if (!copyPasteboardBehaviour_.empty()) {
422         ReportBehaviour(copyPasteboardBehaviour_, COPY_STATE);
423         copyPasteboardBehaviour_.clear();
424     } else {
425         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "InvokePasteBoardBehaviour :copyPasteboardBehaviour_ is empty ");
426     }
427     if (!pastePasteboardBehaviour_.empty()) {
428         ReportBehaviour(pastePasteboardBehaviour_, PASTE_STATE);
429         pastePasteboardBehaviour_.clear();
430     } else {
431         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "InvokePasteBoardBehaviour :pastePasteboardBehaviour_ is empty ");
432     }
433     if (!remotePastePasteboardBehaviour_.empty()) {
434         ReportBehaviour(remotePastePasteboardBehaviour_, REMOTE_PASTE_STATE);
435         remotePastePasteboardBehaviour_.clear();
436     } else {
437         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "remotePastePasteboardBehaviour_ is empty ");
438     }
439 }
440 
StartTimerThread()441 void HiViewAdapter::StartTimerThread()
442 {
443     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread enter");
444     std::lock_guard<std::mutex> lock(runMutex_);
445     if (running_) {
446         return;
447     }
448     running_ = true;
449     auto fun = []() {
450         while (true) {
451             PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread while");
452             time_t current = time(nullptr);
453             if (current == -1) {
454                 sleep(ONE_HOUR_IN_SECONDS);
455                 continue;
456             }
457 
458             tm localTime = { 0 };
459             tm *result = localtime_r(&current, &localTime);
460             if (result == nullptr) {
461                 sleep(ONE_HOUR_IN_SECONDS);
462                 continue;
463             }
464             int currentHour = localTime.tm_hour;
465             int currentMin = localTime.tm_min;
466             PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread get");
467             if ((EXEC_MIN_TIME - currentMin) != EXEC_MIN_TIME) {
468                 int nHours = EXEC_HOUR_TIME - currentHour;
469                 int nMin = EXEC_MIN_TIME - currentMin;
470                 int nTime = (nMin)*ONE_MINUTE_IN_SECONDS + (nHours)*ONE_HOUR_IN_SECONDS;
471                 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE,
472                     " StartTimerThread if needHours=%{public}d,needMin=%{public}d,needTime=%{public}d", nHours, nMin,
473                     nTime);
474                 sleep(nTime);
475                 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread invoke");
476                 InvokePasteBoardBehaviour();
477                 InvokeTimeConsuming();
478             } else {
479                 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread sleep");
480                 sleep(ONE_HOUR_IN_SECONDS * (ONE_DAY_IN_HOURS - currentHour));
481                 current = time(nullptr);
482                 InvokePasteBoardBehaviour();
483                 InvokeTimeConsuming();
484             }
485             PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "StartTimerThread end");
486         }
487     };
488     std::thread th = std::thread(fun);
489     pthread_setname_np(th.native_handle(), "HiViewReport");
490     th.detach();
491 }
492 
ReportUseBehaviour(PasteData & pastData,const char * state,int32_t result)493 void HiViewAdapter::ReportUseBehaviour(PasteData& pastData, const char* state, int32_t result)
494 {
495     std::string stateStr = state;
496     std::string bundleName = pastData.GetBundleName();
497     std::string primaryMimeType = pastData.GetPrimaryMimeType() != nullptr? *pastData.GetPrimaryMimeType() : "null";
498     std::string shareOption;
499     PasteData::ShareOptionToString(pastData.GetShareOption(), shareOption);
500     auto isLocalPaste = pastData.IsLocalPaste();
501     auto isRemote = pastData.IsRemote();
502     std::thread thread([bundleName, primaryMimeType, shareOption, isLocalPaste, isRemote, stateStr, result]() {
503         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start.");
504         auto iter = PasteboardErrorMap.find(PasteboardError(result));
505         const char *appRet;
506         if (iter != PasteboardErrorMap.end()) {
507             appRet = iter->second;
508         } else {
509             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "Match error result: %{public}d.", result);
510             appRet = "MATCH ERROR";
511         }
512         HiSysEventParam params[] = {
513             {.name = {"PASTEBOARD_STATE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)stateStr.c_str()},
514                 .arraySize = 0, },
515             {.name = {"BOOTTIME"}, .t = HISYSEVENT_INT64,
516                 .v = { .i64 = TimeServiceClient::GetInstance()->GetBootTimeMs()}, .arraySize = 0, },
517             {.name = {"WALLTIME"}, .t = HISYSEVENT_INT64,
518                 .v = { .i64 = TimeServiceClient::GetInstance()->GetWallTimeMs()}, .arraySize = 0, },
519 
520             {.name = {"RESULT"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)appRet}, .arraySize = 0, },
521             {.name = {"OPERATE_APP"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)bundleName.c_str()}, .arraySize = 0},
522             {.name = {"PRI_MIME_TYPE"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)primaryMimeType.c_str()},
523                 .arraySize = 0, },
524             {.name = {"ISLOCALPASTE"}, .t = HISYSEVENT_BOOL, .v = { .b = isLocalPaste}, .arraySize = 0, },
525             {.name = {"ISREMOTE"}, .t = HISYSEVENT_BOOL, .v = { .b = isRemote}, .arraySize = 0, },
526             {.name = {"SHAREOPTION"}, .t = HISYSEVENT_STRING, .v = { .s = (char *)shareOption.c_str()},
527                 .arraySize = 0, },
528         };
529         size_t len = sizeof(params) / sizeof(params[0]);
530         int ret = OH_HiSysEvent_Write(PASTEBOARD_DOMAIN, CoverEventID(DfxCodeConstant::USE_BEHAVIOUR).c_str(),
531             HISYSEVENT_BEHAVIOR, params, len);
532         if (ret != HiviewDFX::SUCCESS) {
533             PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "hisysevent write failed! ret %{public}d.", ret);
534         }
535         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "end.");
536     });
537     thread.detach();
538 }
539 
CoverEventID(int dfxCode)540 std::string HiViewAdapter::CoverEventID(int dfxCode)
541 {
542     std::string sysEventID = "";
543     auto operatorIter = EVENT_COVERT_TABLE.find(dfxCode);
544     if (operatorIter != EVENT_COVERT_TABLE.end()) {
545         sysEventID = operatorIter->second;
546     }
547     return sysEventID;
548 }
549 } // namespace MiscServices
550 } // namespace OHOS
551