1 /*
2  * Copyright (c) 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 "soc_perf_client_adapter_impl.h"
17 
18 #include "nweb_log.h"
19 #if defined(NWEB_SOC_PERF)
20 #include "res_sched_client.h"
21 #include "res_type.h"
22 #include "socperf_client.h"
23 #endif
24 
25 namespace OHOS::NWeb {
26 namespace {
27 #if defined(NWEB_SOC_PERF)
28     const uint32_t SOC_PERF_INVALID = UINT32_MAX;
29 #endif
30 }
31 
SocPerfClientAdapterImpl()32 SocPerfClientAdapterImpl::SocPerfClientAdapterImpl()
33 {
34 #if defined(NWEB_SOC_PERF)
35     convertMap_ = {
36         { SOC_PERF_WEB_GESTURE_ID, ResourceSchedule::ResType::RES_TYPE_WEB_GESTURE },
37         { SOC_PERF_WEB_GESTURE_MOVE_ID, ResourceSchedule::ResType::RES_TYPE_WEB_GESTURE_MOVE },
38         { SOC_PERF_SLIDE_NORMAL_ID, ResourceSchedule::ResType::RES_TYPE_WEB_SLIDE_NORMAL },
39         { SOC_PERF_LOAD_URL_ID, ResourceSchedule::ResType::RES_TYPE_LOAD_URL },
40         { SOC_PERF_MOUSEWHEEL_ID, ResourceSchedule::ResType::RES_TYPE_MOUSEWHEEL },
41     };
42 #endif
43 }
44 
~SocPerfClientAdapterImpl()45 SocPerfClientAdapterImpl::~SocPerfClientAdapterImpl()
46 {
47     convertMap_.clear();
48 }
49 
ApplySocPerfConfigById(int32_t id)50 void SocPerfClientAdapterImpl::ApplySocPerfConfigById(int32_t id)
51 {
52 #if defined(NWEB_SOC_PERF)
53     uint32_t resType = ConvertId(id);
54     if (resType == SOC_PERF_INVALID) {
55         return;
56     }
57 
58     std::unordered_map<std::string, std::string> mapPayload;
59     OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, SOC_PERF_START, mapPayload);
60 #endif
61 }
62 
ApplySocPerfConfigByIdEx(int32_t id,bool onOffTag)63 void SocPerfClientAdapterImpl::ApplySocPerfConfigByIdEx(int32_t id, bool onOffTag)
64 {
65 #if defined(NWEB_SOC_PERF)
66     uint32_t resType = ConvertId(id);
67     if (resType == SOC_PERF_INVALID) {
68         return;
69     }
70 
71     std::unordered_map<std::string, std::string> mapPayload;
72     int64_t value = onOffTag ? SOC_PERF_START : SOC_PERF_END;
73     OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, value, mapPayload);
74 #endif
75 }
76 
ConvertId(int32_t id)77 uint32_t SocPerfClientAdapterImpl::ConvertId(int32_t id)
78 {
79 #if defined(NWEB_SOC_PERF)
80     auto mit = convertMap_.find(id);
81     if (mit == convertMap_.end()) {
82         WVLOG_E("invalid id: %{public}d", id);
83         return SOC_PERF_INVALID;
84     }
85     return mit->second;
86 #else
87     return 0;
88 #endif
89 }
90 }  // namespace OHOS::NWeb
91