1 /*
2 * Copyright (C) 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 #define MLOG_TAG "RingtoneBundleManager"
17
18 #include "ringtone_bundle_manager.h"
19
20 #include "ipc_skeleton.h"
21 #include "permission_utils.h"
22
23 namespace OHOS {
24 namespace Media {
25 using namespace std;
26 const int CAPACITY = 50;
27 shared_ptr<RingtoneBundleManager> RingtoneBundleManager::instance_ = nullptr;
28 mutex RingtoneBundleManager::mutex_;
29
GetInstance()30 shared_ptr<RingtoneBundleManager> RingtoneBundleManager::GetInstance()
31 {
32 if (instance_ == nullptr) {
33 lock_guard<mutex> lock(mutex_);
34 if (instance_ == nullptr) {
35 instance_ = make_shared<RingtoneBundleManager>();
36 }
37 }
38 return instance_;
39 }
40
GetBundleNameByUid(const int32_t uid,string & bundleName)41 void RingtoneBundleManager::GetBundleNameByUid(const int32_t uid, string &bundleName)
42 {
43 RingtonePermissionUtils::GetClientBundle(uid, bundleName);
44 if (bundleName.empty()) {
45 return;
46 }
47
48 auto it = cacheMap_.find(uid);
49 if (it != cacheMap_.end()) {
50 cacheList_.erase(it->second);
51 }
52 cacheList_.push_front(make_pair(uid, bundleName));
53 cacheMap_[uid] = cacheList_.begin();
54 if (cacheMap_.size() > CAPACITY) {
55 int32_t deleteKey = cacheList_.back().first;
56 cacheMap_.erase(deleteKey);
57 cacheList_.pop_back();
58 }
59 }
60
GetClientBundleName()61 string RingtoneBundleManager::GetClientBundleName()
62 {
63 lock_guard<mutex> lock(uninstallMutex_);
64 int32_t uid = IPCSkeleton::GetCallingUid();
65 auto iter = cacheMap_.find(uid);
66 if (iter == cacheMap_.end()) {
67 string bundleName;
68 GetBundleNameByUid(uid, bundleName);
69 return bundleName;
70 }
71 cacheList_.splice(cacheList_.begin(), cacheList_, iter->second);
72 return iter->second->second;
73 }
74
Clear()75 void RingtoneBundleManager::Clear()
76 {
77 lock_guard<mutex> lock(uninstallMutex_);
78 cacheList_.clear();
79 cacheMap_.clear();
80 }
81 } // Ringtone
82 } // OHOS
83