1 /*
2  * Copyright (c) 2023-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_primary_stage.h"
17 
18 #include "continuation_handler_stage.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
ReverseContinuationSchedulerPrimaryStage(const std::weak_ptr<IReverseContinuationSchedulerPrimaryHandler> & continuationHandler,const std::shared_ptr<AbilityHandler> & mainHandler)23 ReverseContinuationSchedulerPrimaryStage::ReverseContinuationSchedulerPrimaryStage(
24     const std::weak_ptr<IReverseContinuationSchedulerPrimaryHandler> &continuationHandler,
25     const std::shared_ptr<AbilityHandler> &mainHandler)
26     : continuationHandler_(continuationHandler), mainHandler_(mainHandler)
27 {}
28 
NotifyReplicaTerminated()29 void ReverseContinuationSchedulerPrimaryStage::NotifyReplicaTerminated()
30 {
31     TAG_LOGD(AAFwkTag::CONTINUATION, "Begin");
32     wptr<ReverseContinuationSchedulerPrimaryStage> weak = this;
33     auto task = [weak]() {
34         auto reverseContinuationSchedulerPrimary = weak.promote();
35         if (reverseContinuationSchedulerPrimary == nullptr) {
36             TAG_LOGE(AAFwkTag::CONTINUATION, "reverseContinuationSchedulerPrimary is nullptr");
37             return;
38         }
39         reverseContinuationSchedulerPrimary->HandlerNotifyReplicaTerminated();
40     };
41 
42     if (mainHandler_ == nullptr) {
43         TAG_LOGE(AAFwkTag::CONTINUATION, "mainHandler_ is nullptr");
44         return;
45     }
46 
47     bool ret = mainHandler_->PostTask(task);
48     if (!ret) {
49         TAG_LOGE(AAFwkTag::CONTINUATION, "PostTask error");
50         return;
51     }
52 }
53 
ContinuationBack(const AAFwk::Want & want)54 bool ReverseContinuationSchedulerPrimaryStage::ContinuationBack(const AAFwk::Want &want)
55 {
56     TAG_LOGD(AAFwkTag::CONTINUATION, "Begin");
57     wptr<ReverseContinuationSchedulerPrimaryStage> weak = this;
58     auto task = [weak, want]() {
59         auto reverseContinuationSchedulerPrimary = weak.promote();
60         if (reverseContinuationSchedulerPrimary == nullptr) {
61             TAG_LOGE(AAFwkTag::CONTINUATION, "reverseContinuationSchedulerPrimary is nullptr.");
62             return;
63         }
64         reverseContinuationSchedulerPrimary->HandlerContinuationBack(want);
65     };
66 
67     if (mainHandler_ == nullptr) {
68         TAG_LOGE(AAFwkTag::CONTINUATION, "mainHandler_ is nullptr");
69         return false;
70     }
71 
72     bool ret = mainHandler_->PostTask(task);
73     if (!ret) {
74         TAG_LOGE(AAFwkTag::CONTINUATION, "PostTask error");
75         return false;
76     }
77     return true;
78 }
79 
HandlerNotifyReplicaTerminated()80 void ReverseContinuationSchedulerPrimaryStage::HandlerNotifyReplicaTerminated()
81 {
82     TAG_LOGD(AAFwkTag::CONTINUATION, "Begin");
83     std::shared_ptr<IReverseContinuationSchedulerPrimaryHandler> continuationHandler = continuationHandler_.lock();
84     if (continuationHandler == nullptr) {
85         TAG_LOGE(AAFwkTag::CONTINUATION, "ContinuationHandler is nullptr.");
86         return;
87     }
88     continuationHandler->NotifyReplicaTerminated();
89 }
90 
HandlerContinuationBack(const AAFwk::Want & want)91 void ReverseContinuationSchedulerPrimaryStage::HandlerContinuationBack(const AAFwk::Want &want)
92 {
93     TAG_LOGD(AAFwkTag::CONTINUATION, "Begin");
94     std::shared_ptr<IReverseContinuationSchedulerPrimaryHandler> continuationHandler = continuationHandler_.lock();
95     if (continuationHandler == nullptr) {
96         TAG_LOGE(AAFwkTag::CONTINUATION, "ContinuationHandler is nullptr");
97         return;
98     }
99     continuationHandler->ContinuationBack(want);
100 }
101 } // namespace AppExecFwk
102 } // namespace OHOS
103