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 ABILITY_ABILITY_RUNTIME_JS_RUNTIME_LITE_H
17 #define ABILITY_ABILITY_RUNTIME_JS_RUNTIME_LITE_H
18 
19 #include <mutex>
20 #include <thread>
21 #include <unordered_map>
22 #include <unordered_set>
23 
24 #include "js_environment.h"
25 #include "native_engine/native_engine.h"
26 #include "runtime.h"
27 #include <nlohmann/json.hpp>
28 
29 using Options = OHOS::AbilityRuntime::Runtime::Options;
30 namespace OHOS {
31 namespace AbilityRuntime {
32 class JsRuntimeLite {
33 public:
34     JsRuntimeLite(const JsRuntimeLite&) = delete;
35     JsRuntimeLite& operator=(const JsRuntimeLite&) = delete;
36     void GetPkgContextInfoListMap(const std::map<std::string, std::string> &contextInfoMap,
37         std::map<std::string, std::vector<std::vector<std::string>>> &pkgContextInfoMap,
38         std::map<std::string, std::string> &pkgAliasMap);
39     static JsRuntimeLite& GetInstance();
40     static void InitJsRuntimeLite(const Options& options);
41     std::shared_ptr<Options> GetChildOptions();
42     napi_status CreateJsEnv(const Options& options, std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
43     napi_status RemoveJsEnv(napi_env env);
44     napi_status Init(const Options& options, napi_env env);
45 
46 private:
47     JsRuntimeLite();
48     ~JsRuntimeLite();
49     napi_status AddEnv(napi_env env, std::shared_ptr<JsEnv::JsEnvironment> jsEnv);
50     panda::ecmascript::EcmaVM* GetEcmaVm(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv) const;
51     std::shared_ptr<JsEnv::JsEnvironment> GetJsEnv(napi_env env);
52     void LoadAotFile(const Options& options, const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
53     void InitConsoleModule(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
54     void InitTimerModule(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
55     void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate,
56         const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
57     void SetRequestAotCallback(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
58     bool InitLoop(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
59     void InitWorkerModule(const Options& options, const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv);
60     void ParsePkgContextInfoJson(
61         nlohmann::json &pkgJson, std::vector<std::vector<std::string>> &pkgContextInfoList,
62         std::map<std::string, std::string> &pkgAliasMap);
63     void ParsePkgContextInfoJsonString(
64         const nlohmann::json &itemObject, const std::string &key, std::vector<std::string> &items);
65     void SetChildOptions(const Options& options);
66     std::unordered_map<napi_env, std::shared_ptr<JsEnv::JsEnvironment>> envMap_;
67     std::unordered_set<pid_t> threadIds_;
68     bool preloaded_ = false;
69     std::mutex envMutex_;
70     std::mutex childOptionsMutex_;
71     std::shared_ptr<Options> childOptions_;
72 };
73 } // namespace AbilityRuntime
74 } // namespace OHOS
75 #endif // ABILITY_ABILITY_RUNTIME_JS_RUNTIME_LITE_H