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