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 "core/components_ng/pattern/window_scene/scene/atomicservice_basic_engine_plugin.h"
17 #include "core/components_ng/pattern/window_scene/scene/window_pattern.h"
18 
19 namespace OHOS::Ace::NG {
20 #ifdef ATOMIC_SERVICE_ATTRIBUTION_ENABLE
GetBundleName() const21 std::string AtomicserviceIconInfo::GetBundleName() const
22 {
23     return bundleName_;
24 }
25 
GetModuleName() const26 std::string AtomicserviceIconInfo::GetModuleName() const
27 {
28     return moduleName_;
29 }
30 
GetAbilityName() const31 std::string AtomicserviceIconInfo::GetAbilityName() const
32 {
33     return abilityName_;
34 }
35 
GetCircleIcon() const36 std::string AtomicserviceIconInfo::GetCircleIcon() const
37 {
38     return circleIcon_;
39 }
40 
GetEyelashRingIcon() const41 std::string AtomicserviceIconInfo::GetEyelashRingIcon() const
42 {
43     return eyelashRingIcon_;
44 }
45 
GetAppName() const46 std::string AtomicserviceIconInfo::GetAppName() const
47 {
48     return appName_;
49 }
50 
SetBundleName(const std::string & val)51 void AtomicserviceIconInfo::SetBundleName(const std::string& val)
52 {
53     bundleName_ = val;
54 }
55 
SetModuleName(const std::string & val)56 void AtomicserviceIconInfo::SetModuleName(const std::string& val)
57 {
58     moduleName_ = val;
59 }
60 
SetAbilityName(const std::string & val)61 void AtomicserviceIconInfo::SetAbilityName(const std::string& val)
62 {
63     abilityName_ = val;
64 }
65 
SetCircleIcon(const std::string & val)66 void AtomicserviceIconInfo::SetCircleIcon(const std::string& val)
67 {
68     circleIcon_ = val;
69 }
70 
SetEyelashRingIcon(const std::string & val)71 void AtomicserviceIconInfo::SetEyelashRingIcon(const std::string& val)
72 {
73     eyelashRingIcon_ = val;
74 }
75 
SetAppName(const std::string & val)76 void AtomicserviceIconInfo::SetAppName(const std::string& val)
77 {
78     appName_ = val;
79 }
80 
Marshalling(Parcel & parcel) const81 bool AtomicserviceIconInfo::Marshalling(Parcel& parcel) const
82 {
83     parcel.WriteString(bundleName_);
84     parcel.WriteString(moduleName_);
85     parcel.WriteString(abilityName_);
86     parcel.WriteString(circleIcon_);
87     parcel.WriteString(eyelashRingIcon_);
88     parcel.WriteString(appName_);
89     return true;
90 }
91 
Unmarshalling(Parcel & parcel)92 AtomicserviceIconInfo *AtomicserviceIconInfo::Unmarshalling(Parcel& parcel)
93 {
94     AtomicserviceIconInfo *data = new AtomicserviceIconInfo();
95     data->SetBundleName(parcel.ReadString());
96     data->SetModuleName(parcel.ReadString());
97     data->SetAbilityName(parcel.ReadString());
98     data->SetCircleIcon(parcel.ReadString());
99     data->SetEyelashRingIcon(parcel.ReadString());
100     data->SetAppName(parcel.ReadString());
101     return data;
102 }
103 
AtomicServiceBasicEnginePlugin()104 AtomicServiceBasicEnginePlugin::AtomicServiceBasicEnginePlugin()
105 {
106     // if ACE_ENGINE_PLUGIN_PATH is defined, load so pack
107 #ifdef ACE_ENGINE_PLUGIN_PATH
108     atomicServiceBasicEngine_ = dlopen(
109         (char*)ACE_ENGINE_PLUGIN_PATH, RTLD_LAZY);
110     if (atomicServiceBasicEngine_ == nullptr) {
111         TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE,
112             "dlopen lib %{public}s false", (char*)ACE_ENGINE_PLUGIN_PATH);
113         return;
114     }
115 #else
116     TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE, "ACE_ENGINE_PLUGIN_PATH is not defined");
117     return;
118 #endif
119     // load function GetAtomicserviceIconInfoPlugin
120     getAtomicserviceIconInfoPlugin = (GetAtomicserviceIconInfoPlugin*)(dlsym(
121         atomicServiceBasicEngine_, "GetAtomicserviceIconInfoPlugin"));
122     if (getAtomicserviceIconInfoPlugin == nullptr) {
123         TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE,
124             "fail to dlsym GetAtomicserviceIconInfoPlugin, reason: %{public}s", dlerror());
125         return;
126     }
127     // load function FreeData
128     freeData = (FreeData*)(dlsym(atomicServiceBasicEngine_, "FreeAtomicserviceIconInfoPlugin"));
129     if (freeData == nullptr) {
130         TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE,
131             "fail to dlsym FreeData, reason: %{public}s", dlerror());
132         return;
133     }
134 }
135 
~AtomicServiceBasicEnginePlugin()136 AtomicServiceBasicEnginePlugin::~AtomicServiceBasicEnginePlugin()
137 {
138     // close dlopen so pack
139     if (atomicServiceBasicEngine_ != nullptr) {
140         dlclose(atomicServiceBasicEngine_);
141         atomicServiceBasicEngine_ = nullptr;
142     }
143     getAtomicserviceIconInfoPlugin = nullptr;
144     freeData = nullptr;
145 }
146 
GetInstance()147 AtomicServiceBasicEnginePlugin& AtomicServiceBasicEnginePlugin::GetInstance()
148 {
149     static AtomicServiceBasicEnginePlugin instance;
150     return instance;
151 }
152 
getParamsFromAtomicServiceBasicEngine(const std::string & bundleName)153 std::vector<std::string> AtomicServiceBasicEnginePlugin::getParamsFromAtomicServiceBasicEngine(
154     const std::string& bundleName)
155 {
156     std::lock_guard<std::mutex> lock(mutex_);
157     std::vector<std::string> atomicServiceBasicInfo;
158     if (getAtomicserviceIconInfoPlugin == nullptr) {
159         return atomicServiceBasicInfo;
160     }
161     int32_t ret = getAtomicserviceIconInfoPlugin(bundleName, &atomicserviceIconInfo_);
162     if (ret != 0) {
163         return atomicServiceBasicInfo;
164     }
165     if (atomicserviceIconInfo_ != nullptr) {
166         atomicServiceBasicInfo.push_back(atomicserviceIconInfo_->GetAppName());
167         atomicServiceBasicInfo.push_back(atomicserviceIconInfo_->GetCircleIcon());
168         atomicServiceBasicInfo.push_back(atomicserviceIconInfo_->GetEyelashRingIcon());
169     }
170     return atomicServiceBasicInfo;
171 }
172 
releaseData()173 int32_t AtomicServiceBasicEnginePlugin::releaseData()
174 {
175     std::lock_guard<std::mutex> lock(mutex_);
176     // free data
177     if (freeData != nullptr && atomicserviceIconInfo_ != nullptr) {
178         freeData(atomicserviceIconInfo_);
179         atomicserviceIconInfo_ = nullptr;
180         return 0;
181     }
182     TAG_LOGW(AceLogTag::ACE_WINDOW_SCENE, "dlsym FreeData interface failed, can't release data");
183     return -1;
184 }
185 #endif // ATOMIC_SERVICE_ATTRIBUTION_ENABLE
186 } //namespace OHOS::Ace::NG
187