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