• 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 
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 
SetRequestId(std::string requestId)78 void NapiMediaAssetDataHandler::SetRequestId(std::string requestId)
79 {
80     requestId_ = requestId;
81 }
82 
GetRequestId()83 std::string NapiMediaAssetDataHandler::GetRequestId()
84 {
85     return requestId_;
86 }
87 
SetCompatibleMode(const CompatibleMode & compatibleMode)88 void NapiMediaAssetDataHandler::SetCompatibleMode(const CompatibleMode &compatibleMode)
89 {
90     compatibleMode_ = compatibleMode;
91 }
92 
GetCompatibleMode()93 CompatibleMode NapiMediaAssetDataHandler::GetCompatibleMode()
94 {
95     return compatibleMode_;
96 }
97 
JsOnDataPrepared(napi_env env,napi_value arg,napi_value extraInfo)98 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value arg, napi_value extraInfo)
99 {
100     if (dataHandlerRef_ == nullptr) {
101         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
102         return;
103     }
104 
105     napi_value callback;
106     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
107     if (status != napi_ok) {
108         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
109             static_cast<int>(status));
110         return;
111     }
112 
113     napi_value jsOnDataPrepared;
114     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
115     if (status != napi_ok) {
116         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
117             static_cast<int>(status));
118         return;
119     }
120 
121     constexpr size_t maxArgs = 2;
122     napi_value argv[maxArgs];
123     size_t argc = 0;
124     if (extraInfo != nullptr) {
125         argv[PARAM0] = arg;
126         argv[PARAM1] = extraInfo;
127         argc = ARGS_TWO;
128     } else {
129         argv[PARAM0] = arg;
130         argc = ARGS_ONE;
131     }
132     napi_value promise;
133     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
134     if (status != napi_ok) {
135         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
136         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
137     }
138 }
139 
JsOnDataPrepared(napi_env env,napi_value pictures,napi_value arg,napi_value extraInfo)140 void NapiMediaAssetDataHandler::JsOnDataPrepared(napi_env env, napi_value pictures, napi_value arg,
141     napi_value extraInfo)
142 {
143     if (dataHandlerRef_ == nullptr) {
144         NAPI_ERR_LOG("JsOnDataPrepared js function is null");
145         return;
146     }
147 
148     napi_value callback;
149     napi_status status = napi_get_reference_value(env, dataHandlerRef_, &callback);
150     if (status != napi_ok) {
151         NAPI_ERR_LOG("JsOnDataPrepared napi_get_reference_value fail, napi status: %{public}d",
152             static_cast<int>(status));
153         return;
154     }
155 
156     napi_value jsOnDataPrepared;
157     status = napi_get_named_property(env, callback, ON_DATA_PREPARED_FUNC, &jsOnDataPrepared);
158     if (status != napi_ok) {
159         NAPI_ERR_LOG("JsOnDataPrepared napi_get_named_property fail, napi status: %{public}d",
160             static_cast<int>(status));
161         return;
162     }
163 
164     constexpr size_t maxArgs = 3;
165     napi_value argv[maxArgs];
166     size_t argc = 0;
167     if (extraInfo != nullptr) {
168         argv[PARAM0] = pictures;
169         argv[PARAM1] = arg;
170         argv[PARAM2] = extraInfo;
171         argc = ARGS_THREE;
172     } else {
173         argv[PARAM0] = pictures;
174         argv[PARAM1] = arg;
175         argc = ARGS_TWO;
176     }
177     napi_value promise;
178     status = napi_call_function(env, nullptr, jsOnDataPrepared, argc, argv, &promise);
179     if (status != napi_ok) {
180         NAPI_ERR_LOG("call js function failed %{public}d", static_cast<int32_t>(status));
181         NapiError::ThrowError(env, JS_INNER_FAIL, "calling onDataPrepared failed");
182     }
183 }
184 } // namespace Media
185 } // namespace OHOS
186