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_ref dataHandler,ReturnDataType dataType,const std::string & uri,const std::string & destUri,SourceMode sourceMode)26 NapiMediaAssetDataHandler::NapiMediaAssetDataHandler(napi_env env, napi_ref dataHandler, ReturnDataType dataType,
27     const std::string &uri, const std::string &destUri, SourceMode sourceMode)
28 {
29     dataType_ = dataType;
30     env_ = env;
31     requestUri_ = uri;
32     destUri_ = destUri;
33     sourceMode_ = sourceMode;
34     dataHandlerRef_ = dataHandler;
35 }
36 
DeleteNapiReference(napi_env env)37 void NapiMediaAssetDataHandler::DeleteNapiReference(napi_env env)
38 {
39     if (dataHandlerRef_ != nullptr) {
40         if (env != nullptr) {
41             napi_delete_reference(env, dataHandlerRef_);
42         } else {
43             napi_delete_reference(env_, dataHandlerRef_);
44         }
45     }
46 }
47 
GetReturnDataType()48 ReturnDataType NapiMediaAssetDataHandler::GetReturnDataType()
49 {
50     return dataType_;
51 }
52 
GetRequestUri()53 std::string NapiMediaAssetDataHandler::GetRequestUri()
54 {
55     return requestUri_;
56 }
57 
GetDestUri()58 std::string NapiMediaAssetDataHandler::GetDestUri()
59 {
60     return destUri_;
61 }
62 
GetSourceMode()63 SourceMode NapiMediaAssetDataHandler::GetSourceMode()
64 {
65     return sourceMode_;
66 }
67 
SetNotifyMode(NotifyMode notifyMode)68 void NapiMediaAssetDataHandler::SetNotifyMode(NotifyMode notifyMode)
69 {
70     notifyMode_ = notifyMode;
71 }
72 
GetNotifyMode()73 NotifyMode NapiMediaAssetDataHandler::GetNotifyMode()
74 {
75     return notifyMode_;
76 }
77 
JsOnDataPrepared(napi_env env,napi_value arg,napi_value extraInfo)78 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value arg, napi_value extraInfo)
79 {
80     if (dataHandlerRef_ == nullptr) {
81         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
82         return;
83     }
84 
85     napi_value callback;
86     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
87     if (status != napi_ok) {
88         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
89             static_cast<int>(status));
90         return;
91     }
92 
93     napi_value jsOnDataPrepared;
94     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
95     if (status != napi_ok) {
96         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
97             static_cast<int>(status));
98         return;
99     }
100 
101     constexpr size_t maxArgs = 2;
102     napi_value argv[maxArgs];
103     size_t argc = 0;
104     if (extraInfo != nullptr) {
105         argv[PARAM0] = arg;
106         argv[PARAM1] = extraInfo;
107         argc = ARGS_TWO;
108     } else {
109         argv[PARAM0] = arg;
110         argc = ARGS_ONE;
111     }
112     napi_value promise;
113     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
114     if (status != napi_ok) {
115         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
116         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
117     }
118 }
119 
JsOnDataPrepared(napi_env env,napi_value pictures,napi_value arg,napi_value extraInfo)120 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value pictures, napi_value arg,
121     napi_value extraInfo)
122 {
123     if (dataHandlerRef_ == nullptr) {
124         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
125         return;
126     }
127 
128     napi_value callback;
129     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
130     if (status != napi_ok) {
131         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
132             static_cast<int>(status));
133         return;
134     }
135 
136     napi_value jsOnDataPrepared;
137     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
138     if (status != napi_ok) {
139         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
140             static_cast<int>(status));
141         return;
142     }
143 
144     constexpr size_t maxArgs = 3;
145     napi_value argv[maxArgs];
146     size_t argc = 0;
147     if (extraInfo != nullptr) {
148         argv[PARAM0] = pictures;
149         argv[PARAM1] = arg;
150         argv[PARAM2] = extraInfo;
151         argc = ARGS_THREE;
152     } else {
153         argv[PARAM0] = pictures;
154         argv[PARAM1] = arg;
155         argc = ARGS_TWO;
156     }
157     napi_value promise;
158     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
159     if (status != napi_ok) {
160         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
161         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
162     }
163 }
164 } // namespace Media
165 } // namespace OHOS
166