1 /*
2 * Copyright (c) 2024-2025 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 "extend_resource_manager_host.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "app_log_wrapper.h"
22 #include "appexecfwk_errors.h"
23 #include "bundle_framework_core_ipc_interface_code.h"
24 #include "bundle_memory_guard.h"
25 #include "hitrace_meter.h"
26 #include "ipc_types.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
ExtendResourceManagerHost()30 ExtendResourceManagerHost::ExtendResourceManagerHost()
31 {
32 APP_LOGI("create ExtendResourceManagerHost");
33 }
34
~ExtendResourceManagerHost()35 ExtendResourceManagerHost::~ExtendResourceManagerHost()
36 {
37 APP_LOGI("destroy ExtendResourceManagerHost");
38 }
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int ExtendResourceManagerHost::OnRemoteRequest(uint32_t code, MessageParcel& data,
41 MessageParcel& reply, MessageOption& option)
42 {
43 BundleMemoryGuard memoryGuard;
44 APP_LOGI_NOFUNC("ExtendResourceManagerHost OnRemoteRequest, message code : %{public}u", code);
45 std::u16string descriptor = ExtendResourceManagerHost::GetDescriptor();
46 std::u16string remoteDescriptor = data.ReadInterfaceToken();
47 if (descriptor != remoteDescriptor) {
48 APP_LOGE("descriptor invalid");
49 return OBJECT_NULL;
50 }
51
52 switch (code) {
53 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::ADD_EXT_RESOURCE):
54 return HandleAddExtResource(data, reply);
55 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::REMOVE_EXT_RESOURCE):
56 return HandleRemoveExtResource(data, reply);
57 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::GET_EXT_RESOURCE):
58 return HandleGetExtResource(data, reply);
59 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::ENABLE_DYNAMIC_ICON):
60 return HandleEnableDynamicIcon(data, reply);
61 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::DISABLE_DYNAMIC_ICON):
62 return HandleDisableDynamicIcon(data, reply);
63 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::GET_DYNAMIC_ICON):
64 return HandleGetDynamicIcon(data, reply);
65 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::CREATE_FD):
66 return HandleCreateFd(data, reply);
67 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::GET_ALL_DYNAMIC_ICON_INFO):
68 return HandleGetAllDynamicIconInfo(data, reply);
69 case static_cast<uint32_t>(ExtendResourceManagerInterfaceCode::GET_DYNAMIC_ICON_INFO):
70 return HandleGetDynamicIconInfo(data, reply);
71 default:
72 APP_LOGW("ExtendResourceManagerHost receive unknown code %{public}d", code);
73 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 }
76
HandleAddExtResource(MessageParcel & data,MessageParcel & reply)77 ErrCode ExtendResourceManagerHost::HandleAddExtResource(MessageParcel &data, MessageParcel &reply)
78 {
79 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
80 std::string bundleName = data.ReadString();
81 std::vector<std::string> filePaths;
82 if (!data.ReadStringVector(&filePaths)) {
83 APP_LOGE("read filePaths failed");
84 return ERR_APPEXECFWK_PARCEL_ERROR;
85 }
86 ErrCode ret = AddExtResource(bundleName, filePaths);
87 if (!reply.WriteInt32(ret)) {
88 APP_LOGE("write result failed");
89 return ERR_APPEXECFWK_PARCEL_ERROR;
90 }
91 return ERR_OK;
92 }
93
HandleRemoveExtResource(MessageParcel & data,MessageParcel & reply)94 ErrCode ExtendResourceManagerHost::HandleRemoveExtResource(MessageParcel &data, MessageParcel &reply)
95 {
96 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
97 std::string bundleName = data.ReadString();
98 std::vector<std::string> moduleNames;
99 if (!data.ReadStringVector(&moduleNames)) {
100 APP_LOGE("read moduleNames failed");
101 return ERR_APPEXECFWK_PARCEL_ERROR;
102 }
103 ErrCode ret = RemoveExtResource(bundleName, moduleNames);
104 if (!reply.WriteInt32(ret)) {
105 APP_LOGE("write result failed");
106 return ERR_APPEXECFWK_PARCEL_ERROR;
107 }
108 return ERR_OK;
109 }
110
HandleGetExtResource(MessageParcel & data,MessageParcel & reply)111 ErrCode ExtendResourceManagerHost::HandleGetExtResource(MessageParcel &data, MessageParcel &reply)
112 {
113 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
114 std::string bundleName = data.ReadString();
115 std::vector<std::string> moduleNames;
116 ErrCode ret = GetExtResource(bundleName, moduleNames);
117 if (!reply.WriteInt32(ret)) {
118 APP_LOGE("write result failed");
119 return ERR_APPEXECFWK_PARCEL_ERROR;
120 }
121 if (!reply.WriteStringVector(moduleNames)) {
122 APP_LOGE("write moduleNames failed");
123 return ERR_APPEXECFWK_PARCEL_ERROR;
124 }
125 return ERR_OK;
126 }
127
HandleEnableDynamicIcon(MessageParcel & data,MessageParcel & reply)128 ErrCode ExtendResourceManagerHost::HandleEnableDynamicIcon(MessageParcel& data, MessageParcel& reply)
129 {
130 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
131 std::string bundleName = data.ReadString();
132 std::string moduleName = data.ReadString();
133 int32_t userId = data.ReadInt32();
134 int32_t appIndex = data.ReadInt32();
135 ErrCode ret = EnableDynamicIcon(bundleName, moduleName, userId, appIndex);
136 if (!reply.WriteInt32(ret)) {
137 APP_LOGE("write result failed");
138 return ERR_APPEXECFWK_PARCEL_ERROR;
139 }
140 return ERR_OK;
141 }
142
HandleDisableDynamicIcon(MessageParcel & data,MessageParcel & reply)143 ErrCode ExtendResourceManagerHost::HandleDisableDynamicIcon(MessageParcel& data, MessageParcel& reply)
144 {
145 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
146 std::string bundleName = data.ReadString();
147 int32_t userId = data.ReadInt32();
148 int32_t appIndex = data.ReadInt32();
149 ErrCode ret = DisableDynamicIcon(bundleName, userId, appIndex);
150 if (!reply.WriteInt32(ret)) {
151 APP_LOGE("write result failed");
152 return ERR_APPEXECFWK_PARCEL_ERROR;
153 }
154 return ERR_OK;
155 }
156
HandleGetDynamicIcon(MessageParcel & data,MessageParcel & reply)157 ErrCode ExtendResourceManagerHost::HandleGetDynamicIcon(MessageParcel& data, MessageParcel& reply)
158 {
159 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
160 std::string bundleName = data.ReadString();
161 int32_t userId = data.ReadInt32();
162 int32_t appIndex = data.ReadInt32();
163 std::string moduleName;
164 ErrCode ret = GetDynamicIcon(bundleName, userId, appIndex, moduleName);
165 if (!reply.WriteInt32(ret)) {
166 APP_LOGE("write result failed");
167 return ERR_APPEXECFWK_PARCEL_ERROR;
168 }
169 if (ret != ERR_OK) {
170 return ERR_OK;
171 }
172 if (!reply.WriteString(moduleName)) {
173 APP_LOGE("write moduleName failed");
174 return ERR_APPEXECFWK_PARCEL_ERROR;
175 }
176 return ERR_OK;
177 }
178
HandleCreateFd(MessageParcel & data,MessageParcel & reply)179 ErrCode ExtendResourceManagerHost::HandleCreateFd(MessageParcel& data, MessageParcel& reply)
180 {
181 APP_LOGD("begin to HandleCreateFd");
182 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
183 std::string fileName = data.ReadString();
184 int32_t fd = -1;
185 std::string path;
186 auto ret = CreateFd(fileName, fd, path);
187 if (!reply.WriteInt32(ret)) {
188 APP_LOGE("write ret failed");
189 close(fd);
190 return ERR_APPEXECFWK_PARCEL_ERROR;
191 }
192 if (ret == ERR_OK) {
193 if (!reply.WriteFileDescriptor(fd)) {
194 APP_LOGE("write fd failed");
195 close(fd);
196 return ERR_APPEXECFWK_PARCEL_ERROR;
197 }
198 if (!reply.WriteString(path)) {
199 APP_LOGE("write path failed");
200 close(fd);
201 return ERR_APPEXECFWK_PARCEL_ERROR;
202 }
203 }
204 close(fd);
205 return ERR_OK;
206 }
207
HandleGetAllDynamicIconInfo(MessageParcel & data,MessageParcel & reply)208 ErrCode ExtendResourceManagerHost::HandleGetAllDynamicIconInfo(MessageParcel& data, MessageParcel& reply)
209 {
210 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
211 int32_t userId = data.ReadInt32();
212 std::vector<DynamicIconInfo> dynamicIconInfos;
213 ErrCode ret = GetAllDynamicIconInfo(userId, dynamicIconInfos);
214 if (!reply.WriteInt32(ret)) {
215 APP_LOGE("write result failed");
216 return ERR_APPEXECFWK_PARCEL_ERROR;
217 }
218 if (ret == ERR_OK) {
219 return WriteParcelableVector<DynamicIconInfo>(dynamicIconInfos, reply);
220 }
221 return ERR_OK;
222 }
223
HandleGetDynamicIconInfo(MessageParcel & data,MessageParcel & reply)224 ErrCode ExtendResourceManagerHost::HandleGetDynamicIconInfo(MessageParcel& data, MessageParcel& reply)
225 {
226 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
227 std::string bundleName = data.ReadString();
228 std::vector<DynamicIconInfo> dynamicIconInfos;
229 ErrCode ret = GetDynamicIconInfo(bundleName, dynamicIconInfos);
230 if (!reply.WriteInt32(ret)) {
231 APP_LOGE("write result failed");
232 return ERR_APPEXECFWK_PARCEL_ERROR;
233 }
234 if (ret == ERR_OK) {
235 return WriteParcelableVector<DynamicIconInfo>(dynamicIconInfos, reply);
236 }
237 return ERR_OK;
238 }
239
240 template<typename T>
WriteParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)241 ErrCode ExtendResourceManagerHost::WriteParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
242 {
243 if (!reply.WriteInt32(parcelableVector.size())) {
244 APP_LOGE("write ParcelableVector failed");
245 return ERR_APPEXECFWK_PARCEL_ERROR;
246 }
247
248 for (auto &parcelable : parcelableVector) {
249 if (!reply.WriteParcelable(&parcelable)) {
250 APP_LOGE("write ParcelableVector failed");
251 return ERR_APPEXECFWK_PARCEL_ERROR;
252 }
253 }
254 return ERR_OK;
255 }
256 } // AppExecFwk
257 } // namespace OHOS
258