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 #ifdef MEDIALIBRARY_MTP_ENABLE
16 #define MLOG_TAG "MtpServiceNapi"
17
18 #include "mtp_service_napi.h"
19
20 #include "mtp_service.h"
21
22 namespace OHOS {
23 namespace Media {
24 thread_local napi_ref MtpServiceNapi::sConstructor_ = nullptr;
25
Init(napi_env env,napi_value exports)26 napi_value MtpServiceNapi::Init(napi_env env, napi_value exports)
27 {
28 napi_value ctorObj;
29 napi_property_descriptor media_library_properties[] = {};
30 napi_status status = napi_define_class(env, MTP_SERVICE_NAPI_CLASS_NAME.c_str(),
31 NAPI_AUTO_LENGTH, MtpServiceNapiConstructor, nullptr,
32 sizeof(media_library_properties) / sizeof(napi_property_descriptor),
33 media_library_properties, &ctorObj);
34 if (status != napi_ok) {
35 NAPI_ERR_LOG("napi define class error %{public}d", status);
36 return nullptr;
37 }
38
39 int32_t refCount = 1;
40 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
41 if (status != napi_ok) {
42 NAPI_ERR_LOG("napi create reference error %{public}d", status);
43 return nullptr;
44 }
45
46 status = napi_set_named_property(env, exports, MTP_SERVICE_NAPI_CLASS_NAME.c_str(), ctorObj);
47 if (status != napi_ok) {
48 NAPI_ERR_LOG("napi set named property error %{public}d", status);
49 return nullptr;
50 }
51
52 napi_property_descriptor static_prop[] = {
53 DECLARE_NAPI_STATIC_FUNCTION("startMtpService", StartMtpService),
54 DECLARE_NAPI_STATIC_FUNCTION("stopMtpService", StopMtpService),
55 };
56 status = napi_define_properties(env, exports, sizeof(static_prop) / sizeof(napi_property_descriptor), static_prop);
57 if (status != napi_ok) {
58 NAPI_ERR_LOG("napi define properties error %{public}d", status);
59 return nullptr;
60 }
61
62 return exports;
63 }
64
MtpServiceNapiConstructor(napi_env env,napi_callback_info info)65 napi_value MtpServiceNapi::MtpServiceNapiConstructor(napi_env env, napi_callback_info info)
66 {
67 return nullptr;
68 }
69
MtpServiceNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)70 void MtpServiceNapi::MtpServiceNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint)
71 {
72 }
73
StartMtpService(napi_env env,napi_callback_info info)74 napi_value MtpServiceNapi::StartMtpService(napi_env env, napi_callback_info info)
75 {
76 NAPI_DEBUG_LOG("StartMtpService IN");
77
78 napi_value result = nullptr;
79 std::shared_ptr<OHOS::Media::MtpService> mtpService = OHOS::Media::MtpService::GetInstance();
80 if (mtpService != nullptr) {
81 mtpService->StartService();
82 NAPI_DEBUG_LOG("StartMtpService success");
83 } else {
84 NAPI_ERR_LOG("StartMtpService fail");
85 }
86 napi_get_undefined(env, &result);
87 NAPI_DEBUG_LOG("StartMtpService OUT");
88 return result;
89 }
90
StopMtpService(napi_env env,napi_callback_info info)91 napi_value MtpServiceNapi::StopMtpService(napi_env env, napi_callback_info info)
92 {
93 NAPI_DEBUG_LOG("StopMtpService IN");
94
95 napi_value result = nullptr;
96 std::shared_ptr<OHOS::Media::MtpService> mtpService = OHOS::Media::MtpService::GetInstance();
97 if (mtpService != nullptr) {
98 mtpService->StopService();
99 NAPI_DEBUG_LOG("StopMtpService success");
100 } else {
101 NAPI_ERR_LOG("StopMtpService fail");
102 }
103
104 napi_get_undefined(env, &result);
105 NAPI_DEBUG_LOG("StopMtpService OUT");
106 return result;
107 }
108
109 /*
110 * Function registering all props and functions of ohos.medialibrary module
111 */
Export(napi_env env,napi_value exports)112 static napi_value Export(napi_env env, napi_value exports)
113 {
114 MtpServiceNapi::Init(env, exports);
115 return exports;
116 }
117
118 /*
119 * module define
120 */
121 static napi_module g_module = {
122 .nm_version = 1,
123 .nm_flags = 0,
124 .nm_filename = nullptr,
125 .nm_register_func = Export,
126 .nm_modname = "multimedia.MtpService",
127 .nm_priv = reinterpret_cast<void *>(0),
128 .reserved = {0}
129 };
130
131 /*
132 * module register
133 */
RegisterModule(void)134 extern "C" __attribute__((constructor)) void RegisterModule(void)
135 {
136 napi_module_register(&g_module);
137 }
138 } // namespace Media
139 } // namespace OHOS
140 #endif