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_host_impl.h"
17 
18 #include "app_log_tag_wrapper.h"
19 #include "bundle_mgr_service.h"
20 #include "hitrace_meter.h"
21 #include "ipc_skeleton.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
IsDefaultApplication(const std::string & type,bool & isDefaultApp)25 ErrCode DefaultAppHostImpl::IsDefaultApplication(const std::string& type, bool& isDefaultApp)
26 {
27     int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
28     return DefaultAppMgr::GetInstance().IsDefaultApplication(userId, type, isDefaultApp);
29 }
30 
GetDefaultApplication(int32_t userId,const std::string & type,BundleInfo & bundleInfo)31 ErrCode DefaultAppHostImpl::GetDefaultApplication(int32_t userId, const std::string& type, BundleInfo& bundleInfo)
32 {
33     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
34     return DefaultAppMgr::GetInstance().GetDefaultApplication(userId, type, bundleInfo);
35 }
36 
SetDefaultApplication(int32_t userId,const std::string & type,const Want & want)37 ErrCode DefaultAppHostImpl::SetDefaultApplication(int32_t userId, const std::string& type, const Want& want)
38 {
39     LOG_D(BMS_TAG_DEFAULT, "SetDefaultApplication userId:%{public}d type:%{public}s", userId, type.c_str());
40     const ElementName& elementName = want.GetElement();
41     const std::string& bundleName = elementName.GetBundleName();
42     const std::string& moduleName = elementName.GetModuleName();
43     const std::string& abilityName = elementName.GetAbilityName();
44     LOG_D(BMS_TAG_DEFAULT, "ElementName bundleName:%{public}s moduleName:%{public}s abilityName:%{public}s",
45         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
46     // case1 : ElementName is empty.
47     bool isEmpty = bundleName.empty() && moduleName.empty() && abilityName.empty();
48     if (isEmpty) {
49         LOG_D(BMS_TAG_DEFAULT, "ElementName is empty");
50         Element element;
51         return DefaultAppMgr::GetInstance().SetDefaultApplication(userId, type, element);
52     }
53     // case2 : ElementName is valid ability or valid extension.
54     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
55     if (dataMgr == nullptr) {
56         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
57         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
58     }
59     if (!dataMgr->HasUserId(userId)) {
60         LOG_E(BMS_TAG_DEFAULT, "userId not exist");
61         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
62     }
63     Element element;
64     bool ret = dataMgr->GetElement(userId, elementName, element);
65     if (!ret) {
66         LOG_E(BMS_TAG_DEFAULT, "GetElement failed");
67         return ERR_BUNDLE_MANAGER_ABILITY_AND_TYPE_MISMATCH;
68     }
69     return DefaultAppMgr::GetInstance().SetDefaultApplication(userId, type, element);
70 }
71 
ResetDefaultApplication(int32_t userId,const std::string & type)72 ErrCode DefaultAppHostImpl::ResetDefaultApplication(int32_t userId, const std::string& type)
73 {
74     return DefaultAppMgr::GetInstance().ResetDefaultApplication(userId, type);
75 }
76 }
77 }
78