diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2019-09-24 15:08:09 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-09-24 15:08:09 +0000 |
commit | a5358c08e0e740462d5d1ef81255a4fa828874d3 (patch) | |
tree | 6cb944f674b96173c4a2d001b403a7aae2bef8ba | |
parent | db76b871ea5a12e5dbd461ed5b5a920880b12af7 (diff) | |
parent | 36a1bc7a9f9054ac8c763a850031c625af192474 (diff) | |
download | ml-a5358c08e0e740462d5d1ef81255a4fa828874d3.tar.gz |
Merge "Replace libbinder dependency with libneuralnetworks_packageinfo"
-rw-r--r-- | nn/common/Android.bp | 1 | ||||
-rw-r--r-- | nn/runtime/Android.bp | 2 | ||||
-rw-r--r-- | nn/runtime/TypeManager.cpp | 46 | ||||
-rw-r--r-- | nn/runtime/packageinfo/Android.bp | 41 | ||||
-rw-r--r-- | nn/runtime/packageinfo/PackageInfo.cpp | 80 | ||||
-rw-r--r-- | nn/runtime/packageinfo/include/PackageInfo.h | 54 | ||||
-rw-r--r-- | nn/runtime/packageinfo/libneuralnetworks_packageinfo.map.txt | 22 | ||||
-rw-r--r-- | nn/runtime/test/Android.bp | 2 |
8 files changed, 212 insertions, 36 deletions
diff --git a/nn/common/Android.bp b/nn/common/Android.bp index 346832d88..50503a6d3 100644 --- a/nn/common/Android.bp +++ b/nn/common/Android.bp @@ -220,6 +220,7 @@ cc_defaults { "libhidlmemory", "libnativewindow", "libneuralnetworks", + "libneuralnetworks_packageinfo", ], static_libs: [ "android.hardware.neuralnetworks@1.0", diff --git a/nn/runtime/Android.bp b/nn/runtime/Android.bp index a91433606..770072f0d 100644 --- a/nn/runtime/Android.bp +++ b/nn/runtime/Android.bp @@ -60,12 +60,12 @@ cc_library { "android.hidl.allocator@1.0", "android.hidl.memory@1.0", "libbase", - "libbinder", "libcutils", "libhidlbase", "libhidlmemory", "liblog", "libnativewindow", + "libneuralnetworks_packageinfo", "libui", "libutils", ], diff --git a/nn/runtime/TypeManager.cpp b/nn/runtime/TypeManager.cpp index 6468a0fac..854e28ab9 100644 --- a/nn/runtime/TypeManager.cpp +++ b/nn/runtime/TypeManager.cpp @@ -20,13 +20,17 @@ #include "Utils.h" +#include <PackageInfo.h> #include <android-base/file.h> #include <android-base/properties.h> -#include <android/content/pm/IPackageManagerNative.h> #include <binder/IServiceManager.h> #include <procpartition/procpartition.h> #include <algorithm> +#include <map> +#include <memory> +#include <string> #include <string_view> +#include <vector> namespace android { namespace nn { @@ -100,42 +104,16 @@ std::vector<std::string> getVendorExtensionAllowlistedApps() { // Query PackageManagerNative service about Android app properties. // On success, it will populate appPackageInfo->app* fields. bool fetchAppPackageLocationInfo(uid_t uid, TypeManager::AppPackageInfo* appPackageInfo) { - sp<::android::IServiceManager> sm(::android::defaultServiceManager()); - sp<::android::IBinder> binder(sm->getService(String16("package_native"))); - if (binder == nullptr) { - LOG(ERROR) << "getService package_native failed"; + ANeuralNetworks_PackageInfo packageInfo; + if (!ANeuralNetworks_fetch_PackageInfo(uid, &packageInfo)) { return false; } + appPackageInfo->appPackageName = packageInfo.appPackageName; + appPackageInfo->appIsSystemApp = packageInfo.appIsSystemApp; + appPackageInfo->appIsOnVendorImage = packageInfo.appIsOnVendorImage; + appPackageInfo->appIsOnProductImage = packageInfo.appIsOnProductImage; - sp<content::pm::IPackageManagerNative> packageMgr = - interface_cast<content::pm::IPackageManagerNative>(binder); - std::vector<int> uids{static_cast<int>(uid)}; - std::vector<std::string> names; - binder::Status status = packageMgr->getNamesForUids(uids, &names); - if (!status.isOk()) { - LOG(ERROR) << "package_native::getNamesForUids failed: " - << status.exceptionMessage().c_str(); - return false; - } - const std::string& packageName = names[0]; - - appPackageInfo->appPackageName = packageName; - int flags = 0; - status = packageMgr->getLocationFlags(packageName, &flags); - if (!status.isOk()) { - LOG(ERROR) << "package_native::getLocationFlags failed: " - << status.exceptionMessage().c_str(); - return false; - } - // isSystemApp() - appPackageInfo->appIsSystemApp = - ((flags & content::pm::IPackageManagerNative::LOCATION_SYSTEM) != 0); - // isVendor() - appPackageInfo->appIsOnVendorImage = - ((flags & content::pm::IPackageManagerNative::LOCATION_VENDOR) != 0); - // isProduct() - appPackageInfo->appIsOnProductImage = - ((flags & content::pm::IPackageManagerNative::LOCATION_PRODUCT) != 0); + ANeuralNetworks_free_PackageInfo(&packageInfo); return true; } diff --git a/nn/runtime/packageinfo/Android.bp b/nn/runtime/packageinfo/Android.bp new file mode 100644 index 000000000..a96181b65 --- /dev/null +++ b/nn/runtime/packageinfo/Android.bp @@ -0,0 +1,41 @@ +cc_library_shared { + name: "libneuralnetworks_packageinfo", + defaults: ["neuralnetworks_defaults"], + + srcs: [ + "PackageInfo.cpp", + ], + + shared_libs: [ + "libbase", + "libbinder", + "libcutils", + "liblog", + "libutils", + ], + + export_include_dirs: ["include"], + + cflags: [ + "-Wall", + "-Werror", + "-Wno-error=deprecated-declarations", + ], + + // enumerate stable entry points, for apex use + stubs: { + symbol_file: "libneuralnetworks_packageinfo.map.txt", + versions: [ + "1", + ], + }, + + header_abi_checker: { + enabled: true, + symbol_file: "libneuralnetworks_packageinfo.map.txt", + }, + + visibility: [ + "//frameworks/ml/nn:__subpackages__", + ], +} diff --git a/nn/runtime/packageinfo/PackageInfo.cpp b/nn/runtime/packageinfo/PackageInfo.cpp new file mode 100644 index 000000000..b986acb42 --- /dev/null +++ b/nn/runtime/packageinfo/PackageInfo.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2019 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 "PackageInfo" + +#include "PackageInfo.h" +#include <android-base/logging.h> +#include <android/content/pm/IPackageManagerNative.h> +#include <binder/IServiceManager.h> +#include <string> +#include <vector> + +bool ANeuralNetworks_fetch_PackageInfo(uid_t uid, ANeuralNetworks_PackageInfo* appPackageInfo) { + if (appPackageInfo == nullptr) { + LOG(ERROR) << "appPackageInfo can't be a nullptr"; + return false; + } + + ::android::sp<::android::IServiceManager> sm(::android::defaultServiceManager()); + ::android::sp<::android::IBinder> binder(sm->getService(::android::String16("package_native"))); + if (binder == nullptr) { + LOG(ERROR) << "getService package_native failed"; + return false; + } + + ::android::sp<::android::content::pm::IPackageManagerNative> packageMgr = + ::android::interface_cast<::android::content::pm::IPackageManagerNative>(binder); + std::vector<int> uids{static_cast<int>(uid)}; + std::vector<std::string> names; + ::android::binder::Status status = packageMgr->getNamesForUids(uids, &names); + if (!status.isOk()) { + LOG(ERROR) << "package_native::getNamesForUids failed: " + << status.exceptionMessage().c_str(); + return false; + } + const std::string& packageName = names[0]; + + int flags = 0; + status = packageMgr->getLocationFlags(packageName, &flags); + if (!status.isOk()) { + LOG(ERROR) << "package_native::getLocationFlags failed: " + << status.exceptionMessage().c_str(); + return false; + } + + appPackageInfo->appPackageName = new char[packageName.size() + 1]; + memcpy(appPackageInfo->appPackageName, packageName.c_str(), packageName.size() + 1); + + // isSystemApp() + appPackageInfo->appIsSystemApp = + ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_SYSTEM) != 0); + // isVendor() + appPackageInfo->appIsOnVendorImage = + ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_VENDOR) != 0); + // isProduct() + appPackageInfo->appIsOnProductImage = + ((flags & ::android::content::pm::IPackageManagerNative::LOCATION_PRODUCT) != 0); + return true; +} + +void ANeuralNetworks_free_PackageInfo(ANeuralNetworks_PackageInfo* appPackageInfo) { + if (appPackageInfo != nullptr) { + if (appPackageInfo->appPackageName != nullptr) { + delete[] appPackageInfo->appPackageName; + } + } +} diff --git a/nn/runtime/packageinfo/include/PackageInfo.h b/nn/runtime/packageinfo/include/PackageInfo.h new file mode 100644 index 000000000..19200b70f --- /dev/null +++ b/nn/runtime/packageinfo/include/PackageInfo.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 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. + */ + +#ifndef ANDROID_FRAMEWORKS_ML_NN_RUNTIME_PACKAGEINFO_PACKAGE_INFO_H +#define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_PACKAGEINFO_PACKAGE_INFO_H + +#include <sys/types.h> + +// +// Define a C interface to the neuralnetworks APEX helper functionality +// + +__BEGIN_DECLS + +// Collection of app-related information retreived from Package Manager. +typedef struct ANeuralNetworks_PackageInfo { + // Null-terminated package name (nullptr if not an Android app). + // Referenced memory is allocated by the ANeuralNetworks_fetch_PackageInfo + // method, and MUST be released by a ANeuralNetworks_free_PackageInfo call. + char* appPackageName; + + // Is the app a system app? (false if not an Android app) + bool appIsSystemApp; + // Is the app preinstalled on vendor image? (false if not an Android app) + bool appIsOnVendorImage; + // Is the app preinstalled on product image? (false if not an Android app) + bool appIsOnProductImage; +} ANeuralNetworks_PackageInfo; + +// Query PackageManagerNative service about Android app properties. +// On success, it will allocate memory for PackageInfo fields, which must be +// released by a ANeuralNetworks_free_PackageInfo call +bool ANeuralNetworks_fetch_PackageInfo(uid_t uid, ANeuralNetworks_PackageInfo* appPackageInfo); + +// Free memory allocated for PackageInfo fields (doesn't free the actual package info +// struct). +void ANeuralNetworks_free_PackageInfo(ANeuralNetworks_PackageInfo* appPackageInfo); + +__END_DECLS + +#endif // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_PACKAGEINFO_PACKAGE_INFO_H diff --git a/nn/runtime/packageinfo/libneuralnetworks_packageinfo.map.txt b/nn/runtime/packageinfo/libneuralnetworks_packageinfo.map.txt new file mode 100644 index 000000000..803b34556 --- /dev/null +++ b/nn/runtime/packageinfo/libneuralnetworks_packageinfo.map.txt @@ -0,0 +1,22 @@ +# +# Copyright (C) 2019 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. +# +LIBNEURALNETWORKS_PACKAGE_INFO { + global: + ANeuralNetworks_fetch_PackageInfo; # apex + ANeuralNetworks_free_PackageInfo; # apex + local: + *; +}; diff --git a/nn/runtime/test/Android.bp b/nn/runtime/test/Android.bp index 4694fb12f..d7207dcb8 100644 --- a/nn/runtime/test/Android.bp +++ b/nn/runtime/test/Android.bp @@ -25,13 +25,13 @@ cc_defaults { "android.hidl.memory@1.0", "libandroid", "libbase", - "libbinder", "libcutils", "libfmq", "libhidlbase", "libhidlmemory", "liblog", "libnativewindow", + "libneuralnetworks_packageinfo", "libtextclassifier_hash", "libui", "libutils", |