1 /*
2 * Copyright (c) 2022 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_work_scheduler_extension_context.h"
17
18 #include "js_extension_context.h"
19 #include "js_runtime.h"
20 #include "js_runtime_utils.h"
21 #include "napi/native_api.h"
22
23 namespace OHOS {
24 namespace WorkScheduler {
25 namespace {
26 class JsWorkSchedulerExtensionContext final {
27 public:
JsWorkSchedulerExtensionContext(const std::shared_ptr<WorkSchedulerExtensionContext> & context)28 explicit JsWorkSchedulerExtensionContext(const std::shared_ptr<WorkSchedulerExtensionContext>& context)
29 : context_(context) {}
30 ~JsWorkSchedulerExtensionContext() = default;
31
Finalizer(NativeEngine * engine,void * data,void * hint)32 static void Finalizer(NativeEngine* engine, void* data, void* hint)
33 {
34 std::unique_ptr<JsWorkSchedulerExtensionContext>(
35 static_cast<JsWorkSchedulerExtensionContext*>(data));
36 }
37 private:
38 std::weak_ptr<WorkSchedulerExtensionContext> context_;
39 };
40 } // namespace
41
CreateJsWorkSchedulerExtensionContext(NativeEngine & engine,std::shared_ptr<WorkSchedulerExtensionContext> context)42 NativeValue* CreateJsWorkSchedulerExtensionContext(NativeEngine& engine,
43 std::shared_ptr<WorkSchedulerExtensionContext> context)
44 {
45 NativeValue *objValue = AbilityRuntime::CreateJsExtensionContext(engine, context);
46 NativeObject *object = AbilityRuntime::ConvertNativeValueTo<NativeObject>(objValue);
47
48 std::unique_ptr<JsWorkSchedulerExtensionContext> jsContext =
49 std::make_unique<JsWorkSchedulerExtensionContext>(context);
50 object->SetNativePointer(jsContext.release(), JsWorkSchedulerExtensionContext::Finalizer, nullptr);
51 return objValue;
52 }
53 } // namespace WorkScheduler
54 } // namespace OHOS
55