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 int8_t INITIAL_USER_ID = -1;
36 constexpr int8_t TYPE_PART_COUNT = 2;
37 constexpr int8_t INDEX_ZERO = 0;
38 constexpr int8_t INDEX_ONE = 1;
39 constexpr uint16_t TYPE_MAX_SIZE = 200;
40 constexpr const char* SPLIT = "/";
41 constexpr const char* SCHEME_SIGN = "://";
42 constexpr const char* EMAIL_ACTION = "ohos.want.action.sendToData";
43 constexpr const char* EMAIL_SCHEME = "mailto";
44 constexpr const char* ENTITY_BROWSER = "entity.system.browsable";
45 constexpr const char* HTTP = "http";
46 constexpr const char* HTTPS = "https";
47 constexpr const char* HTTP_SCHEME = "http://";
48 constexpr const char* HTTPS_SCHEME = "https://";
49 constexpr const char* FILE_SCHEME = "file://";
50 constexpr const char* CONTENT_SCHEME = "content://";
51 constexpr const char* WILDCARD = "*";
52 constexpr const char* PARAM_SEPARATOR = "?";
53 constexpr const char* PARAM_ANONYMIZE = "?***";
54 constexpr const char* BROWSER = "BROWSER";
55 constexpr const char* IMAGE = "IMAGE";
56 constexpr const char* AUDIO = "AUDIO";
57 constexpr const char* VIDEO = "VIDEO";
58 constexpr const char* PDF = "PDF";
59 constexpr const char* WORD = "WORD";
60 constexpr const char* EXCEL = "EXCEL";
61 constexpr const char* PPT = "PPT";
62 constexpr const char* EMAIL = "EMAIL";
63 constexpr const char* ACTION_VIEW_DATA = "ohos.want.action.viewData";
64 constexpr const char* APP_TYPES_KEY[] = {
65 IMAGE, AUDIO, VIDEO, PDF, WORD, EXCEL, PPT
66 };
67 const std::set<std::string> APP_TYPES_VALUE[] = {
68 {"general.image"},
69 {"general.audio"},
70 {"general.video"},
71 {"com.adobe.pdf"},
72 {"com.microsoft.word.doc",
73 "com.microsoft.word.dot",
74 "org.openxmlformats.wordprocessingml.document",
75 "org.openxmlformats.wordprocessingml.template",
76 "org.openxmlformats.wordprocessingml.document.macroenabled",
77 "org.openxmlformats.wordprocessingml.template.macroenabled"},
78 {"com.microsoft.excel.xls",
79 "com.microsoft.excel.xlt",
80 "com.microsoft.excel.dif",
81 "org.openxmlformats.spreadsheetml.sheet",
82 "org.openxmlformats.spreadsheetml.template",
83 "org.openxmlformats.spreadsheetml.template.macroenabled",
84 "org.openxmlformats.spreadsheetml.addin.macroenabled",
85 "org.openxmlformats.spreadsheetml.binary.macroenabled",
86 "org.openxmlformats.spreadsheetml.sheet.macroenabled"},
87 {"com.microsoft.powerpoint.ppt",
88 "com.microsoft.powerpoint.pps",
89 "com.microsoft.powerpoint.pot",
90 "org.openxmlformats.presentationml.presentation",
91 "org.openxmlformats.presentationml.template",
92 "org.openxmlformats.presentationml.slideshow",
93 "org.openxmlformats.presentationml.addin.macroenabled",
94 "org.openxmlformats.presentationml.presentation.macroenabled",
95 "org.openxmlformats.presentationml.slideshow.macroenabled",
96 "org.openxmlformats.presentationml.template.macroenabled"}
97 };
98 const std::set<std::string> supportAppTypes = {BROWSER, IMAGE, AUDIO, VIDEO, PDF, WORD, EXCEL, PPT, EMAIL};
99 }
100
GetInstance()101 DefaultAppMgr& DefaultAppMgr::GetInstance()
102 {
103 static DefaultAppMgr defaultAppMgr;
104 return defaultAppMgr;
105 }
106
DefaultAppMgr()107 DefaultAppMgr::DefaultAppMgr()
108 {
109 LOG_D(BMS_TAG_DEFAULT, "create DefaultAppMgr");
110 Init();
111 }
112
~DefaultAppMgr()113 DefaultAppMgr::~DefaultAppMgr()
114 {
115 LOG_D(BMS_TAG_DEFAULT, "destroy DefaultAppMgr");
116 defaultAppDb_->UnRegisterDeathListener();
117 }
118
Init()119 void DefaultAppMgr::Init()
120 {
121 defaultAppDb_ = std::make_shared<DefaultAppRdb>();
122 defaultAppDb_->RegisterDeathListener();
123 }
124
IsDefaultApplication(int32_t userId,const std::string & type,bool & isDefaultApp) const125 ErrCode DefaultAppMgr::IsDefaultApplication(int32_t userId, const std::string& type, bool& isDefaultApp) const
126 {
127 LOG_I(BMS_TAG_DEFAULT, "IsDefault,userId:%{public}d,type:%{private}s", userId, type.c_str());
128 if (type.size() > TYPE_MAX_SIZE) {
129 LOG_W(BMS_TAG_DEFAULT, "type size too large");
130 isDefaultApp = false;
131 return ERR_OK;
132 }
133
134 if (!IsUserIdExist(userId)) {
135 LOG_W(BMS_TAG_DEFAULT, "userId not exist");
136 isDefaultApp = false;
137 return ERR_OK;
138 }
139
140 std::vector<std::string> normalizedTypeVector = Normalize(type);
141 LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
142 if (normalizedTypeVector.empty()) {
143 LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
144 isDefaultApp = false;
145 return ERR_OK;
146 }
147
148 for (const std::string& normalizedType : normalizedTypeVector) {
149 (void)IsDefaultApplicationInternal(userId, normalizedType, isDefaultApp);
150 if (isDefaultApp) {
151 return ERR_OK;
152 }
153 }
154 return ERR_OK;
155 }
156
IsDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,bool & isDefaultApp) const157 ErrCode DefaultAppMgr::IsDefaultApplicationInternal(
158 int32_t userId, const std::string& normalizedType, bool& isDefaultApp) const
159 {
160 std::lock_guard<std::mutex> lock(mutex_);
161 Element element;
162 bool ret = defaultAppDb_->GetDefaultApplicationInfo(userId, normalizedType, element);
163 if (!ret) {
164 LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplicationInfo failed");
165 isDefaultApp = false;
166 return ERR_OK;
167 }
168 ret = IsElementValid(userId, normalizedType, element);
169 if (!ret) {
170 LOG_W(BMS_TAG_DEFAULT, "invalid element");
171 isDefaultApp = false;
172 return ERR_OK;
173 }
174 // get bundle name via calling uid
175 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
176 if (dataMgr == nullptr) {
177 LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
178 isDefaultApp = false;
179 return ERR_OK;
180 }
181 std::string callingBundleName;
182 ret = dataMgr->GetBundleNameForUid(IPCSkeleton::GetCallingUid(), callingBundleName);
183 if (!ret) {
184 LOG_W(BMS_TAG_DEFAULT, "GetBundleNameForUid failed");
185 isDefaultApp = false;
186 return ERR_OK;
187 }
188 LOG_I(BMS_TAG_DEFAULT, "callingBundleName:%{public}s", callingBundleName.c_str());
189 isDefaultApp = element.bundleName == callingBundleName;
190 return ERR_OK;
191 }
192
GetDefaultApplication(int32_t userId,const std::string & type,BundleInfo & bundleInfo,bool backup) const193 ErrCode DefaultAppMgr::GetDefaultApplication(
194 int32_t userId, const std::string& type, BundleInfo& bundleInfo, bool backup) const
195 {
196 LOG_I(BMS_TAG_DEFAULT, "GetDefault,userId:%{public}d,type:%{private}s,backup(bool):%{public}d",
197 userId, type.c_str(), backup);
198
199 ErrCode ret = VerifyPermission(Constants::PERMISSION_GET_DEFAULT_APPLICATION);
200 if (ret != ERR_OK) {
201 return ret;
202 }
203
204 if (!IsUserIdExist(userId)) {
205 LOG_W(BMS_TAG_DEFAULT, "userId not exist");
206 return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
207 }
208
209 std::vector<std::string> normalizedTypeVector = Normalize(type);
210 LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
211 if (normalizedTypeVector.empty()) {
212 LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
213 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
214 }
215
216 for (const std::string& normalizedType : normalizedTypeVector) {
217 ret = GetDefaultApplicationInternal(userId, normalizedType, bundleInfo, backup);
218 if (ret == ERR_OK) {
219 return ret;
220 }
221 }
222 return ret;
223 }
224
GetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,BundleInfo & bundleInfo,bool backup) const225 ErrCode DefaultAppMgr::GetDefaultApplicationInternal(
226 int32_t userId, const std::string& normalizedType, BundleInfo& bundleInfo, bool backup) const
227 {
228 std::lock_guard<std::mutex> lock(mutex_);
229 if (IsAppType(normalizedType)) {
230 return GetBundleInfoByAppType(userId, normalizedType, bundleInfo, backup);
231 }
232 return GetBundleInfoByUtd(userId, normalizedType, bundleInfo, backup);
233 }
234
SetDefaultApplication(int32_t userId,const std::string & type,const Element & element) const235 ErrCode DefaultAppMgr::SetDefaultApplication(
236 int32_t userId, const std::string& type, const Element& element) const
237 {
238 LOG_I(BMS_TAG_DEFAULT, "SetDefault,userId:%{public}d,type:%{private}s", userId, type.c_str());
239
240 ErrCode ret = VerifyPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION);
241 if (ret != ERR_OK) {
242 return ret;
243 }
244
245 if (!IsUserIdExist(userId)) {
246 LOG_W(BMS_TAG_DEFAULT, "userId not exist");
247 return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
248 }
249
250 std::vector<std::string> normalizedTypeVector = Normalize(type);
251 LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
252 if (normalizedTypeVector.empty()) {
253 LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
254 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
255 }
256 std::unordered_map<std::string, std::pair<bool, Element>> originStateMap;
257 GetDefaultInfo(userId, normalizedTypeVector, originStateMap);
258 bool isAnySet = false;
259 std::unordered_map<std::string, ErrCode> setResultMap;
260 for (const std::string& normalizedType : normalizedTypeVector) {
261 ret = SetDefaultApplicationInternal(userId, normalizedType, element);
262 setResultMap.try_emplace(normalizedType, ret);
263 if (ret == ERR_OK) {
264 isAnySet = true;
265 }
266 }
267 if (!isAnySet) {
268 return ret;
269 }
270 // set any success, clear failed records
271 for (const auto& item : setResultMap) {
272 if (item.second != ERR_OK) {
273 LOG_I(BMS_TAG_DEFAULT, "clear record,normalizedType:%{public}s", item.first.c_str());
274 Element element;
275 (void)SetDefaultApplicationInternal(userId, item.first, element);
276 }
277 }
278 SendDefaultAppChangeEventIfNeeded(userId, normalizedTypeVector, originStateMap);
279 return ERR_OK;
280 }
281
SetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType,const Element & element) const282 ErrCode DefaultAppMgr::SetDefaultApplicationInternal(
283 int32_t userId, const std::string& normalizedType, const Element& element) const
284 {
285 std::lock_guard<std::mutex> lock(mutex_);
286 // clear default app
287 bool ret = IsElementEmpty(element);
288 if (ret) {
289 LOG_I(BMS_TAG_DEFAULT, "clear default app");
290 ret = defaultAppDb_->DeleteDefaultApplicationInfo(userId, normalizedType);
291 if (!ret) {
292 LOG_W(BMS_TAG_DEFAULT, "DeleteDefaultApplicationInfo failed");
293 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
294 }
295 LOG_D(BMS_TAG_DEFAULT, "SetDefaultApplication success");
296 return ERR_OK;
297 }
298 // set default app
299 ret = IsElementValid(userId, normalizedType, element);
300 if (!ret) {
301 LOG_W(BMS_TAG_DEFAULT, "invalid element");
302 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
303 }
304 ret = defaultAppDb_->SetDefaultApplicationInfo(userId, normalizedType, element);
305 if (!ret) {
306 LOG_W(BMS_TAG_DEFAULT, "SetDefaultApplicationInfo failed");
307 return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
308 }
309 LOG_D(BMS_TAG_DEFAULT, "SetDefaultApplication success");
310 return ERR_OK;
311 }
312
ResetDefaultApplication(int32_t userId,const std::string & type) const313 ErrCode DefaultAppMgr::ResetDefaultApplication(int32_t userId, const std::string& type) const
314 {
315 LOG_I(BMS_TAG_DEFAULT, "ResetDefault,userId:%{public}d,type:%{private}s", userId, type.c_str());
316 ErrCode ret = VerifyPermission(Constants::PERMISSION_SET_DEFAULT_APPLICATION);
317 if (ret != ERR_OK) {
318 return ret;
319 }
320
321 if (!IsUserIdExist(userId)) {
322 LOG_W(BMS_TAG_DEFAULT, "userId not exist");
323 return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
324 }
325
326 std::vector<std::string> normalizedTypeVector = Normalize(type);
327 LOG_I(BMS_TAG_DEFAULT, "normalized:%{public}s", BundleUtil::ToString(normalizedTypeVector).c_str());
328 if (normalizedTypeVector.empty()) {
329 LOG_W(BMS_TAG_DEFAULT, "normalizedTypeVector empty");
330 return ERR_BUNDLE_MANAGER_INVALID_TYPE;
331 }
332
333 std::unordered_map<std::string, std::pair<bool, Element>> originStateMap;
334 GetDefaultInfo(userId, normalizedTypeVector, originStateMap);
335 bool isAnySet = false;
336 for (const std::string& normalizedType : normalizedTypeVector) {
337 ret = ResetDefaultApplicationInternal(userId, normalizedType);
338 if (ret == ERR_OK) {
339 isAnySet = true;
340 }
341 }
342
343 SendDefaultAppChangeEventIfNeeded(userId, normalizedTypeVector, originStateMap);
344 return isAnySet ? ERR_OK : ret;
345 }
346
ResetDefaultApplicationInternal(int32_t userId,const std::string & normalizedType) const347 ErrCode DefaultAppMgr::ResetDefaultApplicationInternal(int32_t userId, const std::string& normalizedType) const
348 {
349 std::lock_guard<std::mutex> lock(mutex_);
350 Element element;
351 bool ret = defaultAppDb_->GetDefaultApplicationInfo(INITIAL_USER_ID, normalizedType, element);
352 if (!ret) {
353 LOG_I(BMS_TAG_DEFAULT, "directly delete default info");
354 if (defaultAppDb_->DeleteDefaultApplicationInfo(userId, normalizedType)) {
355 return ERR_OK;
356 }
357 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
358 }
359 ret = IsElementValid(userId, normalizedType, element);
360 if (!ret) {
361 LOG_W(BMS_TAG_DEFAULT, "invalid element");
362 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
363 }
364 ret = defaultAppDb_->SetDefaultApplicationInfo(userId, normalizedType, element);
365 if (!ret) {
366 LOG_W(BMS_TAG_DEFAULT, "SetDefaultApplicationInfo failed");
367 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
368 }
369 LOG_D(BMS_TAG_DEFAULT, "ResetDefaultApplication success");
370 return ERR_OK;
371 }
372
HandleUninstallBundle(int32_t userId,const std::string & bundleName) const373 void DefaultAppMgr::HandleUninstallBundle(int32_t userId, const std::string& bundleName) const
374 {
375 std::lock_guard<std::mutex> lock(mutex_);
376 LOG_D(BMS_TAG_DEFAULT, "begin");
377 std::map<std::string, Element> currentInfos;
378 bool ret = defaultAppDb_->GetDefaultApplicationInfos(userId, currentInfos);
379 if (!ret) {
380 LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
381 return;
382 }
383 // if type exist in default_app.json, use it
384 std::map<std::string, Element> newInfos;
385 std::vector<std::string> changedTypeVec;
386 for (const auto& item : currentInfos) {
387 if (item.second.bundleName != bundleName) {
388 newInfos.emplace(item.first, item.second);
389 continue;
390 }
391 Element element;
392 if (defaultAppDb_->GetDefaultApplicationInfo(INITIAL_USER_ID, item.first, element)) {
393 LOG_I(BMS_TAG_DEFAULT, "set default application to preset, type : %{public}s", item.first.c_str());
394 newInfos.emplace(item.first, element);
395 } else {
396 LOG_D(BMS_TAG_DEFAULT, "erase uninstalled application type:%{public}s", item.first.c_str());
397 }
398 changedTypeVec.emplace_back(item.first);
399 }
400 defaultAppDb_->SetDefaultApplicationInfos(userId, newInfos);
401 (void)SendDefaultAppChangeEvent(userId, changedTypeVec);
402 }
403
HandleCreateUser(int32_t userId) const404 void DefaultAppMgr::HandleCreateUser(int32_t userId) const
405 {
406 std::lock_guard<std::mutex> lock(mutex_);
407 LOG_D(BMS_TAG_DEFAULT, "begin");
408 std::map<std::string, Element> infos;
409 bool ret = defaultAppDb_->GetDefaultApplicationInfos(INITIAL_USER_ID, infos);
410 if (!ret) {
411 LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
412 return;
413 }
414 defaultAppDb_->SetDefaultApplicationInfos(userId, infos);
415 }
416
HandleRemoveUser(int32_t userId) const417 void DefaultAppMgr::HandleRemoveUser(int32_t userId) const
418 {
419 std::lock_guard<std::mutex> lock(mutex_);
420 LOG_D(BMS_TAG_DEFAULT, "begin");
421 defaultAppDb_->DeleteDefaultApplicationInfos(userId);
422 }
423
IsBrowserWant(const Want & want) const424 bool DefaultAppMgr::IsBrowserWant(const Want& want) const
425 {
426 bool matchAction = want.GetAction() == ACTION_VIEW_DATA;
427 if (!matchAction) {
428 LOG_D(BMS_TAG_DEFAULT, "Action does not match, not browser want");
429 return false;
430 }
431 std::string uri = want.GetUriString();
432 bool matchUri = uri.rfind(HTTP_SCHEME, 0) == 0 || uri.rfind(HTTPS_SCHEME, 0) == 0;
433 if (!matchUri) {
434 LOG_D(BMS_TAG_DEFAULT, "Uri does not match, not browser want");
435 return false;
436 }
437 LOG_D(BMS_TAG_DEFAULT, "is browser want");
438 return true;
439 }
440
IsEmailWant(const Want & want) const441 bool DefaultAppMgr::IsEmailWant(const Want& want) const
442 {
443 bool matchAction = want.GetAction() == EMAIL_ACTION;
444 if (!matchAction) {
445 LOG_D(BMS_TAG_DEFAULT, "Action does not match, not email want");
446 return false;
447 }
448 std::string uri = want.GetUriString();
449 bool matchUri = uri.rfind(EMAIL_SCHEME, 0) == 0;
450 if (!matchUri) {
451 LOG_D(BMS_TAG_DEFAULT, "Uri does not match, not email want");
452 return false;
453 }
454 LOG_D(BMS_TAG_DEFAULT, "is email want");
455 return true;
456 }
457
GetTypeFromWant(const Want & want) const458 std::string DefaultAppMgr::GetTypeFromWant(const Want& want) const
459 {
460 if (IsBrowserWant(want)) {
461 return BROWSER;
462 }
463 if (IsEmailWant(want)) {
464 return EMAIL;
465 }
466 if (want.GetAction() != ACTION_VIEW_DATA) {
467 return Constants::EMPTY_STRING;
468 }
469 std::string uri = Skill::GetOptParamUri(want.GetUriString());
470 bool containsScheme = uri.find(SCHEME_SIGN) != std::string::npos;
471 bool isLocalScheme = uri.rfind(FILE_SCHEME, 0) == 0 || uri.rfind(CONTENT_SCHEME, 0) == 0;
472 if (containsScheme && !isLocalScheme) {
473 LOG_D(BMS_TAG_DEFAULT, "not local scheme");
474 return Constants::EMPTY_STRING;
475 }
476 // get from type
477 std::string type = want.GetType();
478 if (!type.empty()) {
479 return type;
480 }
481 // get from uri
482 std::string suffix;
483 (void)MimeTypeMgr::GetUriSuffix(uri, suffix);
484 return suffix;
485 }
486
GetDefaultApplication(const Want & want,const int32_t userId,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos,bool backup) const487 bool DefaultAppMgr::GetDefaultApplication(const Want& want, const int32_t userId,
488 std::vector<AbilityInfo>& abilityInfos, std::vector<ExtensionAbilityInfo>& extensionInfos, bool backup) const
489 {
490 std::string type = GetTypeFromWant(want);
491 LOG_I(BMS_TAG_DEFAULT, "backup(bool):%{public}d, type(want):%{private}s", backup, type.c_str());
492 if (type.empty()) {
493 LOG_W(BMS_TAG_DEFAULT, "type empty");
494 return false;
495 }
496 BundleInfo bundleInfo;
497 ErrCode ret = GetDefaultApplication(userId, type, bundleInfo, backup);
498 if (ret != ERR_OK) {
499 LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplication failed");
500 return false;
501 }
502
503 std::string bundleName = want.GetElement().GetBundleName();
504 if (!bundleName.empty() && bundleName != bundleInfo.name) {
505 LOG_I(BMS_TAG_DEFAULT, "request bundleName : %{public}s, default bundleName : %{public}s not same",
506 bundleName.c_str(), bundleInfo.name.c_str());
507 return false;
508 }
509
510 if (bundleInfo.abilityInfos.size() == 1) {
511 abilityInfos = bundleInfo.abilityInfos;
512 LOG_I(BMS_TAG_DEFAULT, "find default ability");
513 return true;
514 } else if (bundleInfo.extensionInfos.size() == 1) {
515 extensionInfos = bundleInfo.extensionInfos;
516 LOG_I(BMS_TAG_DEFAULT, "find default extension");
517 return true;
518 } else {
519 LOG_E(BMS_TAG_DEFAULT, "invalid bundleInfo");
520 return false;
521 }
522 }
523
GetBundleInfoByAppType(int32_t userId,const std::string & appType,BundleInfo & bundleInfo,bool backup) const524 ErrCode DefaultAppMgr::GetBundleInfoByAppType(
525 int32_t userId, const std::string& appType, BundleInfo& bundleInfo, bool backup) const
526 {
527 int32_t key = backup ? ServiceConstants::BACKUP_DEFAULT_APP_KEY : userId;
528 Element element;
529 bool ret = defaultAppDb_->GetDefaultApplicationInfo(key, appType, element);
530 if (!ret) {
531 LOG_W(BMS_TAG_DEFAULT, "GetDefaultApplicationInfo failed");
532 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
533 }
534 ret = GetBundleInfo(userId, appType, element, bundleInfo);
535 if (!ret) {
536 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo failed");
537 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
538 }
539 LOG_I(BMS_TAG_DEFAULT, "get bundleInfo by appType success");
540 return ERR_OK;
541 }
542
GetBundleInfoByUtd(int32_t userId,const std::string & utd,BundleInfo & bundleInfo,bool backup) const543 ErrCode DefaultAppMgr::GetBundleInfoByUtd(
544 int32_t userId, const std::string& utd, BundleInfo& bundleInfo, bool backup) const
545 {
546 int32_t key = backup ? ServiceConstants::BACKUP_DEFAULT_APP_KEY : userId;
547 std::map<std::string, Element> infos;
548 bool ret = defaultAppDb_->GetDefaultApplicationInfos(key, infos);
549 if (!ret) {
550 LOG_I(BMS_TAG_DEFAULT, "GetDefaultApplicationInfos failed");
551 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
552 }
553 std::map<std::string, Element> defaultAppTypeInfos;
554 std::map<std::string, Element> defaultUtdInfos;
555 for (const auto& item : infos) {
556 if (IsAppType(item.first)) {
557 defaultAppTypeInfos.emplace(item.first, item.second);
558 } else {
559 defaultUtdInfos.emplace(item.first, item.second);
560 }
561 }
562 // match default app type
563 size_t len = sizeof(APP_TYPES_KEY) / sizeof(APP_TYPES_KEY[0]);
564 for (const auto& item : defaultAppTypeInfos) {
565 size_t i = 0;
566 for (i = 0; i < len; i++) {
567 if (APP_TYPES_KEY[i] == item.first) break;
568 }
569 if (i == len) continue;
570 Skill skill;
571 for (const auto& utdId : APP_TYPES_VALUE[i]) {
572 if (skill.MatchType(utd, utdId) && GetBundleInfo(userId, utd, item.second, bundleInfo)) {
573 LOG_I(BMS_TAG_DEFAULT, "match default app type success");
574 return ERR_OK;
575 }
576 }
577 }
578 // match default utd
579 for (const auto& item : defaultUtdInfos) {
580 if (item.first == utd && GetBundleInfo(userId, utd, item.second, bundleInfo)) {
581 LOG_I(BMS_TAG_DEFAULT, "match default utd success");
582 return ERR_OK;
583 }
584 }
585 LOG_W(BMS_TAG_DEFAULT, "get bundleInfo by utd failed");
586 return ERR_BUNDLE_MANAGER_DEFAULT_APP_NOT_EXIST;
587 }
588
GetBundleInfo(int32_t userId,const std::string & type,const Element & element,BundleInfo & bundleInfo) const589 bool DefaultAppMgr::GetBundleInfo(int32_t userId, const std::string& type, const Element& element,
590 BundleInfo& bundleInfo) const
591 {
592 LOG_D(BMS_TAG_DEFAULT, "begin to GetBundleInfo");
593 bool ret = VerifyElementFormat(element);
594 if (!ret) {
595 LOG_W(BMS_TAG_DEFAULT, "invalid element format");
596 return false;
597 }
598 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
599 if (dataMgr == nullptr) {
600 LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
601 return false;
602 }
603 AbilityInfo abilityInfo;
604 ExtensionAbilityInfo extensionInfo;
605 std::vector<Skill> skills;
606 // verify if element exists
607 ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
608 if (!ret) {
609 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo, QueryInfoAndSkillsByElement failed");
610 return GetBrokerBundleInfo(element, bundleInfo);
611 }
612 // match type and skills
613 ret = IsMatch(type, skills);
614 if (!ret) {
615 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo, type and skills not match");
616 return false;
617 }
618 ret = dataMgr->GetBundleInfo(element.bundleName, GET_BUNDLE_DEFAULT, bundleInfo, userId);
619 if (!ret) {
620 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfo failed");
621 return false;
622 }
623 bool isAbility = !element.abilityName.empty();
624 if (isAbility) {
625 bundleInfo.abilityInfos.emplace_back(abilityInfo);
626 } else {
627 bundleInfo.extensionInfos.emplace_back(extensionInfo);
628 }
629 LOG_D(BMS_TAG_DEFAULT, "GetBundleInfo success");
630 return true;
631 }
632
MatchActionAndType(const std::string & action,const std::string & type,const std::vector<Skill> & skills) const633 bool DefaultAppMgr::MatchActionAndType(
634 const std::string& action, const std::string& type, const std::vector<Skill>& skills) const
635 {
636 LOG_D(BMS_TAG_DEFAULT, "begin, action : %{public}s, type : %{public}s", action.c_str(), type.c_str());
637 for (const Skill& skill : skills) {
638 auto item = std::find(skill.actions.cbegin(), skill.actions.cend(), action);
639 if (item == skill.actions.cend()) {
640 continue;
641 }
642 for (const SkillUri& skillUri : skill.uris) {
643 if (skill.MatchType(type, skillUri.type)) {
644 return true;
645 }
646 }
647 }
648 LOG_W(BMS_TAG_DEFAULT, "MatchActionAndType failed");
649 return false;
650 }
651
IsMatch(const std::string & type,const std::vector<Skill> & skills) const652 bool DefaultAppMgr::IsMatch(const std::string& type, const std::vector<Skill>& skills) const
653 {
654 if (IsAppType(type)) {
655 return MatchAppType(type, skills);
656 }
657 return MatchUtd(type, skills);
658 }
659
MatchAppType(const std::string & type,const std::vector<Skill> & skills) const660 bool DefaultAppMgr::MatchAppType(const std::string& type, const std::vector<Skill>& skills) const
661 {
662 LOG_D(BMS_TAG_DEFAULT, "begin to match app type, type : %{public}s", type.c_str());
663 if (type == BROWSER) {
664 return IsBrowserSkillsValid(skills);
665 }
666 if (type == EMAIL) {
667 return IsEmailSkillsValid(skills);
668 }
669 size_t i = 0;
670 size_t len = sizeof(APP_TYPES_KEY) / sizeof(APP_TYPES_KEY[0]);
671 for (i = 0; i < len; i++) {
672 if (APP_TYPES_KEY[i] == type) break;
673 }
674 if (i == len) {
675 LOG_E(BMS_TAG_DEFAULT, "invalid app type : %{public}s", type.c_str());
676 return false;
677 }
678 for (const std::string& utdId : APP_TYPES_VALUE[i]) {
679 if (MatchActionAndType(ACTION_VIEW_DATA, utdId, skills)) {
680 return true;
681 }
682 }
683 return false;
684 }
685
IsBrowserSkillsValid(const std::vector<Skill> & skills) const686 bool DefaultAppMgr::IsBrowserSkillsValid(const std::vector<Skill>& skills) const
687 {
688 LOG_D(BMS_TAG_DEFAULT, "begin to verify browser skills");
689 Want httpWant;
690 httpWant.SetAction(ACTION_VIEW_DATA);
691 httpWant.AddEntity(ENTITY_BROWSER);
692 httpWant.SetUri(HTTP);
693
694 Want httpsWant;
695 httpsWant.SetAction(ACTION_VIEW_DATA);
696 httpsWant.AddEntity(ENTITY_BROWSER);
697 httpsWant.SetUri(HTTPS);
698 for (const Skill& skill : skills) {
699 if (skill.Match(httpsWant) || skill.Match(httpWant)) {
700 LOG_D(BMS_TAG_DEFAULT, "browser skills is valid");
701 return true;
702 }
703 }
704 LOG_W(BMS_TAG_DEFAULT, "browser skills is invalid");
705 return false;
706 }
707
IsEmailSkillsValid(const std::vector<Skill> & skills) const708 bool DefaultAppMgr::IsEmailSkillsValid(const std::vector<Skill>& skills) const
709 {
710 LOG_D(BMS_TAG_DEFAULT, "begin to verify email skills");
711 Want want;
712 want.SetAction(EMAIL_ACTION);
713 want.SetUri(EMAIL_SCHEME);
714
715 for (const Skill& skill : skills) {
716 if (skill.Match(want)) {
717 LOG_D(BMS_TAG_DEFAULT, "email skills valid");
718 return true;
719 }
720 }
721 LOG_W(BMS_TAG_DEFAULT, "email skills invalid");
722 return false;
723 }
724
MatchUtd(const std::string & utd,const std::vector<Skill> & skills) const725 bool DefaultAppMgr::MatchUtd(const std::string& utd, const std::vector<Skill>& skills) const
726 {
727 LOG_D(BMS_TAG_DEFAULT, "utd : %{public}s", utd.c_str());
728 if (MatchActionAndType(ACTION_VIEW_DATA, utd, skills)) {
729 return true;
730 }
731 LOG_E(BMS_TAG_DEFAULT, "match utd failed");
732 return false;
733 }
734
IsUserIdExist(int32_t userId) const735 bool DefaultAppMgr::IsUserIdExist(int32_t userId) const
736 {
737 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
738 if (dataMgr == nullptr) {
739 LOG_W(BMS_TAG_DEFAULT, "get BundleDataMgr failed");
740 return false;
741 }
742 return dataMgr->HasUserId(userId);
743 }
744
IsElementEmpty(const Element & element) const745 bool DefaultAppMgr::IsElementEmpty(const Element& element) const
746 {
747 return element.bundleName.empty() && element.moduleName.empty()
748 && element.abilityName.empty() && element.extensionName.empty();
749 }
750
VerifyElementFormat(const Element & element)751 bool DefaultAppMgr::VerifyElementFormat(const Element& element)
752 {
753 const std::string& bundleName = element.bundleName;
754 const std::string& moduleName = element.moduleName;
755 const std::string& abilityName = element.abilityName;
756 const std::string& extensionName = element.extensionName;
757 if (bundleName.empty()) {
758 LOG_W(BMS_TAG_DEFAULT, "bundleName empty, bad Element format");
759 return false;
760 }
761 if (moduleName.empty()) {
762 LOG_W(BMS_TAG_DEFAULT, "moduleName empty, bad Element format");
763 return false;
764 }
765 if (abilityName.empty() && extensionName.empty()) {
766 LOG_W(BMS_TAG_DEFAULT, "abilityName and extensionName both empty, bad Element format");
767 return false;
768 }
769 if (!abilityName.empty() && !extensionName.empty()) {
770 LOG_W(BMS_TAG_DEFAULT, "abilityName and extensionName both non-empty, bad Element format");
771 return false;
772 }
773 return true;
774 }
775
IsElementValid(int32_t userId,const std::string & type,const Element & element) const776 bool DefaultAppMgr::IsElementValid(int32_t userId, const std::string& type, const Element& element) const
777 {
778 bool ret = VerifyElementFormat(element);
779 if (!ret) {
780 LOG_W(BMS_TAG_DEFAULT, "VerifyElementFormat failed");
781 return false;
782 }
783 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
784 if (dataMgr == nullptr) {
785 LOG_W(BMS_TAG_DEFAULT, "dataMgr is null");
786 return false;
787 }
788 AbilityInfo abilityInfo;
789 ExtensionAbilityInfo extensionInfo;
790 std::vector<Skill> skills;
791 // verify if element exists
792 ret = dataMgr->QueryInfoAndSkillsByElement(userId, element, abilityInfo, extensionInfo, skills);
793 if (!ret) {
794 LOG_W(BMS_TAG_DEFAULT, "QueryInfoAndSkillsByElement failed");
795 BundleInfo bundleInfo;
796 return GetBrokerBundleInfo(element, bundleInfo);
797 }
798 // match type and skills
799 ret = IsMatch(type, skills);
800 if (!ret) {
801 LOG_W(BMS_TAG_DEFAULT, "type and skills not match");
802 return false;
803 }
804 LOG_D(BMS_TAG_DEFAULT, "Element is valid");
805 return true;
806 }
807
GetBrokerBundleInfo(const Element & element,BundleInfo & bundleInfo) const808 bool DefaultAppMgr::GetBrokerBundleInfo(const Element& element, BundleInfo& bundleInfo) const
809 {
810 if (element.bundleName.empty() || element.abilityName.empty()) {
811 LOG_W(BMS_TAG_DEFAULT, "invalid param, get broker bundleInfo failed");
812 return false;
813 }
814 if (!DelayedSingleton<BundleMgrService>::GetInstance()->IsBrokerServiceStarted()) {
815 LOG_W(BMS_TAG_DEFAULT, "broker not started, get broker bundleInfo failed");
816 return false;
817 }
818 Want want;
819 ElementName elementName("", element.bundleName, element.abilityName, element.moduleName);
820 want.SetElement(elementName);
821 AbilityInfo abilityInfo;
822 auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
823 ErrCode ret = bmsExtensionClient->QueryAbilityInfo(want, 0, Constants::START_USERID, abilityInfo, true);
824 if (ret != ERR_OK) {
825 LOG_W(BMS_TAG_DEFAULT, "query abilityInfo from broker failed");
826 return false;
827 }
828 bundleInfo.name = abilityInfo.bundleName;
829 bundleInfo.abilityInfos.emplace_back(abilityInfo);
830 LOG_I(BMS_TAG_DEFAULT, "get broker bundleInfo success");
831 return true;
832 }
833
VerifyPermission(const std::string & permissionName) const834 ErrCode DefaultAppMgr::VerifyPermission(const std::string& permissionName) const
835 {
836 if (!BundlePermissionMgr::IsSystemApp()) {
837 LOG_W(BMS_TAG_DEFAULT, "non-system app calling system api");
838 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
839 }
840 if (!BundlePermissionMgr::IsSelfCalling() &&
841 !BundlePermissionMgr::VerifyCallingPermissionForAll(permissionName)) {
842 LOG_W(BMS_TAG_DEFAULT, "verify permission %{public}s failed", permissionName.c_str());
843 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
844 }
845 return ERR_OK;
846 }
847
Normalize(const std::string & param)848 std::vector<std::string> DefaultAppMgr::Normalize(const std::string& param)
849 {
850 if (IsAppType(param)) {
851 return {param};
852 }
853 if (BundleUtil::IsUtd(param)) {
854 if (BundleUtil::IsSpecificUtd(param)) {
855 return {param};
856 }
857 return {};
858 }
859 if (IsSpecificMimeType(param)) {
860 return BundleUtil::GetUtdVectorByMimeType(param);
861 }
862 std::vector<std::string> utdVector;
863 if (!MimeTypeMgr::GetUtdVectorByUri(param, utdVector)) {
864 LOG_W(BMS_TAG_DEFAULT, "GetUtdVectorByUri failed");
865 return {};
866 }
867 return utdVector;
868 }
869
IsAppType(const std::string & param)870 bool DefaultAppMgr::IsAppType(const std::string& param)
871 {
872 return supportAppTypes.find(param) != supportAppTypes.end();
873 }
874
IsSpecificMimeType(const std::string & param)875 bool DefaultAppMgr::IsSpecificMimeType(const std::string& param)
876 {
877 // valid mimeType format : type/subType
878 if (param.find(WILDCARD) != param.npos) {
879 LOG_W(BMS_TAG_DEFAULT, "specific mimeType not allow contains *");
880 return false;
881 }
882 std::vector<std::string> vector;
883 SplitStr(param, SPLIT, vector, false, false);
884 if (vector.size() == TYPE_PART_COUNT && !vector[INDEX_ZERO].empty() && !vector[INDEX_ONE].empty()) {
885 return true;
886 }
887 LOG_W(BMS_TAG_DEFAULT, "not specific mimeType");
888 return false;
889 }
890
GetDefaultInfo(const int32_t userId,const std::vector<std::string> & normalizedTypeVec,std::unordered_map<std::string,std::pair<bool,Element>> & defaultInfo) const891 void DefaultAppMgr::GetDefaultInfo(const int32_t userId, const std::vector<std::string>& normalizedTypeVec,
892 std::unordered_map<std::string, std::pair<bool, Element>>& defaultInfo) const
893 {
894 for (const std::string& normalizedType : normalizedTypeVec) {
895 Element element;
896 bool ret = defaultAppDb_->GetDefaultApplicationInfo(userId, normalizedType, element);
897 defaultInfo[normalizedType] = std::make_pair(ret, element);
898 }
899 }
900
SendDefaultAppChangeEventIfNeeded(const int32_t userId,const std::vector<std::string> & normalizedTypeVec,const std::unordered_map<std::string,std::pair<bool,Element>> & originStateMap) const901 bool DefaultAppMgr::SendDefaultAppChangeEventIfNeeded(
902 const int32_t userId, const std::vector<std::string>& normalizedTypeVec,
903 const std::unordered_map<std::string, std::pair<bool, Element>>& originStateMap) const
904 {
905 std::unordered_map<std::string, std::pair<bool, Element>> currentStateMap;
906 GetDefaultInfo(userId, normalizedTypeVec, currentStateMap);
907 std::vector<std::string> changedTypeVec;
908 for (const auto& originState : originStateMap) {
909 auto currentState = currentStateMap.find(originState.first);
910 if (currentState == currentStateMap.end()) {
911 LOG_W(BMS_TAG_DEFAULT, "currentStateMap not contains type: %{public}s", originState.first.c_str());
912 continue;
913 }
914 if (ShouldSendEvent(originState.second.first, originState.second.second, currentState->second.first,
915 currentState->second.second)) {
916 changedTypeVec.emplace_back(originState.first);
917 }
918 }
919 return SendDefaultAppChangeEvent(userId, changedTypeVec);
920 }
921
ShouldSendEvent(bool originalResult,const Element & originalElement,bool currentResult,const Element & currentElement) const922 bool DefaultAppMgr::ShouldSendEvent(
923 bool originalResult, const Element& originalElement, bool currentResult, const Element& currentElement) const
924 {
925 if (originalResult && currentResult) {
926 return !(originalElement == currentElement);
927 }
928 return originalResult || currentResult;
929 }
930
SendDefaultAppChangeEvent(const int32_t userId,const std::vector<std::string> & typeVec) const931 bool DefaultAppMgr::SendDefaultAppChangeEvent(const int32_t userId, const std::vector<std::string>& typeVec) const
932 {
933 if (typeVec.empty()) {
934 LOG_W(BMS_TAG_DEFAULT, "typeVec is empty");
935 return false;
936 }
937 std::vector<std::string> utdIdVec;
938 for (const auto& type : typeVec) {
939 if (!IsAppType(type)) {
940 utdIdVec.emplace_back(type);
941 continue;
942 }
943
944 size_t i = 0;
945 size_t len = sizeof(APP_TYPES_KEY) / sizeof(APP_TYPES_KEY[0]);
946 for (i = 0; i < len; i++) {
947 if (APP_TYPES_KEY[i] == type) break;
948 }
949 if (i == len) {
950 LOG_W(BMS_TAG_DEFAULT, "APP_TYPES_KEY not contains type: %{public}s", type.c_str());
951 continue;
952 }
953 for (const std::string& utdId : APP_TYPES_VALUE[i]) {
954 utdIdVec.emplace_back(utdId);
955 }
956 }
957 if (utdIdVec.empty()) {
958 LOG_W(BMS_TAG_DEFAULT, "utdIdVec is empty");
959 return false;
960 }
961 std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
962 commonEventMgr->NotifyDefaultAppChanged(userId, utdIdVec);
963 LOG_I(BMS_TAG_DEFAULT, "Send default app change event success");
964 return true;
965 }
966 }
967 }