1 /*
2  * Copyright (c) 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 ECMASCRIPT_OHOS_PRELOAD_APP_INFO_H
17 #define ECMASCRIPT_OHOS_PRELOAD_APP_INFO_H
18 
19 #include <string>
20 #include <set>
21 #include <vector>
22 
23 #include "ecmascript/platform/file.h"
24 #include "macros.h"
25 
26 namespace panda::ecmascript {
27 class OhosPreloadAppInfo {
28 public:
29     constexpr static const char *const PRELOAD_PATH_PREFIX = "/system/";
30     constexpr static const char *const PRELOAD_AN_FOLDER = "/ark-cache/";
31 
32     OhosPreloadAppInfo() = default;
33 
GetPreloadAOTFileName(const std::string &hapPath, const std::string &moduleName)34     static std::string GetPreloadAOTFileName(const std::string &hapPath, const std::string &moduleName)
35     {
36         std::string aotFileName = "";
37         std::string appName = "";
38         std::string folderPath = "";
39         std::string appPath = "";
40 
41         size_t folderEnd = 0;
42         size_t appEnd = 0;
43         if (hapPath.find(PRELOAD_PATH_PREFIX) != std::string::npos) {
44             folderEnd = hapPath.find_last_of('/');
45         }
46         if (folderEnd != std::string::npos) {
47             // "/system/app/AppName"
48             folderPath = hapPath.substr(0, folderEnd);
49             appEnd = folderPath.find_last_of('/');
50             // "AppName"
51             appName = folderPath.substr(appEnd + 1);
52         }
53         if (appEnd != std::string::npos) {
54             // "/system/app"
55             appPath = hapPath.substr(0, appEnd);
56             // "/system/app/ark-cache/AppName/ModuleName"
57             aotFileName = appPath + PRELOAD_AN_FOLDER + appName + "/" + moduleName;
58         }
59         std::string anFile = aotFileName + AOTFileManager::FILE_EXTENSION_AN;
60         std::string aiFile = aotFileName + AOTFileManager::FILE_EXTENSION_AI;
61         if (!FileExist(anFile.c_str()) || !FileExist(aiFile.c_str())) {
62             aotFileName.clear();
63         }
64         return aotFileName;
65     }
66 };
67 }  // namespace panda::ecmascript
68 #endif