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 #include "js_driver_extension_context.h"
17
18 #include <chrono>
19 #include <cstdint>
20
21 #include "ability_manager_client.h"
22 #include "ability_runtime/js_caller_complex.h"
23 #include "hilog_wrapper.h"
24 #include "js_extension_context.h"
25 #include "js_error_utils.h"
26 #include "js_data_struct_converter.h"
27 #include "js_runtime.h"
28 #include "js_runtime_utils.h"
29 #include "napi/native_api.h"
30 #include "napi_common_ability.h"
31 #include "napi_common_want.h"
32 #include "napi_common_util.h"
33 #include "napi_remote_object.h"
34 #include "napi_common_start_options.h"
35 #include "start_options.h"
36 #include "hitrace_meter.h"
37
38 namespace OHOS {
39 namespace AbilityRuntime {
40 namespace {
41 constexpr int32_t INDEX_ZERO = 0;
42 constexpr int32_t ERROR_CODE_ONE = 1;
43 constexpr size_t ARGC_ZERO = 0;
44
45 class JsDriverExtensionContext final {
46 public:
JsDriverExtensionContext(const std::shared_ptr<DriverExtensionContext> & context)47 explicit JsDriverExtensionContext(const std::shared_ptr<DriverExtensionContext>& context) : context_(context) {}
48 ~JsDriverExtensionContext() = default;
49
Finalizer(napi_env env,void * data,void * hint)50 static void Finalizer(napi_env env, void* data, void* hint)
51 {
52 HILOG_INFO("JsAbilityContext::Finalizer is called");
53 std::unique_ptr<JsDriverExtensionContext>(static_cast<JsDriverExtensionContext*>(data));
54 }
55
UpdateDriverState(napi_env env,napi_callback_info info)56 static napi_value UpdateDriverState(napi_env env, napi_callback_info info)
57 {
58 JsDriverExtensionContext* me = CheckParamsAndGetThis<JsDriverExtensionContext>(env, info);
59 return (me != nullptr) ? me->OnUpdateDriverState(env, info) : nullptr;
60 }
61
62 private:
63 std::weak_ptr<DriverExtensionContext> context_;
64 sptr<JsFreeInstallObserver> freeInstallObserver_ = nullptr;
65
OnUpdateDriverState(napi_env env,napi_callback_info info)66 napi_value OnUpdateDriverState(napi_env env, napi_callback_info info)
67 {
68 HILOG_INFO("OnUpdateDriverState is called");
69 size_t argc = 1;
70 napi_value argv[1] = {nullptr};
71 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
72 napi_value lastParam = (argc > ARGC_ZERO) ? argv[INDEX_ZERO] : nullptr;
73
74 napi_value result = nullptr;
75 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
76 auto asyncTask = [weak = context_, env, task = napiAsyncTask.get()]() {
77 HILOG_INFO("UpdateDriverState begin");
78 auto context = weak.lock();
79 if (!context) {
80 HILOG_WARN("context is released");
81 task->Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released"));
82 delete task;
83 return;
84 }
85
86 ErrCode innerErrorCode = context->UpdateDriverState();
87 if (innerErrorCode == 0) {
88 napi_value result = nullptr;
89 napi_get_undefined(env, &result);
90 task->Resolve(env, result);
91 } else {
92 task->Reject(env, CreateJsErrorByNativeErr(env, innerErrorCode));
93 }
94 delete task;
95 };
96 if (napi_status::napi_ok != napi_send_event(env, asyncTask, napi_eprio_high)) {
97 napiAsyncTask->Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context send event failed"));
98 } else {
99 napiAsyncTask.release();
100 }
101 return result;
102 }
103
CreateEmptyAsyncTask(napi_env env,napi_value lastParam,napi_value * result)104 std::unique_ptr<NapiAsyncTask> CreateEmptyAsyncTask(napi_env env, napi_value lastParam, napi_value* result)
105 {
106 napi_valuetype type = napi_undefined;
107 napi_typeof(env, lastParam, &type);
108 if (lastParam == nullptr || type != napi_function) {
109 napi_deferred nativeDeferred = nullptr;
110 napi_create_promise(env, &nativeDeferred, result);
111 return std::make_unique<NapiAsyncTask>(nativeDeferred, std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
112 std::unique_ptr<NapiAsyncTask::CompleteCallback>());
113 } else {
114 napi_get_undefined(env, result);
115 napi_ref callbackRef = nullptr;
116 napi_create_reference(env, lastParam, 1, &callbackRef);
117 return std::make_unique<NapiAsyncTask>(callbackRef, std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
118 std::unique_ptr<NapiAsyncTask::CompleteCallback>());
119 }
120 }
121 };
122 } // namespace
123
CreateJsDriverExtensionContext(napi_env env,std::shared_ptr<DriverExtensionContext> context)124 napi_value CreateJsDriverExtensionContext(napi_env env, std::shared_ptr<DriverExtensionContext> context)
125 {
126 HILOG_INFO("CreateJsDriverExtensionContext begin");
127 std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
128 if (context) {
129 abilityInfo = context->GetAbilityInfo();
130 }
131 napi_value objValue = CreateJsExtensionContext(env, context, abilityInfo);
132
133 std::unique_ptr<JsDriverExtensionContext> jsContext = std::make_unique<JsDriverExtensionContext>(context);
134 napi_wrap(env, objValue, jsContext.release(), JsDriverExtensionContext::Finalizer, nullptr, nullptr);
135
136 const char *moduleName = "JsDriverExtensionContext";
137 BindNativeFunction(env, objValue, "updateDriverState", moduleName, JsDriverExtensionContext::UpdateDriverState);
138 return objValue;
139 }
140 } // namespace AbilityRuntime
141 } // namespace OHOS
142