Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f8676d6 authored by Andy Hung's avatar Andy Hung Committed by Android (Google) Code Review
Browse files

Merge "AudioFlinger: Move checkAttributionSourcePackage to Permission utility"...

Merge "AudioFlinger: Move checkAttributionSourcePackage to Permission utility" into udc-qpr-dev-plus-aosp
parents b43714d2 c3af0114
Loading
Loading
Loading
Loading
+5 −31
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@
//#define BUFLOG_NDEBUG 0
#include <afutils/DumpTryLock.h>
#include <afutils/BufLog.h>
#include <afutils/Permission.h>
#include <afutils/TypedLogger.h>

// ----------------------------------------------------------------------------
@@ -268,33 +269,6 @@ class DevicesFactoryHalCallbackImpl : public DevicesFactoryHalCallback {
    }
};

// TODO b/182392769: use attribution source util
/* static */
AttributionSourceState AudioFlinger::checkAttributionSourcePackage(
        const AttributionSourceState& attributionSource) {
    Vector<String16> packages;
    PermissionController{}.getPackagesForUid(attributionSource.uid, packages);

    AttributionSourceState checkedAttributionSource = attributionSource;
    if (!attributionSource.packageName.has_value()
            || attributionSource.packageName.value().size() == 0) {
        if (!packages.isEmpty()) {
            checkedAttributionSource.packageName =
                std::move(legacy2aidl_String16_string(packages[0]).value());
        }
    } else {
        String16 opPackageLegacy = VALUE_OR_FATAL(
            aidl2legacy_string_view_String16(attributionSource.packageName.value_or("")));
        if (std::find_if(packages.begin(), packages.end(),
                [&opPackageLegacy](const auto& package) {
                return opPackageLegacy == package; }) == packages.end()) {
            ALOGW("The package name(%s) provided does not correspond to the uid %d",
                    attributionSource.packageName.value_or("").c_str(), attributionSource.uid);
        }
    }
    return checkedAttributionSource;
}

// ----------------------------------------------------------------------------

std::string formatToString(audio_format_t format) {
@@ -638,7 +612,7 @@ status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t di
                 __func__, callingUid, callingPid, clientPid);
        adjAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(callingPid));
    }
    adjAttributionSource = AudioFlinger::checkAttributionSourcePackage(
    adjAttributionSource = afutils::checkAttributionSourcePackage(
            adjAttributionSource);

    if (direction == MmapStreamInterface::DIRECTION_OUTPUT) {
@@ -1138,7 +1112,7 @@ status_t AudioFlinger::createTrack(const media::CreateTrackRequest& _input,
        clientPid = callingPid;
        adjAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(callingPid));
    }
    adjAttributionSource = AudioFlinger::checkAttributionSourcePackage(
    adjAttributionSource = afutils::checkAttributionSourcePackage(
            adjAttributionSource);

    audio_session_t sessionId = input.sessionId;
@@ -2397,7 +2371,7 @@ status_t AudioFlinger::createRecord(const media::CreateRecordRequest& _input,
                 __func__, callingUid, callingPid, currentPid);
        adjAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(callingPid));
    }
    adjAttributionSource = AudioFlinger::checkAttributionSourcePackage(
    adjAttributionSource = afutils::checkAttributionSourcePackage(
            adjAttributionSource);
    // we don't yet support anything other than linear PCM
    if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) {
@@ -4141,7 +4115,7 @@ status_t AudioFlinger::createEffect(const media::CreateEffectRequest& request,
        adjAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(callingPid));
        currentPid = callingPid;
    }
    adjAttributionSource = AudioFlinger::checkAttributionSourcePackage(adjAttributionSource);
    adjAttributionSource = afutils::checkAttributionSourcePackage(adjAttributionSource);

    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p",
          adjAttributionSource.pid, effectClient.get(), priority, sessionId, io,
+0 −3
Original line number Diff line number Diff line
@@ -171,9 +171,6 @@ class AudioFlinger
public:
    static void instantiate() ANDROID_API;

    static AttributionSourceState checkAttributionSourcePackage(
        const AttributionSourceState& attributionSource);

private:

    // ---- begin IAudioFlinger interface
+2 −1
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@

#include <pthread.h>
#include <afutils/DumpTryLock.h>
#include <afutils/Permission.h>
#include <afutils/TypedLogger.h>

// ----------------------------------------------------------------------------
@@ -9998,7 +9999,7 @@ status_t MmapThread::start(const AudioClient& client,
    audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;

    audio_io_handle_t io = mId;
    AttributionSourceState adjAttributionSource = AudioFlinger::checkAttributionSourcePackage(
    const AttributionSourceState adjAttributionSource = afutils::checkAttributionSourcePackage(
            client.attributionSource);

    if (isOutput()) {
+4 −0
Original line number Diff line number Diff line
@@ -39,13 +39,17 @@ cc_library {
        "AudioWatchdog.cpp",
        "BufLog.cpp",
        "NBAIO_Tee.cpp",
        "Permission.cpp",
        "PropertyUtils.cpp",
        "TypedLogger.cpp",
    ],

    shared_libs: [
        "framework-permission-aidl-cpp",
        "libaudioclient_aidl_conversion",
        "libaudioutils",
        "libbase",
        "libbinder",
        "libcutils", // property_get_int32
        "liblog",
        "libnbaio",
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Permission"
//#define LOG_NDEBUG 0

#include "Permission.h"

#include <binder/PermissionController.h>
#include <media/AidlConversionCppNdk.h>
#include <utils/Log.h>

namespace android::afutils {

// TODO b/182392769: use attribution source util
content::AttributionSourceState checkAttributionSourcePackage(
        const content::AttributionSourceState& attributionSource) {
    Vector<String16> packages;
    PermissionController{}.getPackagesForUid(attributionSource.uid, packages);

    content::AttributionSourceState checkedAttributionSource = attributionSource;
    if (!attributionSource.packageName.has_value()
            || attributionSource.packageName.value().size() == 0) {
        if (!packages.isEmpty()) {
            checkedAttributionSource.packageName =
                std::move(legacy2aidl_String16_string(packages[0]).value());
        }
    } else {
        const String16 opPackageLegacy = VALUE_OR_FATAL(
                aidl2legacy_string_view_String16(attributionSource.packageName.value_or("")));
        if (std::find_if(packages.begin(), packages.end(),
                [&opPackageLegacy](const auto& package) {
                return opPackageLegacy == package; }) == packages.end()) {
            ALOGW("The package name(%s) provided does not correspond to the uid %d",
                    attributionSource.packageName.value_or("").c_str(), attributionSource.uid);
        }
    }
    return checkedAttributionSource;
}

}  // namespace android::afutils
Loading