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 "reverse_continuation_scheduler_replica.h"
17 #include "hilog_tag_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
ReverseContinuationSchedulerReplica(const std::shared_ptr<AbilityHandler> & mainHandler,const std::weak_ptr<IReverseContinuationSchedulerReplicaHandler> & replicaHandler)21 ReverseContinuationSchedulerReplica::ReverseContinuationSchedulerReplica(
22     const std::shared_ptr<AbilityHandler> &mainHandler,
23     const std::weak_ptr<IReverseContinuationSchedulerReplicaHandler> &replicaHandler)
24 {
25     mainHandler_ = mainHandler;
26     replicaHandler_ = replicaHandler;
27 }
28 
PassPrimary(const sptr<IRemoteObject> & primary)29 void ReverseContinuationSchedulerReplica::PassPrimary(const sptr<IRemoteObject> &primary)
30 {
31     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
32     auto task = [reverseContinuationSchedulerReplica = this, primary]() {
33         reverseContinuationSchedulerReplica->HandlerPassPrimary(primary);
34     };
35 
36     if (mainHandler_ == nullptr) {
37         TAG_LOGE(AAFwkTag::CONTINUATION, "mainHandler_ is nullptr");
38         return;
39     }
40 
41     bool ret = mainHandler_->PostTask(task);
42     if (!ret) {
43         TAG_LOGE(AAFwkTag::CONTINUATION, "PostTask error");
44     }
45 }
46 
ReverseContinuation()47 bool ReverseContinuationSchedulerReplica::ReverseContinuation()
48 {
49     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
50     auto task = [reverseContinuationSchedulerReplica = this]() {
51         reverseContinuationSchedulerReplica->HandlerReverseContinuation();
52     };
53 
54     if (mainHandler_ == nullptr) {
55         TAG_LOGE(AAFwkTag::CONTINUATION,
56             "mainHandler_ is nullptr");
57         return false;
58     }
59 
60     bool ret = mainHandler_->PostTask(task);
61     if (!ret) {
62         TAG_LOGE(AAFwkTag::CONTINUATION, "PostTask error");
63     }
64     return true;
65 }
66 
NotifyReverseResult(int reverseResult)67 void ReverseContinuationSchedulerReplica::NotifyReverseResult(int reverseResult)
68 {
69     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
70     auto task = [reverseContinuationSchedulerReplica = this, reverseResult]() {
71         reverseContinuationSchedulerReplica->HandlerNotifyReverseResult(reverseResult);
72     };
73 
74     if (mainHandler_ == nullptr) {
75         TAG_LOGE(AAFwkTag::CONTINUATION,
76             "mainHandler_ is nullptr");
77         return;
78     }
79 
80     bool ret = mainHandler_->PostTask(task);
81     if (!ret) {
82         TAG_LOGE(AAFwkTag::CONTINUATION, "PostTask error");
83     }
84 }
85 
HandlerPassPrimary(const sptr<IRemoteObject> & primary)86 void ReverseContinuationSchedulerReplica::HandlerPassPrimary(const sptr<IRemoteObject> &primary)
87 {
88     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
89     std::shared_ptr<IReverseContinuationSchedulerReplicaHandler> replicaHandlerTmp = nullptr;
90     replicaHandlerTmp = replicaHandler_.lock();
91     if (replicaHandlerTmp == nullptr) {
92         TAG_LOGE(AAFwkTag::CONTINUATION,
93             "replicaHandlerTmp is nullptr");
94         return;
95     }
96     replicaHandlerTmp->PassPrimary(primary);
97 }
98 
HandlerReverseContinuation()99 bool ReverseContinuationSchedulerReplica::HandlerReverseContinuation()
100 {
101     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
102     std::shared_ptr<IReverseContinuationSchedulerReplicaHandler> replicaHandlerTmp = nullptr;
103     replicaHandlerTmp = replicaHandler_.lock();
104     if (replicaHandlerTmp == nullptr) {
105         TAG_LOGE(AAFwkTag::CONTINUATION,
106             "replicaHandlerTmp is nullptr");
107         return false;
108     }
109     return replicaHandlerTmp->ReverseContinuation();
110 }
111 
HandlerNotifyReverseResult(int reverseResult)112 void ReverseContinuationSchedulerReplica::HandlerNotifyReverseResult(int reverseResult)
113 {
114     TAG_LOGI(AAFwkTag::CONTINUATION, "begin");
115     std::shared_ptr<IReverseContinuationSchedulerReplicaHandler> replicaHandlerTmp = nullptr;
116     replicaHandlerTmp = replicaHandler_.lock();
117     if (replicaHandlerTmp == nullptr) {
118         TAG_LOGE(AAFwkTag::CONTINUATION,
119             "replicaHandlerTmp is nullptr");
120         return;
121     }
122     replicaHandlerTmp->NotifyReverseResult(reverseResult);
123 }
124 }  // namespace AppExecFwk
125 }  // namespace OHOS
126