1 /*
2 * Copyright (c) 2023 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 "ipc_skeleton.h"
17 #include "napi_param_utils.h"
18 #include "drm_napi.h"
19
20 namespace OHOS {
21 namespace DrmStandard {
22 thread_local napi_ref DrmNapi::sConstructor_ = nullptr;
23
DrmNapi()24 DrmNapi::DrmNapi() : env_(nullptr), wrapper_(nullptr)
25 {
26 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
27 }
28
~DrmNapi()29 DrmNapi::~DrmNapi()
30 {
31 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
32 if (wrapper_ != nullptr) {
33 napi_delete_reference(env_, wrapper_);
34 }
35 }
36
Init(napi_env env,napi_value exports)37 napi_value DrmNapi::Init(napi_env env, napi_value exports)
38 {
39 DRM_INFO_LOG("DrmNapi Init enter.");
40 napi_property_descriptor drmproperty[] = {
41 DECLARE_NAPI_FUNCTION("getMediaKeySystemTest", CreateMediaKeySystemInstance),
42 };
43 DRM_DEBUG_LOG("DrmNapi enter properties");
44 napi_property_descriptor drm_static_properties[] = {
45 DECLARE_NAPI_STATIC_FUNCTION("createMediaKeySystem", CreateMediaKeySystemInstance),
46 DECLARE_NAPI_STATIC_FUNCTION("isMediaKeySystemSupported", IsMediaKeySystemSupported),
47 };
48 DRM_DEBUG_LOG("DrmNapi enter napi_define_class");
49 napi_value constructor = nullptr;
50 napi_status status = napi_define_class(env, DRM_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, DrmNapiConstructor, nullptr,
51 sizeof(drmproperty) / sizeof(drmproperty[0]), drmproperty, &constructor);
52
53 DRM_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define DrmNapi class");
54
55 status = napi_create_reference(env, constructor, 1, &sConstructor_);
56 DRM_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of DrmNapi constructor");
57 status = napi_set_named_property(env, exports, DRM_NAPI_CLASS_NAME, constructor);
58 DRM_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set DrmNapi constructor");
59
60 status = napi_define_properties(env, exports, sizeof(drm_static_properties) / sizeof(drm_static_properties[0]),
61 drm_static_properties);
62 DRM_CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static DrmNapi function");
63
64 DRM_INFO_LOG("DrmNapi Init success");
65 return exports;
66 }
67
DrmNapiConstructor(napi_env env,napi_callback_info info)68 napi_value DrmNapi::DrmNapiConstructor(napi_env env, napi_callback_info info)
69 {
70 DRM_INFO_LOG("DrmNapi DrmNapiConstructor enter.");
71 napi_status status;
72 napi_value result = nullptr;
73 napi_value thisVar = nullptr;
74
75 napi_get_undefined(env, &result);
76 DRM_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
77
78 if (status == napi_ok && thisVar != nullptr) {
79 std::unique_ptr<DrmNapi> obj = std::make_unique<DrmNapi>();
80 if (obj != nullptr) {
81 obj->env_ = env;
82 status = napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), DrmNapi::DrmNapiDestructor, nullptr,
83 nullptr);
84 if (status == napi_ok) {
85 obj.release();
86 DRM_INFO_LOG("DrmNapi DrmNapiConstructor exit.");
87 return thisVar;
88 } else {
89 DRM_ERR_LOG("DrmNapiDestructor Failure wrapping js to native napi");
90 }
91 }
92 }
93 DRM_ERR_LOG("DrmNapiDestructor call Failed!");
94 DRM_INFO_LOG("DrmNapi DrmNapiConstructor exit.");
95 return result;
96 }
97
DrmNapiDestructor(napi_env env,void * nativeObject,void * finalize)98 void DrmNapi::DrmNapiDestructor(napi_env env, void *nativeObject, void *finalize)
99 {
100 DRM_INFO_LOG("DrmNapi DrmNapiDestructor enter.");
101 DrmNapi *drmNapi = reinterpret_cast<DrmNapi *>(nativeObject);
102 if (drmNapi != nullptr) {
103 drmNapi->~DrmNapi();
104 }
105 DRM_INFO_LOG("DrmNapi DrmNapiDestructor exit.");
106 }
107
IsMediaKeySystemSupported(napi_env env,napi_callback_info info)108 napi_value DrmNapi::IsMediaKeySystemSupported(napi_env env, napi_callback_info info)
109 {
110 DRM_INFO_LOG("DrmNapi IsMediaKeySystemSupported enter.");
111 napi_value result = nullptr;
112 result = MediaKeySystemNapi::IsMediaKeySystemSupported(env, info);
113 DRM_INFO_LOG("DrmNapi IsMediaKeySystemSupported exit.");
114 return result;
115 }
116
CreateMediaKeySystemInstance(napi_env env,napi_callback_info info)117 napi_value DrmNapi::CreateMediaKeySystemInstance(napi_env env, napi_callback_info info)
118 {
119 DRM_INFO_LOG("DrmNapi CreateMediaKeySystemInstance enter.");
120 napi_value result = nullptr;
121 result = MediaKeySystemNapi::CreateMediaKeySystemInstance(env, info);
122 DRM_INFO_LOG("DrmNapi CreateMediaKeySystemInstance exit.");
123 return result;
124 }
125 } // namespace OHOS
126 } // namespace DrmStandard
127