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_status_callback_host.h"
17
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_framework_core_ipc_interface_code.h"
21 #include "bundle_memory_guard.h"
22 #include "ipc_types.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
QuickFixStatusCallbackHost()27 QuickFixStatusCallbackHost::QuickFixStatusCallbackHost()
28 {
29 APP_LOGI("create QuickFixStatusCallbackHost.");
30 Init();
31 }
32
~QuickFixStatusCallbackHost()33 QuickFixStatusCallbackHost::~QuickFixStatusCallbackHost()
34 {
35 APP_LOGI("destroy QuickFixStatusCallbackHost.");
36 }
37
Init()38 void QuickFixStatusCallbackHost::Init()
39 {
40 funcMap_.emplace(static_cast<uint32_t>(QuickFixStatusCallbackInterfaceCode::ON_PATCH_DEPLOYED),
41 &QuickFixStatusCallbackHost::HandleOnPatchDeployed);
42 funcMap_.emplace(static_cast<uint32_t>(QuickFixStatusCallbackInterfaceCode::ON_PATCH_SWITCHED),
43 &QuickFixStatusCallbackHost::HandleOnPatchSwitched);
44 funcMap_.emplace(static_cast<uint32_t>(QuickFixStatusCallbackInterfaceCode::ON_PATCH_DELETED),
45 &QuickFixStatusCallbackHost::HandleOnPatchDeleted);
46 }
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int QuickFixStatusCallbackHost::OnRemoteRequest(
49 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51 BundleMemoryGuard memoryGuard;
52 APP_LOGD("QuickFixStatusCallbackHost onReceived message, the message code is %{public}u", code);
53 std::u16string descripter = QuickFixStatusCallbackHost::GetDescriptor();
54 std::u16string remoteDescripter = data.ReadInterfaceToken();
55 if (descripter != remoteDescripter) {
56 APP_LOGE("fail to write reply message in clean cache host due to the reply is nullptr");
57 return OBJECT_NULL;
58 }
59 if (funcMap_.find(code) != funcMap_.end() && funcMap_[code] != nullptr) {
60 (this->*funcMap_[code])(data, reply);
61 } else {
62 APP_LOGW("quickfix callback host receives unknown code, code = %{public}u", code);
63 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64 }
65 APP_LOGD("quickfix callback host finish to process message");
66 return NO_ERROR;
67 }
68
HandleOnPatchDeployed(MessageParcel & data,MessageParcel & reply)69 void QuickFixStatusCallbackHost::HandleOnPatchDeployed(MessageParcel &data, MessageParcel &reply)
70 {
71 APP_LOGI("start to process deployed patch callback message");
72 std::shared_ptr<QuickFixResult> resPtr(data.ReadParcelable<DeployQuickFixResult>());
73 if (resPtr == nullptr) {
74 APP_LOGE("read DeployQuickFixResult failed");
75 std::shared_ptr<QuickFixResult> deployRes = std::make_shared<DeployQuickFixResult>();
76 deployRes->SetResCode(ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR);
77 OnPatchDeployed(deployRes);
78 return;
79 }
80
81 OnPatchDeployed(resPtr);
82 }
83
HandleOnPatchSwitched(MessageParcel & data,MessageParcel & reply)84 void QuickFixStatusCallbackHost::HandleOnPatchSwitched(MessageParcel &data, MessageParcel &reply)
85 {
86 APP_LOGI("start to process switched patch callback message");
87 std::shared_ptr<QuickFixResult> resPtr(data.ReadParcelable<SwitchQuickFixResult>());
88 if (resPtr == nullptr) {
89 APP_LOGE("read SwitchQuickFixResult failed");
90 std::shared_ptr<QuickFixResult> switchRes = std::make_shared<SwitchQuickFixResult>();
91 switchRes->SetResCode(ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR);
92 OnPatchSwitched(switchRes);
93 return;
94 }
95
96 OnPatchSwitched(resPtr);
97 }
98
HandleOnPatchDeleted(MessageParcel & data,MessageParcel & reply)99 void QuickFixStatusCallbackHost::HandleOnPatchDeleted(MessageParcel &data, MessageParcel &reply)
100 {
101 APP_LOGI("start to process deleted patch callback message");
102 std::shared_ptr<DeleteQuickFixResult> resPtr(data.ReadParcelable<DeleteQuickFixResult>());
103 if (resPtr == nullptr) {
104 APP_LOGE("read DeleteQuickFixResult failed");
105 std::shared_ptr<QuickFixResult> deleteRes = std::make_shared<DeleteQuickFixResult>();
106 deleteRes->SetResCode(ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR);
107 OnPatchDeleted(deleteRes);
108 return;
109 }
110
111 OnPatchDeleted(resPtr);
112 }
113 } // AppExecFwk
114 } // OHOS
115