• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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("Call");
39 }
40 
~DataAbilityManager()41 DataAbilityManager::~DataAbilityManager()
42 {
43     HILOG_DEBUG("Call");
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("Call");
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<ffrt::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("Call");
127 
128     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
129     CHECK_POINTER_AND_RETURN(client, ERR_NULL_OBJECT);
130 
131     std::lock_guard<ffrt::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("JudgeAbilityVisibleControl error.");
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("Call");
182 
183     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
184 
185     std::lock_guard<ffrt::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("Call");
199 
200     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
201     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
202 
203     std::lock_guard<ffrt::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("Call");
249 
250     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
251 
252     std::lock_guard<ffrt::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_INFO("Call");
295     CHECK_POINTER(abilityRecord);
296 
297     {
298         std::lock_guard<ffrt::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<ffrt::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("Call.");
377 
378     std::lock_guard<ffrt::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_DEBUG("Call.");
396 
397     CHECK_POINTER_AND_RETURN(token, nullptr);
398 
399     std::lock_guard<ffrt::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("Call.");
424 
425     CHECK_POINTER_AND_RETURN(scheduler, nullptr);
426 
427     std::lock_guard<ffrt::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("Call.");
442 
443     std::lock_guard<ffrt::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("name '%{public}s'", 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     DataAbilityRecordPtrMap dataAbilityRecordMap;
529     {
530         std::lock_guard<ffrt::mutex> locker(mutex_);
531         dataAbilityRecordMap = dataAbilityRecordsLoaded_;
532     }
533     if (!args.empty()) {
534         auto it = std::find_if(dataAbilityRecordMap.begin(), dataAbilityRecordMap.end(),
535             [&args](const auto &dataAbilityRecord) { return dataAbilityRecord.first.compare(args) == 0; });
536         if (it != dataAbilityRecordMap.end()) {
537             info.emplace_back("AbilityName [ " + it->first + " ]");
538             if (it->second) {
539                 it->second->Dump(info);
540             }
541         } else {
542             info.emplace_back(args + ": Nothing to dump.");
543         }
544     } else {
545         info.emplace_back("dataAbilityRecords:");
546         for (auto &&dataAbilityRecord : dataAbilityRecordMap) {
547             info.emplace_back("  uri [" + dataAbilityRecord.first + "]");
548             if (dataAbilityRecord.second) {
549                 dataAbilityRecord.second->Dump(info);
550             }
551         }
552     }
553 }
554 
DumpClientInfo(std::vector<std::string> & info,bool isClient,std::shared_ptr<DataAbilityRecord> record) const555 void DataAbilityManager::DumpClientInfo(std::vector<std::string> &info, bool isClient,
556     std::shared_ptr<DataAbilityRecord> record) const
557 {
558     if (record == nullptr) {
559         return;
560     }
561     record->Dump(info);
562     // add dump client info
563     if (isClient && record->GetScheduler() && record->GetAbilityRecord() && record->GetAbilityRecord()->IsReady()) {
564         std::vector<std::string> params;
565         record->GetScheduler()->DumpAbilityInfo(params, info);
566         AppExecFwk::Configuration config;
567         if (DelayedSingleton<AppScheduler>::GetInstance()->GetConfiguration(config) == ERR_OK) {
568             info.emplace_back("          configuration: " + config.GetName());
569         }
570         return;
571     }
572 }
573 
DumpSysState(std::vector<std::string> & info,bool isClient,const std::string & args) const574 void DataAbilityManager::DumpSysState(std::vector<std::string> &info, bool isClient, const std::string &args) const
575 {
576     DataAbilityRecordPtrMap dataAbilityRecordMap;
577     {
578         std::lock_guard<ffrt::mutex> locker(mutex_);
579         dataAbilityRecordMap = dataAbilityRecordsLoaded_;
580     }
581     if (args.empty()) {
582         info.emplace_back("  dataAbilityRecords:");
583         for (auto &&dataAbilityRecord : dataAbilityRecordMap) {
584             info.emplace_back("    uri [" + dataAbilityRecord.first + "]");
585             DumpClientInfo(info, isClient, dataAbilityRecord.second);
586         }
587         return;
588     }
589     auto compareFunction = [&args](const auto &dataAbilityRecord) {
590         return dataAbilityRecord.first.compare(args) == 0;
591     };
592     auto it = std::find_if(dataAbilityRecordMap.begin(), dataAbilityRecordMap.end(), compareFunction);
593     if (it == dataAbilityRecordMap.end()) {
594         info.emplace_back(args + ": Nothing to dump.");
595         return;
596     }
597     info.emplace_back("AbilityName [ " + it->first + " ]");
598     DumpClientInfo(info, isClient, it->second);
599 }
600 
GetAbilityRunningInfos(std::vector<AbilityRunningInfo> & info,bool isPerm)601 void DataAbilityManager::GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm)
602 {
603     HILOG_INFO("Get ability running infos");
604     std::lock_guard<ffrt::mutex> locker(mutex_);
605 
606     auto queryInfo = [&info, isPerm](DataAbilityRecordPtrMap::reference data) {
607         auto dataAbilityRecord = data.second;
608         if (!dataAbilityRecord) {
609             return;
610         }
611 
612         auto abilityRecord = dataAbilityRecord->GetAbilityRecord();
613         if (!abilityRecord) {
614             return;
615         }
616 
617         if (isPerm) {
618             DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord);
619         } else {
620             auto callingTokenId = IPCSkeleton::GetCallingTokenID();
621             auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId;
622             if (callingTokenId == tokenID) {
623                 DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord);
624             }
625         }
626     };
627 
628     std::for_each(dataAbilityRecordsLoading_.begin(), dataAbilityRecordsLoading_.end(), queryInfo);
629     std::for_each(dataAbilityRecordsLoaded_.begin(), dataAbilityRecordsLoaded_.end(), queryInfo);
630 }
631 
RestartDataAbility(const std::shared_ptr<AbilityRecord> & abilityRecord)632 void DataAbilityManager::RestartDataAbility(const std::shared_ptr<AbilityRecord> &abilityRecord)
633 {
634     // restart data ability if necessary
635     auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
636     CHECK_POINTER(bundleMgrHelper);
637     std::vector<AppExecFwk::BundleInfo> bundleInfos;
638     bool getBundleInfos = bundleMgrHelper->GetBundleInfos(
639         OHOS::AppExecFwk::GET_BUNDLE_DEFAULT, bundleInfos, USER_ID_NO_HEAD);
640     if (!getBundleInfos) {
641         HILOG_ERROR("Handle ability died task, get bundle infos failed.");
642         return;
643     }
644 
645     for (size_t i = 0; i < bundleInfos.size(); i++) {
646         if (!bundleInfos[i].isKeepAlive || bundleInfos[i].applicationInfo.process.empty()) {
647             continue;
648         }
649         for (auto hapModuleInfo : bundleInfos[i].hapModuleInfos) {
650             if (hapModuleInfo.isModuleJson) {
651                 // new application model, it cannot be a data ability
652                 continue;
653             }
654             // old application model, it maybe a data ability
655             std::string mainElement = hapModuleInfo.mainAbility;
656             if (abilityRecord->GetAbilityInfo().name != mainElement ||
657                 abilityRecord->GetAbilityInfo().process != bundleInfos[i].applicationInfo.process) {
658                 continue;
659             }
660             std::string uriStr;
661             bool getDataAbilityUri = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
662                 hapModuleInfo.abilityInfos, mainElement, uriStr);
663             if (getDataAbilityUri) {
664                 HILOG_INFO("restart data ability: %{public}s, uri: %{public}s.",
665                     abilityRecord->GetAbilityInfo().name.c_str(), uriStr.c_str());
666                 Uri uri(uriStr);
667                 OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->AcquireDataAbility(uri, true, nullptr);
668                 return;
669             }
670         }
671     }
672 }
673 
ReportDataAbilityAcquired(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)674 void DataAbilityManager::ReportDataAbilityAcquired(const sptr<IRemoteObject> &client, bool isNotHap,
675     std::shared_ptr<DataAbilityRecord> &record)
676 {
677     DataAbilityCaller caller;
678     caller.isNotHap = isNotHap;
679     caller.callerPid = IPCSkeleton::GetCallingPid();
680     caller.callerUid = IPCSkeleton::GetCallingUid();
681     caller.callerToken = client;
682     if (client && !isNotHap) {
683         auto abilityRecord = Token::GetAbilityRecordByToken(client);
684         if (abilityRecord) {
685             caller.callerName = abilityRecord->GetAbilityInfo().bundleName;
686         }
687     } else {
688         caller.callerName = ConnectionStateManager::GetProcessNameByPid(caller.callerPid);
689     }
690 
691     DelayedSingleton<ConnectionStateManager>::GetInstance()->AddDataAbilityConnection(caller, record);
692 }
693 
ReportDataAbilityReleased(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)694 void DataAbilityManager::ReportDataAbilityReleased(const sptr<IRemoteObject> &client, bool isNotHap,
695     std::shared_ptr<DataAbilityRecord> &record)
696 {
697     DataAbilityCaller caller;
698     caller.isNotHap = isNotHap;
699     caller.callerPid = IPCSkeleton::GetCallingPid();
700     caller.callerUid = IPCSkeleton::GetCallingUid();
701     caller.callerToken = client;
702     DelayedSingleton<ConnectionStateManager>::GetInstance()->RemoveDataAbilityConnection(caller, record);
703 }
704 }  // namespace AAFwk
705 }  // namespace OHOS
706