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_EXECUTOR_H
17 #define OHOS_ABILITY_RUNTIME_JS_STARTUP_TASK_EXECUTOR_H
18 
19 #include "js_runtime.h"
20 #include "startup_task_result.h"
21 #include "startup_utils.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 class JsStartupTaskExecutor {
26 public:
27     static int32_t RunOnMainThread(JsRuntime &jsRuntime,
28         const std::unique_ptr<NativeReference> &startup, const std::shared_ptr<NativeReference> &context,
29         std::unique_ptr<StartupTaskResultCallback> callback);
30 
31     static int32_t RunOnTaskPool(JsRuntime &jsRuntime,
32         const std::unique_ptr<NativeReference> &startup,
33         const std::shared_ptr<NativeReference> &context,
34         const std::unique_ptr<NativeReference> &asynctaskexcutor,
35         const std::unique_ptr<NativeReference> &asyncTaskCallback,
36         const std::string &startupName);
37 
38 private:
39     static int32_t CallStartupInit(napi_env env, const std::unique_ptr<NativeReference> &startup,
40         const std::shared_ptr<NativeReference> &context, std::unique_ptr<StartupTaskResultCallback> &callback,
41         napi_value &returnVal);
42 
43     static int32_t HandleReturnVal(napi_env env, napi_value returnVal,
44         std::unique_ptr<StartupTaskResultCallback> &callback);
45 
46     static napi_value ResolveResultCallback(napi_env env, napi_callback_info info);
47 
48     static napi_value RejectResultCallback(napi_env env, napi_callback_info info);
49 
50     static void ReplyFailed(StartupTaskResultCallback *callback,
51         int32_t resultCode, const std::string &resultMessage = "");
52 
53     static void ReplyFailed(std::unique_ptr<StartupTaskResultCallback> callback,
54         int32_t resultCode, const std::string &resultMessage = "");
55 
56     static void ReplySucceeded(StartupTaskResultCallback *callback,
57         const std::shared_ptr<NativeReference> &resultRef);
58 };
59 } // namespace AbilityRuntime
60 } // namespace OHOS
61 #endif // OHOS_ABILITY_RUNTIME_JS_STARTUP_TASK_EXECUTOR_H
62