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 "short_term_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 SHORT_TERM_CODE_SUCCESS = 0;
29 static const string SHORT_TERM_DES_FILE_URIS = "desFileUris";
30 static const string RESULT_PARAM = "result";
31 static const string DATA_PARAM = "data";
32 }
33 
ShortTermCallback(napi_env env,Ace::UIContent * uiContent)34 ShortTermCallback::ShortTermCallback(napi_env env, Ace::UIContent *uiContent)
35 {
36     this->env_ = env;
37     this->uiContent = uiContent;
38 }
39 
OnRelease(int32_t releaseCode)40 void ShortTermCallback::OnRelease(int32_t releaseCode)
41 {
42     CloseModalUIExtension();
43 }
44 
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & want)45 void ShortTermCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want &want)
46 {
47     vector<string> desFileUris;
48     if (resultCode == SHORT_TERM_CODE_SUCCESS) {
49         this->resultCode_ = resultCode;
50         if (!want.HasParameter(SHORT_TERM_DES_FILE_URIS)) {
51             NAPI_ERR_LOG("Can't get string array from want.");
52             CHECK_ARGS_RET_VOID(this->env_, true, JS_INNER_FAIL);
53             return;
54         }
55         desFileUris = want.GetStringArrayParam(SHORT_TERM_DES_FILE_URIS);
56     } else {
57         this->resultCode_ = JS_INNER_FAIL;
58     }
59     size_t len  = desFileUris.size();
60     if (len > 0) {
61         SendMessageBack(desFileUris[0]);
62     } else {
63         string desFileUri;
64         SendMessageBack(desFileUri);
65     }
66 }
67 
OnError(int32_t code,const std::string & name,const std::string & message)68 void ShortTermCallback::OnError(int32_t code, const std::string &name, const std::string &message)
69 {
70     NAPI_INFO_LOG("Code is %{public}d, name is %{public}s, message is %{public}s.", code, name.c_str(),
71         message.c_str());
72     this->resultCode_ = JS_INNER_FAIL;
73     string desFileUri;
74     SendMessageBack(desFileUri);
75 }
76 
OnReceive(const OHOS::AAFwk::WantParams & request)77 void ShortTermCallback::OnReceive(const OHOS::AAFwk::WantParams &request)
78 {
79     NAPI_INFO_LOG("OnReceive enter.");
80 }
81 
SetSessionId(int32_t sessionId)82 void ShortTermCallback::SetSessionId(int32_t sessionId)
83 {
84     this->sessionId_ = sessionId;
85 }
86 
SetFunc(napi_value func)87 void ShortTermCallback::SetFunc(napi_value func)
88 {
89     napi_valuetype valueType = napi_undefined;
90     napi_typeof(this->env_, func, &valueType);
91     if (valueType == napi_function) {
92         napi_create_reference(this->env_, func, ARGS_ONE, &this->callbackRef);
93     }
94 }
95 
SendMessageBack(const string & desFileUri)96 void ShortTermCallback::SendMessageBack(const string &desFileUri)
97 {
98     CloseModalUIExtension();
99 
100     napi_value undefined;
101     CHECK_ARGS_RET_VOID(this->env_, napi_get_undefined(this->env_, &undefined), JS_INNER_FAIL);
102 
103     napi_value results[ARGS_ONE] = {nullptr};
104     CHECK_ARGS_RET_VOID(this->env_, napi_create_object(this->env_, &results[PARAM0]), JS_INNER_FAIL);
105 
106     napi_value result = nullptr;
107     CHECK_ARGS_RET_VOID(this->env_, napi_create_int32(this->env_, this->resultCode_, &result),
108         JS_INNER_FAIL);
109     CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], RESULT_PARAM.c_str(), result),
110         JS_INNER_FAIL);
111 
112     napi_value data = nullptr;
113     CHECK_ARGS_RET_VOID(this->env_, napi_create_string_utf8(this->env_, desFileUri.c_str(), NAPI_AUTO_LENGTH, &data),
114         JS_INNER_FAIL);
115     CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], DATA_PARAM.c_str(), data),
116         JS_INNER_FAIL);
117 
118     napi_value callback = nullptr;
119     CHECK_ARGS_RET_VOID(this->env_, napi_get_reference_value(this->env_, this->callbackRef, &callback), JS_INNER_FAIL);
120 
121     napi_value returnVal;
122     CHECK_ARGS_RET_VOID(this->env_, napi_call_function(this->env_, undefined, callback, ARGS_ONE, results, &returnVal),
123         JS_INNER_FAIL);
124 }
125 
CloseModalUIExtension()126 void ShortTermCallback::CloseModalUIExtension()
127 {
128     if (this->uiContent != nullptr) {
129         uiContent->CloseModalUIExtension(this->sessionId_);
130     }
131 }
132 }
133 }