1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_ABILITY_RUNTIME_CONTINUATION_HANDLER_STAGE_H
17 #define OHOS_ABILITY_RUNTIME_CONTINUATION_HANDLER_STAGE_H
18 
19 #include <string>
20 
21 #include "continuation_manager_stage.h"
22 #include "distribute_schedule_handler_interface.h"
23 #include "iremote_broker.h"
24 #include "reverse_continuation_scheduler_primary_handler_interface.h"
25 #include "reverse_continuation_scheduler_primary_interface.h"
26 #include "reverse_continuation_scheduler_primary_proxy.h"
27 #include "reverse_continuation_scheduler_primary_stub.h"
28 #include "reverse_continuation_scheduler_recipient.h"
29 #include "reverse_continuation_scheduler_replica_handler_interface.h"
30 #include "reverse_continuation_scheduler_replica_interface.h"
31 #include "reverse_continuation_scheduler_replica_proxy.h"
32 #include "ui_ability.h"
33 #include "want.h"
34 #include "want_params.h"
35 
36 using Want = OHOS::AAFwk::Want;
37 namespace OHOS {
38 namespace AppExecFwk {
39 class ContinuationHandlerStage : public IDistributeScheduleHandler,
40                                  public IReverseContinuationSchedulerPrimaryHandler,
41                                  public IReverseContinuationSchedulerReplicaHandler {
42 public:
43     /**
44      * @brief constructed function
45      */
46     ContinuationHandlerStage(const std::weak_ptr<ContinuationManagerStage> &continuationManager,
47         const std::weak_ptr<AbilityRuntime::UIAbility> &uiAbility);
48     virtual ~ContinuationHandlerStage() = default;
49 
50     /**
51      * @brief Handle continuation from DMS.
52      * @param token The token
53      * @param deviceId The device identifier. This value doesn't matter.
54      * @return zero if success.
55      */
56     bool HandleStartContinuation(const sptr<IRemoteObject> &token, const std::string &deviceId) override;
57 
58     /**
59      * @brief Receive a scheduler which could handle reverse continuation.
60      *        Remote side will pass an scheduler before continuation completed if this continuation is
61      *        reversible. This method will not be called if this continuation is not reversible.
62      * @param remoteReplica A scheduler to handle reverse continuation request.
63      */
64     void HandleReceiveRemoteScheduler(const sptr<IRemoteObject> &remoteReplica) override;
65 
66     /**
67      * @brief Called by DMS when the migrate device finished.
68      * @param result Zero indicate the continuation is success, otherwise integer less than zero.
69      */
70     void HandleCompleteContinuation(int result) override;
71 
72     /**
73      * @brief Pass the primary reverse continuation scheduler object
74      * @param Primary The remote object repersenting the reverse continuation sch
75      */
76     void PassPrimary(const sptr<IRemoteObject> &Primary) override;
77 
78     /**
79      * @brief Indicate a reverse continuation
80      * @return true if the reverse continuation was successful initiated,otherwise false
81      */
82     bool ReverseContinuation() override;
83 
84     /**
85      * @brief Notify the result of a reverse continuation
86      * @param reverseResult The result of the reverse continuation
87      */
88     void NotifyReverseResult(int reverseResult) override;
89 
90     /**
91      * @brief Handle the continuation back request from the primary
92      * @param want The want object representing the continuation back request
93      * @return true indicating successful handling of the continuation back request
94      */
95     bool ContinuationBack(const Want &want) override;
96 
97     /**
98      * @brief Notify Replica Terminated
99      */
100     void NotifyReplicaTerminated() override;
101 
102     /**
103      * @brief Notify the primary that the replica has terminated
104      */
105     void NotifyTerminationToPrimary();
106 
107     /**
108      * @brief Set Reversible
109      * @param reversible Indicates the boolen
110      */
111     void SetReversible(bool reversible);
112 
113     /**
114      * @brief Set Ability Inforamtion
115      * @param abilityInfo Indicates the ability Inforamtion
116      */
117     void SetAbilityInfo(std::shared_ptr<AbilityInfo> &abilityInfo);
118 
119     /**
120      * @brief Set the Primary Stub
121      * @param Primary Indicates the Primary to be set up
122      */
123     void SetPrimaryStub(const sptr<IRemoteObject> &Primary);
124 
125     /**
126      * @brief Reverse Continue Ability
127      * @return If the success returns true, the failure returns false.
128      */
129     bool ReverseContinueAbility();
130 
131     /**
132      * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its
133      * ability slices must implement the IAbilityContinuation interface.
134      * @param token Indicates the token
135      * @param deviceId Indicates the ID of the target device where this ability will be migrated to.
136      * @param versionCode Target bundle version.
137      */
138     bool HandleStartContinuationWithStack(
139         const sptr<IRemoteObject> &token, const std::string &deviceId, uint32_t versionCode);
140 
141     static const std::string ORIGINAL_DEVICE_ID;
142 
143 private:
144     void OnReplicaDied(const wptr<IRemoteObject> &remote);
145     void ClearDeviceInfo(std::shared_ptr<AbilityInfo> &abilityInfo);
146     void CleanUpAfterReverse();
147     Want SetWantParams(const WantParams &wantParams);
148 
149     std::shared_ptr<AbilityInfo> abilityInfo_ = nullptr;
150     std::weak_ptr<AbilityRuntime::UIAbility> ability_;
151     std::weak_ptr<ContinuationManagerStage> continuationManager_;
152     bool reversible_ = false;
153     sptr<IReverseContinuationSchedulerReplica> remoteReplicaProxy_ = nullptr;
154     sptr<IReverseContinuationSchedulerPrimary> remotePrimaryProxy_ = nullptr;
155     sptr<IRemoteObject> remotePrimaryStub_ = nullptr;
156     sptr<IRemoteObject::DeathRecipient> schedulerDeathRecipient_ = nullptr;
157 };
158 } // namespace AppExecFwk
159 } // namespace OHOS
160 #endif // OHOS_ABILITY_RUNTIME_CONTINUATION_HANDLER_STAGE_H
161