1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <mutex>
20 #include <optional>
21 #include <unordered_map>
22 
23 #include "IPermissionProvider.h"
24 
25 #include <android-base/thread_annotations.h>
26 #include <com/android/media/permission/BnNativePermissionController.h>
27 #include <error/BinderResult.h>
28 
29 namespace com::android::media::permission {
30 
31 class NativePermissionController : public BnNativePermissionController, public IPermissionProvider {
32     using Status = ::android::binder::Status;
33 
34   public:
35     Status populatePackagesForUids(const std::vector<UidPackageState>& initialPackageStates) final;
36     Status updatePackagesForUid(const UidPackageState& newPackageState) final;
37     Status populatePermissionState(PermissionEnum permission, const std::vector<int>& uids) final;
38     // end binder methods
39 
40     ::android::error::BinderResult<std::vector<std::string>> getPackagesForUid(
41             uid_t uid) const final;
42     ::android::error::BinderResult<bool> validateUidPackagePair(
43             uid_t uid, const std::string& packageName) const final;
44     ::android::error::BinderResult<bool> checkPermission(PermissionEnum permission,
45                                                          uid_t uid) const final;
46 
47   private:
48     mutable std::mutex m_;
49     // map of app_ids to the set of packages names which could run in them (should be 1)
50     std::unordered_map<uid_t, std::vector<std::string>> package_map_ GUARDED_BY(m_);
51     bool is_package_populated_ GUARDED_BY(m_);
52     // (logical) map of PermissionEnum to list of uids (not appid) which hold the perm
53     std::array<std::vector<uid_t>, static_cast<size_t>(PermissionEnum::ENUM_SIZE)> permission_map_
54             GUARDED_BY(m_);
55 };
56 }  // namespace com::android::media::permission
57