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