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