• 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 "want_receiver_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)22 bool 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 
Send(const int32_t resultCode)31 void 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     if (!data.WriteInt32(resultCode)) {
40         TAG_LOGE(AAFwkTag::WANTAGENT, "write resultCode failed");
41         return;
42     }
43     sptr<IRemoteObject> remote = Remote();
44     if (remote == nullptr) {
45         TAG_LOGE(AAFwkTag::WANTAGENT, "null remote");
46         return;
47     }
48     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IWantReceiver::WANT_RECEIVER_SEND), data, reply, option);
49     if (ret != NO_ERROR) {
50         TAG_LOGE(AAFwkTag::WANTAGENT, "error code: %{public}d", ret);
51     }
52 }
53 
PerformReceive(const Want & want,int resultCode,const std::string & data,const WantParams & extras,bool serialized,bool sticky,int sendingUser)54 void WantReceiverProxy::PerformReceive(const Want &want, int resultCode, const std::string &data,
55     const WantParams &extras, bool serialized, bool sticky, int sendingUser)
56 {
57     MessageParcel msgData;
58     MessageParcel reply;
59     MessageOption option(MessageOption::TF_SYNC);
60     if (!WriteInterfaceToken(msgData)) {
61         return;
62     }
63     if (!msgData.WriteParcelable(&want)) {
64         TAG_LOGE(AAFwkTag::WANTAGENT, "write want failed");
65         return;
66     }
67     if (!msgData.WriteInt32(resultCode)) {
68         TAG_LOGE(AAFwkTag::WANTAGENT, "write resultCode failed");
69         return;
70     }
71     if (!msgData.WriteString16(Str8ToStr16(data))) {
72         TAG_LOGE(AAFwkTag::WANTAGENT, "write data failed");
73         return;
74     }
75     if (!msgData.WriteParcelable(&extras)) {
76         TAG_LOGE(AAFwkTag::WANTAGENT, "write extras failed");
77         return;
78     }
79     if (!msgData.WriteBool(serialized)) {
80         TAG_LOGE(AAFwkTag::WANTAGENT, "write serialized failed");
81         return;
82     }
83     if (!msgData.WriteBool(sticky)) {
84         TAG_LOGE(AAFwkTag::WANTAGENT, "write sticky failed");
85         return;
86     }
87     if (!msgData.WriteInt32(sendingUser)) {
88         TAG_LOGE(AAFwkTag::WANTAGENT, "write sendingUser failed");
89         return;
90     }
91     sptr<IRemoteObject> remote = Remote();
92     if (remote == nullptr) {
93         TAG_LOGE(AAFwkTag::WANTAGENT, "null remote");
94         return;
95     }
96     int32_t ret = remote->SendRequest(
97         static_cast<uint32_t>(IWantReceiver::WANT_RECEIVER_PERFORM_RECEIVE), msgData, reply, option);
98     if (ret != NO_ERROR) {
99         TAG_LOGE(AAFwkTag::WANTAGENT, "error code: %{public}d", ret);
100     }
101 }
102 }  // namespace AAFwk
103 }  // namespace OHOS
104