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 #include "reverse_continuation_scheduler_replica_proxy.h"
16 
17 #include "hilog_tag_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
ReverseContinuationSchedulerReplicaProxy(const sptr<IRemoteObject> & remoteObject)21 ReverseContinuationSchedulerReplicaProxy::ReverseContinuationSchedulerReplicaProxy(
22     const sptr<IRemoteObject> &remoteObject)
23     : IRemoteProxy<IReverseContinuationSchedulerReplica>(remoteObject)
24 {}
25 
AsObject()26 sptr<IRemoteObject> ReverseContinuationSchedulerReplicaProxy::AsObject()
27 {
28     sptr<IRemoteObject> remoteObject = Remote();
29 
30     return remoteObject;
31 }
32 
PassPrimary(const sptr<IRemoteObject> & primary)33 void ReverseContinuationSchedulerReplicaProxy::PassPrimary(const sptr<IRemoteObject> &primary)
34 {
35     MessageParcel data;
36     MessageParcel reply;
37     MessageOption option(MessageOption::TF_SYNC);
38     if (!data.WriteInterfaceToken(ReverseContinuationSchedulerReplicaProxy::GetDescriptor())) {
39         TAG_LOGE(AAFwkTag::CONTINUATION,
40             "write interface token failed");
41         return;
42     }
43     if (primary) {
44         if (!data.WriteBool(true) || !data.WriteRemoteObject(primary)) {
45             TAG_LOGE(AAFwkTag::CONTINUATION, "Failed to write flag and primary");
46             return;
47         }
48     } else {
49         TAG_LOGD(AAFwkTag::CONTINUATION, "primary is nullptr");
50         if (!data.WriteBool(false)) {
51             TAG_LOGE(AAFwkTag::CONTINUATION, "Failed to write flag");
52             return;
53         }
54     }
55 
56     int32_t ret = SendTransactCmd(
57         static_cast<uint32_t>(IReverseContinuationSchedulerReplica::Message::PASS_PRIMARY), data, reply, option);
58     if (ret != NO_ERROR) {
59         TAG_LOGE(AAFwkTag::CONTINUATION,
60             "SendRequest failed");
61     }
62 }
63 
ReverseContinuation()64 bool ReverseContinuationSchedulerReplicaProxy::ReverseContinuation()
65 {
66     MessageParcel data;
67     MessageParcel reply;
68     MessageOption option(MessageOption::TF_SYNC);
69     if (!data.WriteInterfaceToken(ReverseContinuationSchedulerReplicaProxy::GetDescriptor())) {
70         TAG_LOGE(AAFwkTag::CONTINUATION,
71             "write interface token failed");
72         return false;
73     }
74 
75     if (SendTransactCmd(
76         static_cast<uint32_t>(IReverseContinuationSchedulerReplica::Message::REVERSE_CONTINUATION),
77         data,
78         reply,
79         option) != NO_ERROR) {
80         TAG_LOGE(AAFwkTag::CONTINUATION,
81             "SendRequest failed");
82         return false;
83     }
84     return true;
85 }
86 
NotifyReverseResult(int reverseResult)87 void ReverseContinuationSchedulerReplicaProxy::NotifyReverseResult(int reverseResult)
88 {
89     MessageParcel data;
90     MessageParcel reply;
91     MessageOption option(MessageOption::TF_SYNC);
92     if (!data.WriteInterfaceToken(ReverseContinuationSchedulerReplicaProxy::GetDescriptor())) {
93         TAG_LOGE(AAFwkTag::CONTINUATION,
94             "write interface token failed");
95         return;
96     }
97     if (!data.WriteInt32(reverseResult)) {
98         TAG_LOGE(AAFwkTag::CONTINUATION,
99             "write parcel flags failed");
100         return;
101     }
102     if (SendTransactCmd(
103         static_cast<uint32_t>(IReverseContinuationSchedulerReplica::Message::NOTIFY_REVERSE_RESULT),
104         data,
105         reply,
106         option) != NO_ERROR) {
107         TAG_LOGE(AAFwkTag::CONTINUATION,
108             "SendRequest failed");
109     }
110 }
111 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int32_t ReverseContinuationSchedulerReplicaProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
113     MessageParcel &reply, MessageOption &option)
114 {
115     sptr<IRemoteObject> remote = Remote();
116     if (remote == nullptr) {
117         TAG_LOGE(AAFwkTag::CONTINUATION, "Remote is nullptr");
118         return ERR_NULL_OBJECT;
119     }
120 
121     return remote->SendRequest(code, data, reply, option);
122 }
123 }  // namespace AppExecFwk
124 }  // namespace OHOS
125