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
NapiMediaAssetDataHandler(napi_env env,napi_value jsMediaAssetDataHandler,ReturnDataType dataType)26 NapiMediaAssetDataHandler::NapiMediaAssetDataHandler(napi_env env,
27 napi_value jsMediaAssetDataHandler, ReturnDataType dataType)
28 {
29 thisVar_ = jsMediaAssetDataHandler;
30 dataType_ = dataType;
31 env_ = env;
32 napi_status status = napi_get_named_property(env_, jsMediaAssetDataHandler, "onDataPrepared", &ondataPreparedFunc_);
33 if (status != napi_ok) {
34 NAPI_ERR_LOG("NapiMediaAssetDataHandler get onDataPrepared function failed");
35 NapiError::ThrowError(env_, OHOS_INVALID_PARAM_CODE, "handler is invalid");
36 }
37 }
38
GetHandlerType()39 ReturnDataType NapiMediaAssetDataHandler::GetHandlerType()
40 {
41 return dataType_;
42 }
43
JsOnDataPreared(napi_value arg)44 void NapiMediaAssetDataHandler::JsOnDataPreared(napi_value arg)
45 {
46 napi_value argv[] = {arg};
47 napi_value result = nullptr;
48 if (ondataPreparedFunc_ == nullptr) {
49 NAPI_ERR_LOG("NapiMediaAssetDataHandler JsOnDataPreared js function is null");
50 NapiError::ThrowError(env_, JS_INNER_FAIL, "handler is invalid");
51 return;
52 }
53 napi_status status = napi_call_function(env_, thisVar_, ondataPreparedFunc_, 1, argv, &result);
54 if (status != napi_ok) {
55 NAPI_ERR_LOG("call js function failed");
56 NapiError::ThrowError(env_, JS_INNER_FAIL, "calling onDataPrepared failed");
57 }
58 }
59 } // namespace Media
60 } // namespace OHOS
61