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 
~ApsMonitorImpl()27 ApsMonitorImpl::~ApsMonitorImpl()
28 {
29     if (loadfilehandle_ != nullptr) {
30         dlclose(loadfilehandle_);
31         loadfilehandle_ = nullptr;
32     }
33 }
34 
35 const set<string> ApsMonitorImpl::apsScenes = {
36     PerfConstants::ABILITY_OR_PAGE_SWITCH,
37 };
38 
SetApsScene(const string & sceneName,bool onOff)39 void ApsMonitorImpl::SetApsScene(const string& sceneName, bool onOff)
40 {
41     string bundleName = AceApplicationInfo::GetInstance().GetPackageName();
42     if (apsScenes.find(sceneName) == apsScenes.end()) {
43         return;
44     }
45 #ifdef APS_ENABLE
46     LoadApsFuncOnce();
47     if (setFunc_ == nullptr) {
48         LOGE("[ApsMonitorImpl]ApsManager setFunction failed!");
49         return;
50     }
51     uint32_t OnOff = static_cast<uint32_t>(onOff);
52     setFunc_(bundleName, sceneName, OnOff);
53 #endif
54     return;
55 }
56 
LoadApsFuncOnce()57 void ApsMonitorImpl::LoadApsFuncOnce()
58 {
59     if (isloadapsfunc_) {
60         return;
61     }
62 
63     loadfilehandle_ = dlopen(APS_CLIENT_SO.c_str(), RTLD_NOW);
64     if (loadfilehandle_ == nullptr) {
65         LOGE("[ApsMonitorImpl]ApsManager handle loaded failed!");
66         return;
67     }
68 
69     setFunc_ = reinterpret_cast<SetSceneFunc>(dlsym(loadfilehandle_, "SetApsScene"));
70     if (setFunc_ == nullptr) {
71         LOGE("[ApsMonitorImpl]ApsManager Function loaded failed!");
72         dlclose(loadfilehandle_);
73         return;
74     }
75     isloadapsfunc_ = true;
76 }
77 } // namespace OHOS::Ace