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 "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(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
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     sptr<IRemoteObject> object = data.ReadRemoteObject();
80     if (object == nullptr) {
81         LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
82         return ERR_APPEXECFWK_PARCEL_ERROR;
83     }
84     sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
85 
86     auto ret = DeployQuickFix(bundleFilePaths, statusCallback, isDebug, targetPath);
87     if (!reply.WriteInt32(ret)) {
88         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
89         return ERR_APPEXECFWK_PARCEL_ERROR;
90     }
91     return ERR_OK;
92 }
93 
HandleSwitchQuickFix(MessageParcel & data,MessageParcel & reply)94 ErrCode QuickFixManagerHost::HandleSwitchQuickFix(MessageParcel& data, MessageParcel& reply)
95 {
96     LOG_I(BMS_TAG_DEFAULT, "begin to HandleSwitchQuickFix");
97     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
98     std::string bundleName = data.ReadString();
99     bool enable = data.ReadBool();
100     sptr<IRemoteObject> object = data.ReadRemoteObject();
101     if (object == nullptr) {
102         LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
103         return ERR_APPEXECFWK_PARCEL_ERROR;
104     }
105     sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
106 
107     auto ret = SwitchQuickFix(bundleName, enable, statusCallback);
108     if (!reply.WriteInt32(ret)) {
109         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
110         return ERR_APPEXECFWK_PARCEL_ERROR;
111     }
112     return ERR_OK;
113 }
114 
HandleDeleteQuickFix(MessageParcel & data,MessageParcel & reply)115 ErrCode QuickFixManagerHost::HandleDeleteQuickFix(MessageParcel& data, MessageParcel& reply)
116 {
117     LOG_I(BMS_TAG_DEFAULT, "begin to HandleDeleteQuickFix");
118     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
119     std::string bundleName = data.ReadString();
120     sptr<IRemoteObject> object = data.ReadRemoteObject();
121     if (object == nullptr) {
122         LOG_E(BMS_TAG_DEFAULT, "read statusCallback failed");
123         return ERR_APPEXECFWK_PARCEL_ERROR;
124     }
125     sptr<IQuickFixStatusCallback> statusCallback = iface_cast<IQuickFixStatusCallback>(object);
126 
127     auto ret = DeleteQuickFix(bundleName, statusCallback);
128     if (!reply.WriteInt32(ret)) {
129         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
130         return ERR_APPEXECFWK_PARCEL_ERROR;
131     }
132     return ERR_OK;
133 }
134 
HandleCreateFd(MessageParcel & data,MessageParcel & reply)135 ErrCode QuickFixManagerHost::HandleCreateFd(MessageParcel& data, MessageParcel& reply)
136 {
137     LOG_D(BMS_TAG_DEFAULT, "begin to HandleCreateFd");
138     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
139     std::string fileName = data.ReadString();
140     int32_t fd = -1;
141     std::string path;
142     auto ret = CreateFd(fileName, fd, path);
143     if (!reply.WriteInt32(ret)) {
144         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
145         close(fd);
146         return ERR_APPEXECFWK_PARCEL_ERROR;
147     }
148     if (ret == ERR_OK) {
149         if (!reply.WriteFileDescriptor(fd)) {
150             LOG_E(BMS_TAG_DEFAULT, "write fd failed");
151             close(fd);
152             return ERR_APPEXECFWK_PARCEL_ERROR;
153         }
154         if (!reply.WriteString(path)) {
155             LOG_E(BMS_TAG_DEFAULT, "write path failed");
156             close(fd);
157             return ERR_APPEXECFWK_PARCEL_ERROR;
158         }
159     }
160     close(fd);
161     return ERR_OK;
162 }
163 } // AppExecFwk
164 } // namespace OHOS
165