• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H
18 
19 #include "bundle_constants.h"
20 #include "bundle_data_mgr.h"
21 #include "event_handler.h"
22 #include "pre_scan_info.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 class BundleMgrService;
27 enum class ScanMode;
28 enum class ResultMode;
29 
30 enum class ResultCode {
31     RECOVER_OK = 0,
32     REINSTALL_OK,
33     NO_INSTALLED_DATA,
34     SYSTEM_ERROR,
35 };
36 
37 enum class ScanResultCode {
38     SCAN_HAS_DATA_PARSE_SUCCESS,
39     SCAN_HAS_DATA_PARSE_FAILED,
40     SCAN_NO_DATA,
41 };
42 
43 class BMSEventHandler : public EventHandler {
44 public:
45     explicit BMSEventHandler(const std::shared_ptr<EventRunner> &runner);
46     virtual ~BMSEventHandler() override;
47     /**
48      * @brief Process the event of install system bundles.
49      * @param event Indicates the event to be processed.
50      * @return
51      */
52     virtual void ProcessEvent(const InnerEvent::Pointer &event) override;
53 
54     /**
55      * @brief Get preInstall root dir list,
56      *        which the catalog of production has higher priority.
57      * @param rootDirList Indicates the root dir list.
58      * @return
59      */
60     static void GetPreInstallRootDirList(std::vector<std::string> &rootDirList);
61     /**
62      * @brief Load all preInstall infos from proFile.
63      * @return Returns true if get the preInstall list successfully; returns false otherwise.
64      */
65     static bool LoadPreInstallProFile();
66     /**
67      * @brief Clear all preInstall infos cache.
68      * @return
69      */
70     static void ClearPreInstallCache();
71     /**
72      * @brief Get the preInstall capability.
73      * @param preBundleConfigInfo Indicates the preBundleConfigInfo.
74      * @return Returns true if get the preInstall capability successfully; returns false otherwise.
75      */
76     static bool GetPreInstallCapability(PreBundleConfigInfo &preBundleConfigInfo);
77     /**
78      * @brief Has preInstall profile or not.
79      * @return Returns result.
80      */
81     static bool HasPreInstallProfile();
82 
83     enum {
84         BMS_START = 0,
85     };
86 
87 private:
88     /**
89      * @brief Bms start event.
90      * @return
91      */
92     void BmsStartEvent();
93     /**
94      * @brief Before Bms start.
95      * @return
96      */
97     void BeforeBmsStart();
98     /**
99      * @brief On Bms starting.
100      * @return
101      */
102     void OnBmsStarting();
103     /**
104      * @brief After Bms start.
105      * @return
106      */
107     void AfterBmsStart();
108     /**
109      * @brief Load install infos from db.
110      * @return Returns true if load successfully; returns false otherwise.
111      */
112     bool LoadInstallInfosFromDb();
113     /**
114      * @brief Guard against install infos lossed strategy.
115      * @return Returns ResultCode for recover install infos.
116      */
117     ResultCode GuardAgainstInstallInfosLossedStrategy();
118     /**
119      * @brief Scan and analyze install infos.
120      * @param installInfos Indicates the install infos.
121      * @return
122      */
123     void ScanAndAnalyzeInstallInfos(
124         std::map<std::string, std::vector<InnerBundleInfo>> &installInfos);
125     /**
126      * @brief Scan and analyze common install dir.
127      * @param installInfos Indicates the install infos.
128      * @return
129      */
130     void ScanInstallDir(
131         std::map<std::string, std::vector<std::string>> &hapPathsMap);
132     /**
133      * @brief Get preInstall haps.
134      * @param bundleDirs Indicates preInstall hapPath.
135      * @return
136      */
137     void GetPreInstallDir(std::vector<std::string> &bundleDirs);
138     /**
139      * @brief Analyze hap to InnerBundleInfo.
140      * @param isPreInstallApp Indicates is preInstallApp or not.
141      * @param hapPathsMap Indicates the hapPathsMap which will be analyzed.
142      * @param installInfos Indicates the install infos.
143      * @return
144      */
145     void AnalyzeHaps(
146         bool isPreInstallApp,
147         const std::map<std::string, std::vector<std::string>> &hapPathsMap,
148         std::map<std::string, std::vector<InnerBundleInfo>> &installInfos);
149     /**
150      * @brief Analyze hap to InnerBundleInfo.
151      * @param isPreInstallApp Indicates is preInstallApp or not.
152      * @param bundleDirs Indicates the bundleDirs which will be analyzed.
153      * @param installInfos Indicates the install infos.
154      * @return
155      */
156     void AnalyzeHaps(
157         bool isPreInstallApp,
158         const std::vector<std::string> &bundleDirs,
159         std::map<std::string, std::vector<InnerBundleInfo>> &installInfos);
160     /**
161      * @brief Get preBundle install dir.
162      * @param bundleDirs Indicates the bundleDirs.
163      * @return
164      */
165     void GetPreBundleDir(std::list<std::string> &bundleDirs);
166     /**
167      * @brief Check scaned hapPath whether end with .hap.
168      * @param hapPaths Indicates the hapPaths.
169      * @return Returns the checked hapPaths.
170      */
171     std::vector<std::string> CheckHapPaths(const std::vector<std::string> &hapPaths);
172     /**
173      * @brief Collect install infos from parse result.
174      * @param hapInfos Indicates the parse result.
175      * @param installInfos Indicates the saved installInfos.
176      * @return.
177      */
178     void CollectInstallInfos(
179         const std::unordered_map<std::string, InnerBundleInfo> &hapInfos,
180         std::map<std::string, std::vector<InnerBundleInfo>> &installInfos);
181     /**
182      * @brief Scan and analyze userDatas.
183      * @param userMaps Indicates the userMaps to save userInfo.
184      * @return Returns ScanResultCode if Scan and analyze infos successfully; returns false otherwise.
185      */
186     ScanResultCode ScanAndAnalyzeUserDatas(
187         std::map<std::string, std::vector<InnerBundleUserInfo>> &userMaps);
188     /**
189      * @brief Analyze userDatas.
190      * @param userId Indicates the userId.
191      * @param userDataDir Indicates the userDataDir.
192      * @param userDataBundleName Indicates the userDataBundleName.
193      * @param userMaps Indicates the userMaps to save userInfo.
194      * @return Returns true if analyze infos successfully; returns false otherwise.
195      */
196     bool AnalyzeUserData(
197         int32_t userId, const std::string &userDataDir, const std::string &userDataBundleName,
198         std::map<std::string, std::vector<InnerBundleUserInfo>> &userMaps);
199     /**
200      * @brief ReInstall all Apps from installDir.
201      * @return Returns the ResultCode indicates the result of this action.
202      */
203     ResultCode ReInstallAllInstallDirApps();
204     /**
205      * @brief Combine install infos and userInfos.
206      * @param installInfos Indicates the installInfos.
207      * @param userInfoMaps Indicates the userInfoMaps.
208      * @return Returns true if combine infos successfully; returns false otherwise.
209      */
210     bool CombineBundleInfoAndUserInfo(
211         const std::map<std::string, std::vector<InnerBundleInfo>> &installInfos,
212         const std::map<std::string, std::vector<InnerBundleUserInfo>> &userInfoMaps);
213     /**
214      * @brief Save recover info to cache.
215      * @param info Indicates the InnerBundleInfo.
216      * @return
217      */
218     void SaveInstallInfoToCache(InnerBundleInfo &info);
219     /**
220      * @brief Scan dir by scanMode and resultMode, this function will perform
221      *        scan through installd because installd has higher permissions.
222      * @param scanMode Indicates the scanMode,
223      *        which maybe SUB_FILE_ALL SUB_FILE_DIR or SUB_FILE_FILE.
224      * @param resultMode Indicates the resultMode,
225      *        which maybe ABSOLUTE_PATH or RELATIVE_PATH.
226      * @param resultList Indicates the scan resultList.
227      * @return Returns true if Scan successfully; returns false otherwise.
228      */
229     bool ScanDir(const std::string& dir, ScanMode scanMode,
230         ResultMode resultMode, std::vector<std::string> &resultList);
231     /**
232      * @brief Bundle boot start event.
233      * @return
234      */
235     void BundleBootStartEvent();
236     /**
237      * @brief Bundle reboot start event.
238      * @return
239      */
240     void BundleRebootStartEvent();
241     /**
242      * @brief start boot scan.
243      * @param userId Indicates the userId.
244      * @return
245      */
246     void OnBundleBootStart(int32_t userId = Constants::UNSPECIFIED_USERID);
247     /**
248      * @brief Process boot bundle install from scan.
249      * @param userId Indicates the userId.
250      * @return
251      */
252     void ProcessBootBundleInstallFromScan(int32_t userId);
253     /**
254      * @brief Process bundle install by scanInfos.
255      * @param userId Indicates the userId.
256      * @return
257      */
258     void InnerProcessBootPreBundleProFileInstall(int32_t userId);
259     /**
260      * @brief Install bundles by scanDir.
261      * @param scanDir Indicates the scanDir.
262      * @param appType Indicates the bundle type.
263      * @param userId Indicates userId.
264      * @return
265      */
266     void ProcessSystemBundleInstall(
267         const std::string &scanDir,
268         Constants::AppType appType,
269         int32_t userId = Constants::UNSPECIFIED_USERID);
270     /**
271      * @brief Install bundles by preScanInfo.
272      * @param preScanInfo Indicates the preScanInfo.
273      * @param appType Indicates the bundle type.
274      * @param userId Indicates userId.
275      * @return
276      */
277     void ProcessSystemBundleInstall(
278         const PreScanInfo &preScanInfo,
279         Constants::AppType appType,
280         int32_t userId = Constants::UNSPECIFIED_USERID);
281     /**
282      * @brief start reboot scan.
283      * @return
284      */
285     void OnBundleRebootStart();
286     /**
287      * @brief Process reboot bundle.
288      * @return
289      */
290     void ProcessRebootBundle();
291     /**
292      * @brief Obtains the PreInstallBundleInfo objects.
293      * @return Returns true if this function is successfully called; returns false otherwise.
294      */
295     bool LoadAllPreInstallBundleInfos();
296     /**
297      * @brief Process reboot bundle install.
298      * @return
299      */
300     void ProcessRebootBundleInstall();
301     /**
302      * @brief Process reboot bundle install by scanInfos.
303      * @return
304      */
305     void ProcessReBootPreBundleProFileInstall();
306     /**
307      * @brief Process reboot bundle install from scan.
308      * @return
309      */
310     void ProcessRebootBundleInstallFromScan();
311     /**
312      * @brief Process reboot install bundles by bundleList.
313      * @param bundleList Indicates store bundle list.
314      * @param appType Indicates the bundle type.
315      * @return
316      */
317     void InnerProcessRebootBundleInstall(
318         const std::list<std::string> &bundleList, Constants::AppType appType);
319     /**
320      * @brief Reboot uninstall system and system vendor bundles.
321      * @return
322      */
323     void ProcessRebootBundleUninstall();
324     /**
325      * @brief Get bundle dir by scan.
326      * @param bundleDirs Indicates the return bundleDirs.
327      * @return
328      */
329     void GetBundleDirFromScan(std::list<std::string> &bundleDirs);
330     /**
331      * @brief Process scan dir.
332      * @param dir Indicates the dir.
333      * @param bundleDirs Indicates the return bundleDirs.
334      * @return
335      */
336     void ProcessScanDir(const std::string &dir, std::list<std::string> &bundleDirs);
337     /**
338      * @brief Process parse pre bundle profile.
339      * @param dir Indicates the dir.
340      * @return
341      */
342     static void ParsePreBundleProFile(const std::string &dir);
343     /**
344      * @brief Set the flag indicates that all system and vendor applications installed.
345      * @return
346      */
347     void SetAllInstallFlag() const;
348     /**
349      * @brief Check and parse hap.
350      * @param hapFilePath Indicates the absolute file path of the HAP.
351      * @param isPreInstallApp Indicates the hap is preInstallApp or not.
352      * @param infos Indicates the obtained BundleInfo object.
353      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
354      */
355     bool CheckAndParseHapFiles(const std::string &hapFilePath,
356         bool isPreInstallApp, std::unordered_map<std::string, InnerBundleInfo> &infos);
357     /**
358      * @brief Parse hap.
359      * @param hapFilePath Indicates the absolute file path of the HAP.
360      * @param infos Indicates the obtained BundleInfo object.
361      * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise.
362      */
363     bool ParseHapFiles(
364         const std::string &hapFilePath,
365         std::unordered_map<std::string, InnerBundleInfo> &infos);
366     /**
367      * @brief OTA Install system app and system vendor bundles.
368      * @param filePaths Indicates the filePaths.
369      * @param appType Indicates the bundle type.
370      * @param removable Indicates the bundle type.
371      * @return Returns true if this function called successfully; returns false otherwise.
372      */
373     bool OTAInstallSystemBundle(
374         const std::vector<std::string> &filePaths,
375         Constants::AppType appType,
376         bool removable);
377     /**
378      * @brief Used to determine whether the module has been installed. If the installation has
379      *        been uninstalled, OTA install and upgrade will not be allowed.
380      * @param bundleName Indicates the bundleName.
381      * @param bundlePath Indicates the bundlePath.
382      * @return Returns true if this function called successfully; returns false otherwise.
383      */
384     bool HasModuleSavedInPreInstalledDb(
385         const std::string &bundleName, const std::string &bundlePath);
386     /**
387      * @brief Delete preInstallInfo to Db.
388      * @param bundleName Indicates the bundleName.
389      * @param bundlePath Indicates the bundlePath.
390      */
391     void DeletePreInfoInDb(
392         const std::string &bundleName, const std::string &bundlePath, bool bundleLevel);
393     /**
394      * @brief Add parseInfos to map.
395      * @param bundleName Indicates the bundleName.
396      * @param infos Indicates the infos.
397      */
398     void AddParseInfosToMap(const std::string &bundleName,
399         const std::unordered_map<std::string, InnerBundleInfo> &infos);
400     /**
401      * @brief Clear cache.
402      */
403     void ClearCache();
404     /**
405      * @brief Judge whether the preInstall app can be removable.
406      * @param path Indicates the path.
407      * @return Returns true if the preInstall is removable; returns false otherwise.
408      */
409     bool IsPreInstallRemovable(const std::string &path);
410     /**
411      * @brief Ota upgrade scenario, uninstall the hap application updated by the app hot patch and retain the data.
412      * @param bundleName Indicates the bundleName.
413      * @return Returns true if this function called successfully; returns false otherwise.
414      */
415     bool HotPatchAppProcessing(const std::string &bundleName);
416     /**
417      * @brief Save the current system version number to the bms database.
418      */
419     void SaveSystemVersion();
420     /**
421      * @brief Judge whether it is OTA upgradde.
422      * @return Returns true if this function called successfully; returns false otherwise.
423      */
424     bool IsSystemUpgrade();
425     /**
426      * @brief Get current system version.
427      * @param curSystemVersion Current system version.
428      * @return Returns true if called successfully; returns false code otherwise.
429      */
430     bool GetCurSystemVersion(std::string &curSystemVersion);
431     /**
432      * @brief Get system version from db.
433      * @param curSystemVersion Current system version.
434      * @return Returns true if called successfully; returns false code otherwise.
435      */
436     bool GetOldSystemVersion(std::string &oldSystemVersion);
437     /**
438      * @brief Judge whether hot patch application.
439      * @param bundleName Indicates the bundleName.
440      * @return Returns true if called successfully; returns false code otherwise.
441      */
442     bool IsHotPatchApp(const std::string &bundleName);
443 
444     bool FetchInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &innerBundleInfo);
445 
446     void GetPreInstallDirFromLoadProFile(std::vector<std::string> &bundleDirs);
447     void GetPreInstallDirFromScan(std::vector<std::string> &bundleDirs);
448 #ifdef USE_PRE_BUNDLE_PROFILE
449     void UpdateRemovable(const std::string &bundleName, bool removable);
450     void UpdateAllPrivilegeCapability();
451     void UpdatePrivilegeCapability(const PreBundleConfigInfo &preBundleConfigInfo);
452     bool MatchSignature(const PreBundleConfigInfo &configInfo, const std::string &signature);
453     void UpdateTrustedPrivilegeCapability(const PreBundleConfigInfo &preBundleConfigInfo);
454 #endif
455 
456     // Used to save the information parsed by Hap in the scanned directory.
457     std::map<std::string, std::unordered_map<std::string, InnerBundleInfo>> hapParseInfoMap_;
458     // Used to save application information that already exists in the Db.
459     std::map<std::string, PreInstallBundleInfo> loadExistData_;
460     // Used to mark Whether trigger OTA check
461     bool needRebootOta_ = false;
462     // Used to notify bundle scan status
463     bool needNotifyBundleScanStatus_ = false;
464 
465     bool hasLoadAllPreInstallBundleInfosFromDb_ = false;
466 };
467 }  // namespace AppExecFwk
468 }  // namespace OHOS
469 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H
470