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 "continuation_manager/app_connection_stub.h"
17 
18 #include "base/continuationmgr_log.h"
19 #include "distributed_ability_manager_service.h"
20 #include "ipc_types.h"
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 using namespace AAFwk;
25 namespace {
26 const std::string TAG = "AppConnectionStub";
27 }
28 
AppConnectionStub(int32_t token,const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams)29 AppConnectionStub::AppConnectionStub(int32_t token,
30     const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
31 {
32     token_ = token;
33     continuationExtraParams_ = continuationExtraParams;
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t AppConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
37     MessageParcel& reply, MessageOption& option)
38 {
39     HILOGD("code = %{public}u", code);
40     std::u16string descriptor = IAbilityConnection::GetDescriptor();
41     std::u16string remoteDescriptor = data.ReadInterfaceToken();
42     if (descriptor != remoteDescriptor) {
43         HILOGE("local descriptor is not equal to remote");
44         return ERR_INVALID_STATE;
45     }
46 
47     sptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
48     if (element == nullptr) {
49         HILOGE("element is nullptr");
50         return ERR_INVALID_VALUE;
51     }
52     int32_t resultCode = ERR_NONE;
53     switch (code) {
54         case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
55             if (auto remoteObject = data.ReadRemoteObject()) {
56                 resultCode = data.ReadInt32();
57                 OnAbilityConnectDone(*element, remoteObject, resultCode);
58                 return ERR_NONE;
59             }
60             HILOGE("remoteObject is nullptr");
61             return ERR_INVALID_DATA;
62         }
63         case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
64             resultCode = data.ReadInt32();
65             OnAbilityDisconnectDone(*element, resultCode);
66             return ERR_NONE;
67         }
68         default: {
69             HILOGE("unknown request code, please check");
70             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71         }
72     }
73 }
74 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)75 void AppConnectionStub::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
76     const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
77 {
78     HILOGD("called.");
79     DistributedAbilityManagerService::GetInstance().ScheduleStartDeviceManager(remoteObject,
80         token_, continuationExtraParams_);
81 }
82 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)83 void AppConnectionStub::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
84     int32_t resultCode)
85 {
86     HILOGD("called.");
87     DistributedAbilityManagerService::GetInstance().ScheduleStartDeviceManager(nullptr,
88         token_, continuationExtraParams_);
89 }
90 } // namespace DistributedSchedule
91 } // namespace OHOS