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