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 
70         NapiAsyncTask::CompleteCallback complete =
71             [weak = context_](napi_env env, NapiAsyncTask& task, int32_t status) {
72                 HILOG_INFO("UpdateDriverState begin");
73                 auto context = weak.lock();
74                 if (!context) {
75                     HILOG_WARN("context is released");
76                     task.Reject(env, CreateJsError(env, ERROR_CODE_ONE, "Context is released"));
77                     return;
78                 }
79 
80                 ErrCode innerErrorCode = context->UpdateDriverState();
81                 if (innerErrorCode == 0) {
82                     napi_value result = nullptr;
83                     napi_get_undefined(env, &result);
84                     task.Resolve(env, result);
85                 } else {
86                     task.Reject(env, CreateJsErrorByNativeErr(env, innerErrorCode));
87                 }
88             };
89         size_t argc = 1;
90         napi_value argv[1] = {nullptr};
91         napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
92         napi_value lastParam = (argc > ARGC_ZERO) ? argv[INDEX_ZERO] : nullptr;
93 
94         napi_value result = nullptr;
95         NapiAsyncTask::Schedule("JSDriverExtensionContext::OnUpdateDriverState",
96             env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
97         return result;
98     }
99 };
100 } // namespace
101 
CreateJsDriverExtensionContext(napi_env env,std::shared_ptr<DriverExtensionContext> context)102 napi_value CreateJsDriverExtensionContext(napi_env env, std::shared_ptr<DriverExtensionContext> context)
103 {
104     HILOG_INFO("CreateJsDriverExtensionContext begin");
105     std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
106     if (context) {
107         abilityInfo = context->GetAbilityInfo();
108     }
109     napi_value objValue = CreateJsExtensionContext(env, context, abilityInfo);
110 
111     std::unique_ptr<JsDriverExtensionContext> jsContext = std::make_unique<JsDriverExtensionContext>(context);
112     napi_wrap(env, objValue, jsContext.release(), JsDriverExtensionContext::Finalizer, nullptr, nullptr);
113 
114     const char *moduleName = "JsDriverExtensionContext";
115     BindNativeFunction(env, objValue, "updateDriverState", moduleName, JsDriverExtensionContext::UpdateDriverState);
116     return objValue;
117 }
118 }  // namespace AbilityRuntime
119 }  // namespace OHOS
120