• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "PackageManagerProxy.h"
18 
19 #include <android-base/properties.h>
20 #include <android/binder_manager.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <log/log.h>
24 #include <utils/Looper.h>
25 
26 #include <android_car_feature.h>
27 
28 #include <sstream>
29 
30 namespace google {
31 namespace sdv {
32 namespace packagemanagerproxy {
33 
34 namespace {
35 
36 using ::aidl::google::sdv::packagemanagerproxy::IPackageManagerProxy;
37 using ::android::IBinder;
38 using ::android::interface_cast;
39 using ::android::IServiceManager;
40 using ::android::sp;
41 using ::android::String16;
42 using ::android::base::Error;
43 using ::android::base::GetProperty;
44 using ::android::base::Result;
45 using ::android::content::pm::IPackageManagerNative;
46 using ::ndk::ScopedAStatus;
47 
48 }  // namespace
49 
init()50 Result<void> PackageManagerProxy::init() {
51     // If the feature flag is not enabled, do not initialize the service
52     if (!android::car::feature::package_manager_extensions_for_sdv()) {
53         ALOGI("Flag package_manager_extensions_for_sdv disabled, disabling service");
54         return {};
55     }
56 
57     const auto instanceName = std::string(IPackageManagerProxy::descriptor) + "/default";
58     const binder_exception_t err =
59             AServiceManager_addService(this->asBinder().get(), instanceName.data());
60     if (err != EX_NONE) {
61         return Error(err) << "Failed to add IPackageManagerProxy to ServiceManager";
62     }
63 
64     mInitialized = true;
65     return {};
66 }
67 
getPackageManagerNative(android::sp<android::content::pm::IPackageManagerNative> * outPackageManagerNativeService)68 ndk::ScopedAStatus PackageManagerProxy::getPackageManagerNative(
69         android::sp<android::content::pm::IPackageManagerNative>* outPackageManagerNativeService) {
70     if (!mInitialized) {
71         return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1000,
72                                                                   "IPackageManagerProxy not "
73                                                                   "initialized or disabled");
74     }
75 
76     sp<IServiceManager> serviceManager = android::defaultServiceManager();
77     if (serviceManager.get() == nullptr) {
78         return ScopedAStatus::
79                 fromServiceSpecificErrorWithMessage(-1001,
80                                                     "Unable to access native ServiceManager");
81     }
82 
83     sp<IBinder> binder = serviceManager->waitForService(String16("package_native"));
84     *outPackageManagerNativeService = interface_cast<IPackageManagerNative>(binder);
85     if (*outPackageManagerNativeService == nullptr) {
86         return ScopedAStatus::
87                 fromServiceSpecificErrorWithMessage(-1002,
88                                                     "Unable to access native PackageManager");
89     }
90 
91     return ScopedAStatus::ok();
92 }
93 
getNamesForUids(const std::vector<int32_t> & uids,std::vector<std::string> * _aidl_return)94 ndk::ScopedAStatus PackageManagerProxy::getNamesForUids(const std::vector<int32_t>& uids,
95                                                         std::vector<std::string>* _aidl_return) {
96     android::sp<android::content::pm::IPackageManagerNative> packageManagerNativeService;
97     ScopedAStatus initStatus = getPackageManagerNative(&packageManagerNativeService);
98     if (!initStatus.isOk()) {
99         return initStatus;
100     }
101 
102     const auto result = packageManagerNativeService->getNamesForUids(uids, _aidl_return);
103 
104     if (!result.isOk()) {
105         return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
106                                                                   result.exceptionMessage()
107                                                                           .c_str());
108     }
109 
110     return ScopedAStatus::ok();
111 }
112 
getPackageUid(const std::string & packageName,int64_t flags,int32_t userId,int32_t * _aidl_return)113 ndk::ScopedAStatus PackageManagerProxy::getPackageUid(const std::string& packageName, int64_t flags,
114                                                       int32_t userId, int32_t* _aidl_return) {
115     android::sp<android::content::pm::IPackageManagerNative> packageManagerNativeService;
116     ScopedAStatus initStatus = getPackageManagerNative(&packageManagerNativeService);
117     if (!initStatus.isOk()) {
118         return initStatus;
119     }
120 
121     const auto result =
122             packageManagerNativeService->getPackageUid(packageName, flags, userId, _aidl_return);
123 
124     if (!result.isOk()) {
125         return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
126                                                                   result.exceptionMessage()
127                                                                           .c_str());
128     }
129 
130     return ScopedAStatus::ok();
131 }
132 
getVersionCodeForPackage(const std::string & packageName,int64_t * _aidl_return)133 ndk::ScopedAStatus PackageManagerProxy::getVersionCodeForPackage(const std::string& packageName,
134                                                                  int64_t* _aidl_return) {
135     android::sp<android::content::pm::IPackageManagerNative> packageManagerNativeService;
136     ScopedAStatus initStatus = getPackageManagerNative(&packageManagerNativeService);
137     if (!initStatus.isOk()) {
138         return initStatus;
139     }
140 
141     const String16 packageNameString16(packageName.c_str(), packageName.length());
142     const auto result = packageManagerNativeService->getVersionCodeForPackage(packageNameString16,
143                                                                               _aidl_return);
144 
145     if (!result.isOk()) {
146         return ScopedAStatus::fromServiceSpecificErrorWithMessage(result.exceptionCode(),
147                                                                   result.exceptionMessage()
148                                                                           .c_str());
149     }
150 
151     return ScopedAStatus::ok();
152 }
153 
154 }  // namespace packagemanagerproxy
155 }  // namespace sdv
156 }  // namespace google
157