• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetProgressHandlerRef()102 napi_ref NapiMediaAssetDataHandler::GetProgressHandlerRef()
103 {
104     return progressHandlerRef_;
105 }
106 
SetProgressHandlerRef(napi_ref & progressHandlerRef)107 void NapiMediaAssetDataHandler::SetProgressHandlerRef(napi_ref &progressHandlerRef)
108 {
109     progressHandlerRef_ = progressHandlerRef;
110 }
111 
JsOnDataPrepared(napi_env env,napi_value arg,napi_value extraInfo)112 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value arg, napi_value extraInfo)
113 {
114     std::unique_lock<std::mutex> dataHandlerLock(dataHandlerRefMutex_);
115     if (dataHandlerRef_ == nullptr) {
116         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
117         dataHandlerLock.unlock();
118         return;
119     }
120 
121     napi_value callback;
122     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
123     dataHandlerLock.unlock();
124     if (status != napi_ok) {
125         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
126             static_cast<int>(status));
127         return;
128     }
129 
130     napi_value jsOnDataPrepared;
131     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
132     if (status != napi_ok) {
133         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
134             static_cast<int>(status));
135         return;
136     }
137 
138     constexpr size_t maxArgs = 2;
139     napi_value argv[maxArgs];
140     size_t argc = 0;
141     if (extraInfo != nullptr) {
142         argv[PARAM0] = arg;
143         argv[PARAM1] = extraInfo;
144         argc = ARGS_TWO;
145     } else {
146         argv[PARAM0] = arg;
147         argc = ARGS_ONE;
148     }
149     napi_value promise;
150     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
151     if (status != napi_ok) {
152         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
153         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
154     }
155 }
156 
JsOnDataPrepared(napi_env env,napi_value pictures,napi_value arg,napi_value extraInfo)157 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value pictures, napi_value arg,
158     napi_value extraInfo)
159 {
160     std::unique_lock<std::mutex> dataHandlerLock(dataHandlerRefMutex_);
161     if (dataHandlerRef_ == nullptr) {
162         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
163         dataHandlerLock.unlock();
164         return;
165     }
166 
167     napi_value callback;
168     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
169     dataHandlerLock.unlock();
170     if (status != napi_ok) {
171         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
172             static_cast<int>(status));
173         return;
174     }
175 
176     napi_value jsOnDataPrepared;
177     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
178     if (status != napi_ok) {
179         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
180             static_cast<int>(status));
181         return;
182     }
183 
184     constexpr size_t maxArgs = 3;
185     napi_value argv[maxArgs];
186     size_t argc = 0;
187     if (extraInfo != nullptr) {
188         argv[PARAM0] = pictures;
189         argv[PARAM1] = arg;
190         argv[PARAM2] = extraInfo;
191         argc = ARGS_THREE;
192     } else {
193         argv[PARAM0] = pictures;
194         argv[PARAM1] = arg;
195         argc = ARGS_TWO;
196     }
197     napi_value promise;
198     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
199     if (status != napi_ok) {
200         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
201         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
202     }
203 }
204 } // namespace Media
205 } // namespace OHOS
206