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 "datashare_log.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 DataShare {
32 using namespace AbilityRuntime;
33 namespace {
34 class JsDataShareExtAbilityContext final {
35 public:
JsDataShareExtAbilityContext(const std::shared_ptr<DataShareExtAbilityContext> & context)36     explicit JsDataShareExtAbilityContext(const std::shared_ptr<DataShareExtAbilityContext>& context)
37         : context_(context) {}
38     ~JsDataShareExtAbilityContext() = default;
39 
Finalizer(napi_env env,void * data,void * hint)40     static void Finalizer(napi_env env, void* data, void* hint)
41     {
42         LOG_DEBUG("JsAbilityContext::Finalizer is called");
43         std::unique_ptr<JsDataShareExtAbilityContext>(static_cast<JsDataShareExtAbilityContext*>(data));
44     }
45 private:
46     std::weak_ptr<DataShareExtAbilityContext> context_;
47 };
48 } // namespace
49 
CreateJsDataShareExtAbilityContext(napi_env env,std::shared_ptr<DataShareExtAbilityContext> context)50 napi_value CreateJsDataShareExtAbilityContext(napi_env env,
51     std::shared_ptr<DataShareExtAbilityContext> context)
52 {
53     LOG_DEBUG("CreateJsDataShareExtAbilityContext begin");
54     napi_value objValue = CreateJsExtensionContext(env, context);
55     std::unique_ptr<JsDataShareExtAbilityContext> jsContext = std::make_unique<JsDataShareExtAbilityContext>(context);
56     napi_wrap(env, objValue, jsContext.release(), JsDataShareExtAbilityContext::Finalizer, nullptr, nullptr);
57     return objValue;
58 }
59 }  // namespace DataShare
60 }  // namespace OHOS