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 "media_asset_data_handler.h"
17
18 #include "medialibrary_client_errno.h"
19 #include "medialibrary_napi_utils.h"
20 #include "napi_error.h"
21
22 namespace OHOS {
23 namespace Media {
24 static const std::string MEDIA_ASSET_DATA_HANDLER_CLASS = "MediaAssetDataHandler";
25 std::mutex NapiMediaAssetDataHandler::dataHandlerRefMutex_;
26
NapiMediaAssetDataHandler(napi_env env, napi_ref dataHandler, ReturnDataType dataType, const std::string &uri, const std::string &destUri, SourceMode sourceMode)27 NapiMediaAssetDataHandler::NapiMediaAssetDataHandler(napi_env env, napi_ref dataHandler, ReturnDataType dataType,
28 const std::string &uri, const std::string &destUri, SourceMode sourceMode)
29 {
30 dataType_ = dataType;
31 env_ = env;
32 requestUri_ = uri;
33 destUri_ = destUri;
34 sourceMode_ = sourceMode;
35 dataHandlerRef_ = dataHandler;
36 }
37
DeleteNapiReference(napi_env env)38 void NapiMediaAssetDataHandler::DeleteNapiReference(napi_env env)
39 {
40 std::unique_lock<std::mutex> dataHandlerLock(dataHandlerRefMutex_);
41 if (dataHandlerRef_ != nullptr) {
42 if (env != nullptr) {
43 napi_delete_reference(env, dataHandlerRef_);
44 } else {
45 napi_delete_reference(env_, dataHandlerRef_);
46 }
47 dataHandlerRef_ = nullptr;
48 }
49 dataHandlerLock.unlock();
50 }
51
GetReturnDataType()52 ReturnDataType NapiMediaAssetDataHandler::GetReturnDataType()
53 {
54 return dataType_;
55 }
56
GetRequestUri()57 std::string NapiMediaAssetDataHandler::GetRequestUri()
58 {
59 return requestUri_;
60 }
61
GetDestUri()62 std::string NapiMediaAssetDataHandler::GetDestUri()
63 {
64 return destUri_;
65 }
66
GetSourceMode()67 SourceMode NapiMediaAssetDataHandler::GetSourceMode()
68 {
69 return sourceMode_;
70 }
71
SetNotifyMode(NotifyMode notifyMode)72 void NapiMediaAssetDataHandler::SetNotifyMode(NotifyMode notifyMode)
73 {
74 notifyMode_ = notifyMode;
75 }
76
GetNotifyMode()77 NotifyMode NapiMediaAssetDataHandler::GetNotifyMode()
78 {
79 return notifyMode_;
80 }
81
SetRequestId(std::string requestId)82 void NapiMediaAssetDataHandler::SetRequestId(std::string requestId)
83 {
84 requestId_ = requestId;
85 }
86
GetRequestId()87 std::string NapiMediaAssetDataHandler::GetRequestId()
88 {
89 return requestId_;
90 }
91
SetCompatibleMode(const CompatibleMode &compatibleMode)92 void NapiMediaAssetDataHandler::SetCompatibleMode(const CompatibleMode &compatibleMode)
93 {
94 compatibleMode_ = compatibleMode;
95 }
96
GetCompatibleMode()97 CompatibleMode NapiMediaAssetDataHandler::GetCompatibleMode()
98 {
99 return compatibleMode_;
100 }
101
JsOnDataPrepared(napi_env env, napi_value arg, napi_value extraInfo)102 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value arg, napi_value extraInfo)
103 {
104 std::unique_lock<std::mutex> dataHandlerLock(dataHandlerRefMutex_);
105 if (dataHandlerRef_ == nullptr) {
106 NAPI_ERR_LOG("JsOnDataPrepared js function is null");
107 dataHandlerLock.unlock();
108 return;
109 }
110
111 napi_value callback;
112 napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
113 dataHandlerLock.unlock();
114 if (status != napi_ok) {
115 NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
116 static_cast<int>(status));
117 return;
118 }
119
120 napi_value jsOnDataPrepared;
121 status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
122 if (status != napi_ok) {
123 NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
124 static_cast<int>(status));
125 return;
126 }
127
128 constexpr size_t maxArgs = 2;
129 napi_value argv[maxArgs];
130 size_t argc = 0;
131 if (extraInfo != nullptr) {
132 argv[PARAM0] = arg;
133 argv[PARAM1] = extraInfo;
134 argc = ARGS_TWO;
135 } else {
136 argv[PARAM0] = arg;
137 argc = ARGS_ONE;
138 }
139 napi_value promise;
140 status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
141 if (status != napi_ok) {
142 NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
143 NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
144 }
145 }
146
JsOnDataPrepared(napi_env env, napi_value pictures, napi_value arg, napi_value extraInfo)147 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value pictures, napi_value arg,
148 napi_value extraInfo)
149 {
150 std::unique_lock<std::mutex> dataHandlerLock(dataHandlerRefMutex_);
151 if (dataHandlerRef_ == nullptr) {
152 NAPI_ERR_LOG("JsOnDataPrepared js function is null");
153 dataHandlerLock.unlock();
154 return;
155 }
156
157 napi_value callback;
158 napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
159 dataHandlerLock.unlock();
160 if (status != napi_ok) {
161 NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
162 static_cast<int>(status));
163 return;
164 }
165
166 napi_value jsOnDataPrepared;
167 status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
168 if (status != napi_ok) {
169 NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
170 static_cast<int>(status));
171 return;
172 }
173
174 constexpr size_t maxArgs = 3;
175 napi_value argv[maxArgs];
176 size_t argc = 0;
177 if (extraInfo != nullptr) {
178 argv[PARAM0] = pictures;
179 argv[PARAM1] = arg;
180 argv[PARAM2] = extraInfo;
181 argc = ARGS_THREE;
182 } else {
183 argv[PARAM0] = pictures;
184 argv[PARAM1] = arg;
185 argc = ARGS_TWO;
186 }
187 napi_value promise;
188 status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
189 if (status != napi_ok) {
190 NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
191 NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
192 }
193 }
194 } // namespace Media
195 } // namespace OHOS
196