1 /*
2  * Copyright (c) 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.h"
17 #include "app_log_wrapper.h"
18 #include "bundle_error.h"
19 #include "cj_common_ffi.h"
20 #include "bundle_mgr_proxy.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "common_func.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 using namespace CJSystemapi::BundleManager;
28 
29 static const std::unordered_map<std::string, std::string> TYPE_MAPPING = {
30     {"Web Browser", "BROWSER"},
31     {"Image Gallery", "IMAGE"},
32     {"Audio Player", "AUDIO"},
33     {"Video Player", "VIDEO"},
34     {"PDF Viewer", "PDF"},
35     {"Word Viewer", "WORD"},
36     {"Excel Viewer", "EXCEL"},
37     {"PPT Viewer", "PPT"},
38     {"Email", "EMAIL"}
39 };
40 
ParseType(std::string& result)41 static bool ParseType(std::string& result)
42 {
43     if (TYPE_MAPPING.find(result) != TYPE_MAPPING.end()) {
44         result = TYPE_MAPPING.at(result);
45     }
46     return true;
47 }
48 
ParamsProcessIsDefaultApplicationSync(std::string& type)49 ErrCode ParamsProcessIsDefaultApplicationSync(std::string& type)
50 {
51     if (!ParseType(type)) {
52         APP_LOGE("type %{public}s invalid", type.c_str());
53         return ERROR_PARAM_CHECK_ERROR;
54     }
55     return SUCCESS_CODE;
56 }
57 
GetDefaultAppProxy()58 static OHOS::sptr<OHOS::AppExecFwk::IDefaultApp> GetDefaultAppProxy()
59 {
60     auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (systemAbilityManager == nullptr) {
62         APP_LOGE("systemAbilityManager is null.");
63         return nullptr;
64     }
65     auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
66     if (bundleMgrSa == nullptr) {
67         APP_LOGE("bundleMgrSa is null.");
68         return nullptr;
69     }
70     auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
71     if (bundleMgr == nullptr) {
72         APP_LOGE("iface_cast failed.");
73         return nullptr;
74     }
75     auto defaultAppProxy = bundleMgr->GetDefaultAppProxy();
76     if (defaultAppProxy == nullptr) {
77         APP_LOGE("GetDefaultAppProxy failed.");
78         return nullptr;
79     }
80     return defaultAppProxy;
81 }
82 
83 extern "C" {
FfiBundleManagerIsDefaultApplication(char* type)84     RetDataBool FfiBundleManagerIsDefaultApplication(char* type)
85     {
86         RetDataBool nRet = {.code = -1, .data = false};
87         bool isDefaultApp = false;
88         std::string strType(type);
89         if (ParamsProcessIsDefaultApplicationSync(strType) != SUCCESS_CODE) {
90             return nRet;
91         }
92 
93         auto defaultAppProxy = GetDefaultAppProxy();
94         if (defaultAppProxy == nullptr) {
95             nRet.code = ERROR_BUNDLE_SERVICE_EXCEPTION;
96             return nRet;
97         }
98 
99         ErrCode ret = defaultAppProxy->IsDefaultApplication(strType, isDefaultApp);
100         ret = CommonFunc::ConvertErrCode(ret);
101         if (ret != SUCCESS_CODE) {
102             APP_LOGE("FfiBundleManagerIsDefaultApplication failed: %{public}d", ret);
103             nRet.code = ret;
104             return nRet;
105         }
106         nRet.data = isDefaultApp;
107         nRet.code = ret;
108         return nRet;
109     }
110 }
111 
112 }
113 }