• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "data_ability_manager.h"
17 
18 #include <chrono>
19 #include <thread>
20 
21 #include "ability_manager_service.h"
22 #include "ability_util.h"
23 #include "connection_state_manager.h"
24 #include "hilog_wrapper.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 using namespace std::chrono;
29 using namespace std::placeholders;
30 
31 namespace {
32 constexpr bool DEBUG_ENABLED = false;
33 constexpr system_clock::duration DATA_ABILITY_LOAD_TIMEOUT = 11000ms;
34 }  // namespace
35 
DataAbilityManager()36 DataAbilityManager::DataAbilityManager()
37 {
38     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
39 }
40 
~DataAbilityManager()41 DataAbilityManager::~DataAbilityManager()
42 {
43     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
44 }
45 
Acquire(const AbilityRequest & abilityRequest,bool tryBind,const sptr<IRemoteObject> & client,bool isNotHap)46 sptr<IAbilityScheduler> DataAbilityManager::Acquire(
47     const AbilityRequest &abilityRequest, bool tryBind, const sptr<IRemoteObject> &client, bool isNotHap)
48 {
49     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
50 
51     if (abilityRequest.abilityInfo.type != AppExecFwk::AbilityType::DATA) {
52         HILOG_ERROR("Data ability manager acquire: not a data ability.");
53         return nullptr;
54     }
55 
56     if (abilityRequest.abilityInfo.bundleName.empty() || abilityRequest.abilityInfo.name.empty()) {
57         HILOG_ERROR("Data ability manager acquire: invalid name.");
58         return nullptr;
59     }
60 
61     std::shared_ptr<AbilityRecord> clientAbilityRecord;
62     const std::string dataAbilityName(abilityRequest.abilityInfo.bundleName + '.' + abilityRequest.abilityInfo.name);
63 
64     if (client && !isNotHap) {
65         clientAbilityRecord = Token::GetAbilityRecordByToken(client);
66         if (!clientAbilityRecord) {
67             HILOG_ERROR("Data ability manager acquire: invalid client token.");
68             return nullptr;
69         }
70         HILOG_INFO("Ability '%{public}s' acquiring data ability '%{public}s'...",
71             clientAbilityRecord->GetAbilityInfo().name.c_str(), dataAbilityName.c_str());
72     } else {
73         HILOG_INFO("Loading data ability '%{public}s'...", dataAbilityName.c_str());
74     }
75 
76     std::lock_guard<std::mutex> locker(mutex_);
77 
78     if (DEBUG_ENABLED) {
79         DumpLocked(__func__, __LINE__);
80     }
81 
82     DataAbilityRecordPtr dataAbilityRecord;
83 
84     auto it = dataAbilityRecordsLoaded_.find(dataAbilityName);
85     if (it == dataAbilityRecordsLoaded_.end()) {
86         HILOG_DEBUG("Acquiring data ability is not existed, loading...");
87         dataAbilityRecord = LoadLocked(dataAbilityName, abilityRequest);
88     } else {
89         HILOG_DEBUG("Acquiring data ability is existed .");
90         dataAbilityRecord = it->second;
91     }
92 
93     if (!dataAbilityRecord) {
94         HILOG_ERROR("Failed to load data ability '%{public}s'.", dataAbilityName.c_str());
95         return nullptr;
96     }
97 
98     auto scheduler = dataAbilityRecord->GetScheduler();
99     if (!scheduler) {
100         if (DEBUG_ENABLED) {
101             HILOG_ERROR("BUG: data ability '%{public}s' is not loaded, removing it...", dataAbilityName.c_str());
102         }
103         auto it = dataAbilityRecordsLoaded_.find(dataAbilityName);
104         if (it != dataAbilityRecordsLoaded_.end()) {
105             dataAbilityRecordsLoaded_.erase(it);
106         }
107         return nullptr;
108     }
109 
110     if (client) {
111         dataAbilityRecord->AddClient(client, tryBind, isNotHap);
112     }
113 
114     if (DEBUG_ENABLED) {
115         DumpLocked(__func__, __LINE__);
116     }
117 
118     ReportDataAbilityAcquired(client, isNotHap, dataAbilityRecord);
119 
120     return scheduler;
121 }
122 
Release(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & client,bool isNotHap)123 int DataAbilityManager::Release(
124     const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &client, bool isNotHap)
125 {
126     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
127 
128     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
129     CHECK_POINTER_AND_RETURN(client, ERR_NULL_OBJECT);
130 
131     std::lock_guard<std::mutex> locker(mutex_);
132 
133     if (DEBUG_ENABLED) {
134         DumpLocked(__func__, __LINE__);
135     }
136 
137     DataAbilityRecordPtr dataAbilityRecord;
138 
139     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
140         if (it->second && it->second->GetScheduler() &&
141             it->second->GetScheduler()->AsObject() == scheduler->AsObject()) {
142             dataAbilityRecord = it->second;
143             HILOG_INFO("Releasing data ability '%{public}s'...", it->first.c_str());
144             break;
145         }
146     }
147 
148     if (!dataAbilityRecord) {
149         HILOG_ERROR("Releasing not existed data ability.");
150         return ERR_UNKNOWN_OBJECT;
151     }
152 
153     auto abilityRecord = dataAbilityRecord->GetAbilityRecord();
154     CHECK_POINTER_AND_RETURN(abilityRecord, ERR_UNKNOWN_OBJECT);
155     auto abilityMs = DelayedSingleton<AbilityManagerService>::GetInstance();
156     CHECK_POINTER_AND_RETURN(abilityMs, GET_ABILITY_SERVICE_FAILED);
157     int result = abilityMs->JudgeAbilityVisibleControl(abilityRecord->GetAbilityInfo());
158     if (result != ERR_OK) {
159         HILOG_ERROR("%{public}s JudgeAbilityVisibleControl error.", __func__);
160         return result;
161     }
162 
163     if (dataAbilityRecord->GetClientCount(client) == 0) {
164         HILOG_ERROR("Release data ability with wrong client.");
165         return ERR_UNKNOWN_OBJECT;
166     }
167 
168     dataAbilityRecord->RemoveClient(client, isNotHap);
169 
170     if (DEBUG_ENABLED) {
171         DumpLocked(__func__, __LINE__);
172     }
173 
174     ReportDataAbilityReleased(client, isNotHap, dataAbilityRecord);
175 
176     return ERR_OK;
177 }
178 
ContainsDataAbility(const sptr<IAbilityScheduler> & scheduler)179 bool DataAbilityManager::ContainsDataAbility(const sptr<IAbilityScheduler> &scheduler)
180 {
181     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
182 
183     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
184 
185     std::lock_guard<std::mutex> locker(mutex_);
186     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
187         if (it->second && it->second->GetScheduler() &&
188             it->second->GetScheduler()->AsObject() == scheduler->AsObject()) {
189             return true;
190         }
191     }
192 
193     return false;
194 }
195 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)196 int DataAbilityManager::AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token)
197 {
198     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
199 
200     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
201     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
202 
203     std::lock_guard<std::mutex> locker(mutex_);
204 
205     if (DEBUG_ENABLED) {
206         DumpLocked(__func__, __LINE__);
207     }
208 
209     HILOG_INFO("Attaching data ability...");
210 
211     auto record = Token::GetAbilityRecordByToken(token);
212     std::string abilityName = "";
213     if (record != nullptr) {
214         abilityName = record->GetAbilityInfo().name;
215     }
216 
217     DataAbilityRecordPtr dataAbilityRecord;
218     auto it = dataAbilityRecordsLoading_.begin();
219     for (; it != dataAbilityRecordsLoading_.end(); ++it) {
220         if (it->second && it->second->GetToken() == token) {
221             dataAbilityRecord = it->second;
222             break;
223         }
224     }
225 
226     if (!dataAbilityRecord) {
227         HILOG_ERROR("Attaching data ability '%{public}s' is not in loading state.", abilityName.c_str());
228         return ERR_UNKNOWN_OBJECT;
229     }
230 
231     if (DEBUG_ENABLED && dataAbilityRecord->GetClientCount() > 0) {
232         HILOG_ERROR("BUG: Attaching data ability '%{public}s' has clients.", abilityName.c_str());
233     }
234 
235     if (DEBUG_ENABLED && dataAbilityRecord->GetScheduler()) {
236         HILOG_ERROR("BUG: Attaching data ability '%{public}s' has ready.", abilityName.c_str());
237     }
238 
239     if (DEBUG_ENABLED && dataAbilityRecordsLoaded_.count(it->first) != 0) {
240         HILOG_ERROR("BUG: The attaching data ability '%{public}s' has already existed.", abilityName.c_str());
241     }
242 
243     return dataAbilityRecord->Attach(scheduler);
244 }
245 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state)246 int DataAbilityManager::AbilityTransitionDone(const sptr<IRemoteObject> &token, int state)
247 {
248     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
249 
250     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
251 
252     std::lock_guard<std::mutex> locker(mutex_);
253 
254     if (DEBUG_ENABLED) {
255         DumpLocked(__func__, __LINE__);
256     }
257 
258     HILOG_INFO("Handling data ability transition done %{public}d...", state);
259 
260     DataAbilityRecordPtrMap::iterator it;
261     DataAbilityRecordPtr dataAbilityRecord;
262     auto record = Token::GetAbilityRecordByToken(token);
263     std::string abilityName = "";
264     if (record != nullptr) {
265         abilityName = record->GetAbilityInfo().name;
266     }
267     for (it = dataAbilityRecordsLoading_.begin(); it != dataAbilityRecordsLoading_.end(); ++it) {
268         if (it->second && it->second->GetToken() == token) {
269             dataAbilityRecord = it->second;
270             break;
271         }
272     }
273     if (!dataAbilityRecord) {
274         HILOG_ERROR("Attaching data ability '%{public}s' is not existed.", abilityName.c_str());
275         return ERR_UNKNOWN_OBJECT;
276     }
277 
278     int ret = dataAbilityRecord->OnTransitionDone(state);
279     if (ret == ERR_OK) {
280         dataAbilityRecordsLoaded_[it->first] = dataAbilityRecord;
281         dataAbilityRecordsLoading_.erase(it);
282     }
283 
284     return ret;
285 }
286 
OnAbilityRequestDone(const sptr<IRemoteObject> & token,const int32_t state)287 void DataAbilityManager::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state)
288 {
289     /* Do nothing now. */
290 }
291 
OnAbilityDied(const std::shared_ptr<AbilityRecord> & abilityRecord)292 void DataAbilityManager::OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord)
293 {
294     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
295     CHECK_POINTER(abilityRecord);
296 
297     {
298         std::lock_guard<std::mutex> locker(mutex_);
299         if (DEBUG_ENABLED) {
300             DumpLocked(__func__, __LINE__);
301         }
302         if (abilityRecord->GetAbilityInfo().type == AppExecFwk::AbilityType::DATA) {
303             // If 'abilityRecord' is a data ability server, trying to remove it from 'dataAbilityRecords_'.
304             for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end();) {
305                 if (it->second && it->second->GetAbilityRecord() == abilityRecord) {
306                     DelayedSingleton<ConnectionStateManager>::GetInstance()->HandleDataAbilityDied(it->second);
307                     it->second->KillBoundClientProcesses();
308                     HILOG_DEBUG("Removing died data ability record...");
309                     it = dataAbilityRecordsLoaded_.erase(it);
310                     break;
311                 } else {
312                     ++it;
313                 }
314             }
315         }
316         if (DEBUG_ENABLED) {
317             DumpLocked(__func__, __LINE__);
318         }
319         // If 'abilityRecord' is a data ability client, tring to remove it from all servers.
320         for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
321             if (it->second) {
322                 it->second->RemoveClients(abilityRecord);
323             }
324         }
325         if (DEBUG_ENABLED) {
326             DumpLocked(__func__, __LINE__);
327         }
328     }
329 
330     RestartDataAbility(abilityRecord);
331 }
332 
OnAppStateChanged(const AppInfo & info)333 void DataAbilityManager::OnAppStateChanged(const AppInfo &info)
334 {
335     std::lock_guard<std::mutex> locker(mutex_);
336 
337     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
338         if (!it->second) {
339             continue;
340         }
341         auto abilityRecord = it->second->GetAbilityRecord();
342         if (abilityRecord && (info.processName == abilityRecord->GetAbilityInfo().process ||
343                                  info.processName == abilityRecord->GetApplicationInfo().bundleName)) {
344             auto appName = abilityRecord->GetApplicationInfo().name;
345             auto uid = abilityRecord->GetAbilityInfo().applicationInfo.uid;
346             auto isExist = [&appName, &uid](
347                                const AppData &appData) { return appData.appName == appName && appData.uid == uid; };
348             auto iter = std::find_if(info.appData.begin(), info.appData.end(), isExist);
349             if (iter != info.appData.end()) {
350                 abilityRecord->SetAppState(info.state);
351             }
352         }
353     }
354 
355     for (auto it = dataAbilityRecordsLoading_.begin(); it != dataAbilityRecordsLoading_.end(); ++it) {
356         if (!it->second) {
357             continue;
358         }
359         auto abilityRecord = it->second->GetAbilityRecord();
360         if (abilityRecord && (info.processName == abilityRecord->GetAbilityInfo().process ||
361                                  info.processName == abilityRecord->GetApplicationInfo().bundleName)) {
362             auto appName = abilityRecord->GetApplicationInfo().name;
363             auto uid = abilityRecord->GetAbilityInfo().applicationInfo.uid;
364             auto isExist = [&appName, &uid](
365                                const AppData &appData) { return appData.appName == appName && appData.uid == uid; };
366             auto iter = std::find_if(info.appData.begin(), info.appData.end(), isExist);
367             if (iter != info.appData.end()) {
368                 abilityRecord->SetAppState(info.state);
369             }
370         }
371     }
372 }
373 
GetAbilityRecordById(int64_t id)374 std::shared_ptr<AbilityRecord> DataAbilityManager::GetAbilityRecordById(int64_t id)
375 {
376     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
377 
378     std::lock_guard<std::mutex> locker(mutex_);
379 
380     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
381         if (!it->second) {
382             continue;
383         }
384         auto abilityRecord = it->second->GetAbilityRecord();
385         if (abilityRecord->GetRecordId() == id) {
386             return abilityRecord;
387         }
388     }
389 
390     return nullptr;
391 }
392 
GetAbilityRecordByToken(const sptr<IRemoteObject> & token)393 std::shared_ptr<AbilityRecord> DataAbilityManager::GetAbilityRecordByToken(const sptr<IRemoteObject> &token)
394 {
395     HILOG_INFO("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
396 
397     CHECK_POINTER_AND_RETURN(token, nullptr);
398 
399     std::lock_guard<std::mutex> locker(mutex_);
400     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
401         if (!it->second) {
402             continue;
403         }
404         auto abilityRecord = it->second->GetAbilityRecord();
405         if (abilityRecord == Token::GetAbilityRecordByToken(token)) {
406             return abilityRecord;
407         }
408     }
409     for (auto it = dataAbilityRecordsLoading_.begin(); it != dataAbilityRecordsLoading_.end(); ++it) {
410         if (!it->second) {
411             continue;
412         }
413         auto abilityRecord = it->second->GetAbilityRecord();
414         if (abilityRecord == Token::GetAbilityRecordByToken(token)) {
415             return abilityRecord;
416         }
417     }
418     return nullptr;
419 }
420 
GetAbilityRecordByScheduler(const sptr<IAbilityScheduler> & scheduler)421 std::shared_ptr<AbilityRecord> DataAbilityManager::GetAbilityRecordByScheduler(const sptr<IAbilityScheduler> &scheduler)
422 {
423     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
424 
425     CHECK_POINTER_AND_RETURN(scheduler, nullptr);
426 
427     std::lock_guard<std::mutex> locker(mutex_);
428 
429     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
430         if (it->second && it->second->GetScheduler() &&
431             it->second->GetScheduler()->AsObject() == scheduler->AsObject()) {
432             return it->second->GetAbilityRecord();
433         }
434     }
435 
436     return nullptr;
437 }
438 
Dump(const char * func,int line)439 void DataAbilityManager::Dump(const char *func, int line)
440 {
441     HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__);
442 
443     std::lock_guard<std::mutex> locker(mutex_);
444 
445     DumpLocked(func, line);
446 }
447 
LoadLocked(const std::string & name,const AbilityRequest & req)448 DataAbilityManager::DataAbilityRecordPtr DataAbilityManager::LoadLocked(
449     const std::string &name, const AbilityRequest &req)
450 {
451     HILOG_DEBUG("%{public}s(%{public}d) name '%{public}s'", __PRETTY_FUNCTION__, __LINE__, name.c_str());
452 
453     DataAbilityRecordPtr dataAbilityRecord;
454 
455     auto it = dataAbilityRecordsLoading_.find(name);
456     if (it == dataAbilityRecordsLoading_.end()) {
457         HILOG_INFO("Acquiring data ability is not in loading, trying to load it...");
458 
459         dataAbilityRecord = std::make_shared<DataAbilityRecord>(req);
460         // Start data ability loading process asynchronously.
461         int startResult = dataAbilityRecord->StartLoading();
462         if (startResult != ERR_OK) {
463             HILOG_ERROR("Failed to load data ability %{public}d", startResult);
464             return nullptr;
465         }
466 
467         auto insertResult = dataAbilityRecordsLoading_.insert({name, dataAbilityRecord});
468         if (!insertResult.second) {
469             HILOG_ERROR("Failed to insert data ability to loading map.");
470             return nullptr;
471         }
472     } else {
473         HILOG_INFO("Acquired data ability is loading...");
474         dataAbilityRecord = it->second;
475     }
476 
477     if (!dataAbilityRecord) {
478         HILOG_ERROR("Failed to load data ability '%{public}s'.", name.c_str());
479         return nullptr;
480     }
481 
482     HILOG_INFO("Waiting for data ability loaded...");
483 
484     // Waiting for data ability loaded.
485     int ret = dataAbilityRecord->WaitForLoaded(mutex_, DATA_ABILITY_LOAD_TIMEOUT);
486     if (ret != ERR_OK) {
487         HILOG_ERROR("Wait for data ability failed %{public}d.", ret);
488         it = dataAbilityRecordsLoading_.find(name);
489         if (it != dataAbilityRecordsLoading_.end()) {
490             dataAbilityRecordsLoading_.erase(it);
491         }
492         DelayedSingleton<AppScheduler>::GetInstance()->AttachTimeOut(dataAbilityRecord->GetToken());
493         return nullptr;
494     }
495 
496     return dataAbilityRecord;
497 }
498 
DumpLocked(const char * func,int line)499 void DataAbilityManager::DumpLocked(const char *func, int line)
500 {
501     if (func && line >= 0) {
502         HILOG_INFO("Data ability manager dump at %{public}s(%{public}d)", func, line);
503     } else {
504         HILOG_INFO("Data ability manager dump");
505     }
506 
507     HILOG_INFO("Available data ability count: %{public}zu", dataAbilityRecordsLoaded_.size());
508 
509     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
510         HILOG_INFO("'%{public}s':", it->first.c_str());
511         if (it->second) {
512             it->second->Dump();
513         }
514     }
515 
516     HILOG_INFO("Loading data ability count: %{public}zu", dataAbilityRecordsLoading_.size());
517 
518     for (auto it = dataAbilityRecordsLoading_.begin(); it != dataAbilityRecordsLoading_.end(); ++it) {
519         HILOG_INFO("'%{public}s':", it->first.c_str());
520         if (it->second) {
521             it->second->Dump();
522         }
523     }
524 }
525 
DumpState(std::vector<std::string> & info,const std::string & args) const526 void DataAbilityManager::DumpState(std::vector<std::string> &info, const std::string &args) const
527 {
528     if (!args.empty()) {
529         auto it = std::find_if(dataAbilityRecordsLoaded_.begin(),
530             dataAbilityRecordsLoaded_.end(),
531             [&args](const auto &dataAbilityRecord) { return dataAbilityRecord.first.compare(args) == 0; });
532         if (it != dataAbilityRecordsLoaded_.end()) {
533             info.emplace_back("AbilityName [ " + it->first + " ]");
534             if (it->second) {
535                 it->second->Dump(info);
536             }
537         } else {
538             info.emplace_back(args + ": Nothing to dump.");
539         }
540     } else {
541         info.emplace_back("dataAbilityRecords:");
542         for (auto &&dataAbilityRecord : dataAbilityRecordsLoaded_) {
543             info.emplace_back("  uri [" + dataAbilityRecord.first + "]");
544             if (dataAbilityRecord.second) {
545                 dataAbilityRecord.second->Dump(info);
546             }
547         }
548     }
549 }
550 
DumpSysState(std::vector<std::string> & info,bool isClient,const std::string & args) const551 void DataAbilityManager::DumpSysState(std::vector<std::string> &info, bool isClient, const std::string &args) const
552 {
553     if (!args.empty()) {
554         auto it = std::find_if(dataAbilityRecordsLoaded_.begin(),
555             dataAbilityRecordsLoaded_.end(),
556             [&args](const auto &dataAbilityRecord) { return dataAbilityRecord.first.compare(args) == 0; });
557         if (it != dataAbilityRecordsLoaded_.end()) {
558             info.emplace_back("AbilityName [ " + it->first + " ]");
559             if (it->second) {
560                 it->second->Dump(info);
561             }
562             // add dump client info
563             if (isClient && it->second && it->second->GetScheduler() &&
564                 it->second->GetAbilityRecord() && it->second->GetAbilityRecord()->IsReady()) {
565                 std::vector<std::string> params;
566                 it->second->GetScheduler()->DumpAbilityInfo(params, info);
567                 AppExecFwk::Configuration config;
568                 if (DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config) == ERR_OK) {
569                     info.emplace_back("          configuration: " + config.GetName());
570                 }
571             }
572         } else {
573             info.emplace_back(args + ": Nothing to dump.");
574         }
575     } else {
576         info.emplace_back("  dataAbilityRecords:");
577         for (auto &&dataAbilityRecord : dataAbilityRecordsLoaded_) {
578             info.emplace_back("    uri [" + dataAbilityRecord.first + "]");
579             if (dataAbilityRecord.second) {
580                 dataAbilityRecord.second->Dump(info);
581                 dataAbilityRecord.second->GetScheduler();
582             }
583             // add dump client info
584             if (isClient && dataAbilityRecord.second && dataAbilityRecord.second->GetScheduler() &&
585                 dataAbilityRecord.second->GetAbilityRecord() &&
586                 dataAbilityRecord.second->GetAbilityRecord()->IsReady()) {
587                 std::vector<std::string> params;
588                 dataAbilityRecord.second->GetScheduler()->DumpAbilityInfo(params, info);
589                 AppExecFwk::Configuration config;
590                 if (DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config) == ERR_OK) {
591                     info.emplace_back("          configuration: " + config.GetName());
592                 }
593             }
594         }
595     }
596     return;
597 }
598 
GetAbilityRunningInfos(std::vector<AbilityRunningInfo> & info,bool isPerm)599 void DataAbilityManager::GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm)
600 {
601     HILOG_INFO("Get ability running infos");
602     std::lock_guard<std::mutex> locker(mutex_);
603 
604     auto queryInfo = [&info, isPerm](DataAbilityRecordPtrMap::reference data) {
605         auto dataAbilityRecord = data.second;
606         if (!dataAbilityRecord) {
607             return;
608         }
609 
610         auto abilityRecord = dataAbilityRecord->GetAbilityRecord();
611         if (!abilityRecord) {
612             return;
613         }
614 
615         if (isPerm) {
616             DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord);
617         } else {
618             auto callingTokenId = IPCSkeleton::GetCallingTokenID();
619             auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId;
620             if (callingTokenId == tokenID) {
621                 DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord);
622             }
623         }
624     };
625 
626     std::for_each(dataAbilityRecordsLoading_.begin(), dataAbilityRecordsLoading_.end(), queryInfo);
627     std::for_each(dataAbilityRecordsLoaded_.begin(), dataAbilityRecordsLoaded_.end(), queryInfo);
628 }
629 
RestartDataAbility(const std::shared_ptr<AbilityRecord> & abilityRecord)630 void DataAbilityManager::RestartDataAbility(const std::shared_ptr<AbilityRecord> &abilityRecord)
631 {
632     // restart data ability if necessary
633     auto bms = AbilityUtil::GetBundleManager();
634     CHECK_POINTER(bms);
635     std::vector<AppExecFwk::BundleInfo> bundleInfos;
636     bool getBundleInfos = bms->GetBundleInfos(OHOS::AppExecFwk::GET_BUNDLE_DEFAULT, bundleInfos, USER_ID_NO_HEAD);
637     if (!getBundleInfos) {
638         HILOG_ERROR("Handle ability died task, get bundle infos failed");
639         return;
640     }
641 
642     for (size_t i = 0; i < bundleInfos.size(); i++) {
643         if (!bundleInfos[i].isKeepAlive || bundleInfos[i].applicationInfo.process.empty()) {
644             continue;
645         }
646         for (auto hapModuleInfo : bundleInfos[i].hapModuleInfos) {
647             if (hapModuleInfo.isModuleJson) {
648                 // new application model, it cannot be a data ability
649                 continue;
650             }
651             // old application model, it maybe a data ability
652             std::string mainElement = hapModuleInfo.mainAbility;
653             if (abilityRecord->GetAbilityInfo().name != mainElement ||
654                 abilityRecord->GetAbilityInfo().process != bundleInfos[i].applicationInfo.process) {
655                 continue;
656             }
657             std::string uriStr;
658             bool getDataAbilityUri = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
659                 hapModuleInfo.abilityInfos, mainElement, uriStr);
660             if (getDataAbilityUri) {
661                 HILOG_INFO("restart data ability: %{public}s, uri: %{public}s",
662                     abilityRecord->GetAbilityInfo().name.c_str(), uriStr.c_str());
663                 Uri uri(uriStr);
664                 OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->AcquireDataAbility(uri, true, nullptr);
665                 return;
666             }
667         }
668     }
669 }
670 
ReportDataAbilityAcquired(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)671 void DataAbilityManager::ReportDataAbilityAcquired(const sptr<IRemoteObject> &client, bool isNotHap,
672     std::shared_ptr<DataAbilityRecord> &record)
673 {
674     DataAbilityCaller caller;
675     caller.isNotHap = isNotHap;
676     caller.callerPid = IPCSkeleton::GetCallingPid();
677     caller.callerUid = IPCSkeleton::GetCallingUid();
678     caller.callerToken = client;
679     if (client && !isNotHap) {
680         auto abilityRecord = Token::GetAbilityRecordByToken(client);
681         if (abilityRecord) {
682             caller.callerName = abilityRecord->GetAbilityInfo().bundleName;
683         }
684     } else {
685         caller.callerName = ConnectionStateManager::GetProcessNameByPid(caller.callerPid);
686     }
687 
688     DelayedSingleton<ConnectionStateManager>::GetInstance()->AddDataAbilityConnection(caller, record);
689 }
690 
ReportDataAbilityReleased(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)691 void DataAbilityManager::ReportDataAbilityReleased(const sptr<IRemoteObject> &client, bool isNotHap,
692     std::shared_ptr<DataAbilityRecord> &record)
693 {
694     DataAbilityCaller caller;
695     caller.isNotHap = isNotHap;
696     caller.callerPid = IPCSkeleton::GetCallingPid();
697     caller.callerUid = IPCSkeleton::GetCallingUid();
698     caller.callerToken = client;
699     DelayedSingleton<ConnectionStateManager>::GetInstance()->RemoveDataAbilityConnection(caller, record);
700 }
701 }  // namespace AAFwk
702 }  // namespace OHOS
703