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_file_utils.h"
18 #include "userfile_client.h"
19
20 namespace OHOS {
21 namespace Media {
22 namespace {
23 static constexpr int32_t CONFIRM_CODE_SUCCESS = 0;
24 static constexpr int32_t CONFIRM_CODE_USER_DENY = -1;
25 static const std::string CONFIRM_BOX_DES_FILE_URIS = "desFileUris";
26 static const std::string RESULT_PARAM = "result";
27 static const std::string DATA_PARAM = "data";
28 }
29 #ifdef HAS_ACE_ENGINE_PART
ConfirmCallback(Ace::UIContent * uiContent,int64_t funcId)30 ConfirmCallback::ConfirmCallback(Ace::UIContent *uiContent, int64_t funcId)
31 {
32 this->uiContent = uiContent;
33 this->funcId = funcId;
34 this->retDataCArrString = {
35 .data = { .head = nullptr, .size = 0 }
36 };
37 }
38 #else
ConfirmCallback(int64_t funcId)39 ConfirmCallback::ConfirmCallback(int64_t funcId)
40 {
41 this->funcId = funcId;
42 this->retDataCArrString = {
43 .data = { .head = nullptr, .size = 0 }
44 };
45 }
46 #endif
OnRelease(int32_t releaseCode)47 void ConfirmCallback::OnRelease(int32_t releaseCode)
48 {
49 LOGI("ReleaseCode is %{public}d.", releaseCode);
50
51 CloseModalUIExtension();
52 }
53
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & want)54 void ConfirmCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want &want)
55 {
56 LOGI("ResultCode is %{public}d.", resultCode);
57
58 this->resultCode_ = resultCode;
59 std::vector<std::string> desFileUris;
60 if (resultCode == CONFIRM_CODE_SUCCESS) {
61 // check if the desFileUris exsit
62 if (!want.HasParameter(CONFIRM_BOX_DES_FILE_URIS)) {
63 LOGE("Can't get string array from want.");
64 this->resultCode_ = JS_INNER_FAIL;
65 SendMessageBack(desFileUris);
66 return;
67 }
68
69 // get desFileUris from want
70 desFileUris = want.GetStringArrayParam(CONFIRM_BOX_DES_FILE_URIS);
71 } else if (resultCode == CONFIRM_CODE_USER_DENY) {
72 this->resultCode_ = CONFIRM_CODE_SUCCESS; // user deny return success with empty uris
73 }
74
75 SendMessageBack(desFileUris);
76 }
77
OnError(int32_t code,const std::string & name,const std::string & message)78 void ConfirmCallback::OnError(int32_t code, const std::string &name, const std::string &message)
79 {
80 LOGI("OnError. Code is %{public}d, name is %{public}s, message is %{public}s.", code, name.c_str(),
81 message.c_str());
82
83 this->resultCode_ = JS_INNER_FAIL;
84 std::vector<std::string> desFileUris;
85 SendMessageBack(desFileUris);
86 }
87
OnReceive(const OHOS::AAFwk::WantParams & request)88 void ConfirmCallback::OnReceive(const OHOS::AAFwk::WantParams &request)
89 {
90 LOGI("ConfirmCallback Called.");
91 }
92
SetSessionId(int32_t sessionId)93 void ConfirmCallback::SetSessionId(int32_t sessionId)
94 {
95 this->sessionId_ = sessionId;
96 }
97
SendMessageBack(const std::vector<std::string> & desFileUris)98 void ConfirmCallback::SendMessageBack(const std::vector<std::string> &desFileUris)
99 {
100 CloseModalUIExtension();
101 this->retDataCArrString.code = this->resultCode_;
102 size_t len = desFileUris.size();
103 if (len > 0) {
104 char** head = static_cast<char **>(malloc(sizeof(char *) * len));
105 if (head == nullptr) {
106 LOGE("SendMessageBack malloc desFileUris failed.");
107 this->resultCode_ = JS_INNER_FAIL;
108 return;
109 }
110 for (size_t i = 0; i < len; ++i) {
111 head[i] = MallocCString(desFileUris[i]);
112 }
113 this->retDataCArrString.data.head = head;
114 this->retDataCArrString.data.size = static_cast<int64_t>(len);
115 }
116 // callback
117 auto func = reinterpret_cast<void(*)(RetDataCArrString)>(this->funcId);
118 auto callbackRef = CJLambda::Create(func);
119 if (callbackRef == nullptr) {
120 LOGE("RegisterNotifyChange on register callback is nullptr.");
121 this->resultCode_ = JS_INNER_FAIL;
122 } else {
123 callbackRef(this->retDataCArrString);
124 }
125 if (this->retDataCArrString.data.size > 0) {
126 for (int64_t i = 0; i < this->retDataCArrString.data.size; i++) {
127 free(this->retDataCArrString.data.head[i]);
128 this->retDataCArrString.data.head[i] = nullptr;
129 }
130 free(this->retDataCArrString.data.head);
131 this->retDataCArrString.data.head = nullptr;
132 this->retDataCArrString.data.size = 0;
133 }
134 }
135
CloseModalUIExtension()136 void ConfirmCallback::CloseModalUIExtension()
137 {
138 LOGI("Called.");
139
140 #ifdef HAS_ACE_ENGINE_PART
141 if (this->uiContent != nullptr) {
142 uiContent->CloseModalUIExtension(this->sessionId_);
143 }
144 #endif
145 }
146 }
147 }