1 /*
2  * Copyright (c) 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 OHOS_ABILITY_RUNTIME_JS_MODULE_READER_H
17 #define OHOS_ABILITY_RUNTIME_JS_MODULE_READER_H
18 
19 #include <sstream>
20 #include <string>
21 
22 #include "js_module_searcher.h"
23 #include "extractor.h"
24 
25 using Extractor = OHOS::AbilityBase::Extractor;
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 class JsModuleReader final : private JsModuleSearcher {
30 public:
31     static constexpr char ABS_CODE_PATH[] = "/data/storage/el1/";
32     static constexpr char ABS_DATA_CODE_PATH[] = "/data/app/el1/bundle/public/";
33     static constexpr char BUNDLE[] = "bundle/";
34     static constexpr char MERGE_ABC_PATH[] = "ets/modules.abc";
35     static constexpr char SYS_ABS_CODE_PATH[] = "/system/app/appServiceFwk/";
36     static constexpr char SHARED_FILE_SUFFIX[] = ".hsp";
37     JsModuleReader(const std::string& bundleName, const std::string& hapPath, bool isFormRender = false);
38     ~JsModuleReader() = default;
39 
40     JsModuleReader(const JsModuleReader&) = default;
41     JsModuleReader(JsModuleReader&&) = default;
42     JsModuleReader& operator=(const JsModuleReader&) = default;
43     JsModuleReader& operator=(JsModuleReader&&) = default;
44 
45     bool operator()(const std::string& inputPath, uint8_t **buff, size_t *buffSize, std::string& errorMsg) const;
46     static std::string GetPresetAppHapPath(const std::string& inputPath, const std::string& bundleName);
47     static void GetHapPathList(const std::string &bundleName, std::vector<std::string> &hapList);
48 
49 private:
50     std::string GetAppHspPath(const std::string& inputPath) const;
51     std::string GetCommonAppHspPath(const std::string& inputPath) const;
52     std::string GetFormAppHspPath(const std::string& inputPath) const;
53     std::string GetModuleName(const std::string& inputPath) const;
54     static std::string GetOtherHspPath(const std::string& bundleName, const std::string& moduleName,
55         const std::string& inputPath);
56 
57     bool isSystemPath_ = false;
58     bool isFormRender_ = false;
59 };
60 } // namespace AbilityRuntime
61 } // namespace OHOS
62 
63 #endif // OHOS_ABILITY_RUNTIME_JS_MODULE_READER_H
64