1 /*
2 * Copyright (c) 2021 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 "ipc/installd_host.h"
17
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_constants.h"
21 #include "parcel_macro.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
InstalldHost()26 InstalldHost::InstalldHost()
27 {
28 APP_LOGI("installd host instance is created");
29 }
30
~InstalldHost()31 InstalldHost::~InstalldHost()
32 {
33 APP_LOGI("installd host instance is destroyed");
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int InstalldHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38 APP_LOGD(
39 "installd host receives message from client, code = %{public}d, flags = %{public}d", code, option.GetFlags());
40 std::u16string descripter = InstalldHost::GetDescriptor();
41 std::u16string remoteDescripter = data.ReadInterfaceToken();
42 if (descripter != remoteDescripter) {
43 APP_LOGE("installd host fail to write reply message due to the reply is nullptr");
44 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
45 }
46 switch (code) {
47 case static_cast<uint32_t>(IInstalld::Message::CREATE_BUNDLE_DIR): {
48 if (!HandleCreateBundleDir(data, reply)) {
49 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
50 }
51 break;
52 }
53 case static_cast<uint32_t>(IInstalld::Message::REMOVE_BUNDLE_DIR): {
54 if (!HandleRemoveBundleDir(data, reply)) {
55 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
56 }
57 break;
58 }
59 case static_cast<uint32_t>(IInstalld::Message::EXTRACT_MODULE_FILES): {
60 if (!HandleExtractModuleFiles(data, reply)) {
61 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
62 }
63 break;
64 }
65 case static_cast<uint32_t>(IInstalld::Message::REMOVE_MODULE_DIR): {
66 if (!HandleRemoveModuleDir(data, reply)) {
67 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
68 }
69 break;
70 }
71 case static_cast<uint32_t>(IInstalld::Message::RENAME_MODULE_DIR): {
72 if (!HandleRenameModuleDir(data, reply)) {
73 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
74 }
75 break;
76 }
77 case static_cast<uint32_t>(IInstalld::Message::CREATE_BUNDLE_DATA_DIR): {
78 if (!HandleCreateBundleDataDir(data, reply)) {
79 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
80 }
81 break;
82 }
83 case static_cast<uint32_t>(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR): {
84 if (!HandleRemoveBundleDataDir(data, reply)) {
85 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
86 }
87 break;
88 }
89 case static_cast<uint32_t>(IInstalld::Message::CREATE_MODULE_DATA_DIR): {
90 if (!HandleCreateModuleDataDir(data, reply)) {
91 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
92 }
93 break;
94 }
95 case static_cast<uint32_t>(IInstalld::Message::REMOVE_MODULE_DATA_DIR): {
96 if (!HandleRemoveModuleDataDir(data, reply)) {
97 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
98 }
99 break;
100 }
101 case static_cast<uint32_t>(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR): {
102 if (!HandleCleanBundleDataDir(data, reply)) {
103 return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
104 }
105 break;
106 }
107 default:
108 APP_LOGW("installd host receives unknown code, code = %{public}d", code);
109 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
110 }
111 APP_LOGD("installd host finish to process message from client");
112 return NO_ERROR;
113 }
114
HandleCreateBundleDir(MessageParcel & data,MessageParcel & reply)115 bool InstalldHost::HandleCreateBundleDir(MessageParcel &data, MessageParcel &reply)
116 {
117 std::string bundleDir = data.ReadString();
118 APP_LOGI("bundleName %{public}s", bundleDir.c_str());
119 ErrCode result = CreateBundleDir(bundleDir);
120 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
121 return true;
122 }
123
HandleRemoveBundleDir(MessageParcel & data,MessageParcel & reply)124 bool InstalldHost::HandleRemoveBundleDir(MessageParcel &data, MessageParcel &reply)
125 {
126 std::string bundleDir = data.ReadString();
127 ErrCode result = RemoveBundleDir(bundleDir);
128 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
129 return true;
130 }
131
HandleExtractModuleFiles(MessageParcel & data,MessageParcel & reply)132 bool InstalldHost::HandleExtractModuleFiles(MessageParcel &data, MessageParcel &reply)
133 {
134 std::string srcModulePath = data.ReadString();
135 std::string targetPath = data.ReadString();
136 APP_LOGI("extract module %{public}s", targetPath.c_str());
137 ErrCode result = ExtractModuleFiles(srcModulePath, targetPath);
138 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
139 return true;
140 }
141
HandleRenameModuleDir(MessageParcel & data,MessageParcel & reply)142 bool InstalldHost::HandleRenameModuleDir(MessageParcel &data, MessageParcel &reply)
143 {
144 std::string oldPath = data.ReadString();
145 std::string newPath = data.ReadString();
146 APP_LOGI("rename moduleDir %{public}s", oldPath.c_str());
147 ErrCode result = RenameModuleDir(oldPath, newPath);
148 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
149 return true;
150 }
151
HandleRemoveModuleDir(MessageParcel & data,MessageParcel & reply)152 bool InstalldHost::HandleRemoveModuleDir(MessageParcel &data, MessageParcel &reply)
153 {
154 std::string moduleDir = data.ReadString();
155 APP_LOGI("remove moduleDir %{public}s", moduleDir.c_str());
156 ErrCode result = RemoveModuleDir(moduleDir);
157 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
158 return true;
159 }
160
HandleCreateBundleDataDir(MessageParcel & data,MessageParcel & reply)161 bool InstalldHost::HandleCreateBundleDataDir(MessageParcel &data, MessageParcel &reply)
162 {
163 std::string bundleDir = data.ReadString();
164 int uid = data.ReadInt32();
165 int gid = data.ReadInt32();
166 ErrCode result = CreateBundleDataDir(bundleDir, uid, gid);
167 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
168 return true;
169 }
170
HandleRemoveBundleDataDir(MessageParcel & data,MessageParcel & reply)171 bool InstalldHost::HandleRemoveBundleDataDir(MessageParcel &data, MessageParcel &reply)
172 {
173 std::string bundleDataDir = data.ReadString();
174 ErrCode result = RemoveBundleDataDir(bundleDataDir);
175 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
176 return true;
177 }
178
HandleCreateModuleDataDir(MessageParcel & data,MessageParcel & reply)179 bool InstalldHost::HandleCreateModuleDataDir(MessageParcel &data, MessageParcel &reply)
180 {
181 std::string bundleDir = data.ReadString();
182 std::vector<std::string> abilityDirs;
183 if (!data.ReadStringVector(&abilityDirs)) {
184 return false;
185 }
186 int uid = data.ReadInt32();
187 int gid = data.ReadInt32();
188 ErrCode result = CreateModuleDataDir(bundleDir, abilityDirs, uid, gid);
189 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
190 return true;
191 }
192
HandleRemoveModuleDataDir(MessageParcel & data,MessageParcel & reply)193 bool InstalldHost::HandleRemoveModuleDataDir(MessageParcel &data, MessageParcel &reply)
194 {
195 std::string bundleDataDir = data.ReadString();
196 ErrCode result = RemoveBundleDataDir(bundleDataDir);
197 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
198 return true;
199 }
200
HandleCleanBundleDataDir(MessageParcel & data,MessageParcel & reply)201 bool InstalldHost::HandleCleanBundleDataDir(MessageParcel &data, MessageParcel &reply)
202 {
203 std::string bundleDir = data.ReadString();
204 ErrCode result = CleanBundleDataDir(bundleDir);
205 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
206 return true;
207 }
208
209 } // namespace AppExecFwk
210 } // namespace OHOS
211