1 /*
2 * Copyright (c) 2021-2022 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 "adapter/ohos/entrance/pa_engine/backend_delegate_impl.h"
16
17 #include <atomic>
18 #include <string>
19
20 #include "ability.h"
21 #include "ability_info.h"
22
23 #include "base/log/ace_trace.h"
24 #include "base/log/event_report.h"
25 #include "base/utils/utils.h"
26 #include "core/common/ace_application_info.h"
27 #include "core/common/platform_bridge.h"
28 #include "core/components/dialog/dialog_component.h"
29 #include "core/components/toast/toast_component.h"
30 #include "frameworks/bridge/common/manifest/manifest_parser.h"
31 #include "frameworks/bridge/common/utils/utils.h"
32
33 namespace OHOS::Ace {
34 namespace {
35 const char PA_MANIFEST_JSON[] = "manifest.json";
36 } // namespace
37
BackendDelegateImpl(const BackendDelegateImplBuilder & builder)38 BackendDelegateImpl::BackendDelegateImpl(const BackendDelegateImplBuilder& builder)
39 : loadJs_(builder.loadCallback), dispatcherCallback_(builder.transferCallback),
40 asyncEvent_(builder.asyncEventCallback), syncEvent_(builder.syncEventCallback), insert_(builder.insertCallback),
41 call_(builder.callCallback), query_(builder.queryCallback), update_(builder.updateCallback),
42 delete_(builder.deleteCallback), batchInsert_(builder.batchInsertCallback), getType_(builder.getTypeCallback),
43 getFileTypes_(builder.getFileTypesCallback), openFile_(builder.openFileCallback),
44 openRawFile_(builder.openRawFileCallback), normalizeUri_(builder.normalizeUriCallback),
45 denormalizeUri_(builder.denormalizeUriCallback), destroyApplication_(builder.destroyApplicationCallback),
46 commandApplication_(builder.commandApplicationCallback), connectCallback_(builder.connectCallback),
47 disConnectCallback_(builder.disConnectCallback), createCallback_(builder.createFormCallback),
48 deleteCallback_(builder.deleteFormCallback), triggerEventCallback_(builder.triggerEventCallback),
49 updateCallback_(builder.updateFormCallback), castTemptoNormalCallback_(builder.castTemptoNormalCallback),
50 visibilityChangedCallback_(builder.visibilityChangedCallback),
51 acquireStateCallback_(builder.acquireStateCallback), commandCallback_(builder.commandCallback),
52 shareFormCallback_(builder.shareFormCallback), dumpHeapSnapshotCallback_(builder.dumpHeapSnapshotCallback),
53 manifestParser_(AceType::MakeRefPtr<Framework::ManifestParser>()), type_(builder.type),
54 taskExecutor_(builder.taskExecutor)
55 {}
56
ParseManifest()57 void BackendDelegateImpl::ParseManifest()
58 {
59 std::call_once(onceFlag_, [this]() {
60 std::string jsonContent;
61 if (!GetAssetContent(PA_MANIFEST_JSON, jsonContent)) {
62 LOGE("RunPa parse manifest.json failed");
63 EventReport::SendFormException(FormExcepType::RUN_PAGE_ERR);
64 return;
65 }
66 manifestParser_->Parse(jsonContent);
67 });
68 }
69
RunPa(const std::string & url,const OHOS::AAFwk::Want & want)70 void BackendDelegateImpl::RunPa(const std::string& url, const OHOS::AAFwk::Want& want)
71 {
72 ACE_SCOPED_TRACE("BackendDelegateImpl::RunService");
73 LOGD("dDelegateImpl RunService url=%{private}s", url.c_str());
74 ParseManifest();
75 // if mutli pa in one hap should parse manifest get right url
76 LoadPa(url, want);
77 }
78
SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher) const79 void BackendDelegateImpl::SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const
80 {
81 taskExecutor_->PostTask([dispatcherCallback = dispatcherCallback_, dispatcher] { dispatcherCallback(dispatcher); },
82 TaskExecutor::TaskType::JS);
83 }
84
SetCallBackResult(const std::string & callBackId,const std::string & result)85 void BackendDelegateImpl::SetCallBackResult(const std::string& callBackId, const std::string& result)
86 {
87 jsCallBackResult_.try_emplace(Framework::StringToInt(callBackId), result);
88 }
89
PostJsTask(std::function<void ()> && task)90 void BackendDelegateImpl::PostJsTask(std::function<void()>&& task)
91 {
92 taskExecutor_->PostTask(task, TaskExecutor::TaskType::JS);
93 }
94
PostDelayedJsTask(std::function<void ()> && task,uint32_t delayTime)95 void BackendDelegateImpl::PostDelayedJsTask(std::function<void()>&& task, uint32_t delayTime)
96 {
97 taskExecutor_->PostDelayedTask(task, TaskExecutor::TaskType::JS, delayTime);
98 }
99
GetAssetContent(const std::string & url,std::string & content)100 bool BackendDelegateImpl::GetAssetContent(const std::string& url, std::string& content)
101 {
102 return Framework::GetAssetContentImpl(assetManager_, url, content);
103 }
104
GetAssetContent(const std::string & url,std::vector<uint8_t> & content)105 bool BackendDelegateImpl::GetAssetContent(const std::string& url, std::vector<uint8_t>& content)
106 {
107 return Framework::GetAssetContentImpl(assetManager_, url, content);
108 }
109
GetAssetPath(const std::string & url)110 std::string BackendDelegateImpl::GetAssetPath(const std::string& url)
111 {
112 return Framework::GetAssetPathImpl(assetManager_, url);
113 }
114
LoadPa(const std::string & url,const OHOS::AAFwk::Want & want)115 void BackendDelegateImpl::LoadPa(const std::string& url, const OHOS::AAFwk::Want& want)
116 {
117 LOGD("BackendDelegateImpl LoadPa: %{private}s.", url.c_str());
118
119 std::unique_lock<std::mutex> lock(LoadPaMutex_);
120 if (isStagingPageExist_) {
121 if (condition_.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout) {
122 LOGE("BackendDelegateImpl, load page failed, waiting for current page loading finish.");
123 return;
124 }
125 }
126
127 isStagingPageExist_ = true;
128
129 if (GetType() == BackendType::FORM) {
130 taskExecutor_->PostSyncTask(
131 [weak = AceType::WeakClaim(this), url, want] {
132 auto delegate = weak.Upgrade();
133 if (!delegate) {
134 return;
135 }
136 delegate->loadJs_(url, want);
137 },
138 TaskExecutor::TaskType::JS);
139 } else {
140 taskExecutor_->PostTask(
141 [weak = AceType::WeakClaim(this), url, want] {
142 auto delegate = weak.Upgrade();
143 CHECK_NULL_VOID_NOLOG(delegate);
144 delegate->loadJs_(url, want);
145 },
146 TaskExecutor::TaskType::JS);
147 }
148 }
149
TransferJsResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data) const150 void BackendDelegateImpl::TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const
151 {
152 LOGI("BackendDelegateImpl TransferJsResponseData");
153 auto weak = AceType::WeakClaim(AceType::RawPtr(groupJsBridge_));
154 taskExecutor_->PostTask(
155 [callbackId, code, data = std::move(data), weak]() mutable {
156 auto groupJsBridge = weak.Upgrade();
157 if (groupJsBridge) {
158 groupJsBridge->TriggerModuleJsCallback(callbackId, code, std::move(data));
159 }
160 },
161 TaskExecutor::TaskType::JS);
162 }
163
TransferJsPluginGetError(int32_t callbackId,int32_t errorCode,std::string && errorMessage) const164 void BackendDelegateImpl::TransferJsPluginGetError(
165 int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const
166 {
167 LOGI("BackendDelegateImpl TransferJsPluginGetError");
168 auto weak = AceType::WeakClaim(AceType::RawPtr(groupJsBridge_));
169 taskExecutor_->PostTask(
170 [callbackId, errorCode, errorMessage = std::move(errorMessage), weak]() mutable {
171 auto groupJsBridge = weak.Upgrade();
172 if (groupJsBridge) {
173 groupJsBridge->TriggerModulePluginGetErrorCallback(callbackId, errorCode, std::move(errorMessage));
174 }
175 },
176 TaskExecutor::TaskType::JS);
177 }
178
TransferJsEventData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data) const179 void BackendDelegateImpl::TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const
180 {
181 LOGI("BackendDelegateImpl TransferJsEventData");
182 auto weak = AceType::WeakClaim(AceType::RawPtr(groupJsBridge_));
183 taskExecutor_->PostTask(
184 [callbackId, code, data = std::move(data), weak]() mutable {
185 auto groupJsBridge = weak.Upgrade();
186 if (groupJsBridge) {
187 groupJsBridge->TriggerEventJsCallback(callbackId, code, std::move(data));
188 }
189 },
190 TaskExecutor::TaskType::JS);
191 }
192
LoadPluginJsCode(std::string && jsCode) const193 void BackendDelegateImpl::LoadPluginJsCode(std::string&& jsCode) const
194 {
195 LOGI("BackendDelegateImpl LoadPluginJsCode");
196 auto weak = AceType::WeakClaim(AceType::RawPtr(groupJsBridge_));
197 taskExecutor_->PostTask(
198 [jsCode = std::move(jsCode), weak]() mutable {
199 auto groupJsBridge = weak.Upgrade();
200 if (groupJsBridge) {
201 groupJsBridge->LoadPluginJsCode(std::move(jsCode));
202 }
203 },
204 TaskExecutor::TaskType::JS);
205 }
206
LoadPluginJsByteCode(std::vector<uint8_t> && jsCode,std::vector<int32_t> && jsCodeLen) const207 void BackendDelegateImpl::LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const
208 {
209 LOGI("BackendDelegateImpl LoadPluginJsByteCode");
210 auto weak = AceType::WeakClaim(AceType::RawPtr(groupJsBridge_));
211 taskExecutor_->PostTask(
212 [jsCode = std::move(jsCode), jsCodeLen = std::move(jsCodeLen), weak]() mutable {
213 auto groupJsBridge = weak.Upgrade();
214 if (groupJsBridge) {
215 groupJsBridge->LoadPluginJsByteCode(std::move(jsCode), std::move(jsCodeLen));
216 }
217 },
218 TaskExecutor::TaskType::JS);
219 }
220
GetAnimationJsTask()221 SingleTaskExecutor BackendDelegateImpl::GetAnimationJsTask()
222 {
223 return SingleTaskExecutor::Make(taskExecutor_, TaskExecutor::TaskType::JS);
224 }
225
AddTaskObserver(std::function<void ()> && task)226 void BackendDelegateImpl::AddTaskObserver(std::function<void()>&& task)
227 {
228 taskExecutor_->AddTaskObserver(std::move(task));
229 }
230
RemoveTaskObserver()231 void BackendDelegateImpl::RemoveTaskObserver()
232 {
233 taskExecutor_->RemoveTaskObserver();
234 }
235
FireAsyncEvent(const std::string & eventId,const std::string & param,const std::string & jsonArgs)236 void BackendDelegateImpl::FireAsyncEvent(
237 const std::string& eventId, const std::string& param, const std::string& jsonArgs)
238 {
239 LOGD("FireAsyncEvent eventId: %{public}s", eventId.c_str());
240 std::string args = param;
241 args.append(",null").append(",null"); // callback and dom changes
242 if (!jsonArgs.empty()) {
243 args.append(",").append(jsonArgs); // method args
244 }
245 taskExecutor_->PostTask(
246 [weak = AceType::WeakClaim(this), eventId, args = std::move(args)] {
247 auto delegate = weak.Upgrade();
248 if (delegate) {
249 delegate->asyncEvent_(eventId, args);
250 }
251 },
252 TaskExecutor::TaskType::JS);
253 }
254
FireSyncEvent(const std::string & eventId,const std::string & param,const std::string & jsonArgs)255 bool BackendDelegateImpl::FireSyncEvent(
256 const std::string& eventId, const std::string& param, const std::string& jsonArgs)
257 {
258 std::string resultStr;
259 FireSyncEvent(eventId, param, jsonArgs, resultStr);
260 return (resultStr == "true");
261 }
262
FireSyncEvent(const std::string & eventId,const std::string & param,const std::string & jsonArgs,std::string & result)263 void BackendDelegateImpl::FireSyncEvent(
264 const std::string& eventId, const std::string& param, const std::string& jsonArgs, std::string& result)
265 {
266 int32_t callbackId = callbackCnt_++;
267 std::string args = param;
268 args.append("{\"_callbackId\":\"").append(std::to_string(callbackId)).append("\"}").append(",null");
269 if (!jsonArgs.empty()) {
270 args.append(",").append(jsonArgs); // method args
271 }
272 taskExecutor_->PostSyncTask(
273 [weak = AceType::WeakClaim(this), eventId, args = std::move(args)] {
274 auto delegate = weak.Upgrade();
275 if (delegate) {
276 delegate->syncEvent_(eventId, args);
277 }
278 },
279 TaskExecutor::TaskType::JS);
280
281 result = jsCallBackResult_[callbackId];
282 LOGD("FireSyncEvent eventId: %{public}s, callbackId: %{public}d", eventId.c_str(), callbackId);
283 jsCallBackResult_.erase(callbackId);
284 }
285
OnApplicationDestroy(const std::string & packageName)286 void BackendDelegateImpl::OnApplicationDestroy(const std::string& packageName)
287 {
288 taskExecutor_->PostSyncTask(
289 [destroyApplication = destroyApplication_, packageName] { destroyApplication(packageName); },
290 TaskExecutor::TaskType::JS);
291 }
292
OnApplicationCommand(const std::string & intent,int startId)293 void BackendDelegateImpl::OnApplicationCommand(const std::string& intent, int startId)
294 {
295 taskExecutor_->PostTask(
296 [weak = AceType::WeakClaim(this), intent, startId] {
297 auto delegate = weak.Upgrade();
298 CHECK_NULL_VOID_NOLOG(delegate);
299 delegate->commandApplication_(intent, startId);
300 },
301 TaskExecutor::TaskType::JS);
302 }
303
MethodChannel(const std::string & methodName,std::string & jsonStr)304 void BackendDelegateImpl::MethodChannel(const std::string& methodName, std::string& jsonStr)
305 {
306 std::string resultStr;
307 FireSyncEvent("_root", std::string("\"").append(methodName).append("\","), jsonStr, resultStr);
308 jsonStr.assign(resultStr);
309 }
310
Insert(const Uri & uri,const OHOS::NativeRdb::ValuesBucket & value)311 int32_t BackendDelegateImpl::Insert(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value)
312 {
313 int32_t ret = 0;
314 CallingInfo callingInfo;
315 NAPI_RemoteObject_getCallingInfo(callingInfo);
316 LOGD("BackendDelegateImpl Insert get callingInfo is %{public}u", callingInfo.callingTokenId);
317 taskExecutor_->PostSyncTask(
318 [insert = insert_, &ret, uri, value, callingInfo] { ret = insert(uri, value, callingInfo); },
319 TaskExecutor::TaskType::JS);
320 return ret;
321 }
322
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)323 std::shared_ptr<AppExecFwk::PacMap> BackendDelegateImpl::Call(
324 const Uri& uri, const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap)
325 {
326 std::shared_ptr<AppExecFwk::PacMap> ret = nullptr;
327 CallingInfo callingInfo;
328 NAPI_RemoteObject_getCallingInfo(callingInfo);
329 LOGD("BackendDelegateImpl Call get callingInfo is %{public}u", callingInfo.callingTokenId);
330 taskExecutor_->PostSyncTask(
331 [call = call_, &ret, method, arg, pacMap, callingInfo] { ret = call(method, arg, pacMap, callingInfo); },
332 TaskExecutor::TaskType::JS);
333 return ret;
334 }
335
Query(const Uri & uri,const std::vector<std::string> & columns,const OHOS::NativeRdb::DataAbilityPredicates & predicates)336 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> BackendDelegateImpl::Query(
337 const Uri& uri, const std::vector<std::string>& columns, const OHOS::NativeRdb::DataAbilityPredicates& predicates)
338 {
339 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> ret;
340 CallingInfo callingInfo;
341 NAPI_RemoteObject_getCallingInfo(callingInfo);
342 LOGD("BackendDelegateImpl Query get callingInfo is %{public}u", callingInfo.callingTokenId);
343 taskExecutor_->PostSyncTask([query = query_, &ret, uri, columns, predicates,
344 callingInfo] { ret = query(uri, columns, predicates, callingInfo); },
345 TaskExecutor::TaskType::JS);
346 return ret;
347 }
348
Update(const Uri & uri,const OHOS::NativeRdb::ValuesBucket & value,const OHOS::NativeRdb::DataAbilityPredicates & predicates)349 int32_t BackendDelegateImpl::Update(const Uri& uri, const OHOS::NativeRdb::ValuesBucket& value,
350 const OHOS::NativeRdb::DataAbilityPredicates& predicates)
351 {
352 int32_t ret = 0;
353 CallingInfo callingInfo;
354 NAPI_RemoteObject_getCallingInfo(callingInfo);
355 LOGD("BackendDelegateImpl Update get callingInfo is %{public}u", callingInfo.callingTokenId);
356 taskExecutor_->PostSyncTask([update = update_, &ret, uri, value, predicates,
357 callingInfo] { ret = update(uri, value, predicates, callingInfo); },
358 TaskExecutor::TaskType::JS);
359 return ret;
360 }
361
Delete(const Uri & uri,const OHOS::NativeRdb::DataAbilityPredicates & predicates)362 int32_t BackendDelegateImpl::Delete(const Uri& uri, const OHOS::NativeRdb::DataAbilityPredicates& predicates)
363 {
364 int32_t ret = 0;
365 CallingInfo callingInfo;
366 NAPI_RemoteObject_getCallingInfo(callingInfo);
367 LOGD("BackendDelegateImpl Delete get callingInfo is %{public}u", callingInfo.callingTokenId);
368 taskExecutor_->PostSyncTask([deleteCallback = delete_, &ret, uri, predicates,
369 callingInfo] { ret = deleteCallback(uri, predicates, callingInfo); },
370 TaskExecutor::TaskType::JS);
371 return ret;
372 }
373
BatchInsert(const Uri & uri,const std::vector<OHOS::NativeRdb::ValuesBucket> & values)374 int32_t BackendDelegateImpl::BatchInsert(const Uri& uri, const std::vector<OHOS::NativeRdb::ValuesBucket>& values)
375 {
376 int32_t ret = 0;
377 CallingInfo callingInfo;
378 NAPI_RemoteObject_getCallingInfo(callingInfo);
379 LOGD("BackendDelegateImpl BatchInsert get callingInfo is %{public}u", callingInfo.callingTokenId);
380 taskExecutor_->PostSyncTask(
381 [batchInsert = batchInsert_, &ret, uri, values, callingInfo] { ret = batchInsert(uri, values, callingInfo); },
382 TaskExecutor::TaskType::JS);
383 return ret;
384 }
385
GetType(const Uri & uri)386 std::string BackendDelegateImpl::GetType(const Uri& uri)
387 {
388 std::string ret;
389 CallingInfo callingInfo;
390 NAPI_RemoteObject_getCallingInfo(callingInfo);
391 LOGD("BackendDelegateImpl GetType get callingInfo is %{public}u", callingInfo.callingTokenId);
392 taskExecutor_->PostSyncTask(
393 [getType = getType_, &ret, uri, callingInfo] { ret = getType(uri, callingInfo); }, TaskExecutor::TaskType::JS);
394 return ret;
395 }
396
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)397 std::vector<std::string> BackendDelegateImpl::GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter)
398 {
399 std::vector<std::string> ret;
400 CallingInfo callingInfo;
401 NAPI_RemoteObject_getCallingInfo(callingInfo);
402 LOGD("BackendDelegateImpl GetFileTypes get callingInfo is %{public}u", callingInfo.callingTokenId);
403 taskExecutor_->PostSyncTask([getFileTypes = getFileTypes_, &ret, uri, mimeTypeFilter,
404 callingInfo] { ret = getFileTypes(uri, mimeTypeFilter, callingInfo); },
405 TaskExecutor::TaskType::JS);
406 return ret;
407 }
408
OpenFile(const Uri & uri,const std::string & mode)409 int32_t BackendDelegateImpl::OpenFile(const Uri& uri, const std::string& mode)
410 {
411 int32_t ret = 0;
412 CallingInfo callingInfo;
413 NAPI_RemoteObject_getCallingInfo(callingInfo);
414 LOGD("BackendDelegateImpl OpenFile get callingInfo is %{public}u", callingInfo.callingTokenId);
415 taskExecutor_->PostSyncTask(
416 [openFile = openFile_, &ret, uri, mode, callingInfo] { ret = openFile(uri, mode, callingInfo); },
417 TaskExecutor::TaskType::JS);
418 return ret;
419 }
420
OpenRawFile(const Uri & uri,const std::string & mode)421 int32_t BackendDelegateImpl::OpenRawFile(const Uri& uri, const std::string& mode)
422 {
423 int32_t ret = 0;
424 CallingInfo callingInfo;
425 NAPI_RemoteObject_getCallingInfo(callingInfo);
426 LOGD("BackendDelegateImpl OpenRawFile get callingInfo is %{public}u", callingInfo.callingTokenId);
427 taskExecutor_->PostSyncTask(
428 [openRawFile = openRawFile_, &ret, uri, mode, callingInfo] { ret = openRawFile(uri, mode, callingInfo); },
429 TaskExecutor::TaskType::JS);
430 return ret;
431 }
432
NormalizeUri(const Uri & uri)433 Uri BackendDelegateImpl::NormalizeUri(const Uri& uri)
434 {
435 Uri ret("");
436 CallingInfo callingInfo;
437 NAPI_RemoteObject_getCallingInfo(callingInfo);
438 LOGD("BackendDelegateImpl NormalizeUri get callingInfo is %{public}u", callingInfo.callingTokenId);
439 taskExecutor_->PostSyncTask(
440 [normalizeUri = normalizeUri_, &ret, uri, callingInfo] { ret = normalizeUri(uri, callingInfo); },
441 TaskExecutor::TaskType::JS);
442 return ret;
443 }
444
DenormalizeUri(const Uri & uri)445 Uri BackendDelegateImpl::DenormalizeUri(const Uri& uri)
446 {
447 Uri ret("");
448 CallingInfo callingInfo;
449 NAPI_RemoteObject_getCallingInfo(callingInfo);
450 LOGD("BackendDelegateImpl DenormalizeUri get callingInfo is %{public}u", callingInfo.callingTokenId);
451 taskExecutor_->PostSyncTask(
452 [denormalizeUri = denormalizeUri_, &ret, uri, callingInfo] { ret = denormalizeUri(uri, callingInfo); },
453 TaskExecutor::TaskType::JS);
454 return ret;
455 }
456
OnConnect(const OHOS::AAFwk::Want & want)457 sptr<IRemoteObject> BackendDelegateImpl::OnConnect(const OHOS::AAFwk::Want& want)
458 {
459 sptr<IRemoteObject> ret = nullptr;
460 taskExecutor_->PostSyncTask([connectCallback = connectCallback_, want, &ret]() { ret = connectCallback(want); },
461 TaskExecutor::TaskType::JS);
462 return ret;
463 }
464
OnDisConnect(const OHOS::AAFwk::Want & want)465 void BackendDelegateImpl::OnDisConnect(const OHOS::AAFwk::Want& want)
466 {
467 taskExecutor_->PostTask(
468 [disConnectCallback = disConnectCallback_, want] { disConnectCallback(want); }, TaskExecutor::TaskType::JS);
469 }
470
OnCreate(const OHOS::AAFwk::Want & want)471 void BackendDelegateImpl::OnCreate(const OHOS::AAFwk::Want& want)
472 {
473 taskExecutor_->PostSyncTask(
474 [createCallback = createCallback_, want] { createCallback(want); }, TaskExecutor::TaskType::JS);
475 }
476
OnDelete(const int64_t formId)477 void BackendDelegateImpl::OnDelete(const int64_t formId)
478 {
479 taskExecutor_->PostTask(
480 [deleteCallback = deleteCallback_, formId] { deleteCallback(formId); }, TaskExecutor::TaskType::JS);
481 }
482
OnTriggerEvent(const int64_t formId,const std::string & message)483 void BackendDelegateImpl::OnTriggerEvent(const int64_t formId, const std::string& message)
484 {
485 taskExecutor_->PostTask(
486 [triggerEventCallback = triggerEventCallback_, formId, message] { triggerEventCallback(formId, message); },
487 TaskExecutor::TaskType::JS);
488 }
489
OnUpdate(const int64_t formId)490 void BackendDelegateImpl::OnUpdate(const int64_t formId)
491 {
492 taskExecutor_->PostTask(
493 [updateCallback = updateCallback_, formId] { updateCallback(formId); }, TaskExecutor::TaskType::JS);
494 }
495
OnCastTemptoNormal(const int64_t formId)496 void BackendDelegateImpl::OnCastTemptoNormal(const int64_t formId)
497 {
498 taskExecutor_->PostTask(
499 [castTemptoNormalCallback = castTemptoNormalCallback_, formId] { castTemptoNormalCallback(formId); },
500 TaskExecutor::TaskType::JS);
501 }
502
OnVisibilityChanged(const std::map<int64_t,int32_t> & formEventsMap)503 void BackendDelegateImpl::OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap)
504 {
505 taskExecutor_->PostTask([visibilityChangedCallback = visibilityChangedCallback_,
506 formEventsMap] { visibilityChangedCallback(formEventsMap); },
507 TaskExecutor::TaskType::JS);
508 }
509
OnAcquireFormState(const OHOS::AAFwk::Want & want)510 int32_t BackendDelegateImpl::OnAcquireFormState(const OHOS::AAFwk::Want& want)
511 {
512 auto ret = (int32_t)AppExecFwk::FormState::UNKNOWN;
513 taskExecutor_->PostSyncTask(
514 [acquireStateCallback = acquireStateCallback_, &ret, want] { ret = acquireStateCallback(want); },
515 TaskExecutor::TaskType::JS);
516 return ret;
517 }
518
OnCommand(const OHOS::AAFwk::Want & want,int startId)519 void BackendDelegateImpl::OnCommand(const OHOS::AAFwk::Want& want, int startId)
520 {
521 taskExecutor_->PostTask([commandCallback = commandCallback_, want, startId] { commandCallback(want, startId); },
522 TaskExecutor::TaskType::JS);
523 }
524
DumpHeapSnapshot(bool isPrivate)525 void BackendDelegateImpl::DumpHeapSnapshot(bool isPrivate)
526 {
527 taskExecutor_->PostTask(
528 [dumpHeapSnapshotCallback = dumpHeapSnapshotCallback_, isPrivate] { dumpHeapSnapshotCallback(isPrivate); },
529 TaskExecutor::TaskType::JS);
530 }
531
ParseFileUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)532 bool BackendDelegateImpl::ParseFileUri(
533 const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
534 {
535 if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
536 return false;
537 }
538
539 std::string fileName;
540 std::string filePath;
541 size_t slashPos = fileUri.find_last_of("/");
542 if (slashPos == std::string::npos) {
543 fileName = fileUri;
544 } else {
545 fileName = fileUri.substr(slashPos + 1);
546 filePath = fileUri.substr(0, slashPos);
547 }
548
549 if (Framework::StartWith(filePath, "/")) {
550 filePath = filePath.substr(1);
551 }
552
553 std::vector<std::string> files;
554 assetManager->GetAssetList(filePath, files);
555
556 bool fileExist = false;
557 for (const auto& file : files) {
558 bool startWithSlash = Framework::StartWith(file, "/");
559 if ((startWithSlash && (file.substr(1) == fileName)) || (!startWithSlash && (file == fileName))) {
560 assetsFilePath = filePath + file;
561 fileExist = true;
562 break;
563 }
564 }
565
566 return fileExist;
567 }
568
GetResourceData(const std::string & fileUri,std::vector<uint8_t> & content,std::string & ami)569 bool BackendDelegateImpl::GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content, std::string& ami)
570 {
571 std::string targetFilePath;
572 if (!ParseFileUri(assetManager_, fileUri, targetFilePath)) {
573 LOGE("GetResourceData parse file uri failed.");
574 return false;
575 }
576 ami = assetManager_->GetAssetPath(targetFilePath) + targetFilePath;
577 if (!Framework::GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
578 LOGE("GetResourceData GetAssetContent failed.");
579 return false;
580 }
581
582 return true;
583 }
584
OnShare(int64_t formId,OHOS::AAFwk::WantParams & wantParams)585 bool BackendDelegateImpl::OnShare(int64_t formId, OHOS::AAFwk::WantParams& wantParams)
586 {
587 bool result = false;
588 taskExecutor_->PostSyncTask([shareFormCallback = shareFormCallback_, formId, &wantParams,
589 &result]() { result = shareFormCallback(formId, wantParams); },
590 TaskExecutor::TaskType::JS);
591
592 return result;
593 }
594 } // namespace OHOS::Ace
595