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