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