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 std::unordered_map<std::string, float> exifOrientationDegree = {
33     {"Top-left", 0},
34     {"Top-right", 90},
35     {"Bottom-right", 180},
36     {"Right-top", 90},
37     {"Left-bottom", 270},
38 };
39 
TransExifOrientationToDegree(const std::string & orientation)40 float TransExifOrientationToDegree(const std::string& orientation)
41 {
42     float degree = .0;
43     if (exifOrientationDegree.count(orientation)) {
44         degree = exifOrientationDegree[orientation];
45     }
46     return degree;
47 }
48 
GetClientBundle(int uid)49 std::string GetClientBundle(int uid)
50 {
51     std::string bundleName = "";
52     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (samgr == nullptr) {
54         DP_ERR_LOG("Get ability manager failed");
55         return bundleName;
56     }
57 
58     sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
59     if (object == nullptr) {
60         DP_DEBUG_LOG("object is NULL.");
61         return bundleName;
62     }
63 
64     sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
65     if (bms == nullptr) {
66         DP_DEBUG_LOG("bundle manager service is NULL.");
67         return bundleName;
68     }
69 
70     auto result = bms->GetNameForUid(uid, bundleName);
71     if (result != ERR_OK || bundleName.empty()) {
72         DP_ERR_LOG("GetBundleNameForUid fail");
73         return "";
74     }
75     AppExecFwk::BundleInfo bundleInfo;
76     bms->GetBundleInfo(bundleName,
77         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
78         bundleInfo, AppExecFwk::Constants::ALL_USERID);
79     std::string versionName = bundleInfo.versionName;
80     if (versionName.empty()) {
81         DP_ERR_LOG("get versionName form application failed.");
82         return "";
83     }
84     DP_INFO_LOG("bundle name is %{public}s versionName:%{public}s", bundleName.c_str(), versionName.c_str());
85 
86     return bundleName;
87 }
88 
GetDpsCallerInfo()89 DpsCallerInfo GetDpsCallerInfo()
90 {
91     DpsCallerInfo dpsCallerInfo;
92     dpsCallerInfo.pid = IPCSkeleton::GetCallingPid();
93     dpsCallerInfo.uid = IPCSkeleton::GetCallingUid();
94     dpsCallerInfo.tokenID = IPCSkeleton::GetCallingTokenID();
95     dpsCallerInfo.bundleName = GetClientBundle(dpsCallerInfo.uid);
96     DP_DEBUG_LOG("GetDpsCallerInfo pid:%{public}d uid:%{public}d", dpsCallerInfo.pid, dpsCallerInfo.uid);
97     return dpsCallerInfo;
98 }
99 } // namespace DeferredProcessing
100 } // namespace CameraStandard
101 } // namespace OHOS