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 FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_FORM_MODULE_PRELOADER_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_FORM_MODULE_PRELOADER_H
18 
19 #include <unordered_set>
20 #include <vector>
21 #include <map>
22 #include "core/common/asset_manager_impl.h"
23 
24 namespace OHOS::Ace {
25 class FormModulePreloader {
26 public:
27     /**
28      * @brief Get a list of components used by cards in the specified bundle.
29      * @param bundleName The name of target bundle.
30      * @param formModuleList Output, the list of components.
31      * @return Returns TRUE on success, FALSE on failure.
32      */
33     static bool CreateFormModuleList(const std::string& bundleName, std::unordered_set<std::string>& formModuleList,
34         const std::map<std::string, std::string>* hapPathMap);
35     /**
36      * @brief In the card bundle upgrade condition, get the list of newly added components.
37      * @param bundleName The name of target bundle.
38      * @param formModuleList Output, the list of newly added components.
39      * @return Returns TRUE on success, FALSE on failure.
40      */
41     static bool GetNewFormModuleList(const std::string& bundleName, std::unordered_set<std::string>& formModuleList,
42         const std::map<std::string, std::string>* hapPathMap);
43 
44 private:
45     FormModulePreloader() = default;
46     ~FormModulePreloader() = default;
47 
48     static bool ReadFormModuleList(const std::string& bundleName, std::unordered_set<std::string>& formModuleList,
49      const std::map<std::string, std::string>* hapPathMap, bool isReloadCondition);
50 
51     static bool ReadFileFromAssetManager(
52         const RefPtr<AssetManager>& assetManager, const std::string& fileName, std::string& content);
53 
54     static bool ParseComponentCollectionJson(
55         const std::string& bundleName, const std::string& content,
56         std::unordered_set<std::string>& formModuleList, bool isReloadCondition);
57 
58     static bool IsFormEtsFilePath(const std::unordered_set<std::string>& formEtsFilePaths, std::string path);
59 
60     static RefPtr<AssetManager> CreateAssetManager(const std::string& hapPath);
61 };
62 } // namespace OHOS::Ace
63 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_FORM_MODULE_PRELOADER_H
64