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 "bundle/bundle_manager_callback_stub.h"
17 
18 #include "ability_manager_client.h"
19 #include "distributed_sched_service.h"
20 #include "dtbschedmgr_log.h"
21 #include "message_parcel.h"
22 #include "ipc_types.h"
23 
24 namespace OHOS {
25 namespace DistributedSchedule {
26 namespace {
27 const std::string TAG = "DmsBundleManagerCallbackStub";
28 constexpr int64_t CONTINUATION_FREE_INSTALL_TIMEOUT = 50000; // 50s
29 }
30 
DmsBundleManagerCallbackStub()31 DmsBundleManagerCallbackStub::DmsBundleManagerCallbackStub()
32 {
33     memberFuncMap_[static_cast<uint32_t>(IDBundleManagerCallbackInterfaceCod::ON_QUERY_INSTALLATION_DONE)] =
34         &DmsBundleManagerCallbackStub::OnQueryInstallationFinishedInner;
35 }
36 
OnQueryInstallationFinishedInner(MessageParcel & data,MessageParcel & reply)37 int32_t DmsBundleManagerCallbackStub::OnQueryInstallationFinishedInner(MessageParcel& data, MessageParcel& reply)
38 {
39     int32_t resultCode = data.ReadInt32();
40     uint32_t versionCode = data.ReadUint32();
41     int32_t missionId = data.ReadInt32();
42 
43     int32_t result = OnQueryInstallationFinished(resultCode, missionId, versionCode);
44     return result;
45 }
46 
OnQueryInstallationFinished(int32_t resultCode,int32_t missionId,int versionCode)47 int32_t DmsBundleManagerCallbackStub::OnQueryInstallationFinished(int32_t resultCode,
48     int32_t missionId, int versionCode)
49 {
50     HILOGI("bms callback received, missionId: %{public}d", missionId);
51     int32_t result = 0;
52     if (resultCode != ERR_OK) {
53         HILOGE("remote not installed and cannot install on remote device, resultCode = %{public}d", resultCode);
54         result = CONTINUE_REMOTE_UNINSTALLED_CANNOT_FREEINSTALL;
55         DistributedSchedService::GetInstance().NotifyContinuationCallbackResult(missionId, result);
56         return result;
57     }
58     DistributedSchedService::GetInstance().RemoveContinuationTimeout(missionId);
59 
60     HILOGI("able to install on target device, start continue ability with freeInstall");
61     std::string deviceId = DistributedSchedService::GetInstance().GetContinuaitonDevice(missionId);
62     DistributedSchedService::GetInstance().SetContinuationTimeout(missionId, CONTINUATION_FREE_INSTALL_TIMEOUT);
63     result = AAFwk::AbilityManagerClient::GetInstance()->ContinueAbility(deviceId, missionId, versionCode);
64     return result;
65 }
66 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int32_t DmsBundleManagerCallbackStub::OnRemoteRequest(
68     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
69 {
70     std::u16string descriptor = GetDescriptor();
71     std::u16string remoteDescriptor = data.ReadInterfaceToken();
72     if (descriptor != remoteDescriptor) {
73         HILOGE("Local descriptor is not equal to remote");
74         return ERR_INVALID_STATE;
75     }
76 
77     auto it = memberFuncMap_.find(code);
78     if (it == memberFuncMap_.end()) {
79         HILOGE("Not found");
80         return ERR_INVALID_STATE;
81     }
82 
83     return (this->*(it->second))(data, reply);
84 }
85 }  // namespace DistributedSchedule
86 }  // namespace OHOS
87