• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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.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 
30 class ExtBackup : public AbilityRuntime::ExtensionBase<ExtBackupContext> {
31 public:
32 
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     /**
92      * @brief Create Extension.
93      *
94      * @param runtime The runtime.
95      * @return The ServiceExtension instance.
96      */
97     static ExtBackup *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime);
98 
99     /**
100      * @brief Get the Extension Action object
101      *
102      * @return BConstants::ExtensionAction
103      */
104     virtual BConstants::ExtensionAction GetExtensionAction() const;
105 
106     /**
107      * @brief Get the User Config, then check if
108      *
109      * @return allowed ro not
110      */
111     virtual bool AllowToBackupRestore() const;
112 
113     /**
114      * @brief Get the user configure
115      *
116      * @return user configure
117      */
118     virtual std::string GetUsrConfig() const;
119 
120     /**
121      * @brief do backup. Subclasses can inherit to implement their own custom functionality.
122      */
123     virtual ErrCode OnBackup(void);
124 
125     /**
126      * @brief Called do restore.
127      */
128     virtual ErrCode OnRestore(void);
129 
130     bool WasFromSpeicalVersion(void);
131 
132 public:
133     ExtBackup() = default;
134     ~ExtBackup() override = default;
135 
136     static void SetCreator(const CreatorFunc& creator);
137 
138 protected:
139 
140     std::string appVersionStr_;
141     int appVersionCode_;
142     int restoreType_;
143 
144 private:
145 
146     BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want,
147             std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo);
148 
149     ErrCode GetParament(const AAFwk::Want &want);
150 
151     BConstants::ExtensionAction extAction_ {BConstants::ExtensionAction::INVALID};
152 
153     static CreatorFunc creator_;
154 };
155 } // namespace OHOS::FileManagement::Backup
156 
157 #endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H
158