1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ability_thread.h"
17 #include <functional>
18 #include "ohos_application.h"
19 #include "ability_loader.h"
20 #include "ability_state.h"
21 #include "ability_impl_factory.h"
22 #include "page_ability_impl.h"
23 #include "application_impl.h"
24 #include "app_log_wrapper.h"
25 #include "context_deal.h"
26 #include "abs_shared_result_set.h"
27 #include "data_ability_predicates.h"
28 #include "values_bucket.h"
29 #include "dataobs_mgr_client.h"
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 using AbilityManagerClient = OHOS::AAFwk::AbilityManagerClient;
34 using DataObsMgrClient = OHOS::AAFwk::DataObsMgrClient;
35 constexpr static char ACE_ABILITY_NAME[] = "AceAbility";
36 constexpr static char ACE_SERVICE_ABILITY_NAME[] = "AceServiceAbility";
37 constexpr static char ACE_DATA_ABILITY_NAME[] = "AceDataAbility";
38 constexpr static char ACE_FORM_ABILITY_NAME[] = "AceFormAbility";
39
40 /**
41 * @brief Default constructor used to create a AbilityThread instance.
42 */
AbilityThread()43 AbilityThread::AbilityThread()
44 : abilityImpl_(nullptr), token_(nullptr), currentAbility_(nullptr), abilityHandler_(nullptr), runner_(nullptr)
45 {}
46
~AbilityThread()47 AbilityThread::~AbilityThread()
48 {
49 DelayedSingleton<AbilityImplFactory>::DestroyInstance();
50 }
51
52 /**
53 * @description: Attach The ability thread to the main process.
54 *
55 * @param abilityRecord Indicates the abilityRecord.
56 *
57 * @return Returns the abilityName.
58 *
59 */
CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> & abilityRecord)60 std::string AbilityThread::CreateAbilityName(const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
61 {
62 std::string abilityName;
63 APP_LOGI("AbilityThread::CreateAbilityName begin");
64 if (abilityRecord == nullptr) {
65 APP_LOGE("AbilityThread::CreateAbilityName failed,abilityRecord is nullptr");
66 return abilityName;
67 }
68
69 std::shared_ptr<AbilityInfo> abilityInfo = abilityRecord->GetAbilityInfo();
70 if (abilityInfo == nullptr) {
71 APP_LOGE("AbilityThread::ability attach failed,abilityInfo is nullptr");
72 return abilityName;
73 }
74
75 APP_LOGI("AbilityThread::ability attach the ability type is %{public}d", abilityInfo->type);
76 APP_LOGI("AbilityThread::ability attach the ability is Native %{public}d", abilityInfo->isNativeAbility);
77
78 if (abilityInfo->isNativeAbility == false) {
79 if (abilityInfo->type == AbilityType::PAGE) {
80 abilityName = ACE_ABILITY_NAME;
81 } else if (abilityInfo->type == AbilityType::SERVICE) {
82 if (abilityInfo->formEnabled == true) {
83 abilityName = ACE_FORM_ABILITY_NAME;
84 } else {
85 abilityName = ACE_SERVICE_ABILITY_NAME;
86 }
87 } else if (abilityInfo->type == AbilityType::DATA) {
88 abilityName = ACE_DATA_ABILITY_NAME;
89 } else {
90 abilityName = abilityInfo->name;
91 }
92 } else {
93 abilityName = abilityInfo->name;
94 }
95
96 APP_LOGI("AbilityThread::CreateAbilityName end");
97 return abilityName;
98 }
99
100 /**
101 * @description: Create and init contextDeal.
102 *
103 * @param application Indicates the main process.
104 * @param abilityRecord Indicates the abilityRecord.
105 * @param abilityObject Indicates the abilityObject.
106 *
107 * @return Returns the contextDeal.
108 *
109 */
CreateAndInitContextDeal(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & abilityRecord,const std::shared_ptr<Context> & abilityObject)110 std::shared_ptr<ContextDeal> AbilityThread::CreateAndInitContextDeal(std::shared_ptr<OHOSApplication> &application,
111 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<Context> &abilityObject)
112 {
113 APP_LOGI("AbilityThread::CreateAndInitContextDeal begin");
114 std::shared_ptr<ContextDeal> contextDeal = nullptr;
115 APP_LOGI("AbilityThread::CreateAndInitContextDeal called");
116 if ((application == nullptr) || (abilityRecord == nullptr) || (abilityObject == nullptr)) {
117 APP_LOGE("AbilityThread::ability attach failed,context or record or abilityObject is nullptr");
118 return contextDeal;
119 }
120
121 contextDeal = std::make_shared<ContextDeal>();
122 if (contextDeal == nullptr) {
123 APP_LOGE("AbilityThread::ability attach failed,contextDeal is nullptr");
124 return contextDeal;
125 }
126
127 contextDeal->SetAbilityInfo(abilityRecord->GetAbilityInfo());
128 contextDeal->SetApplicationInfo(application->GetApplicationInfo());
129 contextDeal->SetProcessInfo(application->GetProcessInfo());
130
131 std::shared_ptr<Context> tmpContext = application->GetApplicationContext();
132 contextDeal->SetApplicationContext(tmpContext);
133
134 contextDeal->SetBundleCodePath(abilityRecord->GetAbilityInfo()->codePath);
135 contextDeal->SetContext(abilityObject);
136 contextDeal->SetRunner(abilityHandler_->GetEventRunner());
137 APP_LOGI("AbilityThread::CreateAndInitContextDeal end");
138 return contextDeal;
139 }
140
141 /**
142 * @description: Attach The ability thread to the main process.
143 * @param application Indicates the main process.
144 * @param abilityRecord Indicates the abilityRecord.
145 * @param mainRunner The runner which main_thread holds.
146 */
Attach(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & abilityRecord,const std::shared_ptr<EventRunner> & mainRunner)147 void AbilityThread::Attach(std::shared_ptr<OHOSApplication> &application,
148 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner)
149 {
150 APP_LOGI("AbilityThread::Attach begin");
151 if ((application == nullptr) || (abilityRecord == nullptr) || (mainRunner == nullptr)) {
152 APP_LOGE("AbilityThread::ability attach failed,context or record is nullptr");
153 return;
154 }
155
156 // 1.new AbilityHandler
157 std::string abilityName = CreateAbilityName(abilityRecord);
158 abilityHandler_ = std::make_shared<AbilityHandler>(mainRunner, this);
159 if (abilityHandler_ == nullptr) {
160 APP_LOGE("AbilityThread::ability attach failed,abilityHandler_ is nullptr");
161 return;
162 }
163
164 // 2.new ability
165 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityName);
166 if (ability == nullptr) {
167 APP_LOGE("AbilityThread::ability attach failed,load ability failed");
168 return;
169 }
170
171 APP_LOGI("AbilityThread::new ability success.");
172 currentAbility_.reset(ability);
173 token_ = abilityRecord->GetToken();
174 abilityRecord->SetEventHandler(abilityHandler_);
175 abilityRecord->SetEventRunner(mainRunner);
176 abilityRecord->SetAbilityThread(this);
177 std::shared_ptr<Context> abilityObject = currentAbility_;
178 std::shared_ptr<ContextDeal> contextDeal = CreateAndInitContextDeal(application, abilityRecord, abilityObject);
179 ability->AttachBaseContext(contextDeal);
180
181 // 3.new abilityImpl
182 abilityImpl_ =
183 DelayedSingleton<AbilityImplFactory>::GetInstance()->MakeAbilityImplObject(abilityRecord->GetAbilityInfo());
184 if (abilityImpl_ == nullptr) {
185 APP_LOGE("AbilityThread::ability abilityImpl_ == nullptr");
186 return;
187 }
188 APP_LOGI("AbilityThread::Attach before abilityImpl_->Init");
189 abilityImpl_->Init(application, abilityRecord, currentAbility_, abilityHandler_, token_, contextDeal);
190 APP_LOGI("AbilityThread::Attach after abilityImpl_->Init");
191 // 4. ability attach : ipc
192 APP_LOGI("AbilityThread::Attach before AttachAbilityThread");
193 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
194 APP_LOGI("AbilityThread::Attach after AttachAbilityThread");
195 if (err != ERR_OK) {
196 APP_LOGE("AbilityThread:: attach success faile err = %{public}d", err);
197 return;
198 }
199
200 APP_LOGI("AbilityThread::Attach end");
201 }
202
203 /**
204 * @description: Attach The ability thread to the main process.
205 * @param application Indicates the main process.
206 * @param abilityRecord Indicates the abilityRecord.
207 */
Attach(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & abilityRecord)208 void AbilityThread::Attach(
209 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
210 {
211 APP_LOGI("AbilityThread::Attach begin");
212 if ((application == nullptr) || (abilityRecord == nullptr)) {
213 APP_LOGE("AbilityThread::ability attach failed,context or record is nullptr");
214 return;
215 }
216 // 1.new AbilityHandler
217 std::string abilityName = CreateAbilityName(abilityRecord);
218 runner_ = EventRunner::Create(abilityName);
219 if (runner_ == nullptr) {
220 APP_LOGE("AbilityThread::ability attach failed,create runner failed");
221 return;
222 }
223 abilityHandler_ = std::make_shared<AbilityHandler>(runner_, this);
224 if (abilityHandler_ == nullptr) {
225 APP_LOGE("AbilityThread::ability attach failed,abilityHandler_ is nullptr");
226 return;
227 }
228
229 // 2.new ability
230 auto ability = AbilityLoader::GetInstance().GetAbilityByName(abilityName);
231 if (ability == nullptr) {
232 APP_LOGE("AbilityThread::ability attach failed,load ability failed");
233 return;
234 }
235
236 APP_LOGI("AbilityThread::new ability success.");
237 currentAbility_.reset(ability);
238 token_ = abilityRecord->GetToken();
239 abilityRecord->SetEventHandler(abilityHandler_);
240 abilityRecord->SetEventRunner(runner_);
241 abilityRecord->SetAbilityThread(this);
242 std::shared_ptr<Context> abilityObject = currentAbility_;
243 std::shared_ptr<ContextDeal> contextDeal = CreateAndInitContextDeal(application, abilityRecord, abilityObject);
244 ability->AttachBaseContext(contextDeal);
245
246 // 3.new abilityImpl
247 abilityImpl_ =
248 DelayedSingleton<AbilityImplFactory>::GetInstance()->MakeAbilityImplObject(abilityRecord->GetAbilityInfo());
249 if (abilityImpl_ == nullptr) {
250 APP_LOGE("AbilityThread::ability abilityImpl_ == nullptr");
251 return;
252 }
253 APP_LOGI("AbilityThread::Attach before abilityImpl_->Init");
254 abilityImpl_->Init(application, abilityRecord, currentAbility_, abilityHandler_, token_, contextDeal);
255 APP_LOGI("AbilityThread::Attach after abilityImpl_->Init");
256 // 4. ability attach : ipc
257 APP_LOGI("AbilityThread::Attach before AttachAbilityThread");
258 ErrCode err = AbilityManagerClient::GetInstance()->AttachAbilityThread(this, token_);
259 APP_LOGI("AbilityThread::Attach after AttachAbilityThread");
260 if (err != ERR_OK) {
261 APP_LOGE("AbilityThread:: attach success faile err = %{public}d", err);
262 return;
263 }
264
265 APP_LOGI("AbilityThread::Attach end");
266 }
267
268 /**
269 * @description: Handle the life cycle of Ability.
270 * @param want Indicates the structure containing lifecycle information about the ability.
271 * @param lifeCycleStateInfo Indicates the lifeCycleStateInfo.
272 */
HandleAbilityTransaction(const Want & want,const LifeCycleStateInfo & lifeCycleStateInfo)273 void AbilityThread::HandleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
274 {
275 APP_LOGI("AbilityThread::HandleAbilityTransaction begin");
276 if (abilityImpl_ == nullptr) {
277 APP_LOGE("AbilityThread::HandleAbilityTransaction abilityImpl_ == nullptr");
278 return;
279 }
280
281 APP_LOGI("AbilityThread::HandleAbilityTransaction before abilityImpl_->SetCallingContext");
282 abilityImpl_->SetCallingContext(lifeCycleStateInfo.caller.deviceId,
283 lifeCycleStateInfo.caller.bundleName,
284 lifeCycleStateInfo.caller.abilityName);
285 APP_LOGI("AbilityThread::HandleAbilityTransaction after abilityImpl_->SetCallingContext");
286 APP_LOGI("AbilityThread::HandleAbilityTransaction before abilityImpl_->HandleAbilityTransaction");
287 abilityImpl_->HandleAbilityTransaction(want, lifeCycleStateInfo);
288 APP_LOGI("AbilityThread::HandleAbilityTransaction after abilityImpl_->HandleAbilityTransaction");
289 APP_LOGI("AbilityThread::HandleAbilityTransaction end");
290 }
291
292 /**
293 * @description: Handle the current connection of Ability.
294 * @param want Indicates the structure containing connection information about the ability.
295 */
HandleConnectAbility(const Want & want)296 void AbilityThread::HandleConnectAbility(const Want &want)
297 {
298 APP_LOGI("AbilityThread::HandleConnectAbility begin");
299 if (abilityImpl_ == nullptr) {
300 APP_LOGE("AbilityThread::HandleConnectAbility abilityImpl_ == nullptr");
301 return;
302 }
303
304 APP_LOGI("AbilityThread::HandleConnectAbility before abilityImpl_->ConnectAbility");
305 sptr<IRemoteObject> service = abilityImpl_->ConnectAbility(want);
306 APP_LOGI("AbilityThread::HandleConnectAbility after abilityImpl_->ConnectAbility");
307 APP_LOGI("AbilityThread::HandleConnectAbility before ScheduleConnectAbilityDone");
308 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleConnectAbilityDone(token_, service);
309 APP_LOGI("AbilityThread::HandleConnectAbility after ScheduleConnectAbilityDone");
310 if (err != ERR_OK) {
311 APP_LOGE("AbilityThread:: HandleConnectAbility faile err = %{public}d", err);
312 }
313 APP_LOGI("AbilityThread::HandleConnectAbility end");
314 }
315
316 /**
317 * @description: Handle the current disconnection of Ability.
318 */
HandleDisconnectAbility(const Want & want)319 void AbilityThread::HandleDisconnectAbility(const Want &want)
320 {
321 APP_LOGI("AbilityThread::HandleDisconnectAbility begin");
322 if (abilityImpl_ == nullptr) {
323 APP_LOGE("AbilityThread::HandleDisconnectAbility abilityImpl_ == nullptr");
324 return;
325 }
326
327 APP_LOGI("AbilityThread::HandleDisconnectAbility before abilityImpl_->DisconnectAbility");
328 abilityImpl_->DisconnectAbility(want);
329 APP_LOGI("AbilityThread::HandleDisconnectAbility after abilityImpl_->DisconnectAbility");
330 APP_LOGI("AbilityThread::HandleDisconnectAbility before ScheduleDisconnectAbilityDone");
331 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleDisconnectAbilityDone(token_);
332 APP_LOGI("AbilityThread::HandleDisconnectAbility after ScheduleDisconnectAbilityDone");
333 if (err != ERR_OK) {
334 APP_LOGE("AbilityThread:: HandleDisconnectAbility faile err = %{public}d", err);
335 }
336 APP_LOGI("AbilityThread::HandleDisconnectAbility end");
337 }
338
339 /**
340 * @brief Handle the current commadn of Ability.
341 *
342 * @param want The Want object to command to.
343 *
344 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
345 * destroyed, and the value false indicates a normal startup.
346 *
347 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1
348 * every time the ability is started. For example, if the ability has been started for six times, the value of startId
349 * is 6.
350 */
HandleCommandAbility(const Want & want,bool restart,int startId)351 void AbilityThread::HandleCommandAbility(const Want &want, bool restart, int startId)
352 {
353 APP_LOGI("AbilityThread::HandleCommandAbility begin");
354 APP_LOGI("AbilityThread::HandleCommandAbility before abilityImpl_->CommandAbility");
355 abilityImpl_->CommandAbility(want, restart, startId);
356 APP_LOGI("AbilityThread::HandleCommandAbility after abilityImpl_->CommandAbility");
357 APP_LOGI("AbilityThread::HandleCommandAbility before ScheduleCommandAbilityDone");
358 ErrCode err = AbilityManagerClient::GetInstance()->ScheduleCommandAbilityDone(token_);
359 APP_LOGI("AbilityThread::HandleCommandAbility after ScheduleCommandAbilityDone");
360 if (err != ERR_OK) {
361 APP_LOGE("AbilityThread:: HandleCommandAbility faile err = %{public}d", err);
362 }
363 APP_LOGI("AbilityThread::HandleCommandAbility end");
364 }
365
366 /**
367 * @description: Handle the SaveAbility state.
368 * @param state Indicates save ability state used to dispatchSaveAbilityState.
369 */
HandleSaveAbilityState(PacMap & state)370 void AbilityThread::HandleSaveAbilityState(PacMap &state)
371 {
372 APP_LOGI("AbilityThread::HandleSaveAbilityState begin");
373 if (abilityImpl_ == nullptr) {
374 APP_LOGE("AbilityThread::HandleSaveAbilityState abilityImpl_ == nullptr");
375 return;
376 }
377
378 APP_LOGI("AbilityThread::HandleSaveAbilityState before abilityImpl_->DispatchSaveAbilityState");
379 abilityImpl_->DispatchSaveAbilityState(state);
380 APP_LOGI("AbilityThread::HandleSaveAbilityState after abilityImpl_->DispatchSaveAbilityState");
381 APP_LOGI("AbilityThread::HandleSaveAbilityState end");
382 }
383
384 /**
385 * @description: Handle the restoreAbility state.
386 * @param state Indicates save ability state used to dispatchRestoreAbilityState.
387 */
HandleRestoreAbilityState(const PacMap & state)388 void AbilityThread::HandleRestoreAbilityState(const PacMap &state)
389 {
390 APP_LOGI("AbilityThread::HandleRestoreAbilityState begin");
391 if (abilityImpl_ == nullptr) {
392 APP_LOGE("AbilityThread::HandleRestoreAbilityState abilityImpl_ == nullptr");
393 return;
394 }
395
396 APP_LOGI("AbilityThread::HandleRestoreAbilityState before abilityImpl_->DispatchRestoreAbilityState");
397 abilityImpl_->DispatchRestoreAbilityState(state);
398 APP_LOGI("AbilityThread::HandleRestoreAbilityState after abilityImpl_->DispatchRestoreAbilityState");
399 APP_LOGI("AbilityThread::HandleRestoreAbilityState end");
400 }
401
402 /**
403 * @description: Provide operating system SaveabilityState information to the observer
404 * @param state Indicates save ability state used to dispatch.
405 */
ScheduleSaveAbilityState(PacMap & state)406 void AbilityThread::ScheduleSaveAbilityState(PacMap &state)
407 {
408 APP_LOGI("AbilityThread::ScheduleSaveAbilityState begin");
409 if (abilityImpl_ == nullptr) {
410 APP_LOGE("AbilityThread::ScheduleSaveAbilityState abilityImpl_ == nullptr");
411 return;
412 }
413
414 APP_LOGI("AbilityThread::ScheduleSaveAbilityState before abilityImpl_->DispatchSaveAbilityState");
415 abilityImpl_->DispatchSaveAbilityState(state);
416 APP_LOGI("AbilityThread::ScheduleSaveAbilityState after abilityImpl_->DispatchSaveAbilityState");
417 APP_LOGI("AbilityThread::ScheduleSaveAbilityState end");
418 }
419
420 /**
421 * @description: Provide operating system RestoreAbilityState information to the observer
422 * @param state Indicates resotre ability state used to dispatchRestoreAbilityState.
423 */
ScheduleRestoreAbilityState(const PacMap & state)424 void AbilityThread::ScheduleRestoreAbilityState(const PacMap &state)
425 {
426 APP_LOGI("AbilityThread::ScheduleRestoreAbilityState begin");
427 if (abilityImpl_ == nullptr) {
428 APP_LOGE("AbilityThread::ScheduleRestoreAbilityState abilityImpl_ == nullptr");
429 return;
430 }
431 APP_LOGI("AbilityThread::ScheduleRestoreAbilityState before abilityImpl_->DispatchRestoreAbilityState");
432 abilityImpl_->DispatchRestoreAbilityState(state);
433 APP_LOGI("AbilityThread::ScheduleRestoreAbilityState after abilityImpl_->DispatchRestoreAbilityState");
434 APP_LOGI("AbilityThread::ScheduleRestoreAbilityState end");
435 }
436
437 /*
438 * @brief ScheduleUpdateConfiguration, scheduling update configuration.
439 */
ScheduleUpdateConfiguration(const DummyConfiguration & config)440 void AbilityThread::ScheduleUpdateConfiguration(const DummyConfiguration &config)
441 {
442 APP_LOGI("AbilityThread::ScheduleUpdateConfiguration begin");
443 if (abilityImpl_ == nullptr) {
444 APP_LOGE("AbilityThread::ScheduleUpdateConfiguration abilityImpl_ is nullptr");
445 return;
446 }
447
448 auto task = [abilitThread = this, config]() { abilitThread->HandleUpdateConfiguration(config); };
449
450 if (abilityHandler_ == nullptr) {
451 APP_LOGE("AbilityThread::ScheduleUpdateConfiguration abilityHandler_ is nullptr");
452 return;
453 }
454
455 bool ret = abilityHandler_->PostTask(task);
456 if (!ret) {
457 APP_LOGE("AbilityThread::ScheduleUpdateConfiguration PostTask error");
458 }
459 APP_LOGI("AbilityThread::ScheduleUpdateConfiguration end");
460 }
461
462 /*
463 * @brief Handle the scheduling update configuration.
464 */
HandleUpdateConfiguration(const DummyConfiguration & config)465 void AbilityThread::HandleUpdateConfiguration(const DummyConfiguration &config)
466 {
467 APP_LOGI("AbilityThread::HandleUpdateConfiguration begin");
468 if (abilityImpl_ == nullptr) {
469 APP_LOGE("AbilityThread::HandleUpdateConfiguration abilityImpl_ is nullptr");
470 return;
471 }
472
473 APP_LOGI("AbilityThread::HandleUpdateConfiguration before abilityImpl_->ScheduleUpdateConfiguration");
474 abilityImpl_->ScheduleUpdateConfiguration(config);
475 APP_LOGI("AbilityThread::HandleUpdateConfiguration after abilityImpl_->ScheduleUpdateConfiguration");
476 APP_LOGI("AbilityThread::HandleUpdateConfiguration end");
477 }
478
479 /**
480 * @description: Provide operating system AbilityTransaction information to the observer
481 * @param want Indicates the structure containing Transaction information about the ability.
482 * @param lifeCycleStateInfo Indicates the lifecycle state.
483 */
ScheduleAbilityTransaction(const Want & want,const LifeCycleStateInfo & lifeCycleStateInfo)484 void AbilityThread::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo)
485 {
486 APP_LOGI("ScheduleAbilityTransaction begin: targeState = %{public}d, isNewWant = %{public}d",
487 lifeCycleStateInfo.state,
488 lifeCycleStateInfo.isNewWant);
489
490 want.DumpInfo(0);
491
492 if ((token_ == nullptr) || abilityImpl_ == nullptr) {
493 APP_LOGE("ScheduleAbilityTransaction::failed");
494 return;
495 }
496 auto task = [abilityThread = this, want, lifeCycleStateInfo]() {
497 abilityThread->HandleAbilityTransaction(want, lifeCycleStateInfo);
498 };
499
500 if (abilityHandler_ == nullptr) {
501 APP_LOGE("AbilityThread::ScheduleAbilityTransaction abilityHandler_ == nullptr");
502 return;
503 }
504
505 bool ret = abilityHandler_->PostTask(task);
506 if (!ret) {
507 APP_LOGE("AbilityThread::ScheduleAbilityTransaction PostTask error");
508 }
509 APP_LOGI("ScheduleAbilityTransaction end");
510 }
511
512 /**
513 * @description: Provide operating system ConnectAbility information to the observer
514 * @param want Indicates the structure containing connect information about the ability.
515 */
ScheduleConnectAbility(const Want & want)516 void AbilityThread::ScheduleConnectAbility(const Want &want)
517 {
518 APP_LOGI("AbilityThread::ScheduleConnectAbility begin");
519 auto task = [abilityThread = this, want]() { abilityThread->HandleConnectAbility(want); };
520
521 if (abilityHandler_ == nullptr) {
522 APP_LOGE("AbilityThread::ScheduleConnectAbility abilityHandler_ == nullptr");
523 return;
524 }
525
526 bool ret = abilityHandler_->PostTask(task);
527 if (!ret) {
528 APP_LOGE("AbilityThread::ScheduleConnectAbility PostTask error");
529 }
530 APP_LOGI("AbilityThread::ScheduleConnectAbility end");
531 }
532
533 /**
534 * @description: Provide operating system ConnectAbility information to the observer
535 * @return None
536 */
ScheduleDisconnectAbility(const Want & want)537 void AbilityThread::ScheduleDisconnectAbility(const Want &want)
538 {
539 APP_LOGI("AbilityThread::ScheduleDisconnectAbility begin");
540 auto task = [abilityThread = this, want]() { abilityThread->HandleDisconnectAbility(want); };
541
542 if (abilityHandler_ == nullptr) {
543 APP_LOGE("AbilityThread::ScheduleDisconnectAbility abilityHandler_ == nullptr");
544 return;
545 }
546
547 bool ret = abilityHandler_->PostTask(task);
548 if (!ret) {
549 APP_LOGE("AbilityThread::ScheduleDisconnectAbility PostTask error");
550 }
551 APP_LOGI("AbilityThread::ScheduleDisconnectAbility end");
552 }
553
554 /**
555 * @description: Provide operating system CommandAbility information to the observer
556 *
557 * @param want The Want object to command to.
558 *
559 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
560 * destroyed, and the value false indicates a normal startup.
561 *
562 * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by 1
563 * every time the ability is started. For example, if the ability has been started for six times, the value of startId
564 * is 6.
565 */
ScheduleCommandAbility(const Want & want,bool restart,int startId)566 void AbilityThread::ScheduleCommandAbility(const Want &want, bool restart, int startId)
567 {
568 APP_LOGI("AbilityThread::ScheduleCommandAbility begin");
569 auto task = [abilityThread = this, want, restart, startId]() {
570 abilityThread->HandleCommandAbility(want, restart, startId);
571 };
572
573 if (abilityHandler_ == nullptr) {
574 APP_LOGE("AbilityThread::ScheduleCommandAbility abilityHandler_ == nullptr");
575 return;
576 }
577
578 bool ret = abilityHandler_->PostTask(task);
579 if (!ret) {
580 APP_LOGE("AbilityThread::ScheduleCommandAbility PostTask error");
581 }
582 APP_LOGI("AbilityThread::ScheduleCommandAbility end");
583 }
584
585 /**
586 * @brief Send the result code and data to be returned by this Page ability to the caller.
587 * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to
588 * receive the result set in the current method. This method can be called only after the ability has been initialized.
589 *
590 * @param requestCode Indicates the request code for send.
591 * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result code
592 * to identify an error.
593 * @param want Indicates the data returned after the ability is destroyed. You can define the data returned. This
594 * parameter can be null.
595 */
SendResult(int requestCode,int resultCode,const Want & want)596 void AbilityThread::SendResult(int requestCode, int resultCode, const Want &want)
597 {
598 APP_LOGI("AbilityThread::SendResult begin");
599 if (abilityImpl_ == nullptr) {
600 APP_LOGE("AbilityThread::SendResult abilityImpl_ == nullptr");
601 return;
602 }
603
604 if (requestCode != -1) {
605 APP_LOGI("AbilityThread::SendResult before abilityImpl_->SendResult");
606 abilityImpl_->SendResult(requestCode, resultCode, want);
607 APP_LOGI("AbilityThread::SendResult after abilityImpl_->SendResult");
608 }
609 APP_LOGI("AbilityThread::SendResult end");
610 }
611
612 /**
613 * @brief Obtains the MIME types of files supported.
614 *
615 * @param uri Indicates the path of the files to obtain.
616 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
617 *
618 * @return Returns the matched MIME types. If there is no match, null is returned.
619 */
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)620 std::vector<std::string> AbilityThread::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
621 {
622 APP_LOGI("AbilityThread::GetFileTypes begin");
623 std::vector<std::string> types;
624 if (abilityImpl_ == nullptr) {
625 APP_LOGE("AbilityThread::GetFileTypes abilityImpl_ is nullptr");
626 return types;
627 }
628
629 APP_LOGI("AbilityThread::GetFileTypes before abilityImpl_->GetFileTypes");
630 types = abilityImpl_->GetFileTypes(uri, mimeTypeFilter);
631 APP_LOGI("AbilityThread::GetFileTypes after abilityImpl_->GetFileTypes");
632 APP_LOGI("AbilityThread::GetFileTypes end");
633 return types;
634 }
635
636 /**
637 * @brief Opens a file in a specified remote path.
638 *
639 * @param uri Indicates the path of the file to open.
640 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
641 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
642 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
643 * or "rwt" for read and write access that truncates any existing file.
644 *
645 * @return Returns the file descriptor.
646 */
OpenFile(const Uri & uri,const std::string & mode)647 int AbilityThread::OpenFile(const Uri &uri, const std::string &mode)
648 {
649 APP_LOGI("AbilityThread::OpenFile begin");
650 int fd = -1;
651 if (abilityImpl_ == nullptr) {
652 APP_LOGE("AbilityThread::OpenFile abilityImpl_ is nullptr");
653 return fd;
654 }
655
656 APP_LOGI("AbilityThread::OpenFile before abilityImpl_->OpenFile");
657 fd = abilityImpl_->OpenFile(uri, mode);
658 APP_LOGI("AbilityThread::OpenFile after abilityImpl_->OpenFile");
659 APP_LOGI("AbilityThread::OpenFile end");
660 return fd;
661 }
662
663 /**
664 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
665 * inside of their .hap.
666 *
667 * @param uri Indicates the path of the file to open.
668 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
669 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
670 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
671 * data, or "rwt" for read and write access that truncates any existing file.
672 *
673 * @return Returns the RawFileDescriptor object containing file descriptor.
674 */
OpenRawFile(const Uri & uri,const std::string & mode)675 int AbilityThread::OpenRawFile(const Uri &uri, const std::string &mode)
676 {
677 APP_LOGI("AbilityThread::OpenRawFile begin");
678 int fd = -1;
679 if (abilityImpl_ == nullptr) {
680 APP_LOGE("AbilityThread::OpenRawFile abilityImpl_ is nullptr");
681 return fd;
682 }
683
684 APP_LOGI("AbilityThread::OpenRawFile before abilityImpl_->OpenRawFile");
685 fd = abilityImpl_->OpenRawFile(uri, mode);
686 APP_LOGI("AbilityThread::OpenRawFile after abilityImpl_->OpenRawFile");
687 APP_LOGI("AbilityThread::OpenRawFile end");
688 return fd;
689 }
690
691 /**
692 * @brief Inserts a single data record into the database.
693 *
694 * @param uri Indicates the path of the data to operate.
695 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
696 *
697 * @return Returns the index of the inserted data record.
698 */
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)699 int AbilityThread::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
700 {
701 APP_LOGI("AbilityThread::Insert begin");
702 int index = -1;
703 if (abilityImpl_ == nullptr) {
704 APP_LOGE("AbilityThread::Insert abilityImpl_ is nullptr");
705 return index;
706 }
707
708 APP_LOGI("AbilityThread::Insert before abilityImpl_->Insert");
709 index = abilityImpl_->Insert(uri, value);
710 APP_LOGI("AbilityThread::Insert after abilityImpl_->Insert");
711 APP_LOGI("AbilityThread::Insert end");
712 return index;
713 }
714
715 /**
716 * @brief Updates data records in the database.
717 *
718 * @param uri Indicates the path of data to update.
719 * @param value Indicates the data to update. This parameter can be null.
720 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
721 *
722 * @return Returns the number of data records updated.
723 */
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)724 int AbilityThread::Update(
725 const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
726 {
727 APP_LOGI("AbilityThread::Update begin");
728 int index = -1;
729 if (abilityImpl_ == nullptr) {
730 APP_LOGE("AbilityThread::Update abilityImpl_ is nullptr");
731 return index;
732 }
733
734 APP_LOGI("AbilityThread::Update before abilityImpl_->Update");
735 index = abilityImpl_->Update(uri, value, predicates);
736 APP_LOGI("AbilityThread::Update after abilityImpl_->Update");
737 APP_LOGI("AbilityThread::Update end");
738 return index;
739 }
740
741 /**
742 * @brief Deletes one or more data records from the database.
743 *
744 * @param uri Indicates the path of the data to operate.
745 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
746 *
747 * @return Returns the number of data records deleted.
748 */
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)749 int AbilityThread::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
750 {
751 APP_LOGI("AbilityThread::Delete begin");
752 int index = -1;
753 if (abilityImpl_ == nullptr) {
754 APP_LOGE("AbilityThread::Delete abilityImpl_ is nullptr");
755 return index;
756 }
757 APP_LOGI("AbilityThread::Delete before abilityImpl_->Delete");
758 index = abilityImpl_->Delete(uri, predicates);
759 APP_LOGI("AbilityThread::Delete after abilityImpl_->Delete");
760 APP_LOGI("AbilityThread::Delete end");
761 return index;
762 }
763
764 /**
765 * @brief Deletes one or more data records from the database.
766 *
767 * @param uri Indicates the path of data to query.
768 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
769 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
770 *
771 * @return Returns the query result.
772 */
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)773 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilityThread::Query(
774 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
775 {
776 APP_LOGI("AbilityThread::Query begin");
777 std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet = nullptr;
778 if (abilityImpl_ == nullptr) {
779 APP_LOGE("AbilityThread::Query abilityImpl_ is nullptr");
780 return resultSet;
781 }
782
783 APP_LOGI("AbilityThread::Query before abilityImpl_->Query");
784 resultSet = abilityImpl_->Query(uri, columns, predicates);
785 APP_LOGI("AbilityThread::Query after abilityImpl_->Query");
786 APP_LOGI("AbilityThread::Query end");
787 return resultSet;
788 }
789
790 /**
791 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
792 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
793 *
794 * @param uri Indicates the URI of the data.
795 *
796 * @return Returns the MIME type that matches the data specified by uri.
797 */
GetType(const Uri & uri)798 std::string AbilityThread::GetType(const Uri &uri)
799 {
800 APP_LOGI("AbilityThread::GetType begin");
801 std::string type;
802 if (abilityImpl_ == nullptr) {
803 APP_LOGE("AbilityThread::GetType abilityImpl_ is nullptr");
804 return type;
805 }
806
807 APP_LOGI("AbilityThread::GetType before abilityImpl_->GetType");
808 type = abilityImpl_->GetType(uri);
809 APP_LOGI("AbilityThread::GetType after abilityImpl_->GetType");
810 APP_LOGI("AbilityThread::GetType end");
811 return type;
812 }
813
814 /**
815 * @brief Reloads data in the database.
816 *
817 * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
818 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
819 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
820 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
821 *
822 * @return Returns true if the data is successfully reloaded; returns false otherwise.
823 */
Reload(const Uri & uri,const PacMap & extras)824 bool AbilityThread::Reload(const Uri &uri, const PacMap &extras)
825 {
826 APP_LOGI("AbilityThread::Reload begin");
827 bool ret = false;
828 if (abilityImpl_ == nullptr) {
829 APP_LOGE("AbilityThread::Reload abilityImpl_ is nullptr");
830 return ret;
831 }
832 APP_LOGI("AbilityThread::Reload before abilityImpl_->Reload");
833 ret = abilityImpl_->Reload(uri, extras);
834 APP_LOGI("AbilityThread::Reload after abilityImpl_->Reload");
835 APP_LOGI("AbilityThread::Reload end");
836 return ret;
837 }
838
839 /**
840 * @brief Inserts multiple data records into the database.
841 *
842 * @param uri Indicates the path of the data to operate.
843 * @param values Indicates the data records to insert.
844 *
845 * @return Returns the number of data records inserted.
846 */
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)847 int AbilityThread::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
848 {
849 APP_LOGI("AbilityThread::BatchInsert begin");
850 int ret = -1;
851 if (abilityImpl_ == nullptr) {
852 APP_LOGE("AbilityThread::BatchInsert abilityImpl_ is nullptr");
853 return ret;
854 }
855
856 APP_LOGI("AbilityThread::BatchInsert before abilityImpl_->BatchInsert");
857 ret = abilityImpl_->BatchInsert(uri, values);
858 APP_LOGI("AbilityThread::BatchInsert after abilityImpl_->BatchInsert");
859 APP_LOGI("AbilityThread::BatchInsert end");
860 return ret;
861 }
862
NotifyMultiWinModeChanged(int32_t winModeKey,bool flag)863 void AbilityThread::NotifyMultiWinModeChanged(int32_t winModeKey, bool flag)
864 {
865 APP_LOGI("NotifyMultiWinModeChanged.key:%{public}d,flag:%{public}d", winModeKey, flag);
866 sptr<Window> window = currentAbility_->GetWindow();
867 if (window == nullptr) {
868 APP_LOGE("NotifyMultiWinModeChanged window == nullptr");
869 return;
870 }
871
872 return;
873 }
874
NotifyTopActiveAbilityChanged(bool flag)875 void AbilityThread::NotifyTopActiveAbilityChanged(bool flag)
876 {
877 APP_LOGI("NotifyTopActiveAbilityChanged,flag:%{public}d", flag);
878 sptr<Window> window = currentAbility_->GetWindow();
879 if (window == nullptr) {
880 APP_LOGE("NotifyMultiWinModeChanged window == nullptr");
881 return;
882 }
883 if (flag) {
884 window->SwitchTop();
885 }
886 return;
887 }
888
889 /**
890 * @description: Attach The ability thread to the main process.
891 * @param application Indicates the main process.
892 * @param abilityRecord Indicates the abilityRecord.
893 * @param mainRunner The runner which main_thread holds.
894 */
AbilityThreadMain(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & abilityRecord,const std::shared_ptr<EventRunner> & mainRunner)895 void AbilityThread::AbilityThreadMain(std::shared_ptr<OHOSApplication> &application,
896 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<EventRunner> &mainRunner)
897 {
898 APP_LOGI("AbilityThread::AbilityThreadMain begin");
899 sptr<AbilityThread> thread = sptr<AbilityThread>(new (std::nothrow) AbilityThread());
900 if (thread == nullptr) {
901 APP_LOGE("AbilityThread::AbilityThreadMain failed,thread is nullptr");
902 return;
903 }
904 thread->Attach(application, abilityRecord, mainRunner);
905 APP_LOGI("AbilityThread::AbilityThreadMain end");
906 }
907
908 /**
909 * @description: Attach The ability thread to the main process.
910 * @param application Indicates the main process.
911 * @param abilityRecord Indicates the abilityRecord.
912 */
AbilityThreadMain(std::shared_ptr<OHOSApplication> & application,const std::shared_ptr<AbilityLocalRecord> & abilityRecord)913 void AbilityThread::AbilityThreadMain(
914 std::shared_ptr<OHOSApplication> &application, const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
915 {
916 APP_LOGI("AbilityThread::AbilityThreadMain begin");
917 sptr<AbilityThread> thread = sptr<AbilityThread>(new (std::nothrow) AbilityThread());
918 if (thread == nullptr) {
919 APP_LOGE("AbilityThread::AbilityThreadMain failed,thread is nullptr");
920 return;
921 }
922 thread->Attach(application, abilityRecord);
923 APP_LOGI("AbilityThread::AbilityThreadMain end");
924 }
925 /**
926 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
927 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
928 * context has changed. If you implement URI normalization for a Data ability, you must also implement
929 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
930 * method that is called on the Data ability must require normalization verification and denormalization. The default
931 * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
932 *
933 * @param uri Indicates the Uri object to normalize.
934 *
935 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
936 */
NormalizeUri(const Uri & uri)937 Uri AbilityThread::NormalizeUri(const Uri &uri)
938 {
939 APP_LOGI("AbilityThread::NormalizeUri begin");
940 Uri urivalue("");
941 if (abilityImpl_ == nullptr) {
942 APP_LOGE("DataAbilityHelper::normalizeUri failed dataAbility == nullptr");
943 return urivalue;
944 }
945
946 APP_LOGI("AbilityThread::NormalizeUri before abilityImpl_->NormalizeUri");
947 urivalue = abilityImpl_->NormalizeUri(uri);
948 APP_LOGI("AbilityThread::NormalizeUri after abilityImpl_->NormalizeUri");
949 APP_LOGI("AbilityThread::NormalizeUri end");
950 return urivalue;
951 }
952
953 /**
954 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
955 * The default implementation of this method returns the original URI passed to it.
956 *
957 * @param uri uri Indicates the Uri object to denormalize.
958 *
959 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed to
960 * this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found in the
961 * current environment.
962 */
DenormalizeUri(const Uri & uri)963 Uri AbilityThread::DenormalizeUri(const Uri &uri)
964 {
965 APP_LOGI("AbilityThread::DenormalizeUri begin");
966 Uri urivalue("");
967 if (abilityImpl_ == nullptr) {
968 APP_LOGE("DataAbilityHelper::denormalizeUri failed dataAbility == nullptr");
969 return urivalue;
970 }
971
972 APP_LOGI("AbilityThread::DenormalizeUri before abilityImpl_->DenormalizeUri");
973 urivalue = abilityImpl_->DenormalizeUri(uri);
974 APP_LOGI("AbilityThread::DenormalizeUri after abilityImpl_->DenormalizeUri");
975 APP_LOGI("AbilityThread::DenormalizeUri end");
976 return urivalue;
977 }
978
979 /**
980 * @brief Registers an observer to DataObsMgr specified by the given Uri.
981 *
982 * @param uri, Indicates the path of the data to operate.
983 * @param dataObserver, Indicates the IDataAbilityObserver object.
984 */
HandleRegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)985 bool AbilityThread::HandleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
986 {
987 auto obsMgrClient = DataObsMgrClient::GetInstance();
988 if (obsMgrClient == nullptr) {
989 APP_LOGE("%{public}s obsMgrClient is nullptr", __func__);
990 return false;
991 }
992
993 ErrCode ret = obsMgrClient->RegisterObserver(uri, dataObserver);
994 if (ret != ERR_OK) {
995 APP_LOGE("%{public}s obsMgrClient->RegisterObserver error return %{public}d", __func__, ret);
996 return false;
997 }
998 return true;
999 }
1000
1001 /**
1002 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
1003 *
1004 * @param uri, Indicates the path of the data to operate.
1005 * @param dataObserver, Indicates the IDataAbilityObserver object.
1006 */
HandleUnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)1007 bool AbilityThread::HandleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1008 {
1009 auto obsMgrClient = DataObsMgrClient::GetInstance();
1010 if (obsMgrClient == nullptr) {
1011 APP_LOGE("%{public}s obsMgrClient is nullptr", __func__);
1012 return false;
1013 }
1014
1015 ErrCode ret = obsMgrClient->UnregisterObserver(uri, dataObserver);
1016 if (ret != ERR_OK) {
1017 APP_LOGE("%{public}s obsMgrClient->UnregisterObserver error return %{public}d", __func__, ret);
1018 return false;
1019 }
1020 return true;
1021 }
1022
1023 /**
1024 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
1025 *
1026 * @param uri, Indicates the path of the data to operate.
1027 */
HandleNotifyChange(const Uri & uri)1028 bool AbilityThread::HandleNotifyChange(const Uri &uri)
1029 {
1030 auto obsMgrClient = DataObsMgrClient::GetInstance();
1031 if (obsMgrClient == nullptr) {
1032 APP_LOGE("%{public}s obsMgrClient is nullptr", __func__);
1033 return false;
1034 }
1035
1036 ErrCode ret = obsMgrClient->NotifyChange(uri);
1037 if (ret != ERR_OK) {
1038 APP_LOGE("%{public}s obsMgrClient->NotifyChange error return %{public}d", __func__, ret);
1039 return false;
1040 }
1041 return true;
1042 }
1043
1044 /**
1045 * @brief Access authority verification.
1046 *
1047 * @return Returns true on success, others on failure.
1048 */
CheckObsPermission()1049 bool AbilityThread::CheckObsPermission()
1050 {
1051 APP_LOGI("%{public}s CheckObsPermission() run Permission Checkout", __func__);
1052 return true;
1053 }
1054
1055 /**
1056 * @brief Registers an observer to DataObsMgr specified by the given Uri.
1057 *
1058 * @param uri, Indicates the path of the data to operate.
1059 * @param dataObserver, Indicates the IDataAbilityObserver object.
1060 */
ScheduleRegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)1061 bool AbilityThread::ScheduleRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1062 {
1063 APP_LOGI("%{public}s called", __func__);
1064
1065 if (!CheckObsPermission()) {
1066 APP_LOGE("%{public}s CheckObsPermission() return false", __func__);
1067 return false;
1068 }
1069
1070 auto task = [abilityThread = this, uri, dataObserver]() {
1071 abilityThread->HandleRegisterObserver(uri, dataObserver);
1072 };
1073
1074 if (abilityHandler_ == nullptr) {
1075 APP_LOGE("AbilityThread::ScheduleRegisterObserver abilityHandler_ == nullptr");
1076 return false;
1077 }
1078
1079 bool ret = abilityHandler_->PostTask(task);
1080 if (!ret) {
1081 APP_LOGE("AbilityThread::ScheduleRegisterObserver PostTask error");
1082 }
1083 return ret;
1084 }
1085
1086 /**
1087 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
1088 *
1089 * @param uri, Indicates the path of the data to operate.
1090 * @param dataObserver, Indicates the IDataAbilityObserver object.
1091 */
ScheduleUnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)1092 bool AbilityThread::ScheduleUnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
1093 {
1094 APP_LOGI("%{public}s called", __func__);
1095
1096 if (!CheckObsPermission()) {
1097 APP_LOGE("%{public}s CheckObsPermission() return false", __func__);
1098 return false;
1099 }
1100
1101 auto task = [abilityThread = this, uri, dataObserver]() {
1102 abilityThread->HandleUnregisterObserver(uri, dataObserver);
1103 };
1104
1105 if (abilityHandler_ == nullptr) {
1106 APP_LOGE("AbilityThread::ScheduleUnregisterObserver abilityHandler_ == nullptr");
1107 return false;
1108 }
1109
1110 bool ret = abilityHandler_->PostTask(task);
1111 if (!ret) {
1112 APP_LOGE("AbilityThread::ScheduleUnregisterObserver PostTask error");
1113 }
1114 return ret;
1115 }
1116
1117 /**
1118 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
1119 *
1120 * @param uri, Indicates the path of the data to operate.
1121 */
ScheduleNotifyChange(const Uri & uri)1122 bool AbilityThread::ScheduleNotifyChange(const Uri &uri)
1123 {
1124 APP_LOGI("%{public}s called", __func__);
1125
1126 if (!CheckObsPermission()) {
1127 APP_LOGE("%{public}s CheckObsPermission() return false", __func__);
1128 return false;
1129 }
1130
1131 auto task = [abilityThread = this, uri]() { abilityThread->HandleNotifyChange(uri); };
1132
1133 if (abilityHandler_ == nullptr) {
1134 APP_LOGE("AbilityThread::ScheduleNotifyChange abilityHandler_ == nullptr");
1135 return false;
1136 }
1137
1138 bool ret = abilityHandler_->PostTask(task);
1139 if (!ret) {
1140 APP_LOGE("AbilityThread::ScheduleNotifyChange PostTask error");
1141 }
1142 return ret;
1143 }
1144
ExecuteBatch(const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)1145 std::vector<std::shared_ptr<DataAbilityResult>> AbilityThread::ExecuteBatch(
1146 const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
1147 {
1148
1149 APP_LOGI("AbilityThread::ExecuteBatch start");
1150 std::vector<std::shared_ptr<DataAbilityResult>> results;
1151 if (abilityImpl_ == nullptr) {
1152 APP_LOGE("AbilityThread::ExecuteBatch abilityImpl_ is nullptr");
1153 results.clear();
1154 return results;
1155 }
1156 APP_LOGI("AbilityThread::ExecuteBatch before abilityImpl_->ExecuteBatch");
1157 results = abilityImpl_->ExecuteBatch(operations);
1158 APP_LOGI("AbilityThread::ExecuteBatch after abilityImpl_->ExecuteBatch");
1159 APP_LOGI("AbilityThread::ExecuteBatch end");
1160 return results;
1161 }
1162
1163 } // namespace AppExecFwk
1164 } // namespace OHOS