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