1 /*
2 * Copyright (c) 2021 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 "ability_connection_wrapper_stub.h"
17
18 #include "ability_connection_wrapper_proxy.h"
19 #include "distributed_sched_adapter.h"
20 #include "dtbschedmgr_log.h"
21 #include "ipc_types.h"
22
23 namespace OHOS {
24 namespace DistributedSchedule {
25 using namespace AAFwk;
26 namespace {
27 const std::string TAG = "AbilityConnectionWrapperStub";
28 }
29
AbilityConnectionWrapperStub(sptr<IRemoteObject> connection)30 AbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection)
31 {
32 distributedConnection_ = connection;
33 }
34
AbilityConnectionWrapperStub(sptr<IRemoteObject> connection,const std::string & localDeviceId)35 AbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection,
36 const std::string& localDeviceId)
37 {
38 distributedConnection_ = connection;
39 isCall_ = true;
40 localDeviceId_ = localDeviceId;
41 }
42
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)43 int32_t AbilityConnectionWrapperStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
44 MessageParcel& reply, MessageOption& option)
45 {
46 HILOGD("AbilityConnectionWrapperStub::OnRemoteRequest code = %{public}u", code);
47 std::u16string descriptor = IAbilityConnection::GetDescriptor();
48 std::u16string remoteDescriptor = data.ReadInterfaceToken();
49 if (descriptor != remoteDescriptor) {
50 HILOGE("AbilityConnectionWrapperStub local descriptor is not equal to remote");
51 return ERR_INVALID_STATE;
52 }
53
54 sptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
55 if (element == nullptr) {
56 HILOGE("AbilityConnectionWrapperStub element is null");
57 return ERR_INVALID_VALUE;
58 }
59 int32_t resultCode = ERR_NONE;
60 switch (code) {
61 case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
62 if (auto remoteObject = data.ReadRemoteObject()) {
63 resultCode = data.ReadInt32();
64 OnAbilityConnectDone(*element, remoteObject, resultCode);
65 return ERR_NONE;
66 }
67 HILOGE("AbilityConnectionWrapperStub remoteObject is null");
68 return ERR_INVALID_DATA;
69 }
70 case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
71 resultCode = data.ReadInt32();
72 OnAbilityDisconnectDone(*element, resultCode);
73 return ERR_NONE;
74 }
75 default: {
76 HILOGE("AbilityConnectionWrapperStub unknown code");
77 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 }
79 }
80 }
81
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)82 void AbilityConnectionWrapperStub::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
83 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
84 {
85 auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
86 if (isCall_) {
87 HILOGD("OnAbilityConnectDone get caller callback");
88 AppExecFwk::ElementName elementWithDeviceId(localDeviceId_, element.GetBundleName(), element.GetAbilityName());
89 DistributedSchedAdapter::GetInstance().ProcessCallResult(remoteObject, distributedConnection_);
90 proxy->OnAbilityConnectDone(elementWithDeviceId, remoteObject, resultCode);
91 return;
92 }
93 proxy->OnAbilityConnectDone(element, remoteObject, resultCode);
94 }
95
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)96 void AbilityConnectionWrapperStub::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
97 int32_t resultCode)
98 {
99 if (isCall_) {
100 HILOGD("OnAbilityDisconnectDone release caller");
101 DistributedSchedAdapter::GetInstance().ProcessCalleeDied(distributedConnection_);
102 return;
103 }
104 auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
105 proxy->OnAbilityDisconnectDone(element, resultCode);
106 }
107 } // namespace DistributedSchedule
108 } // namespace OHOS