1 /*
2 * Copyright (c) 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 "local_abilitys.h"
17
18 using namespace std;
19 using namespace OHOS::HiviewDFX;
20
21 namespace OHOS {
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD001800
24 #undef LOG_TAG
25 #define LOG_TAG "SA"
GetInstance()26 LocalAbilitys& LocalAbilitys::GetInstance()
27 {
28 static auto instance = new LocalAbilitys();
29 return *instance;
30 }
31
AddAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability)32 void LocalAbilitys::AddAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability)
33 {
34 HILOG_DEBUG(LOG_CORE, "AddAbility to cache %{public}d", systemAbilityId);
35 std::lock_guard<std::mutex> autoLock(localSALock_);
36 localSAMap_[systemAbilityId] = ability;
37 }
38
GetAbility(int32_t systemAbilityId)39 sptr<IRemoteObject> LocalAbilitys::GetAbility(int32_t systemAbilityId)
40 {
41 std::lock_guard<std::mutex> autoLock(localSALock_);
42 auto it = localSAMap_.find(systemAbilityId);
43 if (it != localSAMap_.end()) {
44 return it->second;
45 }
46 return nullptr;
47 }
48
RemoveAbility(int32_t systemAbilityId)49 void LocalAbilitys::RemoveAbility(int32_t systemAbilityId)
50 {
51 HILOG_DEBUG(LOG_CORE, "RemoveAbility from cache %{public}d", systemAbilityId);
52 std::lock_guard<std::mutex> autoLock(localSALock_);
53 auto it = localSAMap_.find(systemAbilityId);
54 if (it != localSAMap_.end()) {
55 localSAMap_.erase(systemAbilityId);
56 }
57 }
58 }
59