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 "want_receiver_proxy.h"
17
18 #include "hilog_wrapper.h"
19 #include "ipc_types.h"
20
21 namespace OHOS {
22 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)23 bool WantReceiverProxy::WriteInterfaceToken(MessageParcel &data)
24 {
25 if (!data.WriteInterfaceToken(WantReceiverProxy::GetDescriptor())) {
26 HILOG_ERROR("write interface token failed");
27 return false;
28 }
29 return true;
30 }
31
Send(const int32_t resultCode)32 void WantReceiverProxy::Send(const int32_t resultCode)
33 {
34 MessageParcel data;
35 MessageParcel reply;
36 MessageOption option(MessageOption::TF_SYNC);
37 if (!WriteInterfaceToken(data)) {
38 return;
39 }
40 data.WriteInt32(resultCode);
41 sptr<IRemoteObject> remote = Remote();
42 if (remote == nullptr) {
43 HILOG_ERROR("Remote() is NULL");
44 return;
45 }
46 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IWantReceiver::WANT_RECEIVER_SEND), data, reply, option);
47 if (ret != NO_ERROR) {
48 HILOG_ERROR("SendRequest is failed, error code: %{public}d", ret);
49 }
50 }
51
PerformReceive(const Want & want,int resultCode,const std::string & data,const WantParams & extras,bool serialized,bool sticky,int sendingUser)52 void WantReceiverProxy::PerformReceive(const Want &want, int resultCode, const std::string &data,
53 const WantParams &extras, bool serialized, bool sticky, int sendingUser)
54 {
55 MessageParcel msgData;
56 MessageParcel reply;
57 MessageOption option(MessageOption::TF_SYNC);
58 if (!WriteInterfaceToken(msgData)) {
59 return;
60 }
61 msgData.WriteParcelable(&want);
62 msgData.WriteInt32(resultCode);
63 msgData.WriteString16(Str8ToStr16(data));
64 msgData.WriteParcelable(&extras);
65 msgData.WriteBool(serialized);
66 msgData.WriteBool(sticky);
67 msgData.WriteInt32(sendingUser);
68 sptr<IRemoteObject> remote = Remote();
69 if (remote == nullptr) {
70 HILOG_ERROR("Remote() is NULL");
71 return;
72 }
73 int32_t ret = remote->SendRequest(
74 static_cast<uint32_t>(IWantReceiver::WANT_RECEIVER_PERFORM_RECEIVE), msgData, reply, option);
75 if (ret != NO_ERROR) {
76 HILOG_ERROR("SendRequest is failed, error code: %{public}d", ret);
77 }
78 }
79 } // namespace AAFwk
80 } // namespace OHOS