• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "confirm_callback.h"
16 
17 #include "media_library_napi.h"
18 #include "medialibrary_napi_utils.h"
19 #include "medialibrary_napi_log.h"
20 #include "media_file_utils.h"
21 #include "userfile_client.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace {
26 static constexpr int32_t CONFIRM_CODE_SUCCESS = 0;
27 static constexpr int32_t CONFIRM_CODE_USER_DENY = -1;
28 static const std::string CONFIRM_BOX_DES_FILE_URIS = "desFileUris";
29 static const std::string RESULT_PARAM = "result";
30 static const std::string DATA_PARAM = "data";
31 }
32 #ifdef HAS_ACE_ENGINE_PART
ConfirmCallback(napi_env env,Ace::UIContent * uiContent)33 ConfirmCallback::ConfirmCallback(napi_env env, Ace::UIContent *uiContent)
34 {
35     this->env_ = env;
36     this->uiContent = uiContent;
37 }
38 #else
ConfirmCallback(napi_env env)39 ConfirmCallback::ConfirmCallback(napi_env env)
40 {
41     this->env_ = env;
42 }
43 #endif
44 
~ConfirmCallback()45 ConfirmCallback::~ConfirmCallback()
46 {
47     if (callbackRef != nullptr && env_ != nullptr) {
48         napi_delete_reference(env_, callbackRef);
49     }
50 }
51 
OnRelease(int32_t releaseCode)52 void ConfirmCallback::OnRelease(int32_t releaseCode)
53 {
54     NAPI_INFO_LOG("ReleaseCode is %{public}d.", releaseCode);
55 
56     CloseModalUIExtension();
57 }
58 
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & want)59 void ConfirmCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want &want)
60 {
61     NAPI_INFO_LOG("ResultCode is %{public}d.", resultCode);
62 
63     this->resultCode_ = resultCode;
64 
65     std::vector<std::string> desFileUris;
66     if (resultCode == CONFIRM_CODE_SUCCESS) {
67         // check if the desFileUris exsit
68         if (!want.HasParameter(CONFIRM_BOX_DES_FILE_URIS)) {
69             NAPI_ERR_LOG("Can't get string array from want.");
70             CHECK_ARGS_RET_VOID(this->env_, true, JS_INNER_FAIL);
71             return;
72         }
73 
74         // get desFileUris from want
75         desFileUris = want.GetStringArrayParam(CONFIRM_BOX_DES_FILE_URIS);
76     } else if (resultCode == CONFIRM_CODE_USER_DENY) {
77         this->resultCode_ = CONFIRM_CODE_SUCCESS; // user deny return success with empty uris
78     }
79 
80     SendMessageBack(desFileUris);
81 }
82 
OnError(int32_t code,const std::string & name,const std::string & message)83 void ConfirmCallback::OnError(int32_t code, const std::string &name, const std::string &message)
84 {
85     NAPI_INFO_LOG("Code is %{public}d, name is %{public}s, message is %{public}s.", code, name.c_str(),
86         message.c_str());
87 
88     this->resultCode_ = JS_INNER_FAIL;
89     std::vector<std::string> desFileUris;
90     SendMessageBack(desFileUris);
91 }
92 
OnReceive(const OHOS::AAFwk::WantParams & request)93 void ConfirmCallback::OnReceive(const OHOS::AAFwk::WantParams &request)
94 {
95     NAPI_INFO_LOG("Called.");
96 }
97 
SetSessionId(int32_t sessionId)98 void ConfirmCallback::SetSessionId(int32_t sessionId)
99 {
100     this->sessionId_ = sessionId;
101 }
102 
SetFunc(napi_value func)103 void ConfirmCallback::SetFunc(napi_value func)
104 {
105     napi_valuetype valueType = napi_undefined;
106     napi_typeof(this->env_, func, &valueType);
107     if (valueType == napi_function) {
108         napi_create_reference(this->env_, func, ARGS_ONE, &this->callbackRef);
109     }
110 }
111 
GenerateStringArrayValue(napi_env & env,const std::vector<std::string> & desFileUris,const size_t len,napi_value & value)112 static void GenerateStringArrayValue(napi_env &env, const std::vector<std::string> &desFileUris, const size_t len,
113     napi_value &value)
114 {
115     if (len == 0) {
116         return;
117     }
118 
119     CHECK_ARGS_RET_VOID(env, napi_create_array_with_length(env, len, &value), JS_INNER_FAIL); // set array length
120 
121     for (size_t i = 0; i < len; ++i) {
122         // create string value and set it as array element
123         napi_value element = nullptr;
124         CHECK_ARGS_RET_VOID(env, napi_create_string_utf8(env, desFileUris[i].c_str(), NAPI_AUTO_LENGTH, &element),
125             JS_INNER_FAIL);
126 
127         if ((element == nullptr) || (napi_set_element(env, value, i, element) != napi_ok)) {
128             NAPI_ERR_LOG("Failed to set element %{public}s to array.", desFileUris[i].c_str());
129             break;
130         }
131     }
132 }
133 
SendMessageBack(const std::vector<std::string> & desFileUris)134 void ConfirmCallback::SendMessageBack(const std::vector<std::string> &desFileUris)
135 {
136     CloseModalUIExtension();
137 
138     napi_value undefined = nullptr;
139     CHECK_ARGS_RET_VOID(this->env_, napi_get_undefined(this->env_, &undefined), JS_INNER_FAIL);
140 
141     napi_value results[ARGS_ONE] = {nullptr};
142     CHECK_ARGS_RET_VOID(this->env_, napi_create_object(this->env_, &results[PARAM0]), JS_INNER_FAIL);
143 
144     // create int32_t value bind result code as first napi value
145     napi_value result = nullptr;
146     CHECK_ARGS_RET_VOID(this->env_, napi_create_int32(this->env_, this->resultCode_, &result),
147         JS_INNER_FAIL);
148     CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], RESULT_PARAM.c_str(), result),
149         JS_INNER_FAIL);
150 
151     size_t len = desFileUris.size();
152     if (len > 0) {
153         // transfer desFileUris to a napi value as second param
154         napi_value data = nullptr;
155         GenerateStringArrayValue(this->env_, desFileUris, len, data);
156 
157         CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], DATA_PARAM.c_str(), data),
158             JS_INNER_FAIL);
159     }
160 
161     napi_value callback = nullptr;
162     CHECK_ARGS_RET_VOID(this->env_, napi_get_reference_value(this->env_, this->callbackRef, &callback), JS_INNER_FAIL);
163 
164     napi_value returnVal;
165     CHECK_ARGS_RET_VOID(this->env_, napi_call_function(this->env_, undefined, callback, ARGS_ONE, results, &returnVal),
166         JS_INNER_FAIL);
167 }
168 
CloseModalUIExtension()169 void ConfirmCallback::CloseModalUIExtension()
170 {
171     NAPI_INFO_LOG("Called.");
172 
173 #ifdef HAS_ACE_ENGINE_PART
174     if (this->uiContent != nullptr) {
175         uiContent->CloseModalUIExtension(this->sessionId_);
176     }
177 #endif
178 }
179 }
180 }