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 "adapter/ohos/entrance/ace_container.h"
19 #include "core/common/ace_application_info.h"
20 #include "base/perfmonitor/perf_constants.h"
21
22 namespace OHOS::Ace {
23 using namespace std;
24 namespace {
25 const std::string APS_CLIENT_SO = "libaps_client.z.so";
26 }
27
~ApsMonitorImpl()28 ApsMonitorImpl::~ApsMonitorImpl()
29 {
30 if (loadfilehandle_ != nullptr) {
31 dlclose(loadfilehandle_);
32 loadfilehandle_ = nullptr;
33 }
34 }
35
36 const set<string> ApsMonitorImpl::apsScenes = {
37 PerfConstants::ABILITY_OR_PAGE_SWITCH,
38 };
39
SetApsScene(const string & sceneName,bool onOff)40 void ApsMonitorImpl::SetApsScene(const string& sceneName, bool onOff)
41 {
42 auto container = Container::GetContainer(instanceId_);
43 CHECK_NULL_VOID(container);
44 string bundleName = container->GetBundleName();
45 if (apsScenes.find(sceneName) == apsScenes.end()) {
46 return;
47 }
48 #ifdef APS_ENABLE
49 LoadApsFuncOnce();
50 if (setFunc_ == nullptr) {
51 LOGE("[ApsMonitorImpl]ApsManager setFunction failed!");
52 return;
53 }
54 uint32_t OnOff = static_cast<uint32_t>(onOff);
55 setFunc_(bundleName, sceneName, OnOff);
56 #endif
57 return;
58 }
59
LoadApsFuncOnce()60 void ApsMonitorImpl::LoadApsFuncOnce()
61 {
62 if (isloadapsfunc_) {
63 return;
64 }
65
66 loadfilehandle_ = dlopen(APS_CLIENT_SO.c_str(), RTLD_NOW);
67 if (loadfilehandle_ == nullptr) {
68 LOGE("[ApsMonitorImpl]ApsManager handle loaded failed!");
69 return;
70 }
71
72 setFunc_ = reinterpret_cast<SetSceneFunc>(dlsym(loadfilehandle_, "SetApsScene"));
73 if (setFunc_ == nullptr) {
74 LOGE("[ApsMonitorImpl]ApsManager Function loaded failed!");
75 dlclose(loadfilehandle_);
76 return;
77 }
78 isloadapsfunc_ = true;
79 }
80 } // namespace OHOS::Ace