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 {
GetInstance()22 LocalAbilitys& LocalAbilitys::GetInstance()
23 {
24 static auto instance = new LocalAbilitys();
25 return *instance;
26 }
27
AddAbility(int32_t systemAbilityId,const sptr<IRemoteObject> & ability)28 void LocalAbilitys::AddAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability)
29 {
30 HiLog::Debug(label_, "AddAbility to cache %{public}d", systemAbilityId);
31 std::lock_guard<std::mutex> autoLock(localSALock_);
32 localSAMap_[systemAbilityId] = ability;
33 }
34
GetAbility(int32_t systemAbilityId)35 sptr<IRemoteObject> LocalAbilitys::GetAbility(int32_t systemAbilityId)
36 {
37 std::lock_guard<std::mutex> autoLock(localSALock_);
38 auto it = localSAMap_.find(systemAbilityId);
39 if (it != localSAMap_.end()) {
40 return it->second;
41 }
42 return nullptr;
43 }
44
RemoveAbility(int32_t systemAbilityId)45 void LocalAbilitys::RemoveAbility(int32_t systemAbilityId)
46 {
47 HiLog::Debug(label_, "RemoveAbility from cache %{public}d", systemAbilityId);
48 std::lock_guard<std::mutex> autoLock(localSALock_);
49 auto it = localSAMap_.find(systemAbilityId);
50 if (it != localSAMap_.end()) {
51 localSAMap_.erase(systemAbilityId);
52 }
53 }
54 }
55