1 /*
2  * Copyright (c) 2024 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 <dlfcn.h>
17 #include "aps_monitor_impl.h"
18 #include "core/common/ace_application_info.h"
19 #include "base/perfmonitor/perf_constants.h"
20 
21 namespace OHOS::Ace {
22 using namespace std;
23 namespace {
24     const std::string APS_CLIENT_SO = "libaps_client.z.so";
25 }
26 
27 const set<string> ApsMonitorImpl::apsScenes = {
28     PerfConstants::ABILITY_OR_PAGE_SWITCH,
29 };
30 
SetApsScene(const string& sceneName, bool onOff)31 void ApsMonitorImpl::SetApsScene(const string& sceneName, bool onOff)
32 {
33     string bundleName = AceApplicationInfo::GetInstance().GetPackageName();
34     if (apsScenes.find(sceneName) == apsScenes.end()) {
35         return;
36     }
37 #ifdef APS_ENABLE
38     LoadApsFuncOnce();
39     if (!setFunc_) {
40         LOGE("[ApsMonitorImpl]ApsManager setFunction failed!");
41         return;
42     }
43     uint32_t OnOff = static_cast<uint32_t>(onOff);
44     setFunc_(bundleName, sceneName, OnOff);
45 #endif
46     return;
47 }
48 
LoadApsFuncOnce()49 void ApsMonitorImpl::LoadApsFuncOnce()
50 {
51     if (isloadapsfunc_) {
52         return;
53     }
54 
55     auto handle = dlopen(APS_CLIENT_SO.c_str(), RTLD_NOW);
56     if (!handle) {
57         LOGE("[ApsMonitorImpl]ApsManager handle loaded failed!");
58         return;
59     }
60 
61     setFunc_ = reinterpret_cast<SetSceneFunc>(dlsym(handle, "SetApsScene"));
62     if (!setFunc_) {
63         LOGE("[ApsMonitorImpl]ApsManager Function loaded failed!");
64         dlclose(handle);
65         return;
66     }
67     isloadapsfunc_ = true;
68 }
69 } // namespace OHOS::Ace