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