1 /*
2 * Copyright (c) 2021-2022 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 {
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 HILOG_INFO("%{public}s called begin", __func__);
34 auto task = [reverseContinuationSchedulerPrimary = this]() {
35 reverseContinuationSchedulerPrimary->HandlerNotifyReplicaTerminated();
36 };
37
38 if (mainHandler_ == nullptr) {
39 HILOG_ERROR("ReverseContinuationSchedulerPrimary::NotifyReplicaTerminated mainHandler_ == nullptr");
40 return;
41 }
42
43 bool ret = mainHandler_->PostTask(task);
44 if (!ret) {
45 HILOG_ERROR("ReverseContinuationSchedulerPrimary::NotifyReplicaTerminated PostTask error");
46 return;
47 }
48 HILOG_INFO("%{public}s called end", __func__);
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 HILOG_INFO("%{public}s called begin", __func__);
60 auto task = [reverseContinuationSchedulerPrimary = this, want]() {
61 reverseContinuationSchedulerPrimary->HandlerContinuationBack(want);
62 };
63
64 if (mainHandler_ == nullptr) {
65 HILOG_ERROR("ReverseContinuationSchedulerPrimary::ContinuationBack mainHandler_ == nullptr");
66 return false;
67 }
68
69 bool ret = mainHandler_->PostTask(task);
70 if (!ret) {
71 HILOG_ERROR("ReverseContinuationSchedulerPrimary::ContinuationBack PostTask error");
72 return false;
73 }
74 HILOG_INFO("%{public}s called end", __func__);
75 return true;
76 }
77
HandlerNotifyReplicaTerminated()78 void ReverseContinuationSchedulerPrimary::HandlerNotifyReplicaTerminated()
79 {
80 HILOG_INFO("%{public}s called begin", __func__);
81 std::shared_ptr<IReverseContinuationSchedulerPrimaryHandler> continuationHandler = nullptr;
82 continuationHandler = continuationHandler_.lock();
83 if (continuationHandler == nullptr) {
84 HILOG_ERROR(
85 "ReverseContinuationSchedulerPrimary::HandlerNotifyReplicaTerminated get continuationHandler is nullptr");
86 return;
87 }
88 continuationHandler->NotifyReplicaTerminated();
89 HILOG_INFO("%{public}s called end", __func__);
90 }
91
HandlerContinuationBack(const AAFwk::Want & want)92 void ReverseContinuationSchedulerPrimary::HandlerContinuationBack(const AAFwk::Want &want)
93 {
94 HILOG_INFO("%{public}s called begin", __func__);
95 std::shared_ptr<IReverseContinuationSchedulerPrimaryHandler> continuationHandler = nullptr;
96 continuationHandler = continuationHandler_.lock();
97 if (continuationHandler == nullptr) {
98 HILOG_ERROR("ReverseContinuationSchedulerPrimary::HandlerContinuationBack get continuationHandler is nullptr");
99 return;
100 }
101 continuationHandler->ContinuationBack(want);
102 HILOG_INFO("%{public}s called end", __func__);
103 }
104 } // namespace AppExecFwk
105 } // namespace OHOS
106