1 /*
2  * Copyright (c) 2022-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 "default_app_mgr.h"
17 
18 #include "app_log_tag_wrapper.h"
19 #include "bms_extension_client.h"
20 #include "bundle_mgr_service.h"
21 #include "bundle_permission_mgr.h"
22 #include "default_app_rdb.h"
23 #include "ipc_skeleton.h"
24 #include "mime_type_mgr.h"
25 #ifdef BUNDLE_FRAMEWORK_UDMF_ENABLED
26 #include "type_descriptor.h"
27 #include "utd_client.h"
28 #endif
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 using Want = OHOS::AAFwk::Want;
33 
34 namespace {
35 constexpr int32_t INITIAL_USER_ID = -1;
36 constexpr int32_t TYPE_PART_COUNT = 2;
37 constexpr int32_t INDEX_ZERO = 0;
38 constexpr int32_t INDEX_ONE = 1;
39 constexpr size_t TYPE_MAX_SIZE = 200;
40 const std::string SPLIT = "/";
41 const std::string SCHEME_SIGN = "://";
42 const std::string EMAIL_ACTION = "ohos.want.action.sendToData";
43 const std::string EMAIL_SCHEME = "mailto";
44 const std::string ENTITY_BROWSER = "entity.system.browsable";
45 const std::string HTTP = "http";
46 const std::string HTTPS = "https";
47 const std::string HTTP_SCHEME = "http://";
48 const std::string HTTPS_SCHEME = "https://";
49 const std::string FILE_SCHEME = "file://";
50 const std::string CONTENT_SCHEME = "content://";
51 const std::string WILDCARD = "*";
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 const std::string EMAIL = "EMAIL";
61 constexpr const char* ACTION_VIEW_DATA = "ohos.want.action.viewData";
62 const std::map<std::string, std::set<std::string>> APP_TYPES = {
63     {IMAGE, {"image/*"}},
64     {AUDIO, {"audio/*"}},
65     {VIDEO, {"video/*"}},
66     {PDF, {"application/pdf"}},
67     {WORD, {"application/msword",
68         "application/vnd.ms-word.document",
69         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
70         "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}},
71     {EXCEL, {"application/vnd.ms-excel",
72         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
73         "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}},
74     {PPT, {"application/vnd.ms-powerpoint",
75         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
76         "application/vnd.openxmlformats-officedocument.presentationml.template"}},
77 };
78 const std::set<std::string> supportAppTypes = {BROWSER, IMAGE, AUDIO, VIDEO, PDF, WORD, EXCEL, PPT, EMAIL};
79 }
80 
GetInstance()81 DefaultAppMgr& DefaultAppMgr::GetInstance()
82 {
83     static DefaultAppMgr defaultAppMgr;
84     return defaultAppMgr;
85 }
86 
DefaultAppMgr()87 DefaultAppMgr::DefaultAppMgr()
88 {
89     LOG_D(BMS_TAG_DEFAULT, "create DefaultAppMgr");
90     Init();
91 }
92 
~DefaultAppMgr()93 DefaultAppMgr::~DefaultAppMgr()
94 {
95     LOG_D(BMS_TAG_DEFAULT, "destroy DefaultAppMgr");
96     defaultAppDb_->UnRegisterDeathListener();
97 }
98 
Init()99 void DefaultAppMgr::Init()
100 {
101     defaultAppDb_ = std::make_shared<DefaultAppRdb>();
102     defaultAppDb_->RegisterDeathListener();
103 }
104 
IsDefaultApplication(int32_t userId,const std::string & type,bool & isDefaultApp) const105 ErrCode DefaultAppMgr::IsDefaultApplication(int32_t userId, const std::string& type, bool& isDefaultApp) const
106 {
107     LOG_I(BMS_TAG_DEFAULT, "IsDefault,userId:%{public}d,type:%{public}s", userId, type.c_str());
108     if (type.size() > TYPE_MAX_SIZE) {
109         LOG_W(BMS_TAG_DEFAULT, "type size too large");
110         isDefaultApp = false;
111         return ERR_OK;
112     }
113 
114     if (!IsUserIdExist(userId)) {
115         LOG_W(BMS_TAG_DEFAULT, "userId not exist");
116         isDefaultApp = false;
117         return ERR_OK;
118     }
119 
120     std::vector<std::string> normalizedTypeVector = Normalize(type);
121     LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
122     if (normalizedTypeVector.empty()) {
123         LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
124         isDefaultApp = false;
125         return ERR_OK;
126     }
127 
128     for (const std::string& normalizedType : normalizedTypeVector) {
129         (void)IsDefaultApplicationInternal(userId, normalizedType, isDefaultApp);
130         if (isDefaultApp) {
131             return ERR_OK;
132         }
133     }
134     return ERR_OK;
135 }
136 
IsDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,bool & isDefaultApp) const137 ErrCode DefaultAppMgr::IsDefaultApplicationInternal(
138     int32_t userId, const std::string& normalizedType, bool& isDefaultApp) const
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     Element element;
142     bool ret = defaultAppDb_->GetDefaultApplicationInfo(userId, normalizedType, element);
143     if (!ret) {
144         LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplicationInfo failed");
145         isDefaultApp = false;
146         return ERR_OK;
147     }
148     ret = IsElementValid(userId, normalizedType, element);
149     if (!ret) {
150         LOG_W(BMS_TAG_DEFAULT, "invalid element");
151         isDefaultApp = false;
152         return ERR_OK;
153     }
154     // get bundle name via calling uid
155     std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
156     if (dataMgr == nullptr) {
157         LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
158         isDefaultApp = false;
159         return ERR_OK;
160     }
161     std::string callingBundleName;
162     ret = dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
163     if (!ret) {
164         LOG_W(BMS_TAG_DEFAULT, "GetBundleNameForUid failed");
165         isDefaultApp = false;
166         return ERR_OK;
167     }
168     LOG_I(BMS_TAG_DEFAULT, "callingBundleName:%{public}s", callingBundleName.c_str());
169     isDefaultApp = element.bundleName == callingBundleName;
170     return ERR_OK;
171 }
172 
GetDefaultApplication(int32_t userId,const std::string & type,BundleInfo & bundleInfo,bool backup) const173 ErrCode DefaultAppMgr::GetDefaultApplication(
174     int32_t userId, const std::string& type, BundleInfo& bundleInfo, bool backup) const
175 {
176     LOG_I(BMS_TAG_DEFAULT, "GetDefault,userId:%{public}d,type:%{public}s,backup(bool):%{public}d",
177         userId, type.c_str(), backup);
178 
179     ErrCode ret = VerifyPermission(Constants::PERMISSION_GET_DEFAULT_APPLICATION);
180     if (ret != ERR_OK) {
181         return ret;
182     }
183 
184     if (!IsUserIdExist(userId)) {
185         LOG_W(BMS_TAG_DEFAULT, "userId not exist");
186         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
187     }
188 
189     std::vector<std::string> normalizedTypeVector = Normalize(type);
190     LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
191     if (normalizedTypeVector.empty()) {
192         LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
193         return ERR_BUNDLE_MANAGER_INVALID_TYPE;
194     }
195 
196     for (const std::string& normalizedType : normalizedTypeVector) {
197         ret = GetDefaultApplicationInternal(userId, normalizedType, bundleInfo, backup);
198         if (ret == ERR_OK) {
199             return ret;
200         }
201     }
202     return ret;
203 }
204 
GetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,BundleInfo & bundleInfo,bool backup) const205 ErrCode DefaultAppMgr::GetDefaultApplicationInternal(
206     int32_t userId, const std::string& normalizedType, BundleInfo& bundleInfo, bool backup) const
207 {
208     std::lock_guard<std::mutex> lock(mutex_);
209     if (IsAppType(normalizedType)) {
210         return GetBundleInfoByAppType(userId, normalizedType, bundleInfo, backup);
211     }
212     return GetBundleInfoByUtd(userId, normalizedType, bundleInfo, backup);
213 }
214 
SetDefaultApplication(int32_t userId,const std::string & type,const Element & element) const215 ErrCode DefaultAppMgr::SetDefaultApplication(
216     int32_t userId, const std::string& type, const Element& element) const
217 {
218     LOG_I(BMS_TAG_DEFAULT, "SetDefault,userId:%{public}d,type:%{public}s", userId, type.c_str());
219 
220     ErrCode ret = VerifyPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION);
221     if (ret != ERR_OK) {
222         return ret;
223     }
224 
225     if (!IsUserIdExist(userId)) {
226         LOG_W(BMS_TAG_DEFAULT, "userId not exist");
227         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
228     }
229 
230     std::vector<std::string> normalizedTypeVector = Normalize(type);
231     LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
232     if (normalizedTypeVector.empty()) {
233         LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
234         return ERR_BUNDLE_MANAGER_INVALID_TYPE;
235     }
236 
237     bool isAnySet = false;
238     std::unordered_map<std::string, ErrCode> setResultMap;
239     for (const std::string& normalizedType : normalizedTypeVector) {
240         ret = SetDefaultApplicationInternal(userId, normalizedType, element);
241         setResultMap.try_emplace(normalizedType, ret);
242         if (ret == ERR_OK) {
243             isAnySet = true;
244         }
245     }
246     if (!isAnySet) {
247         return ret;
248     }
249     // set any success, clear failed records
250     for (const auto& item : setResultMap) {
251         if (item.second != ERR_OK) {
252             LOG_I(BMS_TAG_DEFAULT, "clear record,normalizedType:%{public}s", item.first.c_str());
253             Element element;
254             (void)SetDefaultApplicationInternal(userId, item.first, element);
255         }
256     }
257     return ERR_OK;
258 }
259 
SetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,const Element & element) const260 ErrCode DefaultAppMgr::SetDefaultApplicationInternal(
261     int32_t userId, const std::string& normalizedType, const Element& element) const
262 {
263     std::lock_guard<std::mutex> lock(mutex_);
264     // clear default app
265     bool ret = IsElementEmpty(element);
266     if (ret) {
267         LOG_I(BMS_TAG_DEFAULT, "clear default app");
268         ret = defaultAppDb_->DeleteDefaultApplicationInfo(userId, normalizedType);
269         if (!ret) {
270             LOG_W(BMS_TAG_DEFAULT, "DeleteDefaultApplicationInfo failed");
271             return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
272         }
273         LOG_D(BMS_TAG_DEFAULT, "SetDefaultApplication success");
274         return ERR_OK;
275     }
276     // set default app
277     ret = IsElementValid(userId, normalizedType, element);
278     if (!ret) {
279         LOG_W(BMS_TAG_DEFAULT, "invalid element");
280         return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
281     }
282     ret = defaultAppDb_->SetDefaultApplicationInfo(userId, normalizedType, element);
283     if (!ret) {
284         LOG_W(BMS_TAG_DEFAULT, "SetDefaultApplicationInfo failed");
285         return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
286     }
287     LOG_D(BMS_TAG_DEFAULT, "SetDefaultApplication success");
288     return ERR_OK;
289 }
290 
ResetDefaultApplication(int32_t userId,const std::string & type) const291 ErrCode DefaultAppMgr::ResetDefaultApplication(int32_t userId, const std::string& type) const
292 {
293     LOG_I(BMS_TAG_DEFAULT, "ResetDefault,userId:%{public}d,type:%{public}s", userId, type.c_str());
294 
295     ErrCode ret = VerifyPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION);
296     if (ret != ERR_OK) {
297         return ret;
298     }
299 
300     if (!IsUserIdExist(userId)) {
301         LOG_W(BMS_TAG_DEFAULT, "userId not exist");
302         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
303     }
304 
305     std::vector<std::string> normalizedTypeVector = Normalize(type);
306     LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
307     if (normalizedTypeVector.empty()) {
308         LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
309         return ERR_BUNDLE_MANAGER_INVALID_TYPE;
310     }
311 
312     bool isAnySet = false;
313     for (const std::string& normalizedType : normalizedTypeVector) {
314         ret = ResetDefaultApplicationInternal(userId, normalizedType);
315         if (ret == ERR_OK) {
316             isAnySet = true;
317         }
318     }
319     return isAnySet ? ERR_OK : ret;
320 }
321 
ResetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType) const322 ErrCode DefaultAppMgr::ResetDefaultApplicationInternal(int32_t userId, const std::string& normalizedType) const
323 {
324     std::lock_guard<std::mutex> lock(mutex_);
325     Element element;
326     bool ret = defaultAppDb_->GetDefaultApplicationInfo(INITIAL_USER_ID, normalizedType, element);
327     if (!ret) {
328         LOG_I(BMS_TAG_DEFAULT, "directly delete default info");
329         if (defaultAppDb_->DeleteDefaultApplicationInfo(userId, normalizedType)) {
330             return ERR_OK;
331         }
332         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
333     }
334     ret = IsElementValid(userId, normalizedType, element);
335     if (!ret) {
336         LOG_W(BMS_TAG_DEFAULT, "invalid element");
337         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
338     }
339     ret = defaultAppDb_->SetDefaultApplicationInfo(userId, normalizedType, element);
340     if (!ret) {
341         LOG_W(BMS_TAG_DEFAULT, "SetDefaultApplicationInfo failed");
342         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
343     }
344     LOG_D(BMS_TAG_DEFAULT, "ResetDefaultApplication success");
345     return ERR_OK;
346 }
347 
HandleUninstallBundle(int32_t userId,const std::string & bundleName) const348 void DefaultAppMgr::HandleUninstallBundle(int32_t userId, const std::string& bundleName) const
349 {
350     std::lock_guard<std::mutex> lock(mutex_);
351     LOG_D(BMS_TAG_DEFAULT, "begin");
352     std::map<std::string, Element> currentInfos;
353     bool ret = defaultAppDb_->GetDefaultApplicationInfos(userId, currentInfos);
354     if (!ret) {
355         LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
356         return;
357     }
358     // if type exist in default_app.json, use it
359     std::map<std::string, Element> newInfos;
360     for (const auto& item : currentInfos) {
361         if (item.second.bundleName == bundleName) {
362             Element element;
363             if (defaultAppDb_->GetDefaultApplicationInfo(INITIAL_USER_ID, item.first, element)) {
364                 LOG_I(BMS_TAG_DEFAULT, "set default application to preset, type : %{public}s", item.first.c_str());
365                 newInfos.emplace(item.first, element);
366             } else {
367                 LOG_D(BMS_TAG_DEFAULT, "erase uninstalled application type:%{public}s", item.first.c_str());
368             }
369         } else {
370             newInfos.emplace(item.first, item.second);
371         }
372     }
373     defaultAppDb_->SetDefaultApplicationInfos(userId, newInfos);
374 }
375 
HandleCreateUser(int32_t userId) const376 void DefaultAppMgr::HandleCreateUser(int32_t userId) const
377 {
378     std::lock_guard<std::mutex> lock(mutex_);
379     LOG_D(BMS_TAG_DEFAULT, "begin");
380     std::map<std::string, Element> infos;
381     bool ret = defaultAppDb_->GetDefaultApplicationInfos(INITIAL_USER_ID, infos);
382     if (!ret) {
383         LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
384         return;
385     }
386     defaultAppDb_->SetDefaultApplicationInfos(userId, infos);
387 }
388 
HandleRemoveUser(int32_t userId) const389 void DefaultAppMgr::HandleRemoveUser(int32_t userId) const
390 {
391     std::lock_guard<std::mutex> lock(mutex_);
392     LOG_D(BMS_TAG_DEFAULT, "begin");
393     defaultAppDb_->DeleteDefaultApplicationInfos(userId);
394 }
395 
IsBrowserWant(const Want & want) const396 bool DefaultAppMgr::IsBrowserWant(const Want& want) const
397 {
398     bool matchAction = want.GetAction() == ACTION_VIEW_DATA;
399     if (!matchAction) {
400         LOG_D(BMS_TAG_DEFAULT, "Action does not match, not browser want");
401         return false;
402     }
403     std::string uri = want.GetUriString();
404     bool matchUri = uri.rfind(HTTP_SCHEME, 0) == 0 || uri.rfind(HTTPS_SCHEME, 0) == 0;
405     if (!matchUri) {
406         LOG_D(BMS_TAG_DEFAULT, "Uri does not match, not browser want");
407         return false;
408     }
409     LOG_D(BMS_TAG_DEFAULT, "is browser want");
410     return true;
411 }
412 
IsEmailWant(const Want & want) const413 bool DefaultAppMgr::IsEmailWant(const Want& want) const
414 {
415     bool matchAction = want.GetAction() == EMAIL_ACTION;
416     if (!matchAction) {
417         LOG_D(BMS_TAG_DEFAULT, "Action does not match, not email want");
418         return false;
419     }
420     std::string uri = want.GetUriString();
421     bool matchUri = uri.rfind(EMAIL_SCHEME, 0) == 0;
422     if (!matchUri) {
423         LOG_D(BMS_TAG_DEFAULT, "Uri does not match, not email want");
424         return false;
425     }
426     LOG_D(BMS_TAG_DEFAULT, "is email want");
427     return true;
428 }
429 
GetTypeFromWant(const Want & want) const430 std::string DefaultAppMgr::GetTypeFromWant(const Want& want) const
431 {
432     if (IsBrowserWant(want)) {
433         return BROWSER;
434     }
435     if (IsEmailWant(want)) {
436         return EMAIL;
437     }
438     if (want.GetAction() != ACTION_VIEW_DATA) {
439         return Constants::EMPTY_STRING;
440     }
441     // get from type
442     std::string type = want.GetType();
443     if (!type.empty()) {
444         return type;
445     }
446     // get from uri
447     std::string uri = Skill::GetOptParamUri(want.GetUriString());
448     std::string suffix;
449     (void)MimeTypeMgr::GetUriSuffix(uri, suffix);
450     return suffix;
451 }
452 
GetDefaultApplication(const Want & want,const int32_t userId,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos,bool backup) const453 bool DefaultAppMgr::GetDefaultApplication(const Want& want, const int32_t userId,
454     std::vector<AbilityInfo>& abilityInfos, std::vector<ExtensionAbilityInfo>& extensionInfos, bool backup) const
455 {
456     std::string type = GetTypeFromWant(want);
457     LOG_I(BMS_TAG_DEFAULT, "backup(bool):%{public}d, type(want):%{public}s", backup, type.c_str());
458     if (type.empty()) {
459         LOG_W(BMS_TAG_DEFAULT, "type empty");
460         return false;
461     }
462     BundleInfo bundleInfo;
463     ErrCode ret = GetDefaultApplication(userId, type, bundleInfo, backup);
464     if (ret != ERR_OK) {
465         LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplication failed");
466         return false;
467     }
468 
469     std::string bundleName = want.GetElement().GetBundleName();
470     if (!bundleName.empty() && bundleName != bundleInfo.name) {
471         LOG_I(BMS_TAG_DEFAULT, "request bundleName : %{public}s, default bundleName : %{public}s not same",
472             bundleName.c_str(), bundleInfo.name.c_str());
473         return false;
474     }
475 
476     if (bundleInfo.abilityInfos.size() == 1) {
477         abilityInfos = bundleInfo.abilityInfos;
478         LOG_I(BMS_TAG_DEFAULT, "find default ability");
479         return true;
480     } else if (bundleInfo.extensionInfos.size() == 1) {
481         extensionInfos = bundleInfo.extensionInfos;
482         LOG_I(BMS_TAG_DEFAULT, "find default extension");
483         return true;
484     } else {
485         LOG_E(BMS_TAG_DEFAULT, "invalid bundleInfo");
486         return false;
487     }
488 }
489 
GetBundleInfoByAppType(int32_t userId,const std::string & appType,BundleInfo & bundleInfo,bool backup) const490 ErrCode DefaultAppMgr::GetBundleInfoByAppType(
491     int32_t userId, const std::string& appType, BundleInfo& bundleInfo, bool backup) const
492 {
493     int32_t key = backup ? ServiceConstants::BACKUP_DEFAULT_APP_KEY : userId;
494     Element element;
495     bool ret = defaultAppDb_->GetDefaultApplicationInfo(key, appType, element);
496     if (!ret) {
497         LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfo failed");
498         return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
499     }
500     ret = GetBundleInfo(userId, appType, element, bundleInfo);
501     if (!ret) {
502         LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo failed");
503         return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
504     }
505     LOG_I(BMS_TAG_DEFAULT, "get bundleInfo by appType success");
506     return ERR_OK;
507 }
508 
GetBundleInfoByUtd(int32_t userId,const std::string & utd,BundleInfo & bundleInfo,bool backup) const509 ErrCode DefaultAppMgr::GetBundleInfoByUtd(
510     int32_t userId, const std::string& utd, BundleInfo& bundleInfo, bool backup) const
511 {
512     int32_t key = backup ? ServiceConstants::BACKUP_DEFAULT_APP_KEY : userId;
513     std::map<std::string, Element> infos;
514     bool ret = defaultAppDb_->GetDefaultApplicationInfos(key, infos);
515     if (!ret) {
516         LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
517         return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
518     }
519     std::map<std::string, Element> defaultAppTypeInfos;
520     std::map<std::string, Element> defaultUtdInfos;
521     for (const auto& item : infos) {
522         if (IsAppType(item.first)) {
523             defaultAppTypeInfos.emplace(item.first, item.second);
524         } else {
525             defaultUtdInfos.emplace(item.first, item.second);
526         }
527     }
528     // match default app type
529     for (const auto& item : defaultAppTypeInfos) {
530         const auto iter = APP_TYPES.find(item.first);
531         if (iter == APP_TYPES.end()) {
532             continue;
533         }
534         Skill skill;
535         for (const auto& mimeType : iter->second) {
536             if (skill.MatchType(utd, mimeType) && GetBundleInfo(userId, utd, item.second, bundleInfo)) {
537                 LOG_I(BMS_TAG_DEFAULT, "match default app type success");
538                 return ERR_OK;
539             }
540         }
541     }
542     // match default utd
543     for (const auto& item : defaultUtdInfos) {
544         if (item.first == utd && GetBundleInfo(userId, utd, item.second, bundleInfo)) {
545             LOG_I(BMS_TAG_DEFAULT, "match default utd success");
546             return ERR_OK;
547         }
548     }
549     LOG_W(BMS_TAG_DEFAULT, "get bundleInfo by utd failed");
550     return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
551 }
552 
GetBundleInfo(int32_t userId,const std::string & type,const Element & element,BundleInfo & bundleInfo) const553 bool DefaultAppMgr::GetBundleInfo(int32_t userId, const std::string& type, const Element& element,
554     BundleInfo& bundleInfo) const
555 {
556     LOG_D(BMS_TAG_DEFAULT, "begin to GetBundleInfo");
557     bool ret = VerifyElementFormat(element);
558     if (!ret) {
559         LOG_W(BMS_TAG_DEFAULT, "invalid element format");
560         return false;
561     }
562     std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
563     if (dataMgr == nullptr) {
564         LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
565         return false;
566     }
567     AbilityInfo abilityInfo;
568     ExtensionAbilityInfo extensionInfo;
569     std::vector<Skill> skills;
570     // verify if element exists
571     ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
572     if (!ret) {
573         LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo, QueryInfoAndSkillsByElement failed");
574         return GetBrokerBundleInfo(element, bundleInfo);
575     }
576     // match type and skills
577     ret = IsMatch(type, skills);
578     if (!ret) {
579         LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo, type and skills not match");
580         return false;
581     }
582     ret = dataMgr->GetBundleInfo(element.bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
583     if (!ret) {
584         LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo failed");
585         return false;
586     }
587     bool isAbility = !element.abilityName.empty();
588     if (isAbility) {
589         bundleInfo.abilityInfos.emplace_back(abilityInfo);
590     } else {
591         bundleInfo.extensionInfos.emplace_back(extensionInfo);
592     }
593     LOG_D(BMS_TAG_DEFAULT, "GetBundleInfo success");
594     return true;
595 }
596 
MatchActionAndType(const std::string & action,const std::string & type,const std::vector<Skill> & skills) const597 bool DefaultAppMgr::MatchActionAndType(
598     const std::string& action, const std::string& type, const std::vector<Skill>& skills) const
599 {
600     LOG_D(BMS_TAG_DEFAULT, "begin, action : %{public}s, type : %{public}s", action.c_str(), type.c_str());
601     for (const Skill& skill : skills) {
602         auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), action);
603         if (item == skill.actions.cend()) {
604             continue;
605         }
606         for (const SkillUri& skillUri : skill.uris) {
607             if (skill.MatchType(type, skillUri.type)) {
608                 return true;
609             }
610         }
611     }
612     LOG_W(BMS_TAG_DEFAULT, "MatchActionAndType failed");
613     return false;
614 }
615 
IsMatch(const std::string & type,const std::vector<Skill> & skills) const616 bool DefaultAppMgr::IsMatch(const std::string& type, const std::vector<Skill>& skills) const
617 {
618     if (IsAppType(type)) {
619         return MatchAppType(type, skills);
620     }
621     return MatchUtd(type, skills);
622 }
623 
MatchAppType(const std::string & type,const std::vector<Skill> & skills) const624 bool DefaultAppMgr::MatchAppType(const std::string& type, const std::vector<Skill>& skills) const
625 {
626     LOG_D(BMS_TAG_DEFAULT, "begin to match app type, type : %{public}s", type.c_str());
627     if (type == BROWSER) {
628         return IsBrowserSkillsValid(skills);
629     }
630     if (type == EMAIL) {
631         return IsEmailSkillsValid(skills);
632     }
633     auto item = APP_TYPES.find(type);
634     if (item == APP_TYPES.end()) {
635         LOG_E(BMS_TAG_DEFAULT, "invalid app type : %{public}s", type.c_str());
636         return false;
637     }
638     for (const std::string& mimeType : item->second) {
639         if (MatchActionAndType(ACTION_VIEW_DATA, mimeType, skills)) {
640             return true;
641         }
642     }
643     return false;
644 }
645 
IsBrowserSkillsValid(const std::vector<Skill> & skills) const646 bool DefaultAppMgr::IsBrowserSkillsValid(const std::vector<Skill>& skills) const
647 {
648     LOG_D(BMS_TAG_DEFAULT, "begin to verify browser skills");
649     Want httpWant;
650     httpWant.SetAction(ACTION_VIEW_DATA);
651     httpWant.AddEntity(ENTITY_BROWSER);
652     httpWant.SetUri(HTTP);
653 
654     Want httpsWant;
655     httpsWant.SetAction(ACTION_VIEW_DATA);
656     httpsWant.AddEntity(ENTITY_BROWSER);
657     httpsWant.SetUri(HTTPS);
658     for (const Skill& skill : skills) {
659         if (skill.Match(httpsWant) || skill.Match(httpWant)) {
660             LOG_D(BMS_TAG_DEFAULT, "browser skills is valid");
661             return true;
662         }
663     }
664     LOG_W(BMS_TAG_DEFAULT, "browser skills is invalid");
665     return false;
666 }
667 
IsEmailSkillsValid(const std::vector<Skill> & skills) const668 bool DefaultAppMgr::IsEmailSkillsValid(const std::vector<Skill>& skills) const
669 {
670     LOG_D(BMS_TAG_DEFAULT, "begin to verify email skills");
671     Want want;
672     want.SetAction(EMAIL_ACTION);
673     want.SetUri(EMAIL_SCHEME);
674 
675     for (const Skill& skill : skills) {
676         if (skill.Match(want)) {
677             LOG_D(BMS_TAG_DEFAULT, "email skills valid");
678             return true;
679         }
680     }
681     LOG_W(BMS_TAG_DEFAULT, "email skills invalid");
682     return false;
683 }
684 
MatchUtd(const std::string & utd,const std::vector<Skill> & skills) const685 bool DefaultAppMgr::MatchUtd(const std::string& utd, const std::vector<Skill>& skills) const
686 {
687     LOG_D(BMS_TAG_DEFAULT, "utd : %{public}s", utd.c_str());
688     if (MatchActionAndType(ACTION_VIEW_DATA, utd, skills)) {
689         return true;
690     }
691     LOG_E(BMS_TAG_DEFAULT, "match utd failed");
692     return false;
693 }
694 
IsUserIdExist(int32_t userId) const695 bool DefaultAppMgr::IsUserIdExist(int32_t userId) const
696 {
697     std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
698     if (dataMgr == nullptr) {
699         LOG_W(BMS_TAG_DEFAULT, "get BundleDataMgr failed");
700         return false;
701     }
702     return dataMgr->HasUserId(userId);
703 }
704 
IsElementEmpty(const Element & element) const705 bool DefaultAppMgr::IsElementEmpty(const Element& element) const
706 {
707     return element.bundleName.empty() && element.moduleName.empty()
708         && element.abilityName.empty() && element.extensionName.empty();
709 }
710 
VerifyElementFormat(const Element & element)711 bool DefaultAppMgr::VerifyElementFormat(const Element& element)
712 {
713     const std::string& bundleName = element.bundleName;
714     const std::string& moduleName = element.moduleName;
715     const std::string& abilityName = element.abilityName;
716     const std::string& extensionName = element.extensionName;
717     if (bundleName.empty()) {
718         LOG_W(BMS_TAG_DEFAULT, "bundleName empty, bad Element format");
719         return false;
720     }
721     if (moduleName.empty()) {
722         LOG_W(BMS_TAG_DEFAULT, "moduleName empty, bad Element format");
723         return false;
724     }
725     if (abilityName.empty() && extensionName.empty()) {
726         LOG_W(BMS_TAG_DEFAULT, "abilityName and extensionName both empty, bad Element format");
727         return false;
728     }
729     if (!abilityName.empty() && !extensionName.empty()) {
730         LOG_W(BMS_TAG_DEFAULT, "abilityName and extensionName both non-empty, bad Element format");
731         return false;
732     }
733     return true;
734 }
735 
IsElementValid(int32_t userId,const std::string & type,const Element & element) const736 bool DefaultAppMgr::IsElementValid(int32_t userId, const std::string& type, const Element& element) const
737 {
738     bool ret = VerifyElementFormat(element);
739     if (!ret) {
740         LOG_W(BMS_TAG_DEFAULT, "VerifyElementFormat failed");
741         return false;
742     }
743     std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
744     if (dataMgr == nullptr) {
745         LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
746         return false;
747     }
748     AbilityInfo abilityInfo;
749     ExtensionAbilityInfo extensionInfo;
750     std::vector<Skill> skills;
751     // verify if element exists
752     ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
753     if (!ret) {
754         LOG_W(BMS_TAG_DEFAULT, "QueryInfoAndSkillsByElement failed");
755         BundleInfo bundleInfo;
756         return GetBrokerBundleInfo(element, bundleInfo);
757     }
758     // match type and skills
759     ret = IsMatch(type, skills);
760     if (!ret) {
761         LOG_W(BMS_TAG_DEFAULT, "type and skills not match");
762         return false;
763     }
764     LOG_D(BMS_TAG_DEFAULT, "Element is valid");
765     return true;
766 }
767 
GetBrokerBundleInfo(const Element & element,BundleInfo & bundleInfo) const768 bool DefaultAppMgr::GetBrokerBundleInfo(const Element& element, BundleInfo& bundleInfo) const
769 {
770     if (element.bundleName.empty() || element.abilityName.empty()) {
771         LOG_W(BMS_TAG_DEFAULT, "invalid param, get broker bundleInfo failed");
772         return false;
773     }
774     if (!DelayedSingleton<BundleMgrService>::GetInstance()->IsBrokerServiceStarted()) {
775         LOG_W(BMS_TAG_DEFAULT, "broker not started, get broker bundleInfo failed");
776         return false;
777     }
778     Want want;
779     ElementName elementName("", element.bundleName, element.abilityName, element.moduleName);
780     want.SetElement(elementName);
781     AbilityInfo abilityInfo;
782     auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
783     ErrCode ret = bmsExtensionClient->QueryAbilityInfo(want, 0, Constants::START_USERID, abilityInfo, true);
784     if (ret != ERR_OK) {
785         LOG_W(BMS_TAG_DEFAULT, "query abilityInfo from broker failed");
786         return false;
787     }
788     bundleInfo.name = abilityInfo.bundleName;
789     bundleInfo.abilityInfos.emplace_back(abilityInfo);
790     LOG_I(BMS_TAG_DEFAULT, "get broker bundleInfo success");
791     return true;
792 }
793 
VerifyPermission(const std::string & permissionName) const794 ErrCode DefaultAppMgr::VerifyPermission(const std::string& permissionName) const
795 {
796     if (!BundlePermissionMgr::IsSystemApp()) {
797         LOG_W(BMS_TAG_DEFAULT, "non-system app calling system api");
798         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
799     }
800     if (!BundlePermissionMgr::IsSelfCalling() &&
801         !BundlePermissionMgr::VerifyCallingPermissionForAll(permissionName)) {
802         LOG_W(BMS_TAG_DEFAULT, "verify permission %{public}s failed", permissionName.c_str());
803         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
804     }
805     return ERR_OK;
806 }
807 
Normalize(const std::string & param)808 std::vector<std::string> DefaultAppMgr::Normalize(const std::string& param)
809 {
810     if (IsAppType(param)) {
811         return {param};
812     }
813     if (BundleUtil::IsUtd(param)) {
814         if (BundleUtil::IsSpecificUtd(param)) {
815             return {param};
816         }
817         return {};
818     }
819     if (IsSpecificMimeType(param)) {
820         return BundleUtil::GetUtdVectorByMimeType(param);
821     }
822     std::vector<std::string> utdVector;
823     if (!MimeTypeMgr::GetUtdVectorByUri(param, utdVector)) {
824         LOG_W(BMS_TAG_DEFAULT, "GetUtdVectorByUri failed");
825         return {};
826     }
827     return utdVector;
828 }
829 
IsAppType(const std::string & param)830 bool DefaultAppMgr::IsAppType(const std::string& param)
831 {
832     return supportAppTypes.find(param) != supportAppTypes.end();
833 }
834 
IsSpecificMimeType(const std::string & param)835 bool DefaultAppMgr::IsSpecificMimeType(const std::string& param)
836 {
837     // valid mimeType format : type/subType
838     if (param.find(WILDCARD) != param.npos) {
839         LOG_W(BMS_TAG_DEFAULT, "specific mimeType not allow contains *");
840         return false;
841     }
842     std::vector<std::string> vector;
843     SplitStr(param, SPLIT, vector, false, false);
844     if (vector.size() == TYPE_PART_COUNT && !vector[INDEX_ZERO].empty() && !vector[INDEX_ONE].empty()) {
845         return true;
846     }
847     LOG_W(BMS_TAG_DEFAULT, "not specific mimeType");
848     return false;
849 }
850 }
851 }