1 /*
2 * Copyright (c) 2022-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 "quick_fix_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 "app_log_tag_wrapper.h"
24 #include "bundle_framework_core_ipc_interface_code.h"
25 #include "bundle_memory_guard.h"
26 #include "hitrace_meter.h"
27 #include "ipc_types.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
QuickFixManagerHost()31 QuickFixManagerHost::QuickFixManagerHost()
32 {
33 LOG_I(BMS_TAG_DEFAULT, "create QuickFixManagerHost");
34 }
35
~QuickFixManagerHost()36 QuickFixManagerHost::~QuickFixManagerHost()
37 {
38 LOG_I(BMS_TAG_DEFAULT, "destroy QuickFixManagerHost");
39 }
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int QuickFixManagerHost::OnRemoteRequest(uint32_t code, MessageParcel& data,
42 MessageParcel& reply, MessageOption& option)
43 {
44 BundleMemoryGuard memoryGuard;
45 LOG_I(BMS_TAG_DEFAULT, "QuickFixManagerHost OnRemoteRequest, message code : %{public}u", code);
46 std::u16string descriptor = QuickFixManagerHost::GetDescriptor();
47 std::u16string remoteDescriptor = data.ReadInterfaceToken();
48 if (descriptor != remoteDescriptor) {
49 LOG_E(BMS_TAG_DEFAULT, "descriptor invalid");
50 return OBJECT_NULL;
51 }
52
53 switch (code) {
54 case static_cast<uint32_t>(QuickFixManagerInterfaceCode::DEPLOY_QUICK_FIX):
55 return HandleDeployQuickFix(data, reply);
56 case static_cast<uint32_t>(QuickFixManagerInterfaceCode::SWITCH_QUICK_FIX):
57 return HandleSwitchQuickFix(data, reply);
58 case static_cast<uint32_t>(QuickFixManagerInterfaceCode::DELETE_QUICK_FIX):
59 return HandleDeleteQuickFix(data, reply);
60 case static_cast<uint32_t>(QuickFixManagerInterfaceCode::CREATE_FD):
61 return HandleCreateFd(data, reply);
62 default:
63 LOG_W(BMS_TAG_DEFAULT, "QuickFixManagerHost receive unknown code, code = %{public}d", code);
64 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66 }
67
HandleDeployQuickFix(MessageParcel & data,MessageParcel & reply)68 ErrCode QuickFixManagerHost::HandleDeployQuickFix(MessageParcel& data, MessageParcel& reply)
69 {
70 LOG_I(BMS_TAG_DEFAULT, "begin to HandleDeployQuickFix");
71 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
72 std::vector<std::string> bundleFilePaths;
73 if (!data.ReadStringVector(&bundleFilePaths)) {
74 LOG_E(BMS_TAG_DEFAULT, "read bundleFilePaths failed");
75 return ERR_APPEXECFWK_PARCEL_ERROR;
76 }
77 bool isDebug = data.ReadBool();
78 std::string targetPath = data.ReadString();
79 bool isReplace = data.ReadBool();
80 sptr<IRemoteObject> object = data.ReadRemoteObject();
81 if (object == nullptr) {
82 LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
83 return ERR_APPEXECFWK_PARCEL_ERROR;
84 }
85 sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
86
87 auto ret = DeployQuickFix(bundleFilePaths, statusCallback, isDebug, targetPath, isReplace);
88 if (!reply.WriteInt32(ret)) {
89 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
90 return ERR_APPEXECFWK_PARCEL_ERROR;
91 }
92 return ERR_OK;
93 }
94
HandleSwitchQuickFix(MessageParcel & data,MessageParcel & reply)95 ErrCode QuickFixManagerHost::HandleSwitchQuickFix(MessageParcel& data, MessageParcel& reply)
96 {
97 LOG_I(BMS_TAG_DEFAULT, "begin to HandleSwitchQuickFix");
98 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
99 std::string bundleName = data.ReadString();
100 bool enable = data.ReadBool();
101 sptr<IRemoteObject> object = data.ReadRemoteObject();
102 if (object == nullptr) {
103 LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
104 return ERR_APPEXECFWK_PARCEL_ERROR;
105 }
106 sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
107
108 auto ret = SwitchQuickFix(bundleName, enable, statusCallback);
109 if (!reply.WriteInt32(ret)) {
110 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
111 return ERR_APPEXECFWK_PARCEL_ERROR;
112 }
113 return ERR_OK;
114 }
115
HandleDeleteQuickFix(MessageParcel & data,MessageParcel & reply)116 ErrCode QuickFixManagerHost::HandleDeleteQuickFix(MessageParcel& data, MessageParcel& reply)
117 {
118 LOG_I(BMS_TAG_DEFAULT, "begin to HandleDeleteQuickFix");
119 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
120 std::string bundleName = data.ReadString();
121 sptr<IRemoteObject> object = data.ReadRemoteObject();
122 if (object == nullptr) {
123 LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
124 return ERR_APPEXECFWK_PARCEL_ERROR;
125 }
126 sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
127
128 auto ret = DeleteQuickFix(bundleName, statusCallback);
129 if (!reply.WriteInt32(ret)) {
130 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
131 return ERR_APPEXECFWK_PARCEL_ERROR;
132 }
133 return ERR_OK;
134 }
135
HandleCreateFd(MessageParcel & data,MessageParcel & reply)136 ErrCode QuickFixManagerHost::HandleCreateFd(MessageParcel& data, MessageParcel& reply)
137 {
138 LOG_D(BMS_TAG_DEFAULT, "begin to HandleCreateFd");
139 HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
140 std::string fileName = data.ReadString();
141 int32_t fd = -1;
142 std::string path;
143 auto ret = CreateFd(fileName, fd, path);
144 if (!reply.WriteInt32(ret)) {
145 LOG_E(BMS_TAG_DEFAULT, "write ret failed");
146 close(fd);
147 return ERR_APPEXECFWK_PARCEL_ERROR;
148 }
149 if (ret == ERR_OK) {
150 if (!reply.WriteFileDescriptor(fd)) {
151 LOG_E(BMS_TAG_DEFAULT, "write fd failed");
152 close(fd);
153 return ERR_APPEXECFWK_PARCEL_ERROR;
154 }
155 if (!reply.WriteString(path)) {
156 LOG_E(BMS_TAG_DEFAULT, "write path failed");
157 close(fd);
158 return ERR_APPEXECFWK_PARCEL_ERROR;
159 }
160 }
161 close(fd);
162 return ERR_OK;
163 }
164 } // AppExecFwk
165 } // namespace OHOS
166