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