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