1 /*
2  * Copyright (c) 2022-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_ABILITY_RECOVERY_H
17 #define OHOS_ABILITY_ABILITY_RECOVERY_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "ability_info.h"
23 #include "application_info.h"
24 #include "event_handler.h"
25 #include "iremote_object.h"
26 #include "parcel.h"
27 #include "recovery_param.h"
28 #include "ui_ability.h"
29 #include "want.h"
30 #include "want_params.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class AbilityRecovery {
35 public:
36     AbilityRecovery();
37     virtual ~AbilityRecovery();
38     void EnableAbilityRecovery(bool useAppSettedValue, uint16_t restartFlag, uint16_t saveFlag, uint16_t saveMode);
39     bool InitAbilityInfo(const std::shared_ptr<AbilityRuntime::UIAbility> ability,
40         const std::shared_ptr<AbilityInfo> &abilityInfo, const sptr<IRemoteObject> &token);
41     bool ScheduleSaveAbilityState(StateReason reason);
42     bool ScheduleRecoverAbility(StateReason reason, const Want *want = nullptr);
43     bool ScheduleRestoreAbilityState(StateReason reason, const Want &want);
44     bool CallOnRestoreAbilityState(StateReason reason);
45     bool PersistState();
46     bool IsOnForeground();
47     bool IsSameAbility(uintptr_t ability);
48     void SetJsAbility(uintptr_t ability);
49     std::string GetSavedPageStack(StateReason reason);
50     uint16_t GetRestartFlag() const;
51     uint16_t GetSaveOccasionFlag() const;
52     uint16_t GetSaveModeFlag() const;
53     int32_t missionId_ = -1;
54 
GetToken()55     wptr<IRemoteObject> GetToken() const
56     {
57         return token_;
58     }
59 private:
60     bool SaveAbilityState();
61     bool SerializeDataToFile(int32_t savedStateId, AAFwk::WantParams& params);
62     bool ReadSerializeDataFromFile(int32_t savedStateId, AAFwk::WantParams& params);
63     bool LoadSavedState(StateReason reason);
64     bool IsSaveAbilityState(StateReason reason);
65     bool DefaultRecovery() const;
66 
67     bool isEnable_;
68     uint16_t restartFlag_;
69     uint16_t saveOccasion_;
70     uint16_t saveMode_;
71     std::weak_ptr<AbilityRuntime::UIAbility> ability_;
72     std::weak_ptr<AppExecFwk::AbilityInfo> abilityInfo_;
73     uintptr_t jsAbilityPtr_;
74     wptr<IRemoteObject> token_;
75     std::string pageStack_;
76     WantParams params_;
77     bool hasTryLoad_ = false;
78     bool hasLoaded_ = false;
79     std::mutex lock_;
80     std::atomic<bool> useAppSettedValue_ = false; // If the value is true means app call appRecovery.enableAppRecovery
81 };
82 }  // namespace AbilityRuntime
83 }  // namespace OHOS
84 #endif // OHOS_ABILITY_ABILITY_RECOVERY_H