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()30 AbilityConnectionWrapperStub::AbilityConnectionWrapperStub()
31 {
32 }
33
AbilityConnectionWrapperStub(sptr<IRemoteObject> connection)34 AbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection)
35 {
36 distributedConnection_ = connection;
37 }
38
AbilityConnectionWrapperStub(sptr<IRemoteObject> connection,const std::string & localDeviceId)39 AbilityConnectionWrapperStub::AbilityConnectionWrapperStub(sptr<IRemoteObject> connection,
40 const std::string& localDeviceId)
41 {
42 distributedConnection_ = connection;
43 isCall_ = true;
44 localDeviceId_ = localDeviceId;
45 }
46
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)47 int32_t AbilityConnectionWrapperStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
48 MessageParcel& reply, MessageOption& option)
49 {
50 HILOGI("AbilityConnectionWrapperStub::OnRemoteRequest code = %{public}u", code);
51 std::u16string descriptor = IAbilityConnection::GetDescriptor();
52 std::u16string remoteDescriptor = data.ReadInterfaceToken();
53 if (descriptor != remoteDescriptor) {
54 HILOGE("AbilityConnectionWrapperStub local descriptor is not equal to remote");
55 return ERR_INVALID_STATE;
56 }
57
58 sptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
59 if (element == nullptr) {
60 HILOGE("AbilityConnectionWrapperStub element is null");
61 return ERR_INVALID_VALUE;
62 }
63 int32_t resultCode = ERR_NONE;
64 switch (code) {
65 case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
66 if (auto remoteObject = data.ReadRemoteObject()) {
67 resultCode = data.ReadInt32();
68 OnAbilityConnectDone(*element, remoteObject, resultCode);
69 return ERR_NONE;
70 }
71 HILOGE("AbilityConnectionWrapperStub remoteObject is null");
72 return ERR_INVALID_DATA;
73 }
74 case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
75 resultCode = data.ReadInt32();
76 OnAbilityDisconnectDone(*element, resultCode);
77 return ERR_NONE;
78 }
79 default: {
80 HILOGE("AbilityConnectionWrapperStub unknown code");
81 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82 }
83 }
84 }
85
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)86 void AbilityConnectionWrapperStub::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
87 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
88 {
89 if (distributedConnection_ == nullptr) {
90 HILOGI("called by collab");
91 return;
92 }
93 auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
94 if (isCall_) {
95 HILOGI("OnAbilityConnectDone get caller callback");
96 AppExecFwk::ElementName elementWithDeviceId(localDeviceId_, element.GetBundleName(), element.GetAbilityName());
97 DistributedSchedAdapter::GetInstance().ProcessCallResult(remoteObject, distributedConnection_);
98 proxy->OnAbilityConnectDone(elementWithDeviceId, remoteObject, resultCode);
99 return;
100 }
101 proxy->OnAbilityConnectDone(element, remoteObject, resultCode);
102 }
103
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)104 void AbilityConnectionWrapperStub::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
105 int32_t resultCode)
106 {
107 if (distributedConnection_ == nullptr) {
108 HILOGI("called by collab");
109 return;
110 }
111 if (isCall_) {
112 HILOGI("OnAbilityDisconnectDone release caller");
113 DistributedSchedAdapter::GetInstance().ProcessCalleeDied(distributedConnection_);
114 return;
115 }
116 auto proxy = std::make_unique<AbilityConnectionWrapperProxy>(distributedConnection_);
117 proxy->OnAbilityDisconnectDone(element, resultCode);
118 }
119 } // namespace DistributedSchedule
120 } // namespace OHOS