• 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     int error;
40     MessageParcel data;
41     MessageParcel reply;
42     MessageOption option;
43 
44     if (!WriteInterfaceToken(data)) {
45         return;
46     }
47 
48     if (!data.WriteParcelable(&element)) {
49         HILOG_ERROR("Connect done element error.");
50         return;
51     }
52 
53     if (!data.WriteParcelable(remoteObject)) {
54         HILOG_ERROR("Connect done remote object error.");
55         return;
56     }
57 
58     if (!data.WriteInt32(resultCode)) {
59         HILOG_ERROR("Connect done result code error.");
60         return;
61     }
62 
63     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
64     if (error != NO_ERROR) {
65         HILOG_ERROR("Connect done fail, error: %{public}d", error);
66         return;
67     }
68 }
69 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)70 void AbilityConnectionProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
71 {
72     int error;
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76 
77     if (!WriteInterfaceToken(data)) {
78         return;
79     }
80     if (!data.WriteParcelable(&element) || !data.WriteInt32(resultCode)) {
81         HILOG_ERROR("Disconnect done data write error.");
82         return;
83     }
84 
85     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
86     if (error != NO_ERROR) {
87         HILOG_ERROR("Disconnect done fail, error: %d", error);
88         return;
89     }
90 }
91 
AbilityConnectionStub()92 AbilityConnectionStub::AbilityConnectionStub()
93 {}
94 
~AbilityConnectionStub()95 AbilityConnectionStub::~AbilityConnectionStub()
96 {}
97 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)98 int AbilityConnectionStub::OnRemoteRequest(
99     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
100 {
101     std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
102     std::u16string remoteDescriptor = data.ReadInterfaceToken();
103     if (descriptor != remoteDescriptor) {
104         HILOG_INFO("Local descriptor is not equal to remote");
105         return ERR_INVALID_STATE;
106     }
107 
108     auto element = data.ReadParcelable<AppExecFwk::ElementName>();
109     switch (code) {
110         case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
111             if (element == nullptr) {
112                 HILOG_ERROR("callback stub receive element is nullptr");
113                 return ERR_INVALID_VALUE;
114             }
115             auto remoteObject = data.ReadParcelable<IRemoteObject>();
116             auto resultCode = data.ReadInt32();
117             OnAbilityConnectDone(*element, remoteObject, resultCode);
118             delete element;
119             return NO_ERROR;
120         }
121         case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
122             if (element == nullptr) {
123                 HILOG_ERROR("callback stub receive element is nullptr");
124                 return ERR_INVALID_VALUE;
125             }
126             auto resultCode = data.ReadInt32();
127             OnAbilityDisconnectDone(*element, resultCode);
128             delete element;
129             return NO_ERROR;
130         }
131         default: {
132             if (element != nullptr) {
133                 delete element;
134             }
135             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
136         }
137     }
138 }
139 
OnRemoteDied(const wptr<IRemoteObject> & remote)140 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
141 {
142     HILOG_ERROR("On remote died.");
143     if (handler_) {
144         handler_(remote);
145     }
146 }
147 
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)148 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
149 {}
150 
~AbilityConnectCallbackRecipient()151 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
152 {}
153 }  // namespace AAFwk
154 }  // namespace OHOS
155