1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "ability_connection_wrapper_stub.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "ability_connection_wrapper_proxy.h"
19686862fbSopenharmony_ci#include "distributed_sched_adapter.h"
20686862fbSopenharmony_ci#include "dtbschedmgr_log.h"
21686862fbSopenharmony_ci#include "ipc_types.h"
22686862fbSopenharmony_ci
23686862fbSopenharmony_cinamespace OHOS {
24686862fbSopenharmony_cinamespace DistributedSchedule {
25686862fbSopenharmony_ciusing namespace AAFwk;
26686862fbSopenharmony_cinamespace {
27686862fbSopenharmony_ciconst std::string TAG = "AbilityConnectionWrapperStub";
28686862fbSopenharmony_ci}
29686862fbSopenharmony_ci
30686862fbSopenharmony_ciAbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection)
31686862fbSopenharmony_ci{
32686862fbSopenharmony_ci    distributedConnection_ = connection;
33686862fbSopenharmony_ci}
34686862fbSopenharmony_ci
35686862fbSopenharmony_ciAbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection,
36686862fbSopenharmony_ci    const std::string& localDeviceId)
37686862fbSopenharmony_ci{
38686862fbSopenharmony_ci    distributedConnection_ = connection;
39686862fbSopenharmony_ci    isCall_ = true;
40686862fbSopenharmony_ci    localDeviceId_ = localDeviceId;
41686862fbSopenharmony_ci}
42686862fbSopenharmony_ci
43686862fbSopenharmony_ciint32_t AbilityConnectionWrapperStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
44686862fbSopenharmony_ci    MessageParcel& reply, MessageOption& option)
45686862fbSopenharmony_ci{
46686862fbSopenharmony_ci    HILOGD("AbilityConnectionWrapperStub::OnRemoteRequest code = %{public}u", code);
47686862fbSopenharmony_ci    std::u16string descriptor = IAbilityConnection::GetDescriptor();
48686862fbSopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
49686862fbSopenharmony_ci    if (descriptor != remoteDescriptor) {
50686862fbSopenharmony_ci        HILOGE("AbilityConnectionWrapperStub local descriptor is not equal to remote");
51686862fbSopenharmony_ci        return ERR_INVALID_STATE;
52686862fbSopenharmony_ci    }
53686862fbSopenharmony_ci
54686862fbSopenharmony_ci    sptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
55686862fbSopenharmony_ci    if (element == nullptr) {
56686862fbSopenharmony_ci        HILOGE("AbilityConnectionWrapperStub element is null");
57686862fbSopenharmony_ci        return ERR_INVALID_VALUE;
58686862fbSopenharmony_ci    }
59686862fbSopenharmony_ci    int32_t resultCode = ERR_NONE;
60686862fbSopenharmony_ci    switch (code) {
61686862fbSopenharmony_ci        case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
62686862fbSopenharmony_ci            if (auto remoteObject = data.ReadRemoteObject()) {
63686862fbSopenharmony_ci                resultCode = data.ReadInt32();
64686862fbSopenharmony_ci                OnAbilityConnectDone(*element, remoteObject, resultCode);
65686862fbSopenharmony_ci                return ERR_NONE;
66686862fbSopenharmony_ci            }
67686862fbSopenharmony_ci            HILOGE("AbilityConnectionWrapperStub remoteObject is null");
68686862fbSopenharmony_ci            return ERR_INVALID_DATA;
69686862fbSopenharmony_ci        }
70686862fbSopenharmony_ci        case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
71686862fbSopenharmony_ci            resultCode = data.ReadInt32();
72686862fbSopenharmony_ci            OnAbilityDisconnectDone(*element, resultCode);
73686862fbSopenharmony_ci            return ERR_NONE;
74686862fbSopenharmony_ci        }
75686862fbSopenharmony_ci        default: {
76686862fbSopenharmony_ci            HILOGE("AbilityConnectionWrapperStub unknown code");
77686862fbSopenharmony_ci            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78686862fbSopenharmony_ci        }
79686862fbSopenharmony_ci    }
80686862fbSopenharmony_ci}
81686862fbSopenharmony_ci
82686862fbSopenharmony_civoid AbilityConnectionWrapperStub::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
83686862fbSopenharmony_ci    const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
84686862fbSopenharmony_ci{
85686862fbSopenharmony_ci    auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
86686862fbSopenharmony_ci    if (isCall_) {
87686862fbSopenharmony_ci        HILOGD("OnAbilityConnectDone get caller callback");
88686862fbSopenharmony_ci        AppExecFwk::ElementName elementWithDeviceId(localDeviceId_, element.GetBundleName(), element.GetAbilityName());
89686862fbSopenharmony_ci        DistributedSchedAdapter::GetInstance().ProcessCallResult(remoteObject, distributedConnection_);
90686862fbSopenharmony_ci        proxy->OnAbilityConnectDone(elementWithDeviceId, remoteObject, resultCode);
91686862fbSopenharmony_ci        return;
92686862fbSopenharmony_ci    }
93686862fbSopenharmony_ci    proxy->OnAbilityConnectDone(element, remoteObject, resultCode);
94686862fbSopenharmony_ci}
95686862fbSopenharmony_ci
96686862fbSopenharmony_civoid AbilityConnectionWrapperStub::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
97686862fbSopenharmony_ci    int32_t resultCode)
98686862fbSopenharmony_ci{
99686862fbSopenharmony_ci    if (isCall_) {
100686862fbSopenharmony_ci        HILOGD("OnAbilityDisconnectDone release caller");
101686862fbSopenharmony_ci        DistributedSchedAdapter::GetInstance().ProcessCalleeDied(distributedConnection_);
102686862fbSopenharmony_ci        return;
103686862fbSopenharmony_ci    }
104686862fbSopenharmony_ci    auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
105686862fbSopenharmony_ci    proxy->OnAbilityDisconnectDone(element, resultCode);
106686862fbSopenharmony_ci}
107686862fbSopenharmony_ci} // namespace DistributedSchedule
108686862fbSopenharmony_ci} // namespace OHOS