1 /*
2  * Copyright (C) 2023 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 #define LOG_TAG "Permission"
18 //#define LOG_NDEBUG 0
19 
20 #include "Permission.h"
21 
22 #include <binder/PermissionController.h>
23 #include <media/AidlConversionCppNdk.h>
24 #include <utils/Log.h>
25 
26 namespace android::afutils {
27 
28 // TODO b/182392769: use attribution source util
checkAttributionSourcePackage(const content::AttributionSourceState & attributionSource)29 content::AttributionSourceState checkAttributionSourcePackage(
30         const content::AttributionSourceState& attributionSource) {
31     Vector<String16> packages;
32     PermissionController{}.getPackagesForUid(attributionSource.uid, packages);
33 
34     content::AttributionSourceState checkedAttributionSource = attributionSource;
35     if (!attributionSource.packageName.has_value()
36             || attributionSource.packageName.value().size() == 0) {
37         if (!packages.isEmpty()) {
38             checkedAttributionSource.packageName =
39                 std::move(legacy2aidl_String16_string(packages[0]).value());
40         }
41     } else {
42         const String16 opPackageLegacy = VALUE_OR_FATAL(
43                 aidl2legacy_string_view_String16(attributionSource.packageName.value_or("")));
44         if (std::find_if(packages.begin(), packages.end(),
45                 [&opPackageLegacy](const auto& package) {
46                 return opPackageLegacy == package; }) == packages.end()) {
47             ALOGW("The package name(%s) provided does not correspond to the uid %d",
48                     attributionSource.packageName.value_or("").c_str(), attributionSource.uid);
49         }
50     }
51     return checkedAttributionSource;
52 }
53 
54 }  // namespace android::afutils
55