1 /*
2  * Copyright (c) 2021-2024 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 "ability_manager_service.h"
19 #include "ability_resident_process_rdb.h"
20 #include "ability_util.h"
21 #include "connection_state_manager.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
25 using namespace std::chrono;
26 using namespace std::placeholders;
27 
28 namespace {
29 constexpr bool DEBUG_ENABLED = false;
30 constexpr system_clock::duration DATA_ABILITY_LOAD_TIMEOUT = 11000ms;
31 }  // namespace
32 
DataAbilityManager()33 DataAbilityManager::DataAbilityManager()
34 {
35     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
36 }
37 
~DataAbilityManager()38 DataAbilityManager::~DataAbilityManager()
39 {
40     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
41 }
42 
Acquire(const AbilityRequest & abilityRequest,bool tryBind,const sptr<IRemoteObject> & client,bool isNotHap)43 sptr<IAbilityScheduler> DataAbilityManager::Acquire(
44     const AbilityRequest &abilityRequest, bool tryBind, const sptr<IRemoteObject> &client, bool isNotHap)
45 {
46     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
47 
48     if (abilityRequest.abilityInfo.type != AppExecFwk::AbilityType::DATA) {
49         TAG_LOGE(AAFwkTag::DATA_ABILITY, "not a data ability");
50         return nullptr;
51     }
52 
53     if (abilityRequest.abilityInfo.bundleName.empty() || abilityRequest.abilityInfo.name.empty()) {
54         TAG_LOGE(AAFwkTag::DATA_ABILITY, "invalid name");
55         return nullptr;
56     }
57 
58     std::shared_ptr<AbilityRecord> clientAbilityRecord;
59     const std::string dataAbilityName(abilityRequest.abilityInfo.bundleName + '.' + abilityRequest.abilityInfo.name);
60 
61     if (client && !isNotHap) {
62         clientAbilityRecord = Token::GetAbilityRecordByToken(client);
63         if (!clientAbilityRecord) {
64             TAG_LOGE(AAFwkTag::DATA_ABILITY, "invalid client token");
65             return nullptr;
66         }
67         TAG_LOGI(AAFwkTag::DATA_ABILITY, "Ability '%{public}s' acquiring data ability '%{public}s'...",
68             clientAbilityRecord->GetAbilityInfo().name.c_str(), dataAbilityName.c_str());
69     } else {
70         TAG_LOGI(AAFwkTag::DATA_ABILITY, "Loading data ability '%{public}s'...", dataAbilityName.c_str());
71     }
72 
73     std::lock_guard<ffrt::mutex> locker(mutex_);
74 
75     if (DEBUG_ENABLED) {
76         DumpLocked(__func__, __LINE__);
77     }
78 
79     DataAbilityRecordPtr dataAbilityRecord;
80 
81     auto it = dataAbilityRecordsLoaded_.find(dataAbilityName);
82     if (it == dataAbilityRecordsLoaded_.end()) {
83         TAG_LOGD(AAFwkTag::DATA_ABILITY, "data ability not existed, loading...");
84         dataAbilityRecord = LoadLocked(dataAbilityName, abilityRequest);
85     } else {
86         TAG_LOGD(AAFwkTag::DATA_ABILITY, "data ability existed");
87         dataAbilityRecord = it->second;
88     }
89 
90     if (!dataAbilityRecord) {
91         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Failed to load '%{public}s'", dataAbilityName.c_str());
92         return nullptr;
93     }
94 
95     auto scheduler = dataAbilityRecord->GetScheduler();
96     if (!scheduler) {
97         if (DEBUG_ENABLED) {
98             TAG_LOGE(AAFwkTag::DATA_ABILITY, "'%{public}s' not loaded, removing",
99                 dataAbilityName.c_str());
100         }
101         auto it = dataAbilityRecordsLoaded_.find(dataAbilityName);
102         if (it != dataAbilityRecordsLoaded_.end()) {
103             dataAbilityRecordsLoaded_.erase(it);
104         }
105         return nullptr;
106     }
107 
108     if (client) {
109         dataAbilityRecord->AddClient(client, tryBind, isNotHap);
110     }
111 
112     if (DEBUG_ENABLED) {
113         DumpLocked(__func__, __LINE__);
114     }
115 
116     ReportDataAbilityAcquired(client, isNotHap, dataAbilityRecord);
117 
118     return scheduler;
119 }
120 
Release(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & client,bool isNotHap)121 int DataAbilityManager::Release(
122     const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &client, bool isNotHap)
123 {
124     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
125 
126     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
127     CHECK_POINTER_AND_RETURN(client, ERR_NULL_OBJECT);
128 
129     std::lock_guard<ffrt::mutex> locker(mutex_);
130 
131     if (DEBUG_ENABLED) {
132         DumpLocked(__func__, __LINE__);
133     }
134 
135     DataAbilityRecordPtr dataAbilityRecord;
136 
137     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
138         if (it->second && it->second->GetScheduler() &&
139             it->second->GetScheduler()->AsObject() == scheduler->AsObject()) {
140             dataAbilityRecord = it->second;
141             TAG_LOGI(AAFwkTag::DATA_ABILITY, "Releasing '%{public}s'...", it->first.c_str());
142             break;
143         }
144     }
145 
146     if (!dataAbilityRecord) {
147         TAG_LOGE(AAFwkTag::DATA_ABILITY, "data ability not exist");
148         return ERR_UNKNOWN_OBJECT;
149     }
150 
151     auto abilityRecord = dataAbilityRecord->GetAbilityRecord();
152     CHECK_POINTER_AND_RETURN(abilityRecord, ERR_UNKNOWN_OBJECT);
153     auto abilityMs = DelayedSingleton<AbilityManagerService>::GetInstance();
154     CHECK_POINTER_AND_RETURN(abilityMs, GET_ABILITY_SERVICE_FAILED);
155     int result = abilityMs->JudgeAbilityVisibleControl(abilityRecord->GetAbilityInfo());
156     if (result != ERR_OK) {
157         TAG_LOGE(AAFwkTag::DATA_ABILITY, "JudgeAbilityVisibleControl error");
158         return result;
159     }
160 
161     if (dataAbilityRecord->GetClientCount(client) == 0) {
162         TAG_LOGE(AAFwkTag::DATA_ABILITY, "client wrong");
163         return ERR_UNKNOWN_OBJECT;
164     }
165 
166     dataAbilityRecord->RemoveClient(client, isNotHap);
167 
168     if (DEBUG_ENABLED) {
169         DumpLocked(__func__, __LINE__);
170     }
171 
172     ReportDataAbilityReleased(client, isNotHap, dataAbilityRecord);
173 
174     return ERR_OK;
175 }
176 
ContainsDataAbility(const sptr<IAbilityScheduler> & scheduler)177 bool DataAbilityManager::ContainsDataAbility(const sptr<IAbilityScheduler> &scheduler)
178 {
179     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
180 
181     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
182 
183     std::lock_guard<ffrt::mutex> locker(mutex_);
184     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
185         if (it->second && it->second->GetScheduler() &&
186             it->second->GetScheduler()->AsObject() == scheduler->AsObject()) {
187             return true;
188         }
189     }
190 
191     return false;
192 }
193 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)194 int DataAbilityManager::AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token)
195 {
196     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
197 
198     CHECK_POINTER_AND_RETURN(scheduler, ERR_NULL_OBJECT);
199     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
200 
201     std::lock_guard<ffrt::mutex> locker(mutex_);
202 
203     if (DEBUG_ENABLED) {
204         DumpLocked(__func__, __LINE__);
205     }
206 
207     TAG_LOGI(AAFwkTag::DATA_ABILITY, "Attaching data ability");
208 
209     auto record = Token::GetAbilityRecordByToken(token);
210     std::string abilityName = "";
211     if (record != nullptr) {
212         abilityName = record->GetAbilityInfo().name;
213     }
214 
215     DataAbilityRecordPtr dataAbilityRecord;
216     auto it = dataAbilityRecordsLoading_.begin();
217     for (; it != dataAbilityRecordsLoading_.end(); ++it) {
218         if (it->second && it->second->GetToken() == token) {
219             dataAbilityRecord = it->second;
220             break;
221         }
222     }
223 
224     if (!dataAbilityRecord) {
225         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Attaching '%{public}s' not in loading state",
226             abilityName.c_str());
227         return ERR_UNKNOWN_OBJECT;
228     }
229 
230     if (DEBUG_ENABLED && dataAbilityRecord->GetClientCount() > 0) {
231         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Attachingy '%{public}s' has clients", abilityName.c_str());
232     }
233 
234     if (DEBUG_ENABLED && dataAbilityRecord->GetScheduler()) {
235         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Attaching '%{public}s' has ready", abilityName.c_str());
236     }
237 
238     if (DEBUG_ENABLED && dataAbilityRecordsLoaded_.count(it->first) != 0) {
239         TAG_LOGE(AAFwkTag::DATA_ABILITY, "attaching '%{public}s' exist", abilityName.c_str());
240     }
241 
242     return dataAbilityRecord->Attach(scheduler);
243 }
244 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state)245 int DataAbilityManager::AbilityTransitionDone(const sptr<IRemoteObject> &token, int state)
246 {
247     TAG_LOGD(AAFwkTag::DATA_ABILITY, "Call");
248 
249     CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT);
250 
251     std::lock_guard<ffrt::mutex> locker(mutex_);
252 
253     if (DEBUG_ENABLED) {
254         DumpLocked(__func__, __LINE__);
255     }
256 
257     TAG_LOGI(AAFwkTag::DATA_ABILITY, "transition done %{public}d", state);
258 
259     DataAbilityRecordPtrMap::iterator it;
260     DataAbilityRecordPtr dataAbilityRecord;
261     auto record = Token::GetAbilityRecordByToken(token);
262     std::string abilityName = "";
263     if (record != nullptr) {
264         abilityName = record->GetAbilityInfo().name;
265         record->RemoveSignatureInfo();
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         TAG_LOGE(AAFwkTag::DATA_ABILITY, "'%{public}s' not exist", 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     TAG_LOGI(AAFwkTag::DATA_ABILITY, "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                     TAG_LOGD(AAFwkTag::DATA_ABILITY, "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     TAG_LOGD(AAFwkTag::DATA_ABILITY, "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     TAG_LOGD(AAFwkTag::DATA_ABILITY, "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     TAG_LOGD(AAFwkTag::DATA_ABILITY, "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     TAG_LOGD(AAFwkTag::DATA_ABILITY, "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     TAG_LOGD(AAFwkTag::DATA_ABILITY, "name '%{public}s'", name.c_str());
452 
453     DataAbilityRecordPtr dataAbilityRecord;
454 
455     auto it = dataAbilityRecordsLoading_.find(name);
456     if (it == dataAbilityRecordsLoading_.end()) {
457         TAG_LOGI(AAFwkTag::DATA_ABILITY, "data ability not in loading");
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             TAG_LOGE(AAFwkTag::DATA_ABILITY, "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             TAG_LOGE(AAFwkTag::DATA_ABILITY, " insert data ability failed");
470             return nullptr;
471         }
472     } else {
473         TAG_LOGI(AAFwkTag::DATA_ABILITY, "data ability loading");
474         dataAbilityRecord = it->second;
475     }
476 
477     if (!dataAbilityRecord) {
478         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Failed to load '%{public}s'", name.c_str());
479         return nullptr;
480     }
481 
482     TAG_LOGI(AAFwkTag::DATA_ABILITY, "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         TAG_LOGE(AAFwkTag::DATA_ABILITY, "Wait 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         TAG_LOGI(AAFwkTag::DATA_ABILITY, "dump at %{public}s(%{public}d)", func, line);
503     } else {
504         TAG_LOGI(AAFwkTag::DATA_ABILITY, "dump");
505     }
506 
507     TAG_LOGI(AAFwkTag::DATA_ABILITY, "Available count: %{public}zu", dataAbilityRecordsLoaded_.size());
508 
509     for (auto it = dataAbilityRecordsLoaded_.begin(); it != dataAbilityRecordsLoaded_.end(); ++it) {
510         TAG_LOGI(AAFwkTag::DATA_ABILITY, "'%{public}s':", it->first.c_str());
511         if (it->second) {
512             it->second->Dump();
513         }
514     }
515 
516     TAG_LOGI(AAFwkTag::DATA_ABILITY, "Loading count: %{public}zu", dataAbilityRecordsLoading_.size());
517 
518     for (auto it = dataAbilityRecordsLoading_.begin(); it != dataAbilityRecordsLoading_.end(); ++it) {
519         TAG_LOGI(AAFwkTag::DATA_ABILITY, "'%{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     TAG_LOGI(AAFwkTag::DATA_ABILITY, "called");
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         TAG_LOGE(AAFwkTag::DATA_ABILITY, "GetBundleInfos failed");
642         return;
643     }
644 
645     for (size_t i = 0; i < bundleInfos.size(); i++) {
646         bool keepAliveEnable = bundleInfos[i].isKeepAlive;
647         AmsResidentProcessRdb::GetInstance().GetResidentProcessEnable(bundleInfos[i].name, keepAliveEnable);
648         if (!keepAliveEnable || bundleInfos[i].applicationInfo.process.empty()) {
649             continue;
650         }
651         for (auto hapModuleInfo : bundleInfos[i].hapModuleInfos) {
652             if (hapModuleInfo.isModuleJson) {
653                 // new application model, it cannot be a data ability
654                 continue;
655             }
656             // old application model, it maybe a data ability
657             std::string mainElement = hapModuleInfo.mainAbility;
658             if (abilityRecord->GetAbilityInfo().name != mainElement ||
659                 abilityRecord->GetAbilityInfo().process != bundleInfos[i].applicationInfo.process) {
660                 continue;
661             }
662             std::string uriStr;
663             bool getDataAbilityUri = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
664                 hapModuleInfo.abilityInfos, mainElement, uriStr);
665             if (getDataAbilityUri) {
666                 TAG_LOGI(AAFwkTag::DATA_ABILITY, "restart data ability: %{public}s, uri: %{public}s",
667                     abilityRecord->GetAbilityInfo().name.c_str(), uriStr.c_str());
668                 Uri uri(uriStr);
669                 OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->AcquireDataAbility(uri, true, nullptr);
670                 return;
671             }
672         }
673     }
674 }
675 
ReportDataAbilityAcquired(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)676 void DataAbilityManager::ReportDataAbilityAcquired(const sptr<IRemoteObject> &client, bool isNotHap,
677     std::shared_ptr<DataAbilityRecord> &record)
678 {
679     DataAbilityCaller caller;
680     caller.isNotHap = isNotHap;
681     caller.callerPid = IPCSkeleton::GetCallingPid();
682     caller.callerUid = IPCSkeleton::GetCallingUid();
683     caller.callerToken = client;
684     if (client && !isNotHap) {
685         auto abilityRecord = Token::GetAbilityRecordByToken(client);
686         if (abilityRecord) {
687             caller.callerName = abilityRecord->GetAbilityInfo().bundleName;
688         }
689     } else {
690         caller.callerName = ConnectionStateManager::GetProcessNameByPid(caller.callerPid);
691     }
692 
693     DelayedSingleton<ConnectionStateManager>::GetInstance()->AddDataAbilityConnection(caller, record);
694 }
695 
ReportDataAbilityReleased(const sptr<IRemoteObject> & client,bool isNotHap,std::shared_ptr<DataAbilityRecord> & record)696 void DataAbilityManager::ReportDataAbilityReleased(const sptr<IRemoteObject> &client, bool isNotHap,
697     std::shared_ptr<DataAbilityRecord> &record)
698 {
699     DataAbilityCaller caller;
700     caller.isNotHap = isNotHap;
701     caller.callerPid = IPCSkeleton::GetCallingPid();
702     caller.callerUid = IPCSkeleton::GetCallingUid();
703     caller.callerToken = client;
704     DelayedSingleton<ConnectionStateManager>::GetInstance()->RemoveDataAbilityConnection(caller, record);
705 }
706 }  // namespace AAFwk
707 }  // namespace OHOS
708