• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "api_version.h"
16 #include "bundle_mgr_interface.h"
17 #include "i18n_hilog.h"
18 #include "ipc_skeleton.h"
19 #include "iremote_broker.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace Global {
25 namespace I18n {
26 const int32_t ApiVersion::API_VERSION_MOD = 1000;
27 std::mutex ApiVersion::initMutex;
28 thread_local int32_t sysApiVersion = 0;
29 std::unordered_map<std::string, int32_t> ApiVersion::versionMap {
30     {"isSuggested", 18},
31 };
32 
CheckApiTargetVersion(const std::string & apiName)33 ApiCompareResult ApiVersion::CheckApiTargetVersion(const std::string &apiName)
34 {
35     auto iter = versionMap.find(apiName);
36     if (iter == versionMap.end()) {
37         HILOG_ERROR_I18N("CheckApiTargetVersion: invalid api name: %{public}s", apiName.c_str());
38         return ApiCompareResult::INVALID_PARAM;
39     }
40     int32_t currentVersion = InitSystemApiVersion();
41     if (currentVersion == 0) {
42         HILOG_ERROR_I18N("CheckApiTargetVersion: get api target version is 0.");
43         return ApiCompareResult::SYS_ERROR;
44     }
45     int32_t version = iter->second;
46     return currentVersion >= version ? ApiCompareResult::GREATER_THAN : ApiCompareResult::LESS_THAN;
47 }
48 
InitSystemApiVersion()49 int32_t ApiVersion::InitSystemApiVersion()
50 {
51     if (sysApiVersion > 0) {
52         return sysApiVersion;
53     }
54     std::lock_guard<std::mutex> systemApiLock(initMutex);
55     if (sysApiVersion > 0) {
56         return sysApiVersion;
57     }
58     int32_t version = GetApiTargetVersion();
59     HILOG_INFO_I18N("InitSystemApiVersion: Current api version is %{public}d.", version);
60     if (version > 0) {
61         sysApiVersion = version;
62     }
63     return version;
64 }
65 
GetApiTargetVersion()66 int32_t ApiVersion::GetApiTargetVersion()
67 {
68     auto saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
69     if (saManager == nullptr) {
70         HILOG_ERROR_I18N("GetApiTargetVersion: saManager is nullptr");
71         return 0;
72     }
73     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
74         saManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
75     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgrProxy =
76         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
77     if (bundleMgrProxy == nullptr) {
78         HILOG_ERROR_I18N("GetApiTargetVersion: bundleMgrProxy is nullptr");
79         return 0;
80     }
81     OHOS::AppExecFwk::BundleInfo bundleInfo;
82     auto ret =
83         bundleMgrProxy->GetBundleInfoForSelf(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo);
84     if (ret != 0) {
85         HILOG_ERROR_I18N("GetApiTargetVersion: GetBundleInfoForSelf failed");
86         return 0;
87     }
88     int32_t hapApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
89     return hapApiVersion;
90 }
91 } // namespace I18n
92 } // namespace Global
93 } // namespace OHOS
94