• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_JS_ENVIRONMENT_JS_ENVIRONMENT_H
17 #define OHOS_ABILITY_JS_ENVIRONMENT_JS_ENVIRONMENT_H
18 
19 #include <memory>
20 #include "ecmascript/napi/include/dfx_jsnapi.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22 #include "js_environment_impl.h"
23 #include "source_map_operator.h"
24 #include "uncaught_exception_callback.h"
25 
26 namespace OHOS {
27 namespace JsEnv {
28 struct WorkerInfo;
29 class JsEnvironmentImpl;
30 using DebuggerPostTask = std::function<void(std::function<void()>&&)>;
31 using RequestAotCallback =
32     std::function<int32_t(const std::string& bundleName, const std::string& moduleName, int32_t triggerMode)>;
33 using UncatchableTask = std::function<void(std::string summary, const JsEnv::ErrorObject errorObject)>;
34 class JsEnvironment final : public std::enable_shared_from_this<JsEnvironment> {
35 public:
JsEnvironment()36     JsEnvironment() {}
37     explicit JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl);
38     ~JsEnvironment();
39 
40     enum class PROFILERTYPE {
41         PROFILERTYPE_CPU,
42         PROFILERTYPE_HEAP
43     };
44 
45     bool Initialize(const panda::RuntimeOption& pandaOption, void* jsEngine);
46 
GetNativeEngine()47     NativeEngine* GetNativeEngine() const
48     {
49         return engine_;
50     }
51 
GetSourceMapOperator()52     std::shared_ptr<SourceMapOperator> GetSourceMapOperator() const
53     {
54         return sourceMapOperator_;
55     }
56 
GetVM()57     panda::ecmascript::EcmaVM* GetVM() const
58     {
59         return vm_;
60     }
61 
62     void InitTimerModule();
63 
64     void InitWorkerModule(std::shared_ptr<WorkerInfo> workerInfo);
65 
66     void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj);
67 
68     void InitSyscapModule();
69 
70     void PostTask(const std::function<void()>& task, const std::string& name = "", int64_t delayTime = 0);
71 
72     void PostSyncTask(const std::function<void()>& task, const std::string& name);
73 
74     void RemoveTask(const std::string& name);
75 
76     void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
77 
78     void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask);
79 
80     bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
81 
82     bool StartDebugger(
83         std::string& option, uint32_t socketFd, bool isDebugApp);
84 
85     void StopDebugger();
86 
87     void StopDebugger(std::string& option);
88 
89     void InitConsoleModule();
90 
91     bool InitLoop(bool isStage = true);
92 
93     void DeInitLoop();
94 
95     bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle);
96 
97     DebuggerPostTask GetDebuggerPostTask();
98 
99     void StartProfiler(const char* libraryPath,
100         uint32_t instanceId, PROFILERTYPE profiler, int32_t interval, int tid, bool isDebugApp);
101 
102     void DestroyHeapProfiler();
103 
104     void GetHeapPrepare();
105 
106     void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate);
107 
108     void ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl);
109 
110     void SetRequestAotCallback(const RequestAotCallback& cb);
111 
112     void SetDeviceDisconnectCallback(const std::function<bool()> &cb);
113 
114     void NotifyDebugMode(int tid, const char* libraryPath, uint32_t instanceId, bool isDebugApp, bool debugMode);
115 
116     bool GetDebugMode() const;
117 
118     int32_t ParseHdcRegisterOption(std::string& option);
119 
120 private:
121     std::unique_ptr<JsEnvironmentImpl> impl_ = nullptr;
122     NativeEngine* engine_ = nullptr;
123     panda::ecmascript::EcmaVM* vm_ = nullptr;
124     std::shared_ptr<SourceMapOperator> sourceMapOperator_ = nullptr;
125     bool debugMode_ = false;
126 };
127 } // namespace JsEnv
128 } // namespace OHOS
129 #endif // OHOS_ABILITY_JS_ENVIRONMENT_JS_ENVIRONMENT_H
130