1 /*
2 * Copyright (c) 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 "default_app_mgr.h"
17
18 #include "bundle_data_mgr.h"
19 #include "bundle_mgr_service.h"
20 #include "bundle_permission_mgr.h"
21 #ifdef BMS_RDB_ENABLE
22 #include "default_app_rdb.h"
23 #else
24 #include "default_app_db.h"
25 #endif
26 #include "ipc_skeleton.h"
27 #include "string_ex.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 constexpr int32_t INITIAL_USER_ID = -1;
33 constexpr int32_t TYPE_PART_COUNT = 2;
34 constexpr int32_t INDEX_ZERO = 0;
35 constexpr int32_t INDEX_ONE = 1;
36 const std::string SPLIT = "/";
37 const std::string ACTION_VIEW_DATA = "ohos.want.action.viewData";
38 const std::string ENTITY_BROWSER = "entity.system.browsable";
39 const std::string HTTP = "http";
40 const std::string HTTPS = "https";
41 const std::string WILDCARD = "*";
42 const std::string IMAGE_TYPE = "image/*";
43 const std::string AUDIO_TYPE = "audio/*";
44 const std::string VIDEO_TYPE = "video/*";
45 const std::string PDF_TYPE = "application/pdf";
46 const std::string DOC_TYPE = "application/msword";
47 const std::string DOCX_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
48 const std::string XLS_TYPE = "application/vnd.ms-excel";
49 const std::string XLSX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
50 const std::string PPT_TYPE = "application/vnd.ms-powerpoint";
51 const std::string PPTX_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
52 const std::string BROWSER = "BROWSER";
53 const std::string IMAGE = "IMAGE";
54 const std::string AUDIO = "AUDIO";
55 const std::string VIDEO = "VIDEO";
56 const std::string PDF = "PDF";
57 const std::string WORD = "WORD";
58 const std::string EXCEL = "EXCEL";
59 const std::string PPT = "PPT";
60 }
61
62 std::set<std::string> DefaultAppMgr::supportAppTypes = {BROWSER, IMAGE, AUDIO, VIDEO, PDF, WORD, EXCEL, PPT};
63
GetInstance()64 DefaultAppMgr& DefaultAppMgr::GetInstance()
65 {
66 static DefaultAppMgr defaultAppMgr;
67 return defaultAppMgr;
68 }
69
DefaultAppMgr()70 DefaultAppMgr::DefaultAppMgr()
71 {
72 APP_LOGD("create DefaultAppMgr.");
73 Init();
74 }
75
~DefaultAppMgr()76 DefaultAppMgr::~DefaultAppMgr()
77 {
78 APP_LOGD("destroy DefaultAppMgr.");
79 defaultAppDb_->UnRegisterDeathListener();
80 }
81
Init()82 void DefaultAppMgr::Init()
83 {
84 #ifdef BMS_RDB_ENABLE
85 defaultAppDb_ = std::make_shared<DefaultAppRdb>();
86 #else
87 defaultAppDb_ = std::make_shared<DefaultAppDb>();
88 #endif
89 defaultAppDb_->RegisterDeathListener();
90 }
91
IsDefaultApplication(int32_t userId,const std::string & type,bool & isDefaultApp) const92 ErrCode DefaultAppMgr::IsDefaultApplication(int32_t userId, const std::string& type, bool& isDefaultApp) const
93 {
94 if (VerifyUserIdAndType(userId, type) != ERR_OK) {
95 APP_LOGW("VerifyUserIdAndType failed.");
96 isDefaultApp = false;
97 return ERR_OK;
98 }
99 Element element;
100 bool ret = defaultAppDb_->GetDefaultApplicationInfo(userId, type, element);
101 if (!ret) {
102 APP_LOGW("GetDefaultApplicationInfo failed.");
103 isDefaultApp = false;
104 return ERR_OK;
105 }
106 ret = IsElementValid(userId, type, element);
107 if (!ret) {
108 APP_LOGW("Element is invalid.");
109 isDefaultApp = false;
110 return ERR_OK;
111 }
112 // get bundle name via calling uid
113 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
114 if (dataMgr == nullptr) {
115 APP_LOGW("get BundleDataMgr failed.");
116 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
117 }
118 std::string callingBundleName;
119 ret = dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
120 if (!ret) {
121 APP_LOGW("GetBundleNameForUid failed.");
122 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
123 }
124 APP_LOGD("callingBundleName : %{public}s", callingBundleName.c_str());
125 isDefaultApp = element.bundleName == callingBundleName;
126 return ERR_OK;
127 }
128
GetDefaultApplication(int32_t userId,const std::string & type,BundleInfo & bundleInfo) const129 ErrCode DefaultAppMgr::GetDefaultApplication(int32_t userId, const std::string& type, BundleInfo& bundleInfo) const
130 {
131 ErrCode errCode = VerifyUserIdAndType(userId, type);
132 if (errCode != ERR_OK) {
133 APP_LOGW("VerifyUserIdAndType failed.");
134 return errCode;
135 }
136 if (!BundlePermissionMgr::VerifySystemApp()) {
137 APP_LOGE("non-system app calling system api");
138 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
139 }
140 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_GET_DEFAULT_APPLICATION)) {
141 APP_LOGW("verify permission ohos.permission.GET_DEFAULT_APPLICATION failed.");
142 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
143 }
144
145 if (IsAppType(type)) {
146 return GetBundleInfoByAppType(userId, type, bundleInfo);
147 } else if (IsFileType(type)) {
148 return GetBundleInfoByFileType(userId, type, bundleInfo);
149 } else {
150 APP_LOGW("invalid type, not app type or file type.");
151 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
152 }
153 }
154
SetDefaultApplication(int32_t userId,const std::string & type,const Element & element) const155 ErrCode DefaultAppMgr::SetDefaultApplication(int32_t userId, const std::string& type, const Element& element) const
156 {
157 ErrCode errCode = VerifyUserIdAndType(userId, type);
158 if (errCode != ERR_OK) {
159 APP_LOGW("VerifyUserIdAndType failed.");
160 return errCode;
161 }
162 if (!BundlePermissionMgr::VerifySystemApp()) {
163 APP_LOGE("non-system app calling system api");
164 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
165 }
166 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION)) {
167 APP_LOGW("verify permission ohos.permission.SET_DEFAULT_APPLICATION failed.");
168 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
169 }
170 // clear default app
171 bool ret = IsElementEmpty(element);
172 if (ret) {
173 APP_LOGD("clear default app.");
174 ret = defaultAppDb_->DeleteDefaultApplicationInfo(userId, type);
175 if (!ret) {
176 APP_LOGW("DeleteDefaultApplicationInfo failed.");
177 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
178 }
179 APP_LOGD("SetDefaultApplication success.");
180 return ERR_OK;
181 }
182 ret = IsElementValid(userId, type, element);
183 if (!ret) {
184 APP_LOGW("Element is invalid.");
185 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
186 }
187 ret = defaultAppDb_->SetDefaultApplicationInfo(userId, type, element);
188 if (!ret) {
189 APP_LOGW("SetDefaultApplicationInfo failed.");
190 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
191 }
192 APP_LOGD("SetDefaultApplication success.");
193 return ERR_OK;
194 }
195
ResetDefaultApplication(int32_t userId,const std::string & type) const196 ErrCode DefaultAppMgr::ResetDefaultApplication(int32_t userId, const std::string& type) const
197 {
198 ErrCode errCode = VerifyUserIdAndType(userId, type);
199 if (errCode != ERR_OK) {
200 APP_LOGW("VerifyUserIdAndType failed.");
201 return errCode;
202 }
203 if (!BundlePermissionMgr::VerifySystemApp()) {
204 APP_LOGE("non-system app calling system api");
205 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
206 }
207 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION)) {
208 APP_LOGW("verify permission ohos.permission.SET_DEFAULT_APPLICATION failed.");
209 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
210 }
211 Element element;
212 bool ret = defaultAppDb_->GetDefaultApplicationInfo(INITIAL_USER_ID, type, element);
213 if (!ret) {
214 APP_LOGD("directly delete default info.");
215 if (defaultAppDb_->DeleteDefaultApplicationInfo(userId, type)) {
216 return ERR_OK;
217 }
218 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
219 }
220 ret = IsElementValid(userId, type, element);
221 if (!ret) {
222 APP_LOGW("Element is invalid.");
223 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
224 }
225 ret = defaultAppDb_->SetDefaultApplicationInfo(userId, type, element);
226 if (!ret) {
227 APP_LOGW("SetDefaultApplicationInfo failed.");
228 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
229 }
230 APP_LOGD("ResetDefaultApplication success.");
231 return ERR_OK;
232 }
233
HandleUninstallBundle(int32_t userId,const std::string & bundleName) const234 void DefaultAppMgr::HandleUninstallBundle(int32_t userId, const std::string& bundleName) const
235 {
236 APP_LOGD("begin to HandleUninstallBundle.");
237 std::map<std::string, Element> infos;
238 bool ret = defaultAppDb_->GetDefaultApplicationInfos(userId, infos);
239 if (!ret) {
240 APP_LOGW("GetDefaultApplicationInfos failed.");
241 return;
242 }
243 for (auto item = infos.begin(); item != infos.end();) {
244 if (item->second.bundleName == bundleName) {
245 item = infos.erase(item);
246 } else {
247 item++;
248 }
249 }
250 defaultAppDb_->SetDefaultApplicationInfos(userId, infos);
251 }
252
HandleCreateUser(int32_t userId) const253 void DefaultAppMgr::HandleCreateUser(int32_t userId) const
254 {
255 APP_LOGD("begin to HandleCreateUser.");
256 std::map<std::string, Element> infos;
257 bool ret = defaultAppDb_->GetDefaultApplicationInfos(INITIAL_USER_ID, infos);
258 if (!ret) {
259 APP_LOGW("GetDefaultApplicationInfos failed.");
260 return;
261 }
262 defaultAppDb_->SetDefaultApplicationInfos(userId, infos);
263 }
264
HandleRemoveUser(int32_t userId) const265 void DefaultAppMgr::HandleRemoveUser(int32_t userId) const
266 {
267 APP_LOGD("begin to HandleRemoveUser.");
268 defaultAppDb_->DeleteDefaultApplicationInfos(userId);
269 }
270
GetBundleInfoByAppType(int32_t userId,const std::string & type,BundleInfo & bundleInfo) const271 ErrCode DefaultAppMgr::GetBundleInfoByAppType(int32_t userId, const std::string& type, BundleInfo& bundleInfo) const
272 {
273 Element element;
274 bool ret = defaultAppDb_->GetDefaultApplicationInfo(userId, type, element);
275 if (!ret) {
276 APP_LOGW("GetDefaultApplicationInfo failed.");
277 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
278 }
279 ret = GetBundleInfo(userId, type, element, bundleInfo);
280 if (!ret) {
281 APP_LOGW("GetBundleInfo failed.");
282 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
283 }
284 APP_LOGD("GetBundleInfoByAppType success.");
285 return ERR_OK;
286 }
287
GetBundleInfoByFileType(int32_t userId,const std::string & type,BundleInfo & bundleInfo) const288 ErrCode DefaultAppMgr::GetBundleInfoByFileType(int32_t userId, const std::string& type, BundleInfo& bundleInfo) const
289 {
290 std::map<std::string, Element> infos;
291 bool ret = defaultAppDb_->GetDefaultApplicationInfos(userId, infos);
292 if (!ret) {
293 APP_LOGW("GetDefaultApplicationInfos failed.");
294 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
295 }
296 std::map<std::string, Element> defaultAppTypeInfos;
297 std::map<std::string, Element> defaultFileTypeInfos;
298 for (const auto& item : infos) {
299 if (IsAppType(item.first)) {
300 defaultAppTypeInfos.emplace(item.first, item.second);
301 }
302 if (IsFileType(item.first)) {
303 defaultFileTypeInfos.emplace(item.first, item.second);
304 }
305 }
306 // match default app type
307 for (const auto& item : defaultAppTypeInfos) {
308 if (GetBundleInfo(userId, type, item.second, bundleInfo)) {
309 APP_LOGD("match default app type success.");
310 return ERR_OK;
311 }
312 }
313 // match default file type
314 for (const auto& item : defaultFileTypeInfos) {
315 if (item.first == type && GetBundleInfo(userId, type, item.second, bundleInfo)) {
316 APP_LOGD("match default file type success.");
317 return ERR_OK;
318 }
319 }
320 APP_LOGW("GetBundleInfoByFileType failed.");
321 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
322 }
323
GetBundleInfo(int32_t userId,const std::string & type,const Element & element,BundleInfo & bundleInfo) const324 bool DefaultAppMgr::GetBundleInfo(int32_t userId, const std::string& type, const Element& element,
325 BundleInfo& bundleInfo) const
326 {
327 APP_LOGD("begin to GetBundleInfo.");
328 bool ret = VerifyElementFormat(element);
329 if (!ret) {
330 APP_LOGW("VerifyElementFormat failed.");
331 return false;
332 }
333 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
334 if (dataMgr == nullptr) {
335 APP_LOGW("get BundleDataMgr failed.");
336 return false;
337 }
338 AbilityInfo abilityInfo;
339 ExtensionAbilityInfo extensionInfo;
340 std::vector<Skill> skills;
341 // verify if element exists
342 ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
343 if (!ret) {
344 APP_LOGW("GetBundleInfo, QueryInfoAndSkillsByElement failed.");
345 return false;
346 }
347 // match type and skills
348 ret = IsMatch(type, skills);
349 if (!ret) {
350 APP_LOGW("GetBundleInfo, type and skills not match.");
351 return false;
352 }
353 ret = dataMgr->GetBundleInfo(element.bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
354 if (!ret) {
355 APP_LOGW("GetBundleInfo failed.");
356 return false;
357 }
358 bool isAbility = !element.abilityName.empty();
359 if (isAbility) {
360 bundleInfo.abilityInfos.emplace_back(abilityInfo);
361 } else {
362 bundleInfo.extensionInfos.emplace_back(extensionInfo);
363 }
364 APP_LOGD("GetBundleInfo success.");
365 return true;
366 }
367
IsMatch(const std::string & type,const std::vector<Skill> & skills) const368 bool DefaultAppMgr::IsMatch(const std::string& type, const std::vector<Skill>& skills) const
369 {
370 if (IsAppType(type)) {
371 return MatchAppType(type, skills);
372 } else if (IsFileType(type)) {
373 return MatchFileType(type, skills);
374 } else {
375 APP_LOGW("invalid type.");
376 return false;
377 }
378 }
379
MatchAppType(const std::string & type,const std::vector<Skill> & skills) const380 bool DefaultAppMgr::MatchAppType(const std::string& type, const std::vector<Skill>& skills) const
381 {
382 APP_LOGW("begin to match app type, type : %{public}s.", type.c_str());
383 if (type == BROWSER) {
384 return IsBrowserSkillsValid(skills);
385 } else if (type == IMAGE) {
386 return IsImageSkillsValid(skills);
387 } else if (type == AUDIO) {
388 return IsAudioSkillsValid(skills);
389 } else if (type == VIDEO) {
390 return IsVideoSkillsValid(skills);
391 } else if (type == PDF) {
392 return IsPdfSkillsValid(skills);
393 } else if (type == WORD) {
394 return IsWordSkillsValid(skills);
395 } else if (type == EXCEL) {
396 return IsExcelSkillsValid(skills);
397 } else if (type == PPT) {
398 return IsPptSkillsValid(skills);
399 } else {
400 return false;
401 }
402 }
403
IsBrowserSkillsValid(const std::vector<Skill> & skills) const404 bool DefaultAppMgr::IsBrowserSkillsValid(const std::vector<Skill>& skills) const
405 {
406 APP_LOGD("begin to verify browser skills.");
407 for (const Skill& skill : skills) {
408 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
409 if (item == skill.actions.cend()) {
410 continue;
411 }
412 item = std::find(skill.entities.cbegin(), skill.entities.cend(), ENTITY_BROWSER);
413 if (item == skill.entities.cend()) {
414 continue;
415 }
416 for (const SkillUri& skillUri : skill.uris) {
417 if (skillUri.scheme == HTTP || skillUri.scheme == HTTPS) {
418 APP_LOGD("browser skills is valid.");
419 return true;
420 }
421 }
422 }
423 APP_LOGW("browser skills is invalid.");
424 return false;
425 }
426
IsImageSkillsValid(const std::vector<Skill> & skills) const427 bool DefaultAppMgr::IsImageSkillsValid(const std::vector<Skill>& skills) const
428 {
429 APP_LOGD("begin to verify image skills.");
430 for (const Skill& skill : skills) {
431 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
432 if (item == skill.actions.cend()) {
433 continue;
434 }
435 for (const SkillUri& skillUri : skill.uris) {
436 if (skill.MatchType(IMAGE_TYPE, skillUri.type)) {
437 APP_LOGD("image skills is valid.");
438 return true;
439 }
440 }
441 }
442 APP_LOGW("image skills is invalid.");
443 return false;
444 }
445
IsAudioSkillsValid(const std::vector<Skill> & skills) const446 bool DefaultAppMgr::IsAudioSkillsValid(const std::vector<Skill>& skills) const
447 {
448 APP_LOGD("begin to verify audio skills.");
449 for (const Skill& skill : skills) {
450 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
451 if (item == skill.actions.cend()) {
452 continue;
453 }
454 for (const SkillUri& skillUri : skill.uris) {
455 if (skill.MatchType(AUDIO_TYPE, skillUri.type)) {
456 APP_LOGD("audio skills is valid.");
457 return true;
458 }
459 }
460 }
461 APP_LOGW("audio skills is invalid.");
462 return false;
463 }
464
IsVideoSkillsValid(const std::vector<Skill> & skills) const465 bool DefaultAppMgr::IsVideoSkillsValid(const std::vector<Skill>& skills) const
466 {
467 APP_LOGD("begin to verify video skills.");
468 for (const Skill& skill : skills) {
469 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
470 if (item == skill.actions.cend()) {
471 continue;
472 }
473 for (const SkillUri& skillUri : skill.uris) {
474 if (skill.MatchType(VIDEO_TYPE, skillUri.type)) {
475 APP_LOGD("video skills is valid.");
476 return true;
477 }
478 }
479 }
480 APP_LOGW("video skills is invalid.");
481 return false;
482 }
483
IsPdfSkillsValid(const std::vector<Skill> & skills) const484 bool DefaultAppMgr::IsPdfSkillsValid(const std::vector<Skill>& skills) const
485 {
486 APP_LOGD("begin to verify pdf skills.");
487 for (const Skill& skill : skills) {
488 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
489 if (item == skill.actions.cend()) {
490 continue;
491 }
492 for (const SkillUri& skillUri : skill.uris) {
493 if (skillUri.type == PDF_TYPE) {
494 APP_LOGD("pdf skills is valid.");
495 return true;
496 }
497 }
498 }
499 APP_LOGW("pdf skills is invalid.");
500 return false;
501 }
502
IsWordSkillsValid(const std::vector<Skill> & skills) const503 bool DefaultAppMgr::IsWordSkillsValid(const std::vector<Skill>& skills) const
504 {
505 APP_LOGD("begin to verify word skills.");
506 for (const Skill& skill : skills) {
507 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
508 if (item == skill.actions.cend()) {
509 continue;
510 }
511 for (const SkillUri& skillUri : skill.uris) {
512 if (skillUri.type == DOC_TYPE || skillUri.type == DOCX_TYPE) {
513 APP_LOGD("word skills is valid.");
514 return true;
515 }
516 }
517 }
518 APP_LOGW("word skills is invalid.");
519 return false;
520 }
521
IsExcelSkillsValid(const std::vector<Skill> & skills) const522 bool DefaultAppMgr::IsExcelSkillsValid(const std::vector<Skill>& skills) const
523 {
524 APP_LOGD("begin to verify excel skills.");
525 for (const Skill& skill : skills) {
526 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
527 if (item == skill.actions.cend()) {
528 continue;
529 }
530 for (const SkillUri& skillUri : skill.uris) {
531 if (skillUri.type == XLS_TYPE || skillUri.type == XLSX_TYPE) {
532 APP_LOGD("excel skills is valid.");
533 return true;
534 }
535 }
536 }
537 APP_LOGW("excel skills is invalid.");
538 return false;
539 }
540
IsPptSkillsValid(const std::vector<Skill> & skills) const541 bool DefaultAppMgr::IsPptSkillsValid(const std::vector<Skill>& skills) const
542 {
543 APP_LOGD("begin to verify ppt skills.");
544 for (const Skill& skill : skills) {
545 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
546 if (item == skill.actions.cend()) {
547 continue;
548 }
549 for (const SkillUri& skillUri : skill.uris) {
550 if (skillUri.type == PPT_TYPE || skillUri.type == PPTX_TYPE) {
551 APP_LOGD("ppt skills is valid.");
552 return true;
553 }
554 }
555 }
556 APP_LOGW("ppt skills is invalid.");
557 return false;
558 }
559
MatchFileType(const std::string & type,const std::vector<Skill> & skills) const560 bool DefaultAppMgr::MatchFileType(const std::string& type, const std::vector<Skill>& skills) const
561 {
562 APP_LOGW("begin to match file type, type : %{public}s.", type.c_str());
563 for (const Skill& skill : skills) {
564 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), ACTION_VIEW_DATA);
565 if (item == skill.actions.cend()) {
566 continue;
567 }
568 for (const SkillUri& skillUri : skill.uris) {
569 if (skill.MatchType(type, skillUri.type)) {
570 APP_LOGW("match file type success.");
571 return true;
572 }
573 }
574 }
575 APP_LOGW("match file type failed.");
576 return false;
577 }
578
IsTypeValid(const std::string & type) const579 bool DefaultAppMgr::IsTypeValid(const std::string& type) const
580 {
581 return IsAppType(type) || IsFileType(type);
582 }
583
IsAppType(const std::string & type) const584 bool DefaultAppMgr::IsAppType(const std::string& type) const
585 {
586 if (type.empty()) {
587 return false;
588 }
589 return supportAppTypes.find(type) != supportAppTypes.end();
590 }
591
IsFileType(const std::string & type) const592 bool DefaultAppMgr::IsFileType(const std::string& type) const
593 {
594 // valid fileType format : type/subType
595 if (type.empty() || type.find(WILDCARD) != type.npos) {
596 APP_LOGW("file type not allow contains *.");
597 return false;
598 }
599 std::vector<std::string> vector;
600 SplitStr(type, SPLIT, vector, false, false);
601 if (vector.size() == TYPE_PART_COUNT && !vector[INDEX_ZERO].empty() && !vector[INDEX_ONE].empty()) {
602 return true;
603 }
604 APP_LOGW("file type format invalid.");
605 return false;
606 }
607
IsUserIdExist(int32_t userId) const608 bool DefaultAppMgr::IsUserIdExist(int32_t userId) const
609 {
610 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
611 if (dataMgr == nullptr) {
612 APP_LOGW("get BundleDataMgr failed.");
613 return false;
614 }
615 return dataMgr->HasUserId(userId);
616 }
617
VerifyUserIdAndType(int32_t userId,const std::string & type) const618 ErrCode DefaultAppMgr::VerifyUserIdAndType(int32_t userId, const std::string& type) const
619 {
620 bool ret = IsUserIdExist(userId);
621 if (!ret) {
622 APP_LOGW("userId %{public}d doesn't exist.", userId);
623 return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
624 }
625 ret = IsTypeValid(type);
626 if (!ret) {
627 APP_LOGW("invalid type %{public}s, not app type or file type.", type.c_str());
628 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
629 }
630 return ERR_OK;
631 }
632
IsElementEmpty(const Element & element) const633 bool DefaultAppMgr::IsElementEmpty(const Element& element) const
634 {
635 return element.bundleName.empty() && element.moduleName.empty()
636 && element.abilityName.empty() && element.extensionName.empty();
637 }
638
VerifyElementFormat(const Element & element)639 bool DefaultAppMgr::VerifyElementFormat(const Element& element)
640 {
641 const std::string& bundleName = element.bundleName;
642 const std::string& moduleName = element.moduleName;
643 const std::string& abilityName = element.abilityName;
644 const std::string& extensionName = element.extensionName;
645 if (bundleName.empty()) {
646 APP_LOGW("bundleName empty, bad Element format.");
647 return false;
648 }
649 if (moduleName.empty()) {
650 APP_LOGW("moduleName empty, bad Element format.");
651 return false;
652 }
653 if (abilityName.empty() && extensionName.empty()) {
654 APP_LOGW("abilityName and extensionName both empty, bad Element format.");
655 return false;
656 }
657 if (!abilityName.empty() && !extensionName.empty()) {
658 APP_LOGW("abilityName and extensionName both non-empty, bad Element format.");
659 return false;
660 }
661 return true;
662 }
663
IsElementValid(int32_t userId,const std::string & type,const Element & element) const664 bool DefaultAppMgr::IsElementValid(int32_t userId, const std::string& type, const Element& element) const
665 {
666 bool ret = VerifyElementFormat(element);
667 if (!ret) {
668 APP_LOGW("VerifyElementFormat failed.");
669 return false;
670 }
671 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
672 if (dataMgr == nullptr) {
673 APP_LOGW("get BundleDataMgr failed.");
674 return false;
675 }
676 AbilityInfo abilityInfo;
677 ExtensionAbilityInfo extensionInfo;
678 std::vector<Skill> skills;
679 // verify if element exists
680 ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
681 if (!ret) {
682 APP_LOGW("QueryInfoAndSkillsByElement failed.");
683 return false;
684 }
685 // match type and skills
686 ret = IsMatch(type, skills);
687 if (!ret) {
688 APP_LOGW("type and skills not match.");
689 return false;
690 }
691 APP_LOGD("Element is valid.");
692 return true;
693 }
694 }
695 }