• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2021 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ability_connect_callback_stub.h"
18 
19 #include "ability_connect_callback_proxy.h"
20 #include "hilog_wrapper.h"
21 #include "ipc_types.h"
22 #include "message_parcel.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)27 bool AbilityConnectionProxy::WriteInterfaceToken(MessageParcel &data)
28 {
29     if (!data.WriteInterfaceToken(AbilityConnectionProxy::GetDescriptor())) {
30         HILOG_ERROR("Write interface token failed.");
31         return false;
32     }
33     return true;
34 }
35 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)36 void AbilityConnectionProxy::OnAbilityConnectDone(
37     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
38 {
39     HILOG_INFO("OnAbilityConnectDone resultCode: %{public}d", resultCode);
40     int error;
41     MessageParcel data;
42     MessageParcel reply;
43     MessageOption option(MessageOption::TF_ASYNC);
44 
45     if (!WriteInterfaceToken(data)) {
46         HILOG_ERROR("Write interface token failed.");
47         return;
48     }
49 
50     if (!data.WriteParcelable(&element)) {
51         HILOG_ERROR("Connect done element error.");
52         return;
53     }
54 
55     if (!data.WriteRemoteObject(remoteObject)) {
56         HILOG_ERROR("Connect done remote object error.");
57         return;
58     }
59 
60     if (!data.WriteInt32(resultCode)) {
61         HILOG_ERROR("Connect done result code error.");
62         return;
63     }
64 
65     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
66     if (error != NO_ERROR) {
67         HILOG_ERROR("Connect done fail, error: %{public}d", error);
68         return;
69     }
70 }
71 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)72 void AbilityConnectionProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
73 {
74     HILOG_INFO("OnAbilityDisconnectDone resultCode: %{public}d", resultCode);
75     int error;
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option(MessageOption::TF_ASYNC);
79 
80     if (!WriteInterfaceToken(data)) {
81         HILOG_ERROR("Write interface token failed.");
82         return;
83     }
84     if (!data.WriteParcelable(&element) || !data.WriteInt32(resultCode)) {
85         HILOG_ERROR("Disconnect done data write error.");
86         return;
87     }
88 
89     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
90     if (error != NO_ERROR) {
91         HILOG_ERROR("Disconnect done fail, error: %d", error);
92         return;
93     }
94 }
95 
AbilityConnectionStub()96 AbilityConnectionStub::AbilityConnectionStub()
97 {}
98 
~AbilityConnectionStub()99 AbilityConnectionStub::~AbilityConnectionStub()
100 {}
101 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)102 int AbilityConnectionStub::OnRemoteRequest(
103     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
104 {
105     HILOG_INFO("AbilityConnectionStub::OnRemoteRequest code: %{public}ud", code);
106     std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
107     std::u16string remoteDescriptor = data.ReadInterfaceToken();
108     if (descriptor != remoteDescriptor) {
109         HILOG_ERROR("Local descriptor is not equal to remote");
110         return ERR_INVALID_STATE;
111     }
112 
113     auto element = data.ReadParcelable<AppExecFwk::ElementName>();
114     switch (code) {
115         case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
116             if (element == nullptr) {
117                 HILOG_ERROR("callback stub receive element is nullptr");
118                 return ERR_INVALID_VALUE;
119             }
120             auto remoteObject = data.ReadRemoteObject();
121             if (remoteObject == nullptr) {
122                 HILOG_ERROR("callback stub receive remoteObject is nullptr");
123                 delete element;
124                 return ERR_INVALID_VALUE;
125             }
126             auto resultCode = data.ReadInt32();
127             OnAbilityConnectDone(*element, remoteObject, resultCode);
128             delete element;
129             return NO_ERROR;
130         }
131         case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
132             if (element == nullptr) {
133                 HILOG_ERROR("callback stub receive element is nullptr");
134                 return ERR_INVALID_VALUE;
135             }
136             auto resultCode = data.ReadInt32();
137             OnAbilityDisconnectDone(*element, resultCode);
138             delete element;
139             return NO_ERROR;
140         }
141         default: {
142             if (element != nullptr) {
143                 delete element;
144             }
145             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
146         }
147     }
148 }
149 
OnRemoteDied(const wptr<IRemoteObject> & remote)150 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
151 {
152     HILOG_ERROR("On remote died.");
153     if (handler_) {
154         handler_(remote);
155     }
156 }
157 
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)158 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
159 {}
160 
~AbilityConnectCallbackRecipient()161 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
162 {}
163 }  // namespace AAFwk
164 }  // namespace OHOS
165