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