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