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
16 #include "ability_thread.h"
17
18 #include "ability_context_impl.h"
19 #include "ability_impl.h"
20 #include "ability_impl_factory.h"
21 #include "ability_loader.h"
22 #include "ability_state.h"
23 #include "abs_shared_result_set.h"
24 #include "application_impl.h"
25 #include "bytrace.h"
26 #include "context_deal.h"
27 #include "data_ability_predicates.h"
28 #include "dataobs_mgr_client.h"
29 #include "hilog_wrapper.h"
30 #include "ohos_application.h"
31 #ifdef SUPPORT_GRAPHICS
32 #include "page_ability_impl.h"
33 #endif
34 #include "values_bucket.h"
35
36 namespace OHOS {
37 namespace AppExecFwk {
38 using AbilityManagerClient = OHOS::AAFwk::AbilityManagerClient;
39 using DataObsMgrClient = OHOS::AAFwk::DataObsMgrClient;
40 constexpr static char ABILITY_NAME[] = "Ability";
41 constexpr static char ACE_ABILITY_NAME[] = "AceAbility";
42 constexpr static char ACE_SERVICE_ABILITY_NAME[] = "AceServiceAbility";
43 constexpr static char ACE_DATA_ABILITY_NAME[] = "AceDataAbility";
44 #ifdef SUPPORT_GRAPHICS
45 constexpr static char ACE_FORM_ABILITY_NAME[] = "AceFormAbility";
46 constexpr static char FORM_EXTENSION[] = "FormExtension";
47 #endif
48 constexpr static char BASE_SERVICE_EXTENSION[] = "ServiceExtension";
49 constexpr static char STATIC_SUBSCRIBER_EXTENSION[] = "StaticSubscriberExtension";
50 constexpr static char DATA_SHARE_EXT_ABILITY[] = "DataShareExtAbility";
51 constexpr static char WORK_SCHEDULER_EXTENSION[] = "WorkSchedulerExtension";
52 constexpr static char ACCESSIBILITY_EXTENSION[] = "AccessibilityExtension";
53 constexpr static char WALLPAPER_EXTENSION[] = "WallpaperExtension";
54
55 /**
56 * @brief Default constructor used to create a AbilityThread instance.
57 */
AbilityThread()58 AbilityThread::AbilityThread()
59 : abilityImpl_(nullptr), token_(nullptr), currentAbility_(nullptr), abilityHandler_(nullptr), runner_(nullptr)
60 {}
61
~AbilityThread()62 AbilityThread::~AbilityThread()
63 {
64 DelayedSingleton<AbilityImplFactory>::DestroyInstance();
65 }
66
67 /**
68 * @description: Attach The ability thread to the main process.
69 *
70 * @param abilityRecord Indicates the abilityRecord.
71 *
72 * @return Returns the abilityName.
73 *
74 */
CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> & abilityRecord)75 std::string AbilityThread::CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
76 {
77 std::string abilityName;
78 HILOG_INFO("AbilityThread::CreateAbilityName begin");
79 if (abilityRecord == nullptr) {
80 HILOG_ERROR("AbilityThread::CreateAbilityName failed,abilityRecord is nullptr");
81 return abilityName;
82 }
83
84 std::shared_ptr<AbilityInfo> abilityInfo = abilityRecord->GetAbilityInfo();
85 if (abilityInfo == nullptr) {
86 HILOG_ERROR("AbilityThread::ability attach failed,abilityInfo is nullptr");
87 return abilityName;
88 }
89
90 HILOG_INFO("AbilityThread::ability attach the ability type is %{public}d", abilityInfo->type);
91 HILOG_INFO("AbilityThread::ability attach the ability is Native %{public}d", abilityInfo->isNativeAbility);
92
93 if (abilityInfo->isNativeAbility) {
94 HILOG_INFO("AbilityThread::CreateAbilityName end, create native ability.");
95 return abilityInfo->name;
96 }
97 #ifdef SUPPORT_GRAPHICS
98 if (abilityInfo->type == AbilityType::PAGE) {
99 if (abilityInfo->isStageBasedModel) {
100 abilityName = ABILITY_NAME;
101 } else {
102 abilityName = ACE_ABILITY_NAME;
103 }
104 } else if (abilityInfo->type == AbilityType::SERVICE) {
105 #else
106 if (abilityInfo->type == AbilityType::SERVICE) {
107 #endif
108 #ifdef SUPPORT_GRAPHICS
109 if (abilityInfo->formEnabled == true) {
110 abilityName = ACE_FORM_ABILITY_NAME;
111 } else {
112 #endif
113 abilityName = ACE_SERVICE_ABILITY_NAME;
114 #ifdef SUPPORT_GRAPHICS
115 }
116 #endif
117 } else if (abilityInfo->type == AbilityType::DATA) {
118 abilityName = ACE_DATA_ABILITY_NAME;
119 } else if (abilityInfo->type == AbilityType::EXTENSION) {
120 abilityName = BASE_SERVICE_EXTENSION;
121 #ifdef SUPPORT_GRAPHICS
122 if (abilityInfo->formEnabled || abilityInfo->extensionAbilityType == ExtensionAbilityType::FORM) {
123 abilityName = FORM_EXTENSION;
124 }
125 #endif
126 if (abilityInfo->extensionAbilityType == ExtensionAbilityType::STATICSUBSCRIBER) {
127 abilityName = STATIC_SUBSCRIBER_EXTENSION;
128 }
129 if (abilityInfo->extensionAbilityType == ExtensionAbilityType::DATASHARE) {
130 abilityName = DATA_SHARE_EXT_ABILITY;
131 }
132 if (abilityInfo->extensionAbilityType == ExtensionAbilityType::WORK_SCHEDULER) {
133 abilityName = WORK_SCHEDULER_EXTENSION;
134 }
135 if (abilityInfo->extensionAbilityType == ExtensionAbilityType::ACCESSIBILITY) {
136 abilityName = ACCESSIBILITY_EXTENSION;
137 }
138 if (abilityInfo->extensionAbilityType == ExtensionAbilityType::WALLPAPER) {
139 abilityName = WALLPAPER_EXTENSION;
140 }
141 HILOG_INFO("CreateAbilityName extension type, abilityName:%{public}s", abilityName.c_str());
142 } else {
143 abilityName = abilityInfo->name;
144 }
145
146 HILOG_INFO("AbilityThread::CreateAbilityName end");
147 return abilityName;
148 }
149
150 /**
151 * @description: Create and init contextDeal.
152 *
153 * @param application Indicates the main process.
154 * @param abilityRecord Indicates the abilityRecord.
155 * @param abilityObject Indicates the abilityObject.
156 *
157 * @return Returns the contextDeal.
158 *
159 */
160 std::shared_ptr<ContextDeal> AbilityThread::CreateAndInitContextDeal(std::shared_ptr<OHOSApplication> &application,
161 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<Context> &abilityObject)
162 {
163 HILOG_INFO("AbilityThread::CreateAndInitContextDeal begin");
164 std::shared_ptr<ContextDeal> contextDeal = nullptr;
165 HILOG_INFO("AbilityThread::CreateAndInitContextDeal called");
166 if ((application == nullptr) || (abilityRecord == nullptr) || (abilityObject == nullptr)) {
167 HILOG_ERROR("AbilityThread::ability attach failed,context or record or abilityObject is nullptr");
168 return contextDeal;
169 }
170
171 contextDeal = std::make_shared<ContextDeal>();
172 if (contextDeal == nullptr) {
173 HILOG_ERROR("AbilityThread::ability attach failed,contextDeal is nullptr");
174 return contextDeal;
175 }
176
177 contextDeal->SetAbilityInfo(abilityRecord->GetAbilityInfo());
178 contextDeal->SetApplicationInfo(application->GetApplicationInfo());
179 contextDeal->SetProcessInfo(application->GetProcessInfo());
180
181 std::shared_ptr<Context> tmpContext = application->GetApplicationContext();
182 contextDeal->SetApplicationContext(tmpContext);
183
184 contextDeal->SetBundleCodePath(abilityRecord->GetAbilityInfo()->codePath);
185 contextDeal->SetContext(abilityObject);
186 contextDeal->SetRunner(abilityHandler_->GetEventRunner());
187 HILOG_INFO("AbilityThread::CreateAndInitContextDeal end");
188 return contextDeal;
189 }
190
191 /**
192 * @description: Attach The ability thread to the main process.
193 * @param application Indicates the main process.
194 * @param abilityRecord Indicates the abilityRecord.
195 * @param mainRunner The runner which main_thread holds.
196 */
197 void AbilityThread::Attach(std::shared_ptr<OHOSApplication> &application,
198 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner,
199 const std::shared_ptr<AbilityRuntime::Context> &stageContext)
200 {
201 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
202 HILOG_INFO("AbilityThread::Attach begin");
203 if ((application == nullptr) || (abilityRecord == nullptr) || (mainRunner == nullptr)) {
204 HILOG_ERROR("AbilityThread::ability attach failed,context or record is nullptr");
205 return;
206 }
207
208 // 1.new AbilityHandler
209 std::string abilityName = CreateAbilityName(abilityRecord);
210 abilityHandler_ = std::make_shared<AbilityHandler>(mainRunner, this);
211 if (abilityHandler_ == nullptr) {
212 HILOG_ERROR("AbilityThread::ability attach failed,abilityHandler_ is nullptr");
213 return;
214 }
215
216 // 2.new ability
217 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityName);
218 if (ability == nullptr) {
219 HILOG_ERROR("AbilityThread::ability attach failed,load ability failed");
220 return;
221 }
222 ability->SetCompatibleVersion(abilityRecord->GetCompatibleVersion());
223
224 HILOG_INFO("AbilityThread::new ability success.");
225 currentAbility_.reset(ability);
226 token_ = abilityRecord->GetToken();
227 abilityRecord->SetEventHandler(abilityHandler_);
228 abilityRecord->SetEventRunner(mainRunner);
229 abilityRecord->SetAbilityThread(this);
230 std::shared_ptr<Context> abilityObject = currentAbility_;
231 std::shared_ptr<ContextDeal> contextDeal = CreateAndInitContextDeal(application, abilityRecord, abilityObject);
232 ability->AttachBaseContext(contextDeal);
233
234 // new hap requires
235 ability->AttachAbilityContext(BuildAbilityContext(abilityRecord->GetAbilityInfo(), application, token_,
236 stageContext));
237
238 // 3.new abilityImpl
239 abilityImpl_ =
240 DelayedSingleton<AbilityImplFactory>::GetInstance()->MakeAbilityImplObject(abilityRecord->GetAbilityInfo(),
241 abilityRecord->GetCompatibleVersion());
242 if (abilityImpl_ == nullptr) {
243 HILOG_ERROR("AbilityThread::ability abilityImpl_ == nullptr");
244 return;
245 }
246 HILOG_INFO("AbilityThread::Attach before abilityImpl_->Init");
247 abilityImpl_->Init(application, abilityRecord, currentAbility_, abilityHandler_, token_, contextDeal);
248 HILOG_INFO("AbilityThread::Attach after abilityImpl_->Init");
249 // 4. ability attach : ipc
250 HILOG_INFO("AbilityThread::Attach before AttachAbilityThread");
251 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
252 HILOG_INFO("AbilityThread::Attach after AttachAbilityThread");
253 if (err != ERR_OK) {
254 HILOG_ERROR("AbilityThread:: attach success failed err = %{public}d", err);
255 return;
256 }
257
258 HILOG_INFO("AbilityThread::Attach end");
259 }
260
261 /**
262 * @description: Attach The ability thread to the main process.
263 * @param application Indicates the main process.
264 * @param abilityRecord Indicates the abilityRecord.
265 * @param mainRunner The runner which main_thread holds.
266 */
267 void AbilityThread::AttachExtension(std::shared_ptr<OHOSApplication> &application,
268 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner)
269 {
270 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
271 HILOG_INFO("AbilityThread::AttachExtension begin");
272 if ((application == nullptr) || (abilityRecord == nullptr) || (mainRunner == nullptr)) {
273 HILOG_ERROR("AbilityThread::AttachExtension attach failed,context or record is nullptr");
274 return;
275 }
276
277 // 1.new AbilityHandler
278 std::string abilityName = CreateAbilityName(abilityRecord);
279 abilityHandler_ = std::make_shared<AbilityHandler>(mainRunner, this);
280 if (abilityHandler_ == nullptr) {
281 HILOG_ERROR("AbilityThread::AttachExtension attach failed,abilityHandler_ is nullptr");
282 return;
283 }
284
285 // 2.new ability
286 auto extension = AbilityLoader::GetInstance().GetExtensionByName(abilityName);
287 if (extension == nullptr) {
288 HILOG_ERROR("AbilityThread::AttachExtension attach failed,load ability failed");
289 return;
290 }
291
292 HILOG_INFO("AbilityThread::new extension success.");
293 currentExtension_.reset(extension);
294 token_ = abilityRecord->GetToken();
295 abilityRecord->SetEventHandler(abilityHandler_);
296 abilityRecord->SetEventRunner(mainRunner);
297 abilityRecord->SetAbilityThread(this);
298 extensionImpl_ = std::make_shared<AbilityRuntime::ExtensionImpl>();
299 if (extensionImpl_ == nullptr) {
300 HILOG_ERROR("AbilityThread::extension extensionImpl_ == nullptr");
301 return;
302 }
303 // 3.new init
304 HILOG_INFO("AbilityThread::extensionImpl_ init.");
305 extensionImpl_->Init(application, abilityRecord, currentExtension_, abilityHandler_, token_);
306 // 4.ipc attach init
307 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
308 HILOG_INFO("AbilityThread::AttachExtension after AttachAbilityThread");
309 if (err != ERR_OK) {
310 HILOG_ERROR("AbilityThread:: attach extension success failed err = %{public}d", err);
311 return;
312 }
313 HILOG_INFO("AbilityThread::AttachExtension end");
314 }
315
316 /**
317 * @description: Attach The ability thread to the main process.
318 * @param application Indicates the main process.
319 * @param abilityRecord Indicates the abilityRecord.
320 * @param mainRunner The runner which main_thread holds.
321 */
322 void AbilityThread::AttachExtension(std::shared_ptr<OHOSApplication> &application,
323 const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
324 {
325 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
326 HILOG_INFO("AbilityThread::AttachExtension begin");
327 if ((application == nullptr) || (abilityRecord == nullptr)) {
328 HILOG_ERROR("AbilityThread::AttachExtension failed,context or record is nullptr");
329 return;
330 }
331
332 // 1.new AbilityHandler
333 std::string abilityName = CreateAbilityName(abilityRecord);
334 runner_ = EventRunner::Create(abilityName);
335 if (runner_ == nullptr) {
336 HILOG_ERROR("AbilityThread::AttachExtension failed,create runner failed");
337 return;
338 }
339 abilityHandler_ = std::make_shared<AbilityHandler>(runner_, this);
340 if (abilityHandler_ == nullptr) {
341 HILOG_ERROR("AbilityThread::AttachExtension failed,abilityHandler_ is nullptr");
342 return;
343 }
344
345 // 2.new ability
346 auto extension = AbilityLoader::GetInstance().GetExtensionByName(abilityName);
347 if (extension == nullptr) {
348 HILOG_ERROR("AbilityThread::AttachExtension failed,load extension failed");
349 return;
350 }
351
352 HILOG_INFO("AbilityThread::new extension success.");
353 currentExtension_.reset(extension);
354 token_ = abilityRecord->GetToken();
355 abilityRecord->SetEventHandler(abilityHandler_);
356 abilityRecord->SetEventRunner(runner_);
357 abilityRecord->SetAbilityThread(this);
358 extensionImpl_ = std::make_shared<AbilityRuntime::ExtensionImpl>();
359 if (extensionImpl_ == nullptr) {
360 HILOG_ERROR("AbilityThread::extension extensionImpl_ == nullptr");
361 return;
362 }
363 // 3.new init
364 extensionImpl_->Init(application, abilityRecord, currentExtension_, abilityHandler_, token_);
365 // 4.ipc attach init
366 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
367 HILOG_INFO("AbilityThread::Attach after AttachAbilityThread");
368 if (err != ERR_OK) {
369 HILOG_ERROR("AbilityThread:: AttachExtension failed err = %{public}d", err);
370 return;
371 }
372 HILOG_INFO("AbilityThread::AttachExtension end");
373 }
374
375 /**
376 * @description: Attach The ability thread to the main process.
377 * @param application Indicates the main process.
378 * @param abilityRecord Indicates the abilityRecord.
379 */
380 void AbilityThread::Attach(
381 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
382 const std::shared_ptr<AbilityRuntime::Context> &stageContext)
383 {
384 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
385 HILOG_INFO("AbilityThread::Attach begin");
386 if ((application == nullptr) || (abilityRecord == nullptr)) {
387 HILOG_ERROR("AbilityThread::ability attach failed,context or record is nullptr");
388 return;
389 }
390 // 1.new AbilityHandler
391 std::string abilityName = CreateAbilityName(abilityRecord);
392 runner_ = EventRunner::Create(abilityName);
393 if (runner_ == nullptr) {
394 HILOG_ERROR("AbilityThread::ability attach failed,create runner failed");
395 return;
396 }
397 abilityHandler_ = std::make_shared<AbilityHandler>(runner_, this);
398 if (abilityHandler_ == nullptr) {
399 HILOG_ERROR("AbilityThread::ability attach failed,abilityHandler_ is nullptr");
400 return;
401 }
402
403 // 2.new ability
404 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityName);
405 if (ability == nullptr) {
406 HILOG_ERROR("AbilityThread::ability attach failed,load ability failed");
407 return;
408 }
409 ability->SetCompatibleVersion(abilityRecord->GetCompatibleVersion());
410
411 HILOG_INFO("AbilityThread::new ability success.");
412 currentAbility_.reset(ability);
413 token_ = abilityRecord->GetToken();
414 abilityRecord->SetEventHandler(abilityHandler_);
415 abilityRecord->SetEventRunner(runner_);
416 abilityRecord->SetAbilityThread(this);
417 std::shared_ptr<Context> abilityObject = currentAbility_;
418 std::shared_ptr<ContextDeal> contextDeal = CreateAndInitContextDeal(application, abilityRecord, abilityObject);
419 ability->AttachBaseContext(contextDeal);
420
421 // new hap requires
422 ability->AttachAbilityContext(BuildAbilityContext(abilityRecord->GetAbilityInfo(), application, token_,
423 stageContext));
424
425 // 3.new abilityImpl
426 abilityImpl_ =
427 DelayedSingleton<AbilityImplFactory>::GetInstance()->MakeAbilityImplObject(abilityRecord->GetAbilityInfo(),
428 abilityRecord->GetCompatibleVersion());
429 if (abilityImpl_ == nullptr) {
430 HILOG_ERROR("AbilityThread::ability abilityImpl_ == nullptr");
431 return;
432 }
433 HILOG_INFO("AbilityThread::Attach before abilityImpl_->Init");
434 abilityImpl_->Init(application, abilityRecord, currentAbility_, abilityHandler_, token_, contextDeal);
435 HILOG_INFO("AbilityThread::Attach after abilityImpl_->Init");
436 // 4. ability attach : ipc
437 HILOG_INFO("AbilityThread::Attach before AttachAbilityThread");
438 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
439 HILOG_INFO("AbilityThread::Attach after AttachAbilityThread");
440 if (err != ERR_OK) {
441 HILOG_ERROR("AbilityThread:: attach success failed err = %{public}d", err);
442 return;
443 }
444
445 HILOG_INFO("AbilityThread::Attach end");
446 }
447
448 /**
449 * @description: Handle the life cycle of Ability.
450 * @param want Indicates the structure containing lifecycle information about the ability.
451 * @param lifeCycleStateInfo Indicates the lifeCycleStateInfo.
452 */
453 void AbilityThread::HandleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
454 {
455 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
456 HILOG_INFO("AbilityThread::HandleAbilityTransaction begin");
457 if (abilityImpl_ == nullptr) {
458 HILOG_ERROR("AbilityThread::HandleAbilityTransaction abilityImpl_ == nullptr");
459 return;
460 }
461
462 HILOG_INFO("AbilityThread::HandleAbilityTransaction abilityImpl_->SetCallingContext");
463 abilityImpl_->SetCallingContext(lifeCycleStateInfo.caller.deviceId,
464 lifeCycleStateInfo.caller.bundleName,
465 lifeCycleStateInfo.caller.abilityName);
466 HILOG_INFO("AbilityThread::HandleAbilityTransaction abilityImpl_->HandleAbilityTransaction");
467 abilityImpl_->HandleAbilityTransaction(want, lifeCycleStateInfo);
468 HILOG_INFO("AbilityThread::HandleAbilityTransaction end");
469 }
470
471 /**
472 * @brief Handle the life cycle of Extension.
473 *
474 * @param want Indicates the structure containing lifecycle information about the extension.
475 * @param lifeCycleStateInfo Indicates the lifeCycleStateInfo.
476 */
477 void AbilityThread::HandleExtensionTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
478 {
479 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
480 HILOG_INFO("AbilityThread::HandleExtensionTransaction begin");
481 if (extensionImpl_ == nullptr) {
482 HILOG_ERROR("AbilityThread::HandleExtensionTransaction extensionImpl_ == nullptr");
483 return;
484 }
485 extensionImpl_->HandleExtensionTransaction(want, lifeCycleStateInfo);
486 HILOG_INFO("AbilityThread::HandleAbilityTransaction end");
487 }
488
489 /**
490 * @description: Handle the current connection of Ability.
491 * @param want Indicates the structure containing connection information about the ability.
492 */
493 void AbilityThread::HandleConnectAbility(const Want &want)
494 {
495 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
496 HILOG_INFO("AbilityThread::HandleConnectAbility begin");
497 if (abilityImpl_ == nullptr) {
498 HILOG_ERROR("AbilityThread::HandleConnectAbility abilityImpl_ == nullptr");
499 return;
500 }
501
502 HILOG_INFO("AbilityThread::HandleConnectAbility before abilityImpl_->ConnectAbility");
503 sptr<IRemoteObject> service = abilityImpl_->ConnectAbility(want);
504 HILOG_INFO("AbilityThread::HandleConnectAbility after abilityImpl_->ConnectAbility");
505 HILOG_INFO("AbilityThread::HandleConnectAbility before ScheduleConnectAbilityDone");
506 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleConnectAbilityDone(token_, service);
507 HILOG_INFO("AbilityThread::HandleConnectAbility after ScheduleConnectAbilityDone");
508 if (err != ERR_OK) {
509 HILOG_ERROR("AbilityThread:: HandleConnectAbility failed err = %{public}d", err);
510 }
511 HILOG_INFO("AbilityThread::HandleConnectAbility end");
512 }
513
514 /**
515 * @description: Handle the current disconnection of Ability.
516 */
517 void AbilityThread::HandleDisconnectAbility(const Want &want)
518 {
519 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
520 HILOG_INFO("AbilityThread::HandleDisconnectAbility begin");
521 if (abilityImpl_ == nullptr) {
522 HILOG_ERROR("AbilityThread::HandleDisconnectAbility abilityImpl_ == nullptr");
523 return;
524 }
525
526 HILOG_INFO("AbilityThread::HandleDisconnectAbility before abilityImpl_->DisconnectAbility");
527 abilityImpl_->DisconnectAbility(want);
528 HILOG_INFO("AbilityThread::HandleDisconnectAbility after abilityImpl_->DisconnectAbility");
529 HILOG_INFO("AbilityThread::HandleDisconnectAbility before ScheduleDisconnectAbilityDone");
530 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleDisconnectAbilityDone(token_);
531 HILOG_INFO("AbilityThread::HandleDisconnectAbility after ScheduleDisconnectAbilityDone");
532 if (err != ERR_OK) {
533 HILOG_ERROR("AbilityThread:: HandleDisconnectAbility failed err = %{public}d", err);
534 }
535 HILOG_INFO("AbilityThread::HandleDisconnectAbility end");
536 }
537
538 /**
539 * @brief Handle the current command of Ability.
540 *
541 * @param want The Want object to command to.
542 *
543 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
544 * destroyed, and the value false indicates a normal startup.
545 *
546 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1
547 * every time the ability is started. For example, if the ability has been started for six times, the value of startId
548 * is 6.
549 */
550 void AbilityThread::HandleCommandAbility(const Want &want, bool restart, int startId)
551 {
552 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
553 HILOG_INFO("AbilityThread::HandleCommandAbility begin");
554 if (abilityImpl_ == nullptr) {
555 HILOG_ERROR("AbilityThread::HandleCommandAbility failed. abilityImpl_ == nullptr");
556 return;
557 }
558 abilityImpl_->CommandAbility(want, restart, startId);
559 HILOG_INFO("AbilityThread::HandleCommandAbility before ScheduleCommandAbilityDone");
560 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleCommandAbilityDone(token_);
561 if (err != ERR_OK) {
562 HILOG_ERROR("AbilityThread:: HandleCommandAbility failed err = %{public}d", err);
563 }
564 HILOG_INFO("AbilityThread::HandleCommandAbility end");
565 }
566
567 /**
568 * @brief Handle the current connection of Extension.
569 *
570 * @param want Indicates the structure containing connection information about the extension.
571 */
572 void AbilityThread::HandleConnectExtension(const Want &want)
573 {
574 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
575 HILOG_INFO("AbilityThread::HandleConnectExtension begin");
576 if (extensionImpl_ == nullptr) {
577 HILOG_ERROR("AbilityThread::HandleConnectExtension extensionImpl_ == nullptr");
578 return;
579 }
580 sptr<IRemoteObject> service = extensionImpl_->ConnectExtension(want);
581 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleConnectAbilityDone(token_, service);
582 if (err != ERR_OK) {
583 HILOG_ERROR("AbilityThread::HandleConnectExtension failed err = %{public}d", err);
584 }
585 HILOG_INFO("AbilityThread::HandleConnectExtension end");
586 }
587
588 /**
589 * @brief Handle the current disconnection of Extension.
590 */
591 void AbilityThread::HandleDisconnectExtension(const Want &want)
592 {
593 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
594 HILOG_INFO("AbilityThread::HandleDisconnectExtension begin");
595 if (extensionImpl_ == nullptr) {
596 HILOG_ERROR("AbilityThread::HandleDisconnectExtension extensionImpl_ == nullptr");
597 return;
598 }
599 extensionImpl_->DisconnectExtension(want);
600 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleDisconnectAbilityDone(token_);
601 if (err != ERR_OK) {
602 HILOG_ERROR("AbilityThread:: HandleDisconnectExtension failed err = %{public}d", err);
603 }
604 HILOG_INFO("AbilityThread::HandleDisconnectExtension end");
605 }
606
607 /**
608 * @brief Handle the current command of Extension.
609 *
610 * @param want The Want object to command to.
611 * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
612 * destroyed, and the value false indicates a normal startup.
613 * @param startId Indicates the number of times the Service Extension has been started. The startId is incremented by 1
614 * every time the Extension is started. For example, if the Extension has been started for six times,
615 * the value of startId is 6.
616 */
617 void AbilityThread::HandleCommandExtension(const Want &want, bool restart, int startId)
618 {
619 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
620 HILOG_INFO("AbilityThread::HandleCommandExtension begin");
621 if (extensionImpl_ == nullptr) {
622 HILOG_ERROR("AbilityThread::HandleCommandExtension extensionImpl_ == nullptr");
623 return;
624 }
625 extensionImpl_->CommandExtension(want, restart, startId);
626 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleCommandAbilityDone(token_);
627 if (err != ERR_OK) {
628 HILOG_ERROR("AbilityThread::HandleCommandExtension failed err = %{public}d", err);
629 }
630 HILOG_INFO("AbilityThread::HandleCommandExtension end");
631 }
632
633 /**
634 * @description: Handle the restoreAbility state.
635 * @param state Indicates save ability state used to dispatchRestoreAbilityState.
636 */
637 void AbilityThread::HandleRestoreAbilityState(const PacMap &state)
638 {
639 HILOG_INFO("AbilityThread::HandleRestoreAbilityState begin");
640 if (abilityImpl_ == nullptr) {
641 HILOG_ERROR("AbilityThread::HandleRestoreAbilityState abilityImpl_ == nullptr");
642 return;
643 }
644
645 HILOG_INFO("AbilityThread::HandleRestoreAbilityState before abilityImpl_->DispatchRestoreAbilityState");
646 abilityImpl_->DispatchRestoreAbilityState(state);
647 HILOG_INFO("AbilityThread::HandleRestoreAbilityState after abilityImpl_->DispatchRestoreAbilityState");
648 HILOG_INFO("AbilityThread::HandleRestoreAbilityState end");
649 }
650
651 /**
652 * @description: Provide operating system SaveabilityState information to the observer
653 */
654 void AbilityThread::ScheduleSaveAbilityState()
655 {
656 HILOG_INFO("AbilityThread::ScheduleSaveAbilityState begin");
657 if (abilityImpl_ == nullptr) {
658 HILOG_ERROR("AbilityThread::ScheduleSaveAbilityState abilityImpl_ == nullptr");
659 return;
660 }
661
662 HILOG_INFO("AbilityThread::ScheduleSaveAbilityState before abilityImpl_->DispatchSaveAbilityState");
663
664 abilityImpl_->DispatchSaveAbilityState();
665
666 HILOG_INFO("AbilityThread::ScheduleSaveAbilityState after abilityImpl_->DispatchSaveAbilityState");
667 HILOG_INFO("AbilityThread::ScheduleSaveAbilityState end");
668 }
669
670 /**
671 * @description: Provide operating system RestoreAbilityState information to the observer
672 * @param state Indicates resotre ability state used to dispatchRestoreAbilityState.
673 */
674 void AbilityThread::ScheduleRestoreAbilityState(const PacMap &state)
675 {
676 HILOG_INFO("AbilityThread::ScheduleRestoreAbilityState begin");
677 if (abilityImpl_ == nullptr) {
678 HILOG_ERROR("AbilityThread::ScheduleRestoreAbilityState abilityImpl_ == nullptr");
679 return;
680 }
681 HILOG_INFO("AbilityThread::ScheduleRestoreAbilityState before abilityImpl_->DispatchRestoreAbilityState");
682 abilityImpl_->DispatchRestoreAbilityState(state);
683 HILOG_INFO("AbilityThread::ScheduleRestoreAbilityState after abilityImpl_->DispatchRestoreAbilityState");
684 HILOG_INFO("AbilityThread::ScheduleRestoreAbilityState end");
685 }
686
687 /*
688 * @brief ScheduleUpdateConfiguration, scheduling update configuration.
689 */
690 void AbilityThread::ScheduleUpdateConfiguration(const Configuration &config)
691 {
692 HILOG_INFO("AbilityThread::ScheduleUpdateConfiguration begin");
693 wptr<AbilityThread> weak = this;
694 auto task = [weak, config]() {
695 auto abilityThread = weak.promote();
696 if (abilityThread == nullptr) {
697 HILOG_ERROR("abilityThread is nullptr, ScheduleUpdateConfiguration failed.");
698 return;
699 }
700
701 if (abilityThread->isExtension_) {
702 abilityThread->HandleExtensionUpdateConfiguration(config);
703 } else {
704 abilityThread->HandleUpdateConfiguration(config);
705 }
706 };
707
708 if (abilityHandler_ == nullptr) {
709 HILOG_ERROR("AbilityThread::ScheduleUpdateConfiguration abilityHandler_ is nullptr");
710 return;
711 }
712
713 bool ret = abilityHandler_->PostTask(task);
714 if (!ret) {
715 HILOG_ERROR("AbilityThread::ScheduleUpdateConfiguration PostTask error");
716 }
717 HILOG_INFO("AbilityThread::ScheduleUpdateConfiguration end");
718 }
719
720 /*
721 * @brief Handle the scheduling update configuration.
722 */
723 void AbilityThread::HandleUpdateConfiguration(const Configuration &config)
724 {
725 HILOG_INFO("AbilityThread::HandleUpdateConfiguration begin");
726 if (abilityImpl_ == nullptr) {
727 HILOG_ERROR("AbilityThread::HandleUpdateConfiguration abilityImpl_ is nullptr");
728 return;
729 }
730
731 HILOG_INFO("AbilityThread::HandleUpdateConfiguration before abilityImpl_->ScheduleUpdateConfiguration");
732 abilityImpl_->ScheduleUpdateConfiguration(config);
733 HILOG_INFO("AbilityThread::HandleUpdateConfiguration after abilityImpl_->ScheduleUpdateConfiguration");
734 HILOG_INFO("AbilityThread::HandleUpdateConfiguration end");
735 }
736
737 void AbilityThread::HandleExtensionUpdateConfiguration(const Configuration &config)
738 {
739 HILOG_INFO("AbilityThread::HandleExtensionUpdateConfiguration begin");
740 if (!extensionImpl_) {
741 HILOG_ERROR("AbilityThread::HandleExtensionUpdateConfiguration extensionImpl_ is nullptr");
742 return;
743 }
744
745 HILOG_INFO("AbilityThread::HandleExtensionUpdateConfiguration before extensionImpl_->ScheduleUpdateConfiguration");
746 extensionImpl_->ScheduleUpdateConfiguration(config);
747 HILOG_INFO("AbilityThread::HandleExtensionUpdateConfiguration after extensionImpl_->ScheduleUpdateConfiguration");
748 HILOG_INFO("AbilityThread::HandleExtensionUpdateConfiguration end");
749 }
750
751 /**
752 * @description: Provide operating system AbilityTransaction information to the observer
753 * @param want Indicates the structure containing Transaction information about the ability.
754 * @param lifeCycleStateInfo Indicates the lifecycle state.
755 */
756 void AbilityThread::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
757 {
758 BYTRACE_NAME(BYTRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
759 HILOG_INFO("ScheduleAbilityTransaction begin: targeState = %{public}d, isNewWant = %{public}d",
760 lifeCycleStateInfo.state,
761 lifeCycleStateInfo.isNewWant);
762
763 want.DumpInfo(0);
764
765 if (token_ == nullptr) {
766 HILOG_ERROR("ScheduleAbilityTransaction::failed, token_ nullptr");
767 return;
768 }
769 wptr<AbilityThread> weak = this;
770 auto task = [weak, want, lifeCycleStateInfo]() {
771 auto abilityThread = weak.promote();
772 if (abilityThread == nullptr) {
773 HILOG_ERROR("abilityThread is nullptr, ScheduleAbilityTransaction failed.");
774 return;
775 }
776 if (abilityThread->isExtension_) {
777 abilityThread->HandleExtensionTransaction(want, lifeCycleStateInfo);
778 } else {
779 abilityThread->HandleAbilityTransaction(want, lifeCycleStateInfo);
780 }
781 };
782
783 if (abilityHandler_ == nullptr) {
784 HILOG_ERROR("AbilityThread::ScheduleAbilityTransaction abilityHandler_ == nullptr");
785 return;
786 }
787
788 bool ret = abilityHandler_->PostTask(task);
789 if (!ret) {
790 HILOG_ERROR("AbilityThread::ScheduleAbilityTransaction PostTask error");
791 }
792 HILOG_INFO("ScheduleAbilityTransaction end");
793 }
794
795 /**
796 * @description: Provide operating system ConnectAbility information to the observer
797 * @param want Indicates the structure containing connect information about the ability.
798 */
799 void AbilityThread::ScheduleConnectAbility(const Want &want)
800 {
801 HILOG_INFO("AbilityThread::ScheduleConnectAbility begin, isExtension_:%{public}d", isExtension_);
802 wptr<AbilityThread> weak = this;
803 auto task = [weak, want]() {
804 auto abilityThread = weak.promote();
805 if (abilityThread == nullptr) {
806 HILOG_ERROR("abilityThread is nullptr, ScheduleConnectAbility failed.");
807 return;
808 }
809 if (abilityThread->isExtension_) {
810 abilityThread->HandleConnectExtension(want);
811 } else {
812 abilityThread->HandleConnectAbility(want);
813 }
814 };
815
816 if (abilityHandler_ == nullptr) {
817 HILOG_ERROR("AbilityThread::ScheduleConnectAbility abilityHandler_ == nullptr");
818 return;
819 }
820
821 bool ret = abilityHandler_->PostTask(task);
822 if (!ret) {
823 HILOG_ERROR("AbilityThread::ScheduleConnectAbility PostTask error");
824 }
825 HILOG_INFO("AbilityThread::ScheduleConnectAbility end");
826 }
827
828 /**
829 * @description: Provide operating system ConnectAbility information to the observer
830 * @return None
831 */
832 void AbilityThread::ScheduleDisconnectAbility(const Want &want)
833 {
834 HILOG_INFO("AbilityThread::ScheduleDisconnectAbility begin, isExtension_:%{public}d", isExtension_);
835 wptr<AbilityThread> weak = this;
836 auto task = [weak, want]() {
837 auto abilityThread = weak.promote();
838 if (abilityThread == nullptr) {
839 HILOG_ERROR("abilityThread is nullptr, ScheduleDisconnectAbility failed.");
840 return;
841 }
842 if (abilityThread->isExtension_) {
843 abilityThread->HandleDisconnectExtension(want);
844 } else {
845 abilityThread->HandleDisconnectAbility(want);
846 }
847 };
848
849 if (abilityHandler_ == nullptr) {
850 HILOG_ERROR("AbilityThread::ScheduleDisconnectAbility abilityHandler_ == nullptr");
851 return;
852 }
853
854 bool ret = abilityHandler_->PostTask(task);
855 if (!ret) {
856 HILOG_ERROR("AbilityThread::ScheduleDisconnectAbility PostTask error");
857 }
858 HILOG_INFO("AbilityThread::ScheduleDisconnectAbility end");
859 }
860
861 /**
862 * @description: Provide operating system CommandAbility information to the observer
863 *
864 * @param want The Want object to command to.
865 *
866 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
867 * destroyed, and the value false indicates a normal startup.
868 *
869 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1
870 * every time the ability is started. For example, if the ability has been started for six times, the value of startId
871 * is 6.
872 */
873 void AbilityThread::ScheduleCommandAbility(const Want &want, bool restart, int startId)
874 {
875 HILOG_INFO("AbilityThread::ScheduleCommandAbility begin. startId:%{public}d", startId);
876 wptr<AbilityThread> weak = this;
877 auto task = [weak, want, restart, startId]() {
878 auto abilityThread = weak.promote();
879 if (abilityThread == nullptr) {
880 HILOG_ERROR("abilityThread is nullptr, ScheduleCommandAbility failed.");
881 return;
882 }
883 if (abilityThread->isExtension_) {
884 abilityThread->HandleCommandExtension(want, restart, startId);
885 } else {
886 abilityThread->HandleCommandAbility(want, restart, startId);
887 }
888 };
889
890 if (abilityHandler_ == nullptr) {
891 HILOG_ERROR("AbilityThread::ScheduleCommandAbility abilityHandler_ == nullptr");
892 return;
893 }
894
895 bool ret = abilityHandler_->PostTask(task);
896 if (!ret) {
897 HILOG_ERROR("AbilityThread::ScheduleCommandAbility PostTask error");
898 }
899 HILOG_INFO("AbilityThread::ScheduleCommandAbility end");
900 }
901
902 /**
903 * @brief Send the result code and data to be returned by this Page ability to the caller.
904 * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to
905 * receive the result set in the current method. This method can be called only after the ability has been initialized.
906 *
907 * @param requestCode Indicates the request code for send.
908 * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result code
909 * to identify an error.
910 * @param want Indicates the data returned after the ability is destroyed. You can define the data returned. This
911 * parameter can be null.
912 */
913 void AbilityThread::SendResult(int requestCode, int resultCode, const Want &want)
914 {
915 HILOG_INFO("AbilityThread::SendResult begin");
916 if (abilityImpl_ == nullptr) {
917 HILOG_ERROR("AbilityThread::SendResult abilityImpl_ == nullptr");
918 return;
919 }
920 wptr<AbilityThread> weak = this;
921 auto task = [weak, requestCode, resultCode, want]() {
922 auto abilityThread = weak.promote();
923 if (abilityThread == nullptr || abilityThread->abilityImpl_ == nullptr) {
924 HILOG_ERROR("abilityThread or abilityImpl is nullptr, SendResult failed.");
925 return;
926 }
927 if (requestCode != -1) {
928 HILOG_INFO("AbilityThread::SendResult before abilityImpl_->SendResult");
929 abilityThread->abilityImpl_->SendResult(requestCode, resultCode, want);
930 HILOG_INFO("AbilityThread::SendResult after abilityImpl_->SendResult");
931 }
932 };
933
934 if (abilityHandler_ == nullptr) {
935 HILOG_ERROR("AbilityThread::SendResult abilityHandler_ == nullptr");
936 return;
937 }
938
939 bool ret = abilityHandler_->PostTask(task);
940 if (!ret) {
941 HILOG_ERROR("AbilityThread::SendResult PostTask error");
942 }
943 HILOG_INFO("AbilityThread::SendResult end");
944 }
945
946 /**
947 * @brief Obtains the MIME types of files supported.
948 *
949 * @param uri Indicates the path of the files to obtain.
950 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
951 *
952 * @return Returns the matched MIME types. If there is no match, null is returned.
953 */
954 std::vector<std::string> AbilityThread::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
955 {
956 HILOG_INFO("AbilityThread::GetFileTypes begin");
957 std::vector<std::string> types;
958 if (abilityImpl_ == nullptr) {
959 HILOG_ERROR("AbilityThread::GetFileTypes abilityImpl_ is nullptr");
960 return types;
961 }
962
963 HILOG_INFO("AbilityThread::GetFileTypes before abilityImpl_->GetFileTypes");
964 types = abilityImpl_->GetFileTypes(uri, mimeTypeFilter);
965 HILOG_INFO("AbilityThread::GetFileTypes after abilityImpl_->GetFileTypes");
966 HILOG_INFO("AbilityThread::GetFileTypes end");
967 return types;
968 }
969
970 /**
971 * @brief Opens a file in a specified remote path.
972 *
973 * @param uri Indicates the path of the file to open.
974 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
975 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
976 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
977 * or "rwt" for read and write access that truncates any existing file.
978 *
979 * @return Returns the file descriptor.
980 */
981 int AbilityThread::OpenFile(const Uri &uri, const std::string &mode)
982 {
983 HILOG_INFO("AbilityThread::OpenFile begin");
984 int fd = -1;
985 if (abilityImpl_ == nullptr) {
986 HILOG_ERROR("AbilityThread::OpenFile abilityImpl_ is nullptr");
987 return fd;
988 }
989
990 HILOG_INFO("AbilityThread::OpenFile before abilityImpl_->OpenFile");
991 fd = abilityImpl_->OpenFile(uri, mode);
992 HILOG_INFO("AbilityThread::OpenFile after abilityImpl_->OpenFile");
993 HILOG_INFO("AbilityThread::OpenFile end");
994 return fd;
995 }
996
997 /**
998 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
999 * inside of their .hap.
1000 *
1001 * @param uri Indicates the path of the file to open.
1002 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
1003 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
1004 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
1005 * data, or "rwt" for read and write access that truncates any existing file.
1006 *
1007 * @return Returns the RawFileDescriptor object containing file descriptor.
1008 */
1009 int AbilityThread::OpenRawFile(const Uri &uri, const std::string &mode)
1010 {
1011 HILOG_INFO("AbilityThread::OpenRawFile begin");
1012 int fd = -1;
1013 if (abilityImpl_ == nullptr) {
1014 HILOG_ERROR("AbilityThread::OpenRawFile abilityImpl_ is nullptr");
1015 return fd;
1016 }
1017
1018 HILOG_INFO("AbilityThread::OpenRawFile before abilityImpl_->OpenRawFile");
1019 fd = abilityImpl_->OpenRawFile(uri, mode);
1020 HILOG_INFO("AbilityThread::OpenRawFile after abilityImpl_->OpenRawFile");
1021 HILOG_INFO("AbilityThread::OpenRawFile end");
1022 return fd;
1023 }
1024
1025 /**
1026 * @brief Inserts a single data record into the database.
1027 *
1028 * @param uri Indicates the path of the data to operate.
1029 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
1030 *
1031 * @return Returns the index of the inserted data record.
1032 */
1033 int AbilityThread::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
1034 {
1035 HILOG_INFO("AbilityThread::Insert begin");
1036 int index = -1;
1037 if (abilityImpl_ == nullptr) {
1038 HILOG_ERROR("AbilityThread::Insert abilityImpl_ is nullptr");
1039 return index;
1040 }
1041
1042 HILOG_INFO("AbilityThread::Insert before abilityImpl_->Insert");
1043 index = abilityImpl_->Insert(uri, value);
1044 HILOG_INFO("AbilityThread::Insert after abilityImpl_->Insert");
1045 HILOG_INFO("AbilityThread::Insert end");
1046 return index;
1047 }
1048
1049 std::shared_ptr<AppExecFwk::PacMap> AbilityThread::Call(
1050 const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
1051 {
1052 HILOG_INFO("AbilityThread::Call begin");
1053 if (abilityImpl_ == nullptr) {
1054 HILOG_ERROR("AbilityThread::Insert abilityImpl_ is nullptr");
1055 return nullptr;
1056 }
1057
1058 HILOG_INFO("AbilityThread::Call before abilityImpl_->Call");
1059 std::shared_ptr<AppExecFwk::PacMap> result = abilityImpl_->Call(uri, method, arg, pacMap);
1060 HILOG_INFO("AbilityThread::Call after abilityImpl_->Call");
1061 HILOG_INFO("AbilityThread::Call end");
1062 return result;
1063 }
1064
1065 /**
1066 * @brief Updates data records in the database.
1067 *
1068 * @param uri Indicates the path of data to update.
1069 * @param value Indicates the data to update. This parameter can be null.
1070 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
1071 *
1072 * @return Returns the number of data records updated.
1073 */
1074 int AbilityThread::Update(
1075 const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
1076 {
1077 HILOG_INFO("AbilityThread::Update begin");
1078 int index = -1;
1079 if (abilityImpl_ == nullptr) {
1080 HILOG_ERROR("AbilityThread::Update abilityImpl_ is nullptr");
1081 return index;
1082 }
1083
1084 HILOG_INFO("AbilityThread::Update before abilityImpl_->Update");
1085 index = abilityImpl_->Update(uri, value, predicates);
1086 HILOG_INFO("AbilityThread::Update after abilityImpl_->Update");
1087 HILOG_INFO("AbilityThread::Update end");
1088 return index;
1089 }
1090
1091 /**
1092 * @brief Deletes one or more data records from the database.
1093 *
1094 * @param uri Indicates the path of the data to operate.
1095 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
1096 *
1097 * @return Returns the number of data records deleted.
1098 */
1099 int AbilityThread::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
1100 {
1101 HILOG_INFO("AbilityThread::Delete begin");
1102 int index = -1;
1103 if (abilityImpl_ == nullptr) {
1104 HILOG_ERROR("AbilityThread::Delete abilityImpl_ is nullptr");
1105 return index;
1106 }
1107 HILOG_INFO("AbilityThread::Delete before abilityImpl_->Delete");
1108 index = abilityImpl_->Delete(uri, predicates);
1109 HILOG_INFO("AbilityThread::Delete after abilityImpl_->Delete");
1110 HILOG_INFO("AbilityThread::Delete end");
1111 return index;
1112 }
1113
1114 /**
1115 * @brief Deletes one or more data records from the database.
1116 *
1117 * @param uri Indicates the path of data to query.
1118 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
1119 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
1120 *
1121 * @return Returns the query result.
1122 */
1123 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilityThread::Query(
1124 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
1125 {
1126 HILOG_INFO("AbilityThread::Query begin");
1127 std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
1128 if (abilityImpl_ == nullptr) {
1129 HILOG_ERROR("AbilityThread::Query abilityImpl_ is nullptr");
1130 return resultSet;
1131 }
1132
1133 HILOG_INFO("AbilityThread::Query before abilityImpl_->Query");
1134 resultSet = abilityImpl_->Query(uri, columns, predicates);
1135 HILOG_INFO("AbilityThread::Query after abilityImpl_->Query");
1136 HILOG_INFO("AbilityThread::Query end");
1137 return resultSet;
1138 }
1139
1140 /**
1141 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
1142 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
1143 *
1144 * @param uri Indicates the URI of the data.
1145 *
1146 * @return Returns the MIME type that matches the data specified by uri.
1147 */
1148 std::string AbilityThread::GetType(const Uri &uri)
1149 {
1150 HILOG_INFO("AbilityThread::GetType begin");
1151 std::string type;
1152 if (abilityImpl_ == nullptr) {
1153 HILOG_ERROR("AbilityThread::GetType abilityImpl_ is nullptr");
1154 return type;
1155 }
1156
1157 HILOG_INFO("AbilityThread::GetType before abilityImpl_->GetType");
1158 type = abilityImpl_->GetType(uri);
1159 HILOG_INFO("AbilityThread::GetType after abilityImpl_->GetType");
1160 HILOG_INFO("AbilityThread::GetType end");
1161 return type;
1162 }
1163
1164 /**
1165 * @brief Reloads data in the database.
1166 *
1167 * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
1168 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
1169 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
1170 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
1171 *
1172 * @return Returns true if the data is successfully reloaded; returns false otherwise.
1173 */
1174 bool AbilityThread::Reload(const Uri &uri, const PacMap &extras)
1175 {
1176 HILOG_INFO("AbilityThread::Reload begin");
1177 bool ret = false;
1178 if (abilityImpl_ == nullptr) {
1179 HILOG_ERROR("AbilityThread::Reload abilityImpl_ is nullptr");
1180 return ret;
1181 }
1182 HILOG_INFO("AbilityThread::Reload before abilityImpl_->Reload");
1183 ret = abilityImpl_->Reload(uri, extras);
1184 HILOG_INFO("AbilityThread::Reload after abilityImpl_->Reload");
1185 HILOG_INFO("AbilityThread::Reload end");
1186 return ret;
1187 }
1188
1189 /**
1190 * @brief Inserts multiple data records into the database.
1191 *
1192 * @param uri Indicates the path of the data to operate.
1193 * @param values Indicates the data records to insert.
1194 *
1195 * @return Returns the number of data records inserted.
1196 */
1197 int AbilityThread::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
1198 {
1199 HILOG_INFO("AbilityThread::BatchInsert begin");
1200 int ret = -1;
1201 if (abilityImpl_ == nullptr) {
1202 HILOG_ERROR("AbilityThread::BatchInsert abilityImpl_ is nullptr");
1203 return ret;
1204 }
1205
1206 HILOG_INFO("AbilityThread::BatchInsert before abilityImpl_->BatchInsert");
1207 ret = abilityImpl_->BatchInsert(uri, values);
1208 HILOG_INFO("AbilityThread::BatchInsert after abilityImpl_->BatchInsert");
1209 HILOG_INFO("AbilityThread::BatchInsert end");
1210 return ret;
1211 }
1212
1213 #ifdef SUPPORT_GRAPHICS
1214 void AbilityThread::NotifyMultiWinModeChanged(int32_t winModeKey, bool flag)
1215 {
1216 HILOG_INFO("NotifyMultiWinModeChanged.key:%{public}d,flag:%{public}d", winModeKey, flag);
1217 auto window = currentAbility_->GetWindow();
1218 if (window == nullptr) {
1219 HILOG_ERROR("NotifyMultiWinModeChanged window == nullptr");
1220 return;
1221 }
1222
1223 if (flag) {
1224 // true: normal windowMode -> free windowMode
1225 if (winModeKey == MULTI_WINDOW_DISPLAY_FLOATING) {
1226 HILOG_INFO("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_FREE begin.");
1227 window->SetWindowType(Rosen::WindowType::WINDOW_TYPE_FLOAT);
1228 HILOG_INFO("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_FREE end.");
1229 } else {
1230 HILOG_INFO("NotifyMultiWinModeChanged.key:%{public}d", winModeKey);
1231 }
1232 } else {
1233 // false: free windowMode -> normal windowMode
1234 HILOG_INFO("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_TOP begin.");
1235 window->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
1236 HILOG_INFO("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_TOP end.");
1237 }
1238
1239 return;
1240 }
1241
1242 void AbilityThread::NotifyTopActiveAbilityChanged(bool flag)
1243 {
1244 HILOG_INFO("NotifyTopActiveAbilityChanged,flag:%{public}d", flag);
1245 auto window = currentAbility_->GetWindow();
1246 if (window == nullptr) {
1247 HILOG_ERROR("NotifyMultiWinModeChanged window == nullptr");
1248 return;
1249 }
1250 if (flag) {
1251 window->RequestFocus();
1252 }
1253 return;
1254 }
1255 #endif
1256
1257 void AbilityThread::ContinueAbility(const std::string& deviceId)
1258 {
1259 HILOG_INFO("ContinueAbility, deviceId:%{public}s", deviceId.c_str());
1260 if (abilityImpl_ == nullptr) {
1261 HILOG_ERROR("AbilityThread::ContinueAbility abilityImpl_ is nullptr");
1262 return;
1263 }
1264 abilityImpl_->ContinueAbility(deviceId);
1265 }
1266
1267 void AbilityThread::NotifyContinuationResult(int32_t result)
1268 {
1269 HILOG_INFO("NotifyContinuationResult, result:%{public}d", result);
1270 if (abilityImpl_ == nullptr) {
1271 HILOG_ERROR("AbilityThread::NotifyContinuationResult abilityImpl_ is nullptr");
1272 return;
1273 }
1274 abilityImpl_->NotifyContinuationResult(result);
1275 }
1276
1277 /**
1278 * @description: Attach The ability thread to the main process.
1279 * @param application Indicates the main process.
1280 * @param abilityRecord Indicates the abilityRecord.
1281 * @param mainRunner The runner which main_thread holds.
1282 * @param stageContext the AbilityStage context
1283 */
1284 void AbilityThread::AbilityThreadMain(std::shared_ptr<OHOSApplication> &application,
1285 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner,
1286 const std::shared_ptr<AbilityRuntime::Context> &stageContext)
1287 {
1288 HILOG_INFO("AbilityThread::AbilityThreadMain begin");
1289 sptr<AbilityThread> thread = sptr<AbilityThread>(new (std::nothrow) AbilityThread());
1290 if (thread == nullptr) {
1291 HILOG_ERROR("AbilityThread::AbilityThreadMain failed,thread is nullptr");
1292 return;
1293 }
1294 thread->InitExtensionFlag(abilityRecord);
1295 if (thread->isExtension_) {
1296 thread->AttachExtension(application, abilityRecord, mainRunner);
1297 } else {
1298 thread->Attach(application, abilityRecord, mainRunner, stageContext);
1299 }
1300 HILOG_INFO("AbilityThread::AbilityThreadMain end");
1301 }
1302
1303 /**
1304 * @description: Attach The ability thread to the main process.
1305 * @param application Indicates the main process.
1306 * @param abilityRecord Indicates the abilityRecord.
1307 * @param stageContext the AbilityStage context
1308 */
1309 void AbilityThread::AbilityThreadMain(
1310 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
1311 const std::shared_ptr<AbilityRuntime::Context> &stageContext)
1312 {
1313 HILOG_INFO("AbilityThread::AbilityThreadMain begin");
1314 sptr<AbilityThread> thread = sptr<AbilityThread>(new (std::nothrow) AbilityThread());
1315 if (thread == nullptr || abilityRecord == nullptr) {
1316 HILOG_ERROR("AbilityThread::AbilityThreadMain failed, thread is nullptr");
1317 return;
1318 }
1319 thread->InitExtensionFlag(abilityRecord);
1320 if (thread->isExtension_) {
1321 thread->AttachExtension(application, abilityRecord);
1322 } else {
1323 thread->Attach(application, abilityRecord, stageContext);
1324 }
1325 HILOG_INFO("AbilityThread::AbilityThreadMain end");
1326 }
1327
1328 void AbilityThread::InitExtensionFlag(const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
1329 {
1330 HILOG_INFO("AbilityThread::InitExtensionFlag start");
1331 if (abilityRecord == nullptr) {
1332 HILOG_ERROR("AbilityThread::InitExtensionFlag abilityRecord null");
1333 return;
1334 }
1335 std::shared_ptr<AbilityInfo> abilityInfo = abilityRecord->GetAbilityInfo();
1336 if (abilityInfo == nullptr) {
1337 HILOG_ERROR("AbilityThread::InitExtensionFlag abilityInfo null");
1338 return;
1339 }
1340 HILOG_INFO("AbilityThread::InitExtensionFlag:%{public}d", abilityInfo->type);
1341 if (abilityInfo->type == AppExecFwk::AbilityType::EXTENSION) {
1342 HILOG_INFO("AbilityThread::InitExtensionFlag true");
1343 isExtension_ = true;
1344 } else {
1345 isExtension_ = false;
1346 }
1347 }
1348
1349 /**
1350 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
1351 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
1352 * context has changed. If you implement URI normalization for a Data ability, you must also implement
1353 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
1354 * method that is called on the Data ability must require normalization verification and denormalization. The default
1355 * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
1356 *
1357 * @param uri Indicates the Uri object to normalize.
1358 *
1359 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
1360 */
1361 Uri AbilityThread::NormalizeUri(const Uri &uri)
1362 {
1363 HILOG_INFO("AbilityThread::NormalizeUri begin");
1364 Uri urivalue("");
1365 if (abilityImpl_ == nullptr) {
1366 HILOG_ERROR("DataAbilityHelper::normalizeUri failed dataAbility == nullptr");
1367 return urivalue;
1368 }
1369
1370 HILOG_INFO("AbilityThread::NormalizeUri before abilityImpl_->NormalizeUri");
1371 urivalue = abilityImpl_->NormalizeUri(uri);
1372 HILOG_INFO("AbilityThread::NormalizeUri after abilityImpl_->NormalizeUri");
1373 HILOG_INFO("AbilityThread::NormalizeUri end");
1374 return urivalue;
1375 }
1376
1377 /**
1378 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
1379 * The default implementation of this method returns the original URI passed to it.
1380 *
1381 * @param uri uri Indicates the Uri object to denormalize.
1382 *
1383 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed to
1384 * this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found in the
1385 * current environment.
1386 */
1387 Uri AbilityThread::DenormalizeUri(const Uri &uri)
1388 {
1389 HILOG_INFO("AbilityThread::DenormalizeUri begin");
1390 Uri urivalue("");
1391 if (abilityImpl_ == nullptr) {
1392 HILOG_ERROR("DataAbilityHelper::denormalizeUri failed dataAbility == nullptr");
1393 return urivalue;
1394 }
1395
1396 HILOG_INFO("AbilityThread::DenormalizeUri before abilityImpl_->DenormalizeUri");
1397 urivalue = abilityImpl_->DenormalizeUri(uri);
1398 HILOG_INFO("AbilityThread::DenormalizeUri after abilityImpl_->DenormalizeUri");
1399 HILOG_INFO("AbilityThread::DenormalizeUri end");
1400 return urivalue;
1401 }
1402
1403 /**
1404 * @brief Registers an observer to DataObsMgr specified by the given Uri.
1405 *
1406 * @param uri, Indicates the path of the data to operate.
1407 * @param dataObserver, Indicates the IDataAbilityObserver object.
1408 */
1409 bool AbilityThread::HandleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1410 {
1411 auto obsMgrClient = DataObsMgrClient::GetInstance();
1412 if (obsMgrClient == nullptr) {
1413 HILOG_ERROR("%{public}s obsMgrClient is nullptr", __func__);
1414 return false;
1415 }
1416
1417 ErrCode ret = obsMgrClient->RegisterObserver(uri, dataObserver);
1418 if (ret != ERR_OK) {
1419 HILOG_ERROR("%{public}s obsMgrClient->RegisterObserver error return %{public}d", __func__, ret);
1420 return false;
1421 }
1422 return true;
1423 }
1424
1425 /**
1426 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
1427 *
1428 * @param uri, Indicates the path of the data to operate.
1429 * @param dataObserver, Indicates the IDataAbilityObserver object.
1430 */
1431 bool AbilityThread::HandleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1432 {
1433 auto obsMgrClient = DataObsMgrClient::GetInstance();
1434 if (obsMgrClient == nullptr) {
1435 HILOG_ERROR("%{public}s obsMgrClient is nullptr", __func__);
1436 return false;
1437 }
1438
1439 ErrCode ret = obsMgrClient->UnregisterObserver(uri, dataObserver);
1440 if (ret != ERR_OK) {
1441 HILOG_ERROR("%{public}s obsMgrClient->UnregisterObserver error return %{public}d", __func__, ret);
1442 return false;
1443 }
1444 return true;
1445 }
1446
1447 /**
1448 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
1449 *
1450 * @param uri, Indicates the path of the data to operate.
1451 */
1452 bool AbilityThread::HandleNotifyChange(const Uri &uri)
1453 {
1454 auto obsMgrClient = DataObsMgrClient::GetInstance();
1455 if (obsMgrClient == nullptr) {
1456 HILOG_ERROR("%{public}s obsMgrClient is nullptr", __func__);
1457 return false;
1458 }
1459
1460 ErrCode ret = obsMgrClient->NotifyChange(uri);
1461 if (ret != ERR_OK) {
1462 HILOG_ERROR("%{public}s obsMgrClient->NotifyChange error return %{public}d", __func__, ret);
1463 return false;
1464 }
1465 return true;
1466 }
1467
1468 /**
1469 * @brief Access authority verification.
1470 *
1471 * @return Returns true on success, others on failure.
1472 */
1473 bool AbilityThread::CheckObsPermission()
1474 {
1475 HILOG_INFO("%{public}s CheckObsPermission() run Permission Checkout", __func__);
1476 return true;
1477 }
1478
1479 /**
1480 * @brief Registers an observer to DataObsMgr specified by the given Uri.
1481 *
1482 * @param uri, Indicates the path of the data to operate.
1483 * @param dataObserver, Indicates the IDataAbilityObserver object.
1484 */
1485 bool AbilityThread::ScheduleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1486 {
1487 HILOG_INFO("%{public}s called", __func__);
1488 if (!CheckObsPermission()) {
1489 HILOG_ERROR("%{public}s CheckObsPermission() return false", __func__);
1490 return false;
1491 }
1492
1493 wptr<AbilityThread> weak = this;
1494 auto task = [weak, uri, dataObserver]() {
1495 auto abilityThread = weak.promote();
1496 if (abilityThread == nullptr) {
1497 HILOG_ERROR("abilityThread is nullptr, ScheduleRegisterObserver failed.");
1498 return;
1499 }
1500 abilityThread->HandleRegisterObserver(uri, dataObserver);
1501 };
1502
1503 if (abilityHandler_ == nullptr) {
1504 HILOG_ERROR("AbilityThread::ScheduleRegisterObserver abilityHandler_ == nullptr");
1505 return false;
1506 }
1507
1508 bool ret = abilityHandler_->PostTask(task);
1509 if (!ret) {
1510 HILOG_ERROR("AbilityThread::ScheduleRegisterObserver PostTask error");
1511 }
1512 return ret;
1513 }
1514
1515 /**
1516 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
1517 *
1518 * @param uri, Indicates the path of the data to operate.
1519 * @param dataObserver, Indicates the IDataAbilityObserver object.
1520 */
1521 bool AbilityThread::ScheduleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1522 {
1523 HILOG_INFO("%{public}s called", __func__);
1524 if (!CheckObsPermission()) {
1525 HILOG_ERROR("%{public}s CheckObsPermission() return false", __func__);
1526 return false;
1527 }
1528
1529 wptr<AbilityThread> weak = this;
1530 auto task = [weak, uri, dataObserver]() {
1531 auto abilityThread = weak.promote();
1532 if (abilityThread == nullptr) {
1533 HILOG_ERROR("abilityThread is nullptr, ScheduleUnregisterObserver failed.");
1534 return;
1535 }
1536 abilityThread->HandleUnregisterObserver(uri, dataObserver);
1537 };
1538
1539 if (abilityHandler_ == nullptr) {
1540 HILOG_ERROR("AbilityThread::ScheduleUnregisterObserver abilityHandler_ == nullptr");
1541 return false;
1542 }
1543
1544 bool ret = abilityHandler_->PostSyncTask(task);
1545 if (!ret) {
1546 HILOG_ERROR("AbilityThread::ScheduleUnregisterObserver PostTask error");
1547 }
1548 return ret;
1549 }
1550
1551 /**
1552 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
1553 *
1554 * @param uri, Indicates the path of the data to operate.
1555 */
1556 bool AbilityThread::ScheduleNotifyChange(const Uri &uri)
1557 {
1558 HILOG_INFO("%{public}s called", __func__);
1559 if (!CheckObsPermission()) {
1560 HILOG_ERROR("%{public}s CheckObsPermission() return false", __func__);
1561 return false;
1562 }
1563
1564 wptr<AbilityThread> weak = this;
1565 auto task = [weak, uri]() {
1566 auto abilityThread = weak.promote();
1567 if (abilityThread == nullptr) {
1568 HILOG_ERROR("abilityThread is nullptr, ScheduleNotifyChange failed.");
1569 return;
1570 }
1571 abilityThread->HandleNotifyChange(uri);
1572 };
1573
1574 if (abilityHandler_ == nullptr) {
1575 HILOG_ERROR("AbilityThread::ScheduleNotifyChange abilityHandler_ == nullptr");
1576 return false;
1577 }
1578
1579 bool ret = abilityHandler_->PostTask(task);
1580 if (!ret) {
1581 HILOG_ERROR("AbilityThread::ScheduleNotifyChange PostTask error");
1582 }
1583 return ret;
1584 }
1585
1586 std::vector<std::shared_ptr<DataAbilityResult>> AbilityThread::ExecuteBatch(
1587 const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
1588 {
1589
1590 HILOG_INFO("AbilityThread::ExecuteBatch start");
1591 std::vector<std::shared_ptr<DataAbilityResult>> results;
1592 if (abilityImpl_ == nullptr) {
1593 HILOG_ERROR("AbilityThread::ExecuteBatch abilityImpl_ is nullptr");
1594 results.clear();
1595 return results;
1596 }
1597 HILOG_INFO("AbilityThread::ExecuteBatch before abilityImpl_->ExecuteBatch");
1598 results = abilityImpl_->ExecuteBatch(operations);
1599 HILOG_INFO("AbilityThread::ExecuteBatch after abilityImpl_->ExecuteBatch");
1600 HILOG_INFO("AbilityThread::ExecuteBatch end");
1601 return results;
1602 }
1603
1604 std::shared_ptr<AbilityRuntime::AbilityContext> AbilityThread::BuildAbilityContext(
1605 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
1606 const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityRuntime::Context> &stageContext)
1607 {
1608 auto abilityContextImpl = std::make_shared<AbilityRuntime::AbilityContextImpl>();
1609 abilityContextImpl->SetStageContext(stageContext);
1610 abilityContextImpl->SetToken(token);
1611 abilityContextImpl->SetAbilityInfo(abilityInfo);
1612 abilityContextImpl->SetConfiguration(application->GetConfiguration());
1613 return abilityContextImpl;
1614 }
1615
1616 void AbilityThread::DumpAbilityInfo(const std::vector<std::string> ¶ms, std::vector<std::string> &info)
1617 {
1618 HILOG_INFO("%{public}s begin.", __func__);
1619 if (token_ == nullptr) {
1620 HILOG_ERROR("DumpAbilityInfo::failed, token_ nullptr");
1621 return;
1622 }
1623 wptr<AbilityThread> weak = this;
1624 auto task = [weak, params, token = token_]() {
1625 auto abilityThread = weak.promote();
1626 if (abilityThread == nullptr) {
1627 HILOG_ERROR("abilityThread is nullptr, ScheduleAbilityTransaction failed.");
1628 return;
1629 }
1630 std::vector<std::string> dumpInfo;
1631 abilityThread->DumpAbilityInfoInner(params, dumpInfo);
1632 ErrCode err = AbilityManagerClient::GetInstance()->DumpAbilityInfoDone(dumpInfo, token);
1633 if (err != ERR_OK) {
1634 HILOG_ERROR("AbilityThread:: DumpAbilityInfo failed err = %{public}d", err);
1635 }
1636 };
1637
1638 if (abilityHandler_ == nullptr) {
1639 HILOG_ERROR("AbilityThread::ScheduleAbilityTransaction abilityHandler_ == nullptr");
1640 return;
1641 }
1642
1643 abilityHandler_->PostTask(task);
1644 }
1645
1646 #ifdef SUPPORT_GRAPHICS
1647 void AbilityThread::DumpAbilityInfoInner(const std::vector<std::string> ¶ms, std::vector<std::string> &info)
1648 {
1649 HILOG_INFO("%{public}s begin.", __func__);
1650 if (currentAbility_ == nullptr && currentExtension_ == nullptr) {
1651 HILOG_INFO("currentAbility and currentExtension_ is nullptr.");
1652 return;
1653 }
1654
1655 if (currentAbility_ != nullptr) {
1656 if (abilityImpl_->IsStageBasedModel()) {
1657 auto scene = currentAbility_->GetScene();
1658 if (scene == nullptr) {
1659 HILOG_ERROR("DumpAbilityInfo scene == nullptr");
1660 return;
1661 }
1662 auto window = scene->GetMainWindow();
1663 if (window == nullptr) {
1664 HILOG_ERROR("DumpAbilityInfo window == nullptr");
1665 return;
1666 }
1667 window->DumpInfo(params, info);
1668 }
1669 currentAbility_->Dump(params, info);
1670 }
1671
1672 if (params.empty()) {
1673 HILOG_INFO("params not empty");
1674 DumpOtherInfo(info);
1675 return;
1676 }
1677 HILOG_INFO("%{public}s end.", __func__);
1678 }
1679 #else
1680 void AbilityThread::DumpAbilityInfoInner(const std::vector<std::string> ¶ms, std::vector<std::string> &info)
1681 {
1682 HILOG_INFO("%{public}s begin.", __func__);
1683 if (currentAbility_ != nullptr) {
1684 currentAbility_->Dump(params, info);
1685 }
1686 DumpOtherInfo(info);
1687 }
1688 #endif
1689
1690 void AbilityThread::DumpOtherInfo(std::vector<std::string> &info)
1691 {
1692 std::string dumpInfo = " event:";
1693 info.push_back(dumpInfo);
1694
1695 if (!abilityHandler_) {
1696 HILOG_INFO("abilityHandler_ is nullptr.");
1697 return;
1698 }
1699 auto runner = abilityHandler_->GetEventRunner();
1700 if (!runner) {
1701 HILOG_INFO("runner_ is nullptr.");
1702 return;
1703 }
1704
1705 dumpInfo = "";
1706 runner->DumpRunnerInfo(dumpInfo);
1707 info.push_back(dumpInfo);
1708
1709 if (currentAbility_ != nullptr) {
1710 const auto ablityContext = currentAbility_->GetAbilityContext();
1711 if (!ablityContext) {
1712 HILOG_INFO("current ability context is nullptr.");
1713 return;
1714 }
1715 const auto localCallContainer = ablityContext->GetLocalCallContainer();
1716 if (!localCallContainer) {
1717 HILOG_INFO("current ability context locall call container is nullptr.");
1718 return;
1719 }
1720 localCallContainer->DumpCalls(info);
1721 }
1722 }
1723
1724 sptr<IRemoteObject> AbilityThread::CallRequest()
1725 {
1726 HILOG_INFO("AbilityThread::CallRequest begin");
1727
1728 if (!currentAbility_) {
1729 HILOG_ERROR("ability is nullptr.");
1730 return nullptr;
1731 }
1732
1733 sptr<IRemoteObject> retval = nullptr;
1734 std::weak_ptr<OHOS::AppExecFwk::Ability> weakAbility = currentAbility_;
1735 auto syncTask = [ability = weakAbility, &retval] () {
1736 auto currentAbility = ability.lock();
1737 if (currentAbility == nullptr) {
1738 HILOG_ERROR("ability is nullptr.");
1739 return;
1740 }
1741
1742 HILOG_DEBUG("AbilityThread::CallRequest syncTask CallRequest begin");
1743 retval = currentAbility->CallRequest();
1744 HILOG_DEBUG("AbilityThread::CallRequest syncTask CallRequest end %{public}p", retval.GetRefPtr());
1745 };
1746
1747 if (abilityHandler_ == nullptr) {
1748 HILOG_ERROR("ability Handler is nullptr.");
1749 return nullptr;
1750 }
1751
1752 HILOG_DEBUG("AbilityThread::CallRequest post sync task");
1753
1754 abilityHandler_->PostSyncTask(syncTask);
1755
1756 HILOG_INFO("AbilityThread::CallRequest end");
1757 return retval;
1758 }
1759 } // namespace AppExecFwk
1760 } // namespace OHOS
1761