1 /*
2  * Copyright (c) 2022-2023 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 "overlay_manager_host.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_framework_core_ipc_interface_code.h"
21 #include "bundle_memory_guard.h"
22 #include "hitrace_meter.h"
23 #include "ipc_types.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
OverlayManagerHost()27 OverlayManagerHost::OverlayManagerHost()
28 {
29     APP_LOGD("create OverlayManagerHost");
30 }
31 
~OverlayManagerHost()32 OverlayManagerHost::~OverlayManagerHost()
33 {
34     APP_LOGD("destroy OverlayManagerHost");
35 }
36 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int OverlayManagerHost::OnRemoteRequest(uint32_t code, MessageParcel& data,
38     MessageParcel& reply, MessageOption& option)
39 {
40     BundleMemoryGuard memoryGuard;
41     APP_LOGD("bundle mgr host onReceived message, the message code is %{public}u", code);
42     std::u16string descriptor = OverlayManagerHost::GetDescriptor();
43     std::u16string remoteDescriptor = data.ReadInterfaceToken();
44     if (descriptor != remoteDescriptor) {
45         APP_LOGE("fail to write reply message in bundle mgr host due to the reply is nullptr");
46         return OBJECT_NULL;
47     }
48 
49     ErrCode errCode = ERR_OK;
50     switch (code) {
51         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_ALL_OVERLAY_MODULE_INFO):
52             errCode = this->HandleGetAllOverlayModuleInfo(data, reply);
53             break;
54         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_OVERLAY_MODULE_INFO_BY_NAME):
55             errCode = this->HandleGetOverlayModuleInfoByName(data, reply);
56             break;
57         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_OVERLAY_MODULE_INFO):
58             errCode = this->HandleGetOverlayModuleInfo(data, reply);
59             break;
60         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_TARGET_OVERLAY_MODULE_INFOS):
61             errCode = this->HandleGetTargetOverlayModuleInfo(data, reply);
62             break;
63         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_OVERLAY_MODULE_INFO_BY_BUNDLE_NAME):
64             errCode = this->HandleGetOverlayModuleInfoByBundleName(data, reply);
65             break;
66         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_OVERLAY_BUNDLE_INFO_FOR_TARGET):
67             errCode = this->HandleGetOverlayBundleInfoForTarget(data, reply);
68             break;
69         case static_cast<uint32_t>(OverlayManagerInterfaceCode::GET_OVERLAY_MODULE_INFO_FOR_TARGET):
70             errCode = this->HandleGetOverlayModuleInfoForTarget(data, reply);
71             break;
72         case static_cast<uint32_t>(OverlayManagerInterfaceCode::SET_OVERLAY_ENABLED):
73             errCode = this->HandleSetOverlayEnabled(data, reply);
74             break;
75         case static_cast<uint32_t>(OverlayManagerInterfaceCode::SET_OVERLAY_ENABLED_FOR_SELF):
76             errCode = this->HandleSetOverlayEnabledForSelf(data, reply);
77             break;
78         default :
79             APP_LOGW("overlayMgr host receives unknown code %{public}u", code);
80             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81     }
82     APP_LOGD("overlayMgr host finish to process message");
83     return (errCode == ERR_OK) ? NO_ERROR : UNKNOWN_ERROR;
84 }
85 
HandleGetAllOverlayModuleInfo(MessageParcel & data,MessageParcel & reply)86 ErrCode OverlayManagerHost::HandleGetAllOverlayModuleInfo(MessageParcel &data, MessageParcel &reply)
87 {
88     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
89     std::string bundleName = data.ReadString();
90     int32_t userId = data.ReadInt32();
91     APP_LOGD("bundleName %{public}s, userId %{public}d", bundleName.c_str(), userId);
92 
93     std::vector<OverlayModuleInfo> infos;
94     auto res = GetAllOverlayModuleInfo(bundleName, infos, userId);
95     if (!reply.WriteInt32(res)) {
96         APP_LOGE("write failed");
97         return ERR_APPEXECFWK_PARCEL_ERROR;
98     }
99     if (res == ERR_OK) {
100         if (!WriteParcelableVector(infos, reply)) {
101             APP_LOGE("write failed");
102             return ERR_APPEXECFWK_PARCEL_ERROR;
103         }
104     }
105     return ERR_OK;
106 }
107 
HandleGetOverlayModuleInfoByName(MessageParcel & data,MessageParcel & reply)108 ErrCode OverlayManagerHost::HandleGetOverlayModuleInfoByName(MessageParcel &data, MessageParcel &reply)
109 {
110     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
111     std::string bundleName = data.ReadString();
112     std::string moduleName = data.ReadString();
113     int32_t userId = data.ReadInt32();
114     APP_LOGD("bundleName %{public}s, moduleName %{public}s, userId %{public}d", bundleName.c_str(),
115         moduleName.c_str(), userId);
116 
117     OverlayModuleInfo info;
118     auto res = GetOverlayModuleInfo(bundleName, moduleName, info, userId);
119     if (!reply.WriteInt32(res)) {
120         APP_LOGE("write failed");
121         return ERR_APPEXECFWK_PARCEL_ERROR;
122     }
123     if (res == ERR_OK) {
124         if (!reply.WriteParcelable(&info)) {
125             APP_LOGE("write failed");
126             return ERR_APPEXECFWK_PARCEL_ERROR;
127         }
128     }
129     return ERR_OK;
130 }
131 
HandleGetOverlayModuleInfo(MessageParcel & data,MessageParcel & reply)132 ErrCode OverlayManagerHost::HandleGetOverlayModuleInfo(MessageParcel &data, MessageParcel &reply)
133 {
134     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
135     std::string moduleName = data.ReadString();
136     int32_t userId = data.ReadInt32();
137     APP_LOGD("moduleName %{public}s, userId %{public}d", moduleName.c_str(), userId);
138 
139     OverlayModuleInfo info;
140     auto res = GetOverlayModuleInfo(moduleName, info, userId);
141     if (!reply.WriteInt32(res)) {
142         APP_LOGE("write failed");
143         return ERR_APPEXECFWK_PARCEL_ERROR;
144     }
145     if (res == ERR_OK) {
146         if (!reply.WriteParcelable(&info)) {
147             APP_LOGE("write failed");
148             return ERR_APPEXECFWK_PARCEL_ERROR;
149         }
150     }
151     return ERR_OK;
152 }
153 
HandleGetTargetOverlayModuleInfo(MessageParcel & data,MessageParcel & reply)154 ErrCode OverlayManagerHost::HandleGetTargetOverlayModuleInfo(MessageParcel &data, MessageParcel &reply)
155 {
156     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
157     std::string targetModuleName = data.ReadString();
158     int32_t userId = data.ReadInt32();
159 
160     std::vector<OverlayModuleInfo> overlayModuleInfos;
161     auto res = GetTargetOverlayModuleInfo(targetModuleName, overlayModuleInfos, userId);
162     if (!reply.WriteInt32(res)) {
163         APP_LOGE("write failed");
164         return ERR_APPEXECFWK_PARCEL_ERROR;
165     }
166 
167     if (res == ERR_OK) {
168         if (!WriteParcelableVector(overlayModuleInfos, reply)) {
169             APP_LOGE("write failed");
170             return ERR_APPEXECFWK_PARCEL_ERROR;
171         }
172     }
173     return ERR_OK;
174 }
175 
HandleGetOverlayModuleInfoByBundleName(MessageParcel & data,MessageParcel & reply)176 ErrCode OverlayManagerHost::HandleGetOverlayModuleInfoByBundleName(MessageParcel &data, MessageParcel &reply)
177 {
178     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
179     std::string bundleName = data.ReadString();
180     std::string moduleName = data.ReadString();
181     int32_t userId = data.ReadInt32();
182     APP_LOGD("bundleName %{public}s, userId %{public}d", bundleName.c_str(), userId);
183 
184     std::vector<OverlayModuleInfo> infos;
185     auto res = GetOverlayModuleInfoByBundleName(bundleName, moduleName, infos, userId);
186     if (!reply.WriteInt32(res)) {
187         APP_LOGE("write failed");
188         return ERR_APPEXECFWK_PARCEL_ERROR;
189     }
190     if (res == ERR_OK) {
191         if (!WriteParcelableVector(infos, reply)) {
192             APP_LOGE("write failed");
193             return ERR_APPEXECFWK_PARCEL_ERROR;
194         }
195     }
196     return ERR_OK;
197 }
198 
HandleGetOverlayBundleInfoForTarget(MessageParcel & data,MessageParcel & reply)199 ErrCode OverlayManagerHost::HandleGetOverlayBundleInfoForTarget(MessageParcel &data, MessageParcel &reply)
200 {
201     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
202     std::string targetBundleName = data.ReadString();
203     int32_t userId = data.ReadInt32();
204 
205     std::vector<OverlayBundleInfo> overlayBundleInfo;
206     auto res = GetOverlayBundleInfoForTarget(targetBundleName, overlayBundleInfo, userId);
207     if (!reply.WriteInt32(res)) {
208         APP_LOGE("write failed");
209         return ERR_APPEXECFWK_PARCEL_ERROR;
210     }
211 
212     if (res == ERR_OK) {
213         if (!WriteParcelableVector(overlayBundleInfo, reply)) {
214             APP_LOGE("write failed");
215             return ERR_APPEXECFWK_PARCEL_ERROR;
216         }
217     }
218     return ERR_OK;
219 }
220 
HandleGetOverlayModuleInfoForTarget(MessageParcel & data,MessageParcel & reply)221 ErrCode OverlayManagerHost::HandleGetOverlayModuleInfoForTarget(MessageParcel &data, MessageParcel &reply)
222 {
223     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
224     std::string targetBundleName = data.ReadString();
225     std::string targetModuleName = data.ReadString();
226     int32_t userId = data.ReadInt32();
227 
228     std::vector<OverlayModuleInfo> overlayModuleInfo;
229     auto res = GetOverlayModuleInfoForTarget(targetBundleName, targetModuleName, overlayModuleInfo, userId);
230     if (!reply.WriteInt32(res)) {
231         APP_LOGE("write failed");
232         return ERR_APPEXECFWK_PARCEL_ERROR;
233     }
234 
235     if (res == ERR_OK) {
236         if (!WriteParcelableVector(overlayModuleInfo, reply)) {
237             APP_LOGE("write failed");
238             return ERR_APPEXECFWK_PARCEL_ERROR;
239         }
240     }
241     return ERR_OK;
242 }
243 
HandleSetOverlayEnabled(MessageParcel & data,MessageParcel & reply)244 ErrCode OverlayManagerHost::HandleSetOverlayEnabled(MessageParcel &data, MessageParcel &reply)
245 {
246     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
247     std::string bundleName = data.ReadString();
248     std::string moduleName = data.ReadString();
249     bool isEnabled = data.ReadBool();
250     int32_t userId = data.ReadInt32();
251 
252     auto res = SetOverlayEnabled(bundleName, moduleName, isEnabled, userId);
253     if (!reply.WriteInt32(res)) {
254         APP_LOGE("write failed");
255         return ERR_APPEXECFWK_PARCEL_ERROR;
256     }
257     return ERR_OK;
258 }
259 
HandleSetOverlayEnabledForSelf(MessageParcel & data,MessageParcel & reply)260 ErrCode OverlayManagerHost::HandleSetOverlayEnabledForSelf(MessageParcel &data, MessageParcel &reply)
261 {
262     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
263     std::string moduleName = data.ReadString();
264     bool isEnabled = data.ReadBool();
265     int32_t userId = data.ReadInt32();
266 
267     auto res = SetOverlayEnabledForSelf(moduleName, isEnabled, userId);
268     if (!reply.WriteInt32(res)) {
269         APP_LOGE("write failed");
270         return ERR_APPEXECFWK_PARCEL_ERROR;
271     }
272     return ERR_OK;
273 }
274 
275 template<typename T>
WriteParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)276 bool OverlayManagerHost::WriteParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
277 {
278     if (!reply.WriteInt32(parcelableVector.size())) {
279         APP_LOGE("write ParcelableVector failed");
280         return false;
281     }
282 
283     for (auto &parcelable : parcelableVector) {
284         if (!reply.WriteParcelable(&parcelable)) {
285             APP_LOGE("write ParcelableVector failed");
286             return false;
287         }
288     }
289     return true;
290 }
291 } // AppExecFwk
292 } // namespace OHOS
293