• 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 
16 #include "pasteboard_common.h"
17 
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include <unistd.h>
22 
23 #include "pasteboard_hilog.h"
24 #include "pasteboard_error.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int32_t MIMETYPE_MAX_SIZE = 1024;
GetAppBundleManager(void)29 sptr<AppExecFwk::IBundleMgr> PasteBoardCommon::GetAppBundleManager(void)
30 {
31     auto sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
33         sysAbilityMgr != nullptr, nullptr, PASTEBOARD_MODULE_SERVICE, "Failed to get system ability manager.");
34     auto remoteObject = sysAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
35     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(
36         remoteObject != nullptr, nullptr, PASTEBOARD_MODULE_SERVICE, "Failed to get bundle mgr service.");
37     return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
38 }
39 
GetApiTargetVersionForSelf(void)40 int32_t PasteBoardCommon::GetApiTargetVersionForSelf(void)
41 {
42     PASTEBOARD_CHECK_AND_RETURN_RET_LOGI(
43         apiTargetVersion_ <= 0, apiTargetVersion_, PASTEBOARD_MODULE_SERVICE, "apiTargetVersion valid.");
44     static constexpr int32_t API_VERSION_MOD = 1000;
45     sptr<AppExecFwk::IBundleMgr> bundleMgrProxy = GetAppBundleManager();
46     PASTEBOARD_CHECK_AND_RETURN_RET_LOGI(
47         bundleMgrProxy != nullptr, 0, PASTEBOARD_MODULE_SERVICE, "Failed to get bundle manager proxy.");
48     AppExecFwk::BundleInfo bundleInfo;
49     auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
50     auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
51     PASTEBOARD_CHECK_AND_RETURN_RET_LOGI(
52         ret == ERR_OK, 0, PASTEBOARD_MODULE_SERVICE, "GetBundleInfoForSelf: bundleName get failed %{public}d.", ret);
53     int32_t targetApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
54     apiTargetVersion_ = targetApiVersion;
55     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_COMMON, "Got target API version %{public}d.", targetApiVersion);
56     return targetApiVersion;
57 }
58 
GetAnonymousString(const std::string & str)59 std::string PasteBoardCommon::GetAnonymousString(const std::string &str)
60 {
61     const int32_t ANONYMOUS_LEN_LIMIT = 8;
62     const int32_t TWO_TIMES = 2;
63     if (str.length() <= ANONYMOUS_LEN_LIMIT) {
64         return str;
65     }
66     int32_t printLen = ANONYMOUS_LEN_LIMIT / TWO_TIMES;
67     return str.substr(0, printLen) + "**" + str.substr(str.length() - printLen);
68 }
69 
GetDirByBundleNameAndAppIndex(const std::string & bundleName,int32_t appIndex,std::string & dataDir)70 int32_t PasteBoardCommon::GetDirByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex,
71     std::string &dataDir)
72 {
73     sptr<AppExecFwk::IBundleMgr> bundleMgrProxy = PasteBoardCommon::GetAppBundleManager();
74     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(bundleMgrProxy != nullptr,
75         static_cast<int32_t>(PasteboardError::GET_BUNDLE_MGR_FAILED), PASTEBOARD_MODULE_SERVICE,
76         "Get bunlde manager failed, bundleName:%{public}s, appIndex:%{public}d",
77         bundleName.c_str(), appIndex);
78     auto ret = bundleMgrProxy->GetDirByBundleNameAndAppIndex(bundleName, appIndex, dataDir);
79     PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(ret == ERR_OK,
80         static_cast<int32_t>(ret), PASTEBOARD_MODULE_SERVICE,
81         "Get dir failed, bundleName:%{public}s, appIndex:%{public}d, ret:%{public}d",
82         bundleName.c_str(), appIndex, ret);
83     return ERR_OK;
84 }
85 
IsValidMimeType(const std::string & mimeType)86 bool PasteBoardCommon::IsValidMimeType(const std::string &mimeType)
87 {
88     const bool isNonEmpty = !mimeType.empty();
89     const bool withinSizeLimit = mimeType.size() <= MIMETYPE_MAX_SIZE;
90     return isNonEmpty && withinSizeLimit;
91 }
92 } // namespace MiscServices
93 } // namespace OHOS
94