• 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 OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
17 #define OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "native_engine/native_engine.h"
27 #include "runtime.h"
28 #include "source_map.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 class EventHandler;
33 } // namespace AppExecFwk
34 namespace AbilityRuntime {
35 class TimerTask;
36 class ModSourceMap;
37 class RuntimeExtractor;
38 
39 void *DetachCallbackFunc(NativeEngine *engine, void *value, void *hint);
40 
41 class JsRuntime : public Runtime {
42 public:
43     static std::unique_ptr<Runtime> Create(const Options& options);
44 
45     static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(NativeEngine* engine,
46         const std::string& moduleName, NativeValue* const* argv, size_t argc);
47 
48     ~JsRuntime() override = default;
49 
GetNativeEngine()50     NativeEngine& GetNativeEngine() const
51     {
52         return *nativeEngine_;
53     }
54 
GetSourceMap()55     ModSourceMap& GetSourceMap() const
56     {
57         return *bindSourceMaps_;
58     }
59 
GetLanguage()60     Language GetLanguage() const override
61     {
62         return Language::JS;
63     }
64 
65     std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
66         const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false);
67     std::unique_ptr<NativeReference> LoadSystemModule(
68         const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0);
69     void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime);
70     void RemoveTask(const std::string& name);
71     void DumpHeapSnapshot(bool isPrivate) override;
72     bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrames>& jsFrames) override;
73     void NotifyApplicationState(bool isBackground) override;
74 
75     bool RunSandboxScript(const std::string& path, const std::string& hapPath);
76     virtual bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false) = 0;
77 
78     void PreloadSystemModule(const std::string& moduleName) override;
UpdateModuleNameAndAssetPath(const std::string & moduleName)79     virtual void UpdateModuleNameAndAssetPath(const std::string& moduleName) {}
80     void UpdateExtensionType(int32_t extensionType) override;
81 
82 protected:
83     JsRuntime() = default;
84 
85     virtual bool Initialize(const Options& options);
86     void Deinitialize();
87 
88     virtual NativeValue* LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
89     virtual NativeValue* LoadJsModule(const std::string& path, const std::string& hapPath) = 0;
90 
91     bool isArkEngine_ = false;
92     bool debugMode_ = false;
93     bool preloaded_ = false;
94     bool isBundle_ = true;
95     std::unique_ptr<NativeEngine> nativeEngine_;
96     std::unique_ptr<ModSourceMap> bindSourceMaps_;
97     std::string codePath_;
98     std::string moduleName_;
99     std::unique_ptr<NativeReference> methodRequireNapiRef_;
100     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
101     std::unordered_map<std::string, NativeReference*> modules_;
102     std::map<std::string, std::shared_ptr<RuntimeExtractor>> runtimeExtractorMap_;
103 };
104 }  // namespace AbilityRuntime
105 }  // namespace OHOS
106 
107 #endif  // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
108