1 /*
2 * Copyright (c) 2023-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 "app_manager.h"
17
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23
24 using namespace OHOS::Security::AccessToken;
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 DM_IMPLEMENT_SINGLE_INSTANCE(AppManager);
29
GetAppId()30 const std::string AppManager::GetAppId()
31 {
32 std::string appId = "";
33 AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
34 if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
35 LOGE("The caller is not token_hap.");
36 return appId;
37 }
38 HapTokenInfo tokenInfo;
39 int32_t result = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
40 if (result != RET_SUCCESS) {
41 LOGE("GetHapTokenInfo failed ret is %{public}d.", result);
42 return appId;
43 }
44 appId = tokenInfo.appID;
45 return appId;
46 }
47
RegisterCallerAppId(const std::string & pkgName)48 void AppManager::RegisterCallerAppId(const std::string &pkgName)
49 {
50 if (pkgName.empty()) {
51 LOGE("Invalid parameter, pkgName is empty.");
52 return;
53 }
54 std::string appId = GetAppId();
55 if (appId.empty()) {
56 LOGE("PkgName %{public}s get appid failed.", pkgName.c_str());
57 return;
58 }
59 LOGI("PkgName %{public}s, appId %{public}s.", pkgName.c_str(), GetAnonyString(appId).c_str());
60 std::lock_guard<std::mutex> lock(appIdMapLock_);
61 appIdMap_[pkgName] = appId;
62 }
63
UnRegisterCallerAppId(const std::string & pkgName)64 void AppManager::UnRegisterCallerAppId(const std::string &pkgName)
65 {
66 if (pkgName.empty()) {
67 LOGE("Invalid parameter, pkgName is empty.");
68 return;
69 }
70 LOGI("PkgName %{public}s.", pkgName.c_str());
71 std::lock_guard<std::mutex> lock(appIdMapLock_);
72 if (appIdMap_.find(pkgName) == appIdMap_.end()) {
73 LOGE("AppIdMap not find pkgName.");
74 return;
75 }
76 appIdMap_.erase(pkgName);
77 }
78
GetAppIdByPkgName(const std::string & pkgName,std::string & appId)79 int32_t AppManager::GetAppIdByPkgName(const std::string &pkgName, std::string &appId)
80 {
81 if (pkgName.empty()) {
82 LOGE("Invalid parameter, pkgName is empty.");
83 return ERR_DM_INPUT_PARA_INVALID;
84 }
85 LOGI("PkgName %{public}s.", pkgName.c_str());
86 std::lock_guard<std::mutex> lock(appIdMapLock_);
87 if (appIdMap_.find(pkgName) == appIdMap_.end()) {
88 LOGE("AppIdMap not find pkgName.");
89 return ERR_DM_FAILED;
90 }
91 appId = appIdMap_[pkgName];
92 return DM_OK;
93 }
94 } // namespace DistributedHardware
95 } // namespace OHOS
96