• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_enterprise_admin_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 EDM {
25 namespace {
26 class JsEnterpriseAdminExtensionContext final {
27 public:
JsEnterpriseAdminExtensionContext(const std::shared_ptr<EnterpriseAdminExtensionContext> & context)28     explicit JsEnterpriseAdminExtensionContext(const std::shared_ptr<EnterpriseAdminExtensionContext>& context)
29         : context_(context) {}
30     ~JsEnterpriseAdminExtensionContext() = default;
31 
Finalizer(NativeEngine * engine,void * data,void * hint)32     static void Finalizer(NativeEngine* engine, void* data, void* hint)
33     {
34         std::unique_ptr<JsEnterpriseAdminExtensionContext>(
35             static_cast<JsEnterpriseAdminExtensionContext*>(data));
36     }
37 private:
38     std::weak_ptr<EnterpriseAdminExtensionContext> context_;
39 };
40 } // namespace
41 
CreateJsEnterpriseAdminExtensionContext(NativeEngine & engine,std::shared_ptr<EnterpriseAdminExtensionContext> context)42 NativeValue* CreateJsEnterpriseAdminExtensionContext(NativeEngine& engine,
43     std::shared_ptr<EnterpriseAdminExtensionContext> context)
44 {
45     NativeValue *objValue = AbilityRuntime::CreateJsExtensionContext(engine, context);
46     NativeObject *object = AbilityRuntime::ConvertNativeValueTo<NativeObject>(objValue);
47 
48     std::unique_ptr<JsEnterpriseAdminExtensionContext> jsContext =
49         std::make_unique<JsEnterpriseAdminExtensionContext>(context);
50     object->SetNativePointer(jsContext.release(), JsEnterpriseAdminExtensionContext::Finalizer, nullptr);
51     return objValue;
52 }
53 } // namespace EDM
54 } // namespace OHOS
55