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(NativeEngine * engine,void * data,void * hint)50 static void Finalizer(NativeEngine* engine, void* data, void* hint)
51 {
52 HILOG_INFO("JsAbilityContext::Finalizer is called");
53 std::unique_ptr<JsDriverExtensionContext>(static_cast<JsDriverExtensionContext*>(data));
54 }
55
UpdateDriverState(NativeEngine * engine,NativeCallbackInfo * info)56 static NativeValue* UpdateDriverState(NativeEngine* engine, NativeCallbackInfo* info)
57 {
58 JsDriverExtensionContext* me = CheckParamsAndGetThis<JsDriverExtensionContext>(engine, info);
59 return (me != nullptr) ? me->OnUpdateDriverState(*engine, *info) : nullptr;
60 }
61
62 private:
63 std::weak_ptr<DriverExtensionContext> context_;
64 sptr<JsFreeInstallObserver> freeInstallObserver_ = nullptr;
65
OnUpdateDriverState(NativeEngine & engine,NativeCallbackInfo & info)66 NativeValue* OnUpdateDriverState(NativeEngine& engine, NativeCallbackInfo& info)
67 {
68 HILOG_INFO("OnUpdateDriverState is called");
69
70 AsyncTask::CompleteCallback complete =
71 [weak = context_](NativeEngine& engine, AsyncTask& 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(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released"));
77 return;
78 }
79
80 ErrCode innerErrorCode = context->UpdateDriverState();
81 if (innerErrorCode == 0) {
82 task.Resolve(engine, engine.CreateUndefined());
83 } else {
84 task.Reject(engine, CreateJsErrorByNativeErr(engine, innerErrorCode));
85 }
86 };
87
88 NativeValue* lastParam = (info.argc == ARGC_ZERO) ? nullptr : info.argv[INDEX_ZERO];
89 NativeValue* result = nullptr;
90 AsyncTask::Schedule("JSDriverExtensionContext::OnUpdateDriverState",
91 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
92 return result;
93 }
94 };
95 } // namespace
96
CreateJsDriverExtensionContext(NativeEngine & engine,std::shared_ptr<DriverExtensionContext> context)97 NativeValue* CreateJsDriverExtensionContext(NativeEngine& engine, std::shared_ptr<DriverExtensionContext> context)
98 {
99 HILOG_INFO("CreateJsDriverExtensionContext begin");
100 std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
101 if (context) {
102 abilityInfo = context->GetAbilityInfo();
103 }
104 NativeValue* objValue = CreateJsExtensionContext(engine, context, abilityInfo);
105 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
106
107 std::unique_ptr<JsDriverExtensionContext> jsContext = std::make_unique<JsDriverExtensionContext>(context);
108 object->SetNativePointer(jsContext.release(), JsDriverExtensionContext::Finalizer, nullptr);
109
110 const char *moduleName = "JsDriverExtensionContext";
111 BindNativeFunction(engine, *object, "updateDriverState", moduleName, JsDriverExtensionContext::UpdateDriverState);
112
113 return objValue;
114 }
115 } // namespace AbilityRuntime
116 } // namespace OHOS
117