176fd607bSopenharmony_ci/* 276fd607bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 376fd607bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 476fd607bSopenharmony_ci * you may not use this file except in compliance with the License. 576fd607bSopenharmony_ci * You may obtain a copy of the License at 676fd607bSopenharmony_ci * 776fd607bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 876fd607bSopenharmony_ci * 976fd607bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1076fd607bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1176fd607bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1276fd607bSopenharmony_ci * See the License for the specific language governing permissions and 1376fd607bSopenharmony_ci * limitations under the License. 1476fd607bSopenharmony_ci */ 1576fd607bSopenharmony_ci#include "confirm_callback.h" 1676fd607bSopenharmony_ci 1776fd607bSopenharmony_ci#include "media_library_napi.h" 1876fd607bSopenharmony_ci#include "medialibrary_napi_utils.h" 1976fd607bSopenharmony_ci#include "medialibrary_napi_log.h" 2076fd607bSopenharmony_ci#include "media_file_utils.h" 2176fd607bSopenharmony_ci#include "userfile_client.h" 2276fd607bSopenharmony_ci 2376fd607bSopenharmony_cinamespace OHOS { 2476fd607bSopenharmony_cinamespace Media { 2576fd607bSopenharmony_cinamespace { 2676fd607bSopenharmony_cistatic constexpr int32_t CONFIRM_CODE_SUCCESS = 0; 2776fd607bSopenharmony_cistatic constexpr int32_t CONFIRM_CODE_USER_DENY = -1; 2876fd607bSopenharmony_cistatic const std::string CONFIRM_BOX_DES_FILE_URIS = "desFileUris"; 2976fd607bSopenharmony_cistatic const std::string RESULT_PARAM = "result"; 3076fd607bSopenharmony_cistatic const std::string DATA_PARAM = "data"; 3176fd607bSopenharmony_ci} 3276fd607bSopenharmony_ci#ifdef HAS_ACE_ENGINE_PART 3376fd607bSopenharmony_ciConfirmCallback::ConfirmCallback(napi_env env, Ace::UIContent *uiContent) 3476fd607bSopenharmony_ci{ 3576fd607bSopenharmony_ci this->env_ = env; 3676fd607bSopenharmony_ci this->uiContent = uiContent; 3776fd607bSopenharmony_ci} 3876fd607bSopenharmony_ci#else 3976fd607bSopenharmony_ciConfirmCallback::ConfirmCallback(napi_env env) 4076fd607bSopenharmony_ci{ 4176fd607bSopenharmony_ci this->env_ = env; 4276fd607bSopenharmony_ci} 4376fd607bSopenharmony_ci#endif 4476fd607bSopenharmony_ci 4576fd607bSopenharmony_civoid ConfirmCallback::OnRelease(int32_t releaseCode) 4676fd607bSopenharmony_ci{ 4776fd607bSopenharmony_ci NAPI_INFO_LOG("ReleaseCode is %{public}d.", releaseCode); 4876fd607bSopenharmony_ci 4976fd607bSopenharmony_ci CloseModalUIExtension(); 5076fd607bSopenharmony_ci} 5176fd607bSopenharmony_ci 5276fd607bSopenharmony_civoid ConfirmCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want &want) 5376fd607bSopenharmony_ci{ 5476fd607bSopenharmony_ci NAPI_INFO_LOG("ResultCode is %{public}d.", resultCode); 5576fd607bSopenharmony_ci 5676fd607bSopenharmony_ci this->resultCode_ = resultCode; 5776fd607bSopenharmony_ci 5876fd607bSopenharmony_ci std::vector<std::string> desFileUris; 5976fd607bSopenharmony_ci if (resultCode == CONFIRM_CODE_SUCCESS) { 6076fd607bSopenharmony_ci // check if the desFileUris exsit 6176fd607bSopenharmony_ci if (!want.HasParameter(CONFIRM_BOX_DES_FILE_URIS)) { 6276fd607bSopenharmony_ci NAPI_ERR_LOG("Can't get string array from want."); 6376fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, true, JS_INNER_FAIL); 6476fd607bSopenharmony_ci return; 6576fd607bSopenharmony_ci } 6676fd607bSopenharmony_ci 6776fd607bSopenharmony_ci // get desFileUris from want 6876fd607bSopenharmony_ci desFileUris = want.GetStringArrayParam(CONFIRM_BOX_DES_FILE_URIS); 6976fd607bSopenharmony_ci } else if (resultCode == CONFIRM_CODE_USER_DENY) { 7076fd607bSopenharmony_ci this->resultCode_ = CONFIRM_CODE_SUCCESS; // user deny return success with empty uris 7176fd607bSopenharmony_ci } 7276fd607bSopenharmony_ci 7376fd607bSopenharmony_ci SendMessageBack(desFileUris); 7476fd607bSopenharmony_ci} 7576fd607bSopenharmony_ci 7676fd607bSopenharmony_civoid ConfirmCallback::OnError(int32_t code, const std::string &name, const std::string &message) 7776fd607bSopenharmony_ci{ 7876fd607bSopenharmony_ci NAPI_INFO_LOG("Code is %{public}d, name is %{public}s, message is %{public}s.", code, name.c_str(), 7976fd607bSopenharmony_ci message.c_str()); 8076fd607bSopenharmony_ci 8176fd607bSopenharmony_ci this->resultCode_ = JS_INNER_FAIL; 8276fd607bSopenharmony_ci std::vector<std::string> desFileUris; 8376fd607bSopenharmony_ci SendMessageBack(desFileUris); 8476fd607bSopenharmony_ci} 8576fd607bSopenharmony_ci 8676fd607bSopenharmony_civoid ConfirmCallback::OnReceive(const OHOS::AAFwk::WantParams &request) 8776fd607bSopenharmony_ci{ 8876fd607bSopenharmony_ci NAPI_INFO_LOG("Called."); 8976fd607bSopenharmony_ci} 9076fd607bSopenharmony_ci 9176fd607bSopenharmony_civoid ConfirmCallback::SetSessionId(int32_t sessionId) 9276fd607bSopenharmony_ci{ 9376fd607bSopenharmony_ci this->sessionId_ = sessionId; 9476fd607bSopenharmony_ci} 9576fd607bSopenharmony_ci 9676fd607bSopenharmony_civoid ConfirmCallback::SetFunc(napi_value func) 9776fd607bSopenharmony_ci{ 9876fd607bSopenharmony_ci napi_valuetype valueType = napi_undefined; 9976fd607bSopenharmony_ci napi_typeof(this->env_, func, &valueType); 10076fd607bSopenharmony_ci if (valueType == napi_function) { 10176fd607bSopenharmony_ci napi_create_reference(this->env_, func, ARGS_ONE, &this->callbackRef); 10276fd607bSopenharmony_ci } 10376fd607bSopenharmony_ci} 10476fd607bSopenharmony_ci 10576fd607bSopenharmony_cistatic void GenerateStringArrayValue(napi_env &env, const std::vector<std::string> &desFileUris, const size_t len, 10676fd607bSopenharmony_ci napi_value &value) 10776fd607bSopenharmony_ci{ 10876fd607bSopenharmony_ci if (len == 0) { 10976fd607bSopenharmony_ci return; 11076fd607bSopenharmony_ci } 11176fd607bSopenharmony_ci 11276fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(env, napi_create_array_with_length(env, len, &value), JS_INNER_FAIL); // set array length 11376fd607bSopenharmony_ci 11476fd607bSopenharmony_ci for (size_t i = 0; i < len; ++i) { 11576fd607bSopenharmony_ci // create string value and set it as array element 11676fd607bSopenharmony_ci napi_value element = nullptr; 11776fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(env, napi_create_string_utf8(env, desFileUris[i].c_str(), NAPI_AUTO_LENGTH, &element), 11876fd607bSopenharmony_ci JS_INNER_FAIL); 11976fd607bSopenharmony_ci 12076fd607bSopenharmony_ci if ((element == nullptr) || (napi_set_element(env, value, i, element) != napi_ok)) { 12176fd607bSopenharmony_ci NAPI_ERR_LOG("Failed to set element %{public}s to array.", desFileUris[i].c_str()); 12276fd607bSopenharmony_ci break; 12376fd607bSopenharmony_ci } 12476fd607bSopenharmony_ci } 12576fd607bSopenharmony_ci} 12676fd607bSopenharmony_ci 12776fd607bSopenharmony_civoid ConfirmCallback::SendMessageBack(const std::vector<std::string> &desFileUris) 12876fd607bSopenharmony_ci{ 12976fd607bSopenharmony_ci CloseModalUIExtension(); 13076fd607bSopenharmony_ci 13176fd607bSopenharmony_ci napi_value undefined = nullptr; 13276fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_get_undefined(this->env_, &undefined), JS_INNER_FAIL); 13376fd607bSopenharmony_ci 13476fd607bSopenharmony_ci napi_value results[ARGS_ONE] = {nullptr}; 13576fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_create_object(this->env_, &results[PARAM0]), JS_INNER_FAIL); 13676fd607bSopenharmony_ci 13776fd607bSopenharmony_ci // create int32_t value bind result code as first napi value 13876fd607bSopenharmony_ci napi_value result = nullptr; 13976fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_create_int32(this->env_, this->resultCode_, &result), 14076fd607bSopenharmony_ci JS_INNER_FAIL); 14176fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], RESULT_PARAM.c_str(), result), 14276fd607bSopenharmony_ci JS_INNER_FAIL); 14376fd607bSopenharmony_ci 14476fd607bSopenharmony_ci size_t len = desFileUris.size(); 14576fd607bSopenharmony_ci if (len > 0) { 14676fd607bSopenharmony_ci // transfer desFileUris to a napi value as second param 14776fd607bSopenharmony_ci napi_value data = nullptr; 14876fd607bSopenharmony_ci GenerateStringArrayValue(this->env_, desFileUris, len, data); 14976fd607bSopenharmony_ci 15076fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], DATA_PARAM.c_str(), data), 15176fd607bSopenharmony_ci JS_INNER_FAIL); 15276fd607bSopenharmony_ci } 15376fd607bSopenharmony_ci 15476fd607bSopenharmony_ci napi_value callback = nullptr; 15576fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_get_reference_value(this->env_, this->callbackRef, &callback), JS_INNER_FAIL); 15676fd607bSopenharmony_ci 15776fd607bSopenharmony_ci napi_value returnVal; 15876fd607bSopenharmony_ci CHECK_ARGS_RET_VOID(this->env_, napi_call_function(this->env_, undefined, callback, ARGS_ONE, results, &returnVal), 15976fd607bSopenharmony_ci JS_INNER_FAIL); 16076fd607bSopenharmony_ci} 16176fd607bSopenharmony_ci 16276fd607bSopenharmony_civoid ConfirmCallback::CloseModalUIExtension() 16376fd607bSopenharmony_ci{ 16476fd607bSopenharmony_ci NAPI_INFO_LOG("Called."); 16576fd607bSopenharmony_ci 16676fd607bSopenharmony_ci#ifdef HAS_ACE_ENGINE_PART 16776fd607bSopenharmony_ci if (this->uiContent != nullptr) { 16876fd607bSopenharmony_ci uiContent->CloseModalUIExtension(this->sessionId_); 16976fd607bSopenharmony_ci } 17076fd607bSopenharmony_ci#endif 17176fd607bSopenharmony_ci} 17276fd607bSopenharmony_ci} 17376fd607bSopenharmony_ci}