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