• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "core/components/video/resource/resource.h"
17 
18 namespace OHOS::Ace {
19 
20 const char PARAM_NONE[] = "";
21 const char PARAM_AND[] = "#HWJS-&-#";
22 const char PARAM_VALUE[] = "value";
23 const char PARAM_EQUALS[] = "#HWJS-=-#";
24 const char PARAM_BEGIN[] = "#HWJS-?-#";
25 const char METHOD[] = "method";
26 const char EVENT[] = "event";
27 const char RESULT_FAIL[] = "fail";
28 
Release(const std::function<void (bool)> & onRelease)29 void Resource::Release(const std::function<void(bool)>& onRelease)
30 {
31     if (id_ == INVALID_ID) {
32         return;
33     }
34 
35     auto context = context_.Upgrade();
36     if (!context) {
37         LOGE("fail to release resource due to context is null");
38         return;
39     }
40 
41     auto resRegister = context->GetPlatformResRegister();
42     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
43     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
44     auto releaseTask = [weak = AceType::WeakClaim(this), weakRes, onRelease] {
45         auto resource = weak.Upgrade();
46         auto resRegister = weakRes.Upgrade();
47         if (resource == nullptr || resRegister == nullptr) {
48             LOGE("resource or resRegister is nullptr");
49             return;
50         }
51         bool ret = resRegister->ReleaseResource(resource->hash_);
52         if (ret) {
53             resource->id_ = INVALID_ID;
54             resource->hash_.clear();
55         }
56         if (onRelease) {
57             onRelease(ret);
58         }
59     };
60     if (platformTaskExecutor.IsRunOnCurrentThread()) {
61         releaseTask();
62     } else {
63         platformTaskExecutor.PostTask(releaseTask, "ArkUIVideoResourceRelease");
64     }
65 }
66 
GetDoubleParam(const std::string & param,const std::string & name) const67 double Resource::GetDoubleParam(const std::string& param, const std::string& name) const
68 {
69     size_t len = name.length();
70     size_t pos = param.find(name);
71     double result = 0.0;
72 
73     if (pos != std::string::npos) {
74         std::stringstream ss;
75 
76         ss << param.substr(pos + 1 + len);
77         ss >> result;
78     }
79 
80     return result;
81 }
82 
GetIntParam(const std::string & param,const std::string & name) const83 int32_t Resource::GetIntParam(const std::string& param, const std::string& name) const
84 {
85     size_t len = name.length();
86     size_t pos = param.find(name);
87     int32_t result = 0;
88 
89     if (pos != std::string::npos) {
90         std::stringstream ss;
91 
92         ss << param.substr(pos + 1 + len);
93         ss >> result;
94     }
95 
96     return result;
97 }
98 
GetInt64Param(const std::string & param,const std::string & name) const99 int64_t Resource::GetInt64Param(const std::string& param, const std::string& name) const
100 {
101     size_t len = name.length();
102     size_t pos = param.find(name);
103     int64_t result = 0;
104 
105     if (pos != std::string::npos) {
106         std::stringstream ss;
107         ss << param.substr(pos + 1 + len);
108         ss >> result;
109     }
110 
111     return result;
112 }
113 
GetFloatArrayParam(const std::string & param,const std::string & name,std::vector<float> & matrix) const114 void Resource::GetFloatArrayParam(const std::string& param, const std::string& name, std::vector<float>& matrix) const
115 {
116     size_t len = name.length();
117     size_t pos = param.find(name);
118     matrix.clear();
119     if (pos == std::string::npos) {
120         return;
121     }
122     std::string data = param.substr(pos + 1 + len);
123     pos = data.find("[");
124     if (pos == std::string::npos) {
125         return;
126     }
127     do {
128         std::stringstream ss;
129         ss << data.substr(pos + 1);
130         float tmp = 0;
131         ss >> tmp;
132         matrix.emplace_back(tmp);
133         data = data.substr(pos + 1);
134         pos = data.find(",");
135         if (pos == std::string::npos) {
136             return;
137         }
138     } while (1);
139     return;
140 }
141 
ParseMapFromString(const std::string & param)142 std::map<std::string, std::string> Resource::ParseMapFromString(const std::string& param)
143 {
144     size_t equalsLen = sizeof(PARAM_EQUALS) - 1;
145     size_t andLen = sizeof(PARAM_EQUALS) - 1;
146     size_t totalLen = param.length();
147     size_t index = 0;
148     std::map<std::string, std::string> result;
149     while (index < totalLen) {
150         size_t end = param.find(PARAM_AND, index);
151         if (end == std::string::npos) {
152             end = totalLen;
153         }
154 
155         size_t mid = param.find(PARAM_EQUALS, index);
156         if (mid == std::string::npos) {
157             index = end + andLen;
158             continue;
159         }
160         std::string key = param.substr(index, mid - index);
161         std::string value = param.substr(mid + equalsLen, end - mid - equalsLen);
162         result[key] = value;
163         index = end + andLen;
164     }
165     return result;
166 }
167 
MakeResourceHash() const168 std::string Resource::MakeResourceHash() const
169 {
170     std::stringstream hashCode;
171     hashCode << type_ << "@" << id_;
172 
173     return hashCode.str();
174 }
175 
MakeEventHash(const std::string & event) const176 std::string Resource::MakeEventHash(const std::string& event) const
177 {
178     std::string eventHash = hash_;
179 
180     eventHash += std::string(EVENT);
181     eventHash += std::string(PARAM_EQUALS);
182     eventHash += event;
183     eventHash += std::string(PARAM_BEGIN);
184 
185     return eventHash;
186 }
187 
MakeMethodHash(const std::string & method) const188 std::string Resource::MakeMethodHash(const std::string& method) const
189 {
190     std::string methodHash = hash_;
191 
192     methodHash += std::string(METHOD);
193     methodHash += std::string(PARAM_EQUALS);
194     methodHash += method;
195     methodHash += std::string(PARAM_BEGIN);
196 
197     return methodHash;
198 }
199 
IsResultSuccess(const std::string & result) const200 bool Resource::IsResultSuccess(const std::string& result) const
201 {
202     size_t pos = result.find(RESULT_FAIL);
203 
204     return pos != 0;
205 }
206 
OnError(const std::string & errorCode,const std::string & errorMsg)207 void Resource::OnError(const std::string& errorCode, const std::string& errorMsg)
208 {
209     if (onError_) {
210         onError_(errorCode, errorMsg);
211     }
212 }
213 
CallResRegisterMethod(const std::string & method,const std::string & param,const std::function<void (std::string &)> & callback)214 void Resource::CallResRegisterMethod(
215     const std::string& method, const std::string& param, const std::function<void(std::string&)>& callback)
216 {
217     if (method.empty()) {
218         return;
219     }
220 
221     auto context = context_.Upgrade();
222     if (!context) {
223         LOGE("fail to get context to call res register method");
224         return;
225     }
226 
227     auto resRegister = context->GetPlatformResRegister();
228     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
229     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
230     platformTaskExecutor.PostTask([method, param, weakRes, callback] {
231         auto resRegister = weakRes.Upgrade();
232         if (resRegister == nullptr) {
233             LOGE("resRegister is nullptr");
234             return;
235         }
236         std::string result;
237         resRegister->OnMethodCall(method, param, result);
238         if (callback) {
239             callback(result);
240         }
241     }, "ArkUIVideoCallResRegisterMethod");
242 }
243 
CallSyncResRegisterMethod(const Method & method,const std::string & param,const std::function<void (std::string &)> & callback)244 void Resource::CallSyncResRegisterMethod(
245     const Method& method, const std::string& param, const std::function<void(std::string&)>& callback)
246 {
247     if (method.empty()) {
248         return;
249     }
250 
251     auto context = context_.Upgrade();
252     if (!context) {
253         LOGE("fail to get context to call res register method");
254         return;
255     }
256 
257     auto resRegister = context->GetPlatformResRegister();
258     std::string result;
259     if (!resRegister) {
260         LOGE("fail to get resRegister to call res register method");
261         return;
262     }
263     resRegister->OnMethodCall(method, param, result);
264     if (callback) {
265         callback(result);
266     }
267 }
268 
269 } // namespace OHOS::Ace