• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_connect_callback_stub.h"
17 
18 #include "ability_connect_callback_proxy.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)23 bool AbilityConnectionProxy::WriteInterfaceToken(MessageParcel &data)
24 {
25     if (!data.WriteInterfaceToken(AbilityConnectionProxy::GetDescriptor())) {
26         TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteInterfaceToken fail");
27         return false;
28     }
29     return true;
30 }
31 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)32 void AbilityConnectionProxy::OnAbilityConnectDone(
33     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
34 {
35     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnAbilityConnectDone resultCode: %{public}d", resultCode);
36     int error;
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option(MessageOption::TF_ASYNC);
40 
41     if (!WriteInterfaceToken(data)) {
42         TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteInterfaceToken fail");
43         return;
44     }
45 
46     if (!data.WriteParcelable(&element)) {
47         TAG_LOGE(AAFwkTag::ABILITYMGR, "element error");
48         return;
49     }
50 
51     if (!data.WriteRemoteObject(remoteObject)) {
52         TAG_LOGE(AAFwkTag::ABILITYMGR, "remoteObject error");
53         return;
54     }
55 
56     if (!data.WriteInt32(resultCode)) {
57         TAG_LOGE(AAFwkTag::ABILITYMGR, "resultCode error");
58         return;
59     }
60 
61     error = SendTransactCmd(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
62     if (error != NO_ERROR) {
63         TAG_LOGE(AAFwkTag::ABILITYMGR, "Connect done fail, error: %{public}d", error);
64         return;
65     }
66 }
67 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)68 void AbilityConnectionProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
69 {
70     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnAbilityDisconnectDone resultCode: %{public}d", resultCode);
71     int error;
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option(MessageOption::TF_ASYNC);
75 
76     if (!WriteInterfaceToken(data)) {
77         TAG_LOGE(AAFwkTag::ABILITYMGR, "WriteInterfaceToken fail");
78         return;
79     }
80     if (!data.WriteParcelable(&element) || !data.WriteInt32(resultCode)) {
81         TAG_LOGE(AAFwkTag::ABILITYMGR, "data write error");
82         return;
83     }
84 
85     error = SendTransactCmd(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
86     if (error != NO_ERROR) {
87         TAG_LOGE(AAFwkTag::ABILITYMGR, "Disconnect done fail, error: %d", error);
88         return;
89     }
90 }
91 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)92 int32_t AbilityConnectionProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
93     MessageParcel &reply, MessageOption &option)
94 {
95     sptr<IRemoteObject> remote = Remote();
96     if (remote == nullptr) {
97         TAG_LOGE(AAFwkTag::ABILITYMGR, "null remote");
98         return ERR_NULL_OBJECT;
99     }
100 
101     int32_t ret = remote->SendRequest(code, data, reply, option);
102     if (ret != NO_ERROR) {
103         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail. code: %{public}d, ret: %{public}d", code, ret);
104         return ret;
105     }
106     return NO_ERROR;
107 }
108 
AbilityConnectionStub()109 AbilityConnectionStub::AbilityConnectionStub()
110 {}
111 
~AbilityConnectionStub()112 AbilityConnectionStub::~AbilityConnectionStub()
113 {}
114 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)115 int AbilityConnectionStub::OnRemoteRequest(
116     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
117 {
118     TAG_LOGD(AAFwkTag::ABILITYMGR, "code: %{public}u", code);
119     std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
120     std::u16string remoteDescriptor = data.ReadInterfaceToken();
121     if (descriptor != remoteDescriptor) {
122         TAG_LOGE(AAFwkTag::ABILITYMGR, "descriptor not equal to remote");
123         return ERR_INVALID_STATE;
124     }
125 
126     std::unique_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
127     if (element == nullptr) {
128         TAG_LOGE(AAFwkTag::ABILITYMGR, "null element");
129         return ERR_INVALID_VALUE;
130     }
131     TAG_LOGD(AAFwkTag::ABILITYMGR, "Call callback");
132     auto abilityName = element->GetAbilityName();
133     switch (code) {
134         case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
135             auto remoteObject = data.ReadRemoteObject();
136             if (remoteObject == nullptr) {
137                 TAG_LOGE(AAFwkTag::ABILITYMGR, "null remoteObject");
138                 return ERR_INVALID_VALUE;
139             }
140             auto resultCode = data.ReadInt32();
141             TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_CONNECT_DONE");
142             OnAbilityConnectDone(*element, remoteObject, resultCode);
143             TAG_LOGI(AAFwkTag::ABILITYMGR, "ON_ABILITY_CONNECT_DONE end,%{public}s",
144                 abilityName.c_str());
145             return NO_ERROR;
146         }
147         case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
148             auto resultCode = data.ReadInt32();
149             TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_DISCONNECT_DONE");
150             OnAbilityDisconnectDone(*element, resultCode);
151             TAG_LOGI(AAFwkTag::ABILITYMGR, "ON_ABILITY_DISCONNECT_DONE end,%{public}s",
152                 abilityName.c_str());
153             return NO_ERROR;
154         }
155         case IAbilityConnection::ON_REMOTE_STATE_CHANGED: {
156             int32_t abilityState = data.ReadInt32();
157             OnRemoteStateChanged(*element, abilityState);
158             return NO_ERROR;
159         }
160         default: {
161             TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityConnectionStub default");
162             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
163         }
164     }
165 }
166 
OnRemoteDied(const wptr<IRemoteObject> & remote)167 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
168 {
169     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
170     if (handler_) {
171         handler_(remote);
172     }
173 }
174 
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)175 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
176 {}
177 
~AbilityConnectCallbackRecipient()178 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
179 {}
180 }  // namespace AAFwk
181 }  // namespace OHOS
182