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 "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         default:
68             APP_LOGW("ExtendResourceManagerHost receive unknown code %{public}d", code);
69             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70     }
71 }
72 
HandleAddExtResource(MessageParcel & data,MessageParcel & reply)73 ErrCode ExtendResourceManagerHost::HandleAddExtResource(MessageParcel &data, MessageParcel &reply)
74 {
75     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
76     std::string bundleName = data.ReadString();
77     std::vector<std::string> filePaths;
78     if (!data.ReadStringVector(&filePaths)) {
79         APP_LOGE("read filePaths failed");
80         return ERR_APPEXECFWK_PARCEL_ERROR;
81     }
82     ErrCode ret = AddExtResource(bundleName, filePaths);
83     if (!reply.WriteInt32(ret)) {
84         APP_LOGE("write result failed");
85         return ERR_APPEXECFWK_PARCEL_ERROR;
86     }
87     return ERR_OK;
88 }
89 
HandleRemoveExtResource(MessageParcel & data,MessageParcel & reply)90 ErrCode ExtendResourceManagerHost::HandleRemoveExtResource(MessageParcel &data, MessageParcel &reply)
91 {
92     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
93     std::string bundleName = data.ReadString();
94     std::vector<std::string> moduleNames;
95     if (!data.ReadStringVector(&moduleNames)) {
96         APP_LOGE("read moduleNames failed");
97         return ERR_APPEXECFWK_PARCEL_ERROR;
98     }
99     ErrCode ret = RemoveExtResource(bundleName, moduleNames);
100     if (!reply.WriteInt32(ret)) {
101         APP_LOGE("write result failed");
102         return ERR_APPEXECFWK_PARCEL_ERROR;
103     }
104     return ERR_OK;
105 }
106 
HandleGetExtResource(MessageParcel & data,MessageParcel & reply)107 ErrCode ExtendResourceManagerHost::HandleGetExtResource(MessageParcel &data, MessageParcel &reply)
108 {
109     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
110     std::string bundleName = data.ReadString();
111     std::vector<std::string> moduleNames;
112     ErrCode ret = GetExtResource(bundleName, moduleNames);
113     if (!reply.WriteInt32(ret)) {
114         APP_LOGE("write result failed");
115         return ERR_APPEXECFWK_PARCEL_ERROR;
116     }
117     if (!reply.WriteStringVector(moduleNames)) {
118         APP_LOGE("write moduleNames failed");
119         return ERR_APPEXECFWK_PARCEL_ERROR;
120     }
121     return ERR_OK;
122 }
123 
HandleEnableDynamicIcon(MessageParcel & data,MessageParcel & reply)124 ErrCode ExtendResourceManagerHost::HandleEnableDynamicIcon(MessageParcel& data, MessageParcel& reply)
125 {
126     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
127     std::string bundleName = data.ReadString();
128     std::string moduleName = data.ReadString();
129     ErrCode ret = EnableDynamicIcon(bundleName, moduleName);
130     if (!reply.WriteInt32(ret)) {
131         APP_LOGE("write result failed");
132         return ERR_APPEXECFWK_PARCEL_ERROR;
133     }
134     return ERR_OK;
135 }
136 
HandleDisableDynamicIcon(MessageParcel & data,MessageParcel & reply)137 ErrCode ExtendResourceManagerHost::HandleDisableDynamicIcon(MessageParcel& data, MessageParcel& reply)
138 {
139     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
140     std::string bundleName = data.ReadString();
141     ErrCode ret = DisableDynamicIcon(bundleName);
142     if (!reply.WriteInt32(ret)) {
143         APP_LOGE("write result failed");
144         return ERR_APPEXECFWK_PARCEL_ERROR;
145     }
146     return ERR_OK;
147 }
148 
HandleGetDynamicIcon(MessageParcel & data,MessageParcel & reply)149 ErrCode ExtendResourceManagerHost::HandleGetDynamicIcon(MessageParcel& data, MessageParcel& reply)
150 {
151     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
152     std::string bundleName = data.ReadString();
153     std::string moduleName;
154     ErrCode ret = GetDynamicIcon(bundleName, moduleName);
155     if (!reply.WriteInt32(ret)) {
156         APP_LOGE("write result failed");
157         return ERR_APPEXECFWK_PARCEL_ERROR;
158     }
159     if (ret != ERR_OK) {
160         return ERR_OK;
161     }
162     if (!reply.WriteString(moduleName)) {
163         APP_LOGE("write moduleName failed");
164         return ERR_APPEXECFWK_PARCEL_ERROR;
165     }
166     return ERR_OK;
167 }
168 
HandleCreateFd(MessageParcel & data,MessageParcel & reply)169 ErrCode ExtendResourceManagerHost::HandleCreateFd(MessageParcel& data, MessageParcel& reply)
170 {
171     APP_LOGD("begin to HandleCreateFd");
172     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
173     std::string fileName = data.ReadString();
174     int32_t fd = -1;
175     std::string path;
176     auto ret = CreateFd(fileName, fd, path);
177     if (!reply.WriteInt32(ret)) {
178         APP_LOGE("write ret failed");
179         close(fd);
180         return ERR_APPEXECFWK_PARCEL_ERROR;
181     }
182     if (ret == ERR_OK) {
183         if (!reply.WriteFileDescriptor(fd)) {
184             APP_LOGE("write fd failed");
185             close(fd);
186             return ERR_APPEXECFWK_PARCEL_ERROR;
187         }
188         if (!reply.WriteString(path)) {
189             APP_LOGE("write path failed");
190             close(fd);
191             return ERR_APPEXECFWK_PARCEL_ERROR;
192         }
193     }
194     close(fd);
195     return ERR_OK;
196 }
197 } // AppExecFwk
198 } // namespace OHOS
199