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