1 /*
2  * Copyright (c) 2022-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 #ifndef OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H
17 #define OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H
18 
19 #include "b_resources/b_constants.h"
20 #include "ext_backup_context_js.h"
21 #include "extension_base.h"
22 #include "runtime.h"
23 #include "want.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 
27 class ExtBackup;
28 using CreatorFunc = std::function<ExtBackup *(const std::unique_ptr<AbilityRuntime::Runtime> &runtime)>;
29 class BackupExtExtension;
30 
31 class ExtBackup : public AbilityRuntime::ExtensionBase<ExtBackupContext> {
32 public:
33     /**
34      * @brief Called when this extension is started. You must override this function if you want to perform some
35      *        initialization operations during extension startup.
36      *
37      * This function can be called only once in the entire lifecycle of an extension.
38      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
39      */
40     void OnStart(const AAFwk::Want &want) override;
41 
42     /**
43      * @brief Init the extension.
44      *
45      * @param record the extension record.
46      * @param application the application info.
47      * @param handler the extension handler.
48      * @param token the remote token.
49      */
50     void Init(const std::shared_ptr<AbilityRuntime::AbilityLocalRecord> &record,
51               const std::shared_ptr<AbilityRuntime::OHOSApplication> &application,
52               std::shared_ptr<AbilityRuntime::AbilityHandler> &handler,
53               const sptr<IRemoteObject> &token) override;
54 
55     /**
56      * @brief Called back when Service is started.
57      * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start
58      * Service. Then the system calls back the current method to use the transferred want parameter to execute its own
59      * logic.
60      *
61      * @param want Indicates the want of Service to start.
62      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
63      * destroyed, and the value false indicates a normal startup.
64      * @param startId Indicates the number of times the Service extension has been started. The startId is
65      * incremented by 1 every time the extension is started. For example, if the extension has been started
66      * for six times, the value of startId is 6.
67      */
68     void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
69 
70     /**
71      * @brief Called when this backup extension ability is connected for the first time.
72      *
73      * You can override this function to implement your own processing logic.
74      *
75      * @param want Indicates the {@link Want} structure containing connection information about the backup
76      * extension.
77      * @return Returns a pointer to the <b>sid</b> of the connected backup extension ability.
78      */
79     sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
80 
81     /**
82      * @brief Called when all abilities connected to this Wallpaper extension are disconnected.
83      *
84      * You can override this function to implement your own processing logic.
85      *
86      */
87     void OnDisconnect(const AAFwk::Want &want) override;
88 
89 public:
90     /**
91      * @brief Create Extension.
92      *
93      * @param runtime The runtime.
94      * @return The ServiceExtension instance.
95      */
96     static ExtBackup *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime);
97 
98     /**
99      * @brief Get the Extension Action object
100      *
101      * @return BConstants::ExtensionAction
102      */
103     virtual BConstants::ExtensionAction GetExtensionAction() const;
104 
105     /**
106      * @brief Get the User Config, then check if
107      *
108      * @return allowed ro not
109      */
110     virtual bool AllowToBackupRestore();
111 
112     /**
113      * @brief Get whether FullBackupOnly or not
114      *
115      * @return FullBackupOnly ro not
116      */
117     virtual bool UseFullBackupOnly(void) const;
118 
119     /**
120      * @brief Get the user configure
121      *
122      * @return user configure
123      */
124     virtual std::string GetUsrConfig() const;
125 
126     /**
127      * @brief do backup. Subclasses can inherit to implement their own custom functionality.
128      */
129     virtual ErrCode OnBackup(std::function<void(ErrCode, std::string)> callback);
130 
131     virtual ErrCode OnBackup(std::function<void(ErrCode, std::string)> callback,
132         std::function<void(ErrCode, const std::string)> callbackEx);
133 
134     /**
135      * @brief Called do restore.
136      */
137     virtual ErrCode OnRestore(std::function<void(ErrCode, std::string)> callback,
138         std::function<void(ErrCode, const std::string)> callbackEx);
139 
140     /**
141      * @brief Called do restore.
142      */
143     virtual ErrCode OnRestore(std::function<void(ErrCode, std::string)> callback);
144 
145     /**
146      * @brief Called do GetBackupInfo.
147      */
148     virtual ErrCode GetBackupInfo(std::function<void(ErrCode, std::string)> callback);
149 
150     /**
151      * @brief Called do OnProcess
152      */
153     virtual ErrCode OnProcess(std::function<void(ErrCode, std::string)> callback);
154 
155     /**
156      * @brief 数据迁移判断
157      *
158      * @return true
159      * @return false
160      */
161     bool WasFromSpecialVersion(void);
162 
163     /**
164      * @brief Version for clone and cloud
165      *
166      * @return true
167      * @return false
168      */
169     bool SpecialVersionForCloneAndCloud(void);
170 
171     /**
172      * @brief 数据以准备就绪
173      *
174      * @return true
175      * @return false
176      */
177     bool RestoreDataReady();
178 
179     /**
180      * @brief Invoke the extended function of the APP
181      */
182     virtual ErrCode InvokeAppExtMethod(ErrCode, const std::string);
183 
184 public:
185     ExtBackup() = default;
186     ~ExtBackup() override = default;
187 
188     static void SetCreator(const CreatorFunc &creator);
189 
190     void SetBackupExtExtension(const wptr<BackupExtExtension> &extExtension);
191 
192 protected:
193     std::string appVersionStr_;
194     std::string restoreRetInfo_;
195     std::string restoreExtInfo_;
196     std::string backupExtInfo_;
197     int64_t appVersionCode_;
198     int restoreType_;
199 
200 private:
201     BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want,
202                                                    std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo);
203     BConstants::ExtensionAction extAction_ {BConstants::ExtensionAction::INVALID};
204     ErrCode GetParament(const AAFwk::Want &want);
205     static CreatorFunc creator_;
206     wptr<BackupExtExtension> bakExtExtension_;
207 };
208 } // namespace OHOS::FileManagement::Backup
209 
210 #endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H
211