• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 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 "dp_utils.h"
17 
18 #include "bundle_mgr_interface.h"
19 #include "dp_log.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
GetGlobalWatchdog()27 Watchdog& GetGlobalWatchdog()
28 {
29     static Watchdog instance("DPSGlobalWatchdog");
30     return instance;
31 }
32 
GetClientBundle(int uid)33 std::string GetClientBundle(int uid)
34 {
35     std::string bundleName = "";
36     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37     if (samgr == nullptr) {
38         DP_ERR_LOG("Get ability manager failed");
39         return bundleName;
40     }
41 
42     sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
43     if (object == nullptr) {
44         DP_DEBUG_LOG("object is NULL.");
45         return bundleName;
46     }
47 
48     sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
49     if (bms == nullptr) {
50         DP_DEBUG_LOG("bundle manager service is NULL.");
51         return bundleName;
52     }
53 
54     auto result = bms->GetNameForUid(uid, bundleName);
55     if (result != ERR_OK || bundleName.empty()) {
56         DP_ERR_LOG("GetBundleNameForUid fail");
57         return "";
58     }
59     AppExecFwk::BundleInfo bundleInfo;
60     bms->GetBundleInfo(bundleName,
61         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
62         bundleInfo, AppExecFwk::Constants::ALL_USERID);
63     std::string versionName = bundleInfo.versionName;
64     if (versionName.empty()) {
65         DP_ERR_LOG("get versionName form application failed.");
66         return "";
67     }
68     DP_INFO_LOG("bundle name is %{public}s versionName:%{public}s", bundleName.c_str(), versionName.c_str());
69 
70     return bundleName;
71 }
72 
GetDpsCallerInfo()73 DpsCallerInfo GetDpsCallerInfo()
74 {
75     DpsCallerInfo dpsCallerInfo;
76     dpsCallerInfo.pid = IPCSkeleton::GetCallingPid();
77     dpsCallerInfo.uid = IPCSkeleton::GetCallingUid();
78     dpsCallerInfo.tokenID = IPCSkeleton::GetCallingTokenID();
79     dpsCallerInfo.bundleName = GetClientBundle(dpsCallerInfo.uid);
80     DP_DEBUG_LOG("GetDpsCallerInfo pid:%{public}d uid:%{public}d", dpsCallerInfo.pid, dpsCallerInfo.uid);
81     return dpsCallerInfo;
82 }
83 
ParseKeyValue(const std::string & input)84 std::unordered_map<std::string, std::string> ParseKeyValue(const std::string& input)
85 {
86     std::unordered_map<std::string, std::string> result;
87     std::stringstream src(input);
88     std::string pair;
89     while (std::getline(src, pair, ':')) {
90         size_t pos = pair.find('=');
91         if (pos != std::string::npos) {
92             std::string key = pair.substr(0, pos);
93             std::string value = pair.substr(pos + 1);
94             result[key] = value;
95         }
96     }
97     return result;
98 }
99 } // namespace DeferredProcessing
100 } // namespace CameraStandard
101 } // namespace OHOS