1 /*
2 * Copyright (c) 2024-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 "bms_adapter.h"
17 #include "camera_log.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
21 sptr<BmsAdapter> BmsAdapter::bmsAdapter_;
22 std::mutex BmsAdapter::instanceMutex_;
23
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)24 void BmsSaListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
25 {
26 MEDIA_INFO_LOG("OnAddSystemAbility,id: %{public}d", systemAbilityId);
27 }
28
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)29 void BmsSaListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
30 {
31 MEDIA_INFO_LOG("OnRemoveSystemAbility,id: %{public}d", systemAbilityId);
32 CHECK_EXECUTE(systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, callback_());
33 }
34
BmsAdapter()35 BmsAdapter::BmsAdapter()
36 {
37 }
38
~BmsAdapter()39 BmsAdapter::~BmsAdapter()
40 {
41 UnregisterListener();
42 }
43
GetInstance()44 sptr<BmsAdapter> BmsAdapter::GetInstance()
45 {
46 if (BmsAdapter::bmsAdapter_ == nullptr) {
47 std::unique_lock<std::mutex> lock(instanceMutex_);
48 if (BmsAdapter::bmsAdapter_ == nullptr) {
49 MEDIA_INFO_LOG("Initializing bms adapter instance");
50 BmsAdapter::bmsAdapter_ = new BmsAdapter();
51 }
52 }
53 return BmsAdapter::bmsAdapter_;
54 }
55
GetBms()56 sptr<OHOS::AppExecFwk::IBundleMgr> BmsAdapter::GetBms()
57 {
58 std::lock_guard<std::mutex> lock(bmslock_);
59 CHECK_ERROR_RETURN_RET(bms_, bms_);
60 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, nullptr, "GetBms failed, samgr is null");
62 sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
63 CHECK_ERROR_RETURN_RET_LOG(object == nullptr, nullptr, "GetBms failed, object is null");
64 bms_ = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
65 CHECK_ERROR_RETURN_RET_LOG(bms_ == nullptr, nullptr, "GetBms failed, bms is null");
66 RegisterListener();
67 return bms_;
68 }
69
SetBms(sptr<OHOS::AppExecFwk::IBundleMgr> bms)70 void BmsAdapter::SetBms(sptr<OHOS::AppExecFwk::IBundleMgr> bms)
71 {
72 std::lock_guard<std::mutex> lock(bmslock_);
73 bms_ = bms;
74 }
75
GetBundleName(int uid)76 std::string BmsAdapter::GetBundleName(int uid)
77 {
78 std::string bundleName = "";
79 if (cacheMap.Get(uid, bundleName)) {
80 MEDIA_DEBUG_LOG("GetBundleName by cache, uid: %{public}d bundleName is %{public}s", uid, bundleName.c_str());
81 return bundleName;
82 }
83 auto bms = GetBms();
84 CHECK_ERROR_RETURN_RET_LOG(bms == nullptr, "", "bms is null");
85 auto result = bms->GetNameForUid(uid, bundleName);
86 if (result != ERR_OK) {
87 MEDIA_DEBUG_LOG("GetNameForUid fail, ret: %{public}d", result);
88 return "";
89 }
90 MEDIA_DEBUG_LOG("GetBundleName by GetNameForUid, uid: %{public}d bundleName is %{public}s",
91 uid, bundleName.c_str());
92 cacheMap.Put(uid, bundleName);
93 return bundleName;
94 }
95
RegisterListener()96 bool BmsAdapter::RegisterListener()
97 {
98 MEDIA_INFO_LOG("Enter Into BmsAdapter::RegisterListener");
99 CHECK_ERROR_RETURN_RET_LOG(bmsSaListener_ != nullptr, false, "has register listener");
100 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, false, "samgr is null");
102 auto bmsAdapterWptr = wptr<BmsAdapter>(this);
103 auto removeCallback = [bmsAdapterWptr]() {
104 auto adapter = bmsAdapterWptr.promote();
105 CHECK_EXECUTE(adapter, adapter->SetBms(nullptr));
106 };
107 bmsSaListener_ = new BmsSaListener(removeCallback);
108 CHECK_ERROR_RETURN_RET_LOG(bmsSaListener_ == nullptr, false, "bmsSaListener_ alloc failed");
109 int32_t ret = samgr->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, bmsSaListener_);
110 CHECK_ERROR_RETURN_RET_LOG(ret != 0, false, "SubscribeSystemAbility ret = %{public}d", ret);
111 return true;
112 }
113
UnregisterListener()114 bool BmsAdapter::UnregisterListener()
115 {
116 MEDIA_INFO_LOG("Enter Into BmsAdapter::UnregisterListener");
117 CHECK_ERROR_RETURN_RET_LOG(bmsSaListener_ == nullptr, false, "bmsSaListener_ is null not need unregister");
118 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
119 CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, false, "samgr is null");
120 CHECK_ERROR_RETURN_RET_LOG(bmsSaListener_ == nullptr, false, "bmsSaListener_ is null");
121 int32_t ret = samgr->UnSubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, bmsSaListener_);
122 CHECK_ERROR_RETURN_RET_LOG(ret != 0, false, "UnSubscribeSystemAbility ret = %{public}d", ret);
123 bmsSaListener_ = nullptr;
124 return true;
125 }
126 } // namespace CameraStandard
127 } // namespace OHOS