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 #include <sstream>
19
20 #include "base/log/log.h"
21
22 namespace OHOS::Ace {
23
24 const char PARAM_NONE[] = "";
25 const char PARAM_AND[] = "#HWJS-&-#";
26 const char PARAM_VALUE[] = "value";
27 const char PARAM_EQUALS[] = "#HWJS-=-#";
28 const char PARAM_BEGIN[] = "#HWJS-?-#";
29 const char METHOD[] = "method";
30 const char EVENT[] = "event";
31 const char RESULT_FAIL[] = "fail";
32
Release(const std::function<void (bool)> & onRelease)33 void Resource::Release(const std::function<void(bool)>& onRelease)
34 {
35 if (id_ == INVALID_ID) {
36 return;
37 }
38
39 auto context = context_.Upgrade();
40 if (!context) {
41 LOGE("fail to release resource due to context is null");
42 return;
43 }
44
45 auto resRegister = context->GetPlatformResRegister();
46 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
47 auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
48 auto releaseTask = [weak = AceType::WeakClaim(this), weakRes, onRelease] {
49 auto resource = weak.Upgrade();
50 auto resRegister = weakRes.Upgrade();
51 if (resource == nullptr || resRegister == nullptr) {
52 LOGE("resource or resRegister is nullptr");
53 return;
54 }
55 bool ret = resRegister->ReleaseResource(resource->hash_);
56 if (ret) {
57 resource->id_ = INVALID_ID;
58 resource->hash_.clear();
59 }
60 if (onRelease) {
61 onRelease(ret);
62 }
63 };
64 if (platformTaskExecutor.IsRunOnCurrentThread()) {
65 releaseTask();
66 } else {
67 platformTaskExecutor.PostTask(releaseTask);
68 }
69 }
70
GetDoubleParam(const std::string & param,const std::string & name) const71 double Resource::GetDoubleParam(const std::string& param, const std::string& name) const
72 {
73 size_t len = name.length();
74 size_t pos = param.find(name);
75 double result = 0.0;
76
77 if (pos != std::string::npos) {
78 std::stringstream ss;
79
80 ss << param.substr(pos + 1 + len);
81 ss >> result;
82 }
83
84 return result;
85 }
86
GetIntParam(const std::string & param,const std::string & name) const87 int32_t Resource::GetIntParam(const std::string& param, const std::string& name) const
88 {
89 size_t len = name.length();
90 size_t pos = param.find(name);
91 int32_t result = 0;
92
93 if (pos != std::string::npos) {
94 std::stringstream ss;
95
96 ss << param.substr(pos + 1 + len);
97 ss >> result;
98 }
99
100 return result;
101 }
102
ParseMapFromString(const std::string & param)103 std::map<std::string, std::string> Resource::ParseMapFromString(const std::string& param)
104 {
105 size_t equalsLen = sizeof(PARAM_EQUALS) - 1;
106 size_t andLen = sizeof(PARAM_EQUALS) - 1;
107 size_t totalLen = param.length();
108 size_t index = 0;
109 std::map<std::string, std::string> result;
110 while (index < totalLen) {
111 size_t end = param.find(PARAM_AND, index);
112 if (end == std::string::npos) {
113 end = totalLen;
114 }
115
116 size_t mid = param.find(PARAM_EQUALS, index);
117 if (mid == std::string::npos) {
118 index = end + andLen;
119 continue;
120 }
121 std::string key = param.substr(index, mid - index);
122 std::string value = param.substr(mid + equalsLen, end - mid - equalsLen);
123 result[key] = value;
124 index = end + andLen;
125 }
126 return result;
127 }
128
MakeResourceHash() const129 std::string Resource::MakeResourceHash() const
130 {
131 std::stringstream hashCode;
132 hashCode << type_ << "@" << id_;
133
134 return hashCode.str();
135 }
136
MakeEventHash(const std::string & event) const137 std::string Resource::MakeEventHash(const std::string& event) const
138 {
139 std::string eventHash = hash_;
140
141 eventHash += std::string(EVENT);
142 eventHash += std::string(PARAM_EQUALS);
143 eventHash += event;
144 eventHash += std::string(PARAM_BEGIN);
145
146 return eventHash;
147 }
148
MakeMethodHash(const std::string & method) const149 std::string Resource::MakeMethodHash(const std::string& method) const
150 {
151 std::string methodHash = hash_;
152
153 methodHash += std::string(METHOD);
154 methodHash += std::string(PARAM_EQUALS);
155 methodHash += method;
156 methodHash += std::string(PARAM_BEGIN);
157
158 return methodHash;
159 }
160
IsResultSuccess(const std::string & result) const161 bool Resource::IsResultSuccess(const std::string& result) const
162 {
163 size_t pos = result.find(RESULT_FAIL);
164
165 return pos != 0;
166 }
167
OnError(const std::string & errorCode,const std::string & errorMsg)168 void Resource::OnError(const std::string& errorCode, const std::string& errorMsg)
169 {
170 if (onError_) {
171 onError_(errorCode, errorMsg);
172 }
173 }
174
CallResRegisterMethod(const std::string & method,const std::string & param,const std::function<void (std::string &)> & callback)175 void Resource::CallResRegisterMethod(
176 const std::string& method, const std::string& param, const std::function<void(std::string&)>& callback)
177 {
178 if (method.empty()) {
179 return;
180 }
181
182 auto context = context_.Upgrade();
183 if (!context) {
184 LOGE("fail to get context to call res register method");
185 return;
186 }
187
188 auto resRegister = context->GetPlatformResRegister();
189 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
190 auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
191 platformTaskExecutor.PostTask([method, param, weakRes, callback] {
192 auto resRegister = weakRes.Upgrade();
193 if (resRegister == nullptr) {
194 LOGE("resRegister is nullptr");
195 return;
196 }
197 std::string result;
198 resRegister->OnMethodCall(method, param, result);
199 if (callback) {
200 callback(result);
201 }
202 });
203 }
204
205 } // namespace OHOS::Ace