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 OHOS_ABILITY_RUNTIME_JS_STARTUP_TASK_H
17 #define OHOS_ABILITY_RUNTIME_JS_STARTUP_TASK_H
18 
19 #include "js_runtime.h"
20 #include "js_runtime_utils.h"
21 #include "js_startup_task_executor.h"
22 #include "js_startup_task_result.h"
23 #include "startup_task.h"
24 #include "startup_utils.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 class AsyncTaskCallBack {
29 public:
30     AsyncTaskCallBack() = default;
31     ~AsyncTaskCallBack() = default;
32 
33     static napi_value AsyncTaskCompleted(napi_env env, napi_callback_info info);
34     static napi_value Constructor(napi_env env, napi_callback_info cbinfo);
35     static std::map<std::string, std::weak_ptr<StartupTask>> jsStartupTaskObjects_;
36 };
37 
38 class JsStartupTask : public StartupTask {
39 public:
40     JsStartupTask(const std::string &name, JsRuntime &jsRuntime,
41         std::unique_ptr<NativeReference> &startupJsRef, std::shared_ptr<NativeReference> &contextJsRef_);
42 
43     ~JsStartupTask() override;
44 
45     int32_t Init();
46 
47     int32_t RunTaskInit(std::unique_ptr<StartupTaskResultCallback> callback) override;
48 
49     int32_t RunTaskOnDependencyCompleted(const std::string &dependencyName,
50         const std::shared_ptr<StartupTaskResult> &result) override;
51 
52     int32_t LoadJsAsyncTaskExcutor();
53 
54     void LoadJsAsyncTaskCallback();
55 
56     void OnAsyncTaskCompleted(const std::shared_ptr<StartupTaskResult> &result) override;
57 
58 private:
59     JsRuntime &jsRuntime_;
60     std::unique_ptr<NativeReference> startupJsRef_;
61     std::shared_ptr<NativeReference> contextJsRef_;
62     std::unique_ptr<NativeReference> AsyncTaskExcutorJsRef_;
63     std::unique_ptr<NativeReference> AsyncTaskExcutorCallbackJsRef_;
64     std::unique_ptr<StartupTaskResultCallback> startupTaskResultCallback_;
65 
66     static napi_value GetDependencyResult(napi_env env, const std::string &dependencyName,
67         const std::shared_ptr<StartupTaskResult> &result);
68 };
69 } // namespace AbilityRuntime
70 } // namespace OHOS
71 #endif // OHOS_ABILITY_RUNTIME_JS_STARTUP_TASK_H
72