Skip to content

Commit 296daf4

Browse files
OpenXR loader: Refactor content provider access code.
De-duplicate implementations, and be more precise in naming. Co-authored-by: Rylie Pavlik <[email protected]>
1 parent 3175cdd commit 296daf4

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

src/loader/android_utilities.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ namespace functions {
125125
static constexpr auto TABLE_PATH = "functions";
126126

127127
/**
128-
* Create a content URI for querying all rows of the function remapping data for a given
128+
* Create a content URI for querying all rows of the runtime function remapping data for a given
129129
* runtime package and major version of OpenXR.
130130
*
131131
* @param systemBroker If the system runtime broker (instead of the installable one) should be queried.
@@ -134,7 +134,7 @@ static constexpr auto TABLE_PATH = "functions";
134134
* @param abi The Android ABI name in use.
135135
* @return A content URI for the entire table: the function remapping for that runtime.
136136
*/
137-
static Uri makeContentUri(bool systemBroker, int majorVersion, std::string const &packageName, const char *abi) {
137+
static Uri makeRuntimeContentUri(bool systemBroker, int majorVersion, std::string const &packageName, const char *abi) {
138138
auto builder = Uri_Builder::construct();
139139
builder.scheme("content")
140140
.authority(getBrokerAuthority(systemBroker))
@@ -213,34 +213,6 @@ inline JsonManifestBuilder &JsonManifestBuilder::function(const std::string &fun
213213

214214
static constexpr const char *getBrokerTypeName(bool systemBroker) { return systemBroker ? "system" : "installable"; }
215215

216-
static int populateFunctions(wrap::android::content::Context const &context, bool systemBroker, const std::string &packageName,
217-
JsonManifestBuilder &builder) {
218-
jni::Array<std::string> projection = makeArray({functions::Columns::FUNCTION_NAME, functions::Columns::SYMBOL_NAME});
219-
220-
auto uri = functions::makeContentUri(systemBroker, XR_VERSION_MAJOR(XR_CURRENT_API_VERSION), packageName, ABI);
221-
ALOGI("populateFunctions: Querying URI: %s", uri.toString().c_str());
222-
223-
Cursor cursor = context.getContentResolver().query(uri, projection);
224-
225-
if (cursor.isNull()) {
226-
ALOGE("Null cursor when querying content resolver for functions.");
227-
return -1;
228-
}
229-
if (cursor.getCount() < 1) {
230-
ALOGE("Non-null but empty cursor when querying content resolver for functions.");
231-
cursor.close();
232-
return -1;
233-
}
234-
auto functionIndex = cursor.getColumnIndex(functions::Columns::FUNCTION_NAME);
235-
auto symbolIndex = cursor.getColumnIndex(functions::Columns::SYMBOL_NAME);
236-
while (cursor.moveToNext()) {
237-
builder.function(cursor.getString(functionIndex), cursor.getString(symbolIndex));
238-
}
239-
240-
cursor.close();
241-
return 0;
242-
}
243-
244216
// The current file relies on android-jni-wrappers and jnipp, which may throw on failure.
245217
// This is problematic when the loader is compiled with exception handling disabled - the consumers can reasonably
246218
// expect that the compilation with -fno-exceptions will succeed, but the compiler will not accept the code that
@@ -261,33 +233,61 @@ static int populateFunctions(wrap::android::content::Context const &context, boo
261233

262234
#endif // XRLOADER_DISABLE_EXCEPTION_HANDLING
263235

264-
/// Get cursor for active runtime, parameterized by whether or not we use the system broker
265-
static bool getActiveRuntimeCursor(wrap::android::content::Context const &context, jni::Array<std::string> const &projection,
266-
bool systemBroker, Cursor &cursor) {
267-
auto uri = active_runtime::makeContentUri(systemBroker, XR_VERSION_MAJOR(XR_CURRENT_API_VERSION), ABI);
268-
ALOGI("getActiveRuntimeCursor: Querying URI: %s", uri.toString().c_str());
236+
/// Generic content resolver query function
237+
static bool getCursor(wrap::android::content::Context const &context, jni::Array<std::string> const &projection, Uri const &uri,
238+
bool systemBroker, const char *contentDesc, Cursor &out_cursor) {
239+
ALOGI("getCursor: Querying URI: %s", uri.toString().c_str());
269240

270-
ANDROID_UTILITIES_TRY { cursor = context.getContentResolver().query(uri, projection); }
241+
ANDROID_UTILITIES_TRY { out_cursor = context.getContentResolver().query(uri, projection); }
271242
ANDROID_UTILITIES_CATCH_FALLBACK({
272-
ALOGW("Exception when querying %s content resolver: %s", getBrokerTypeName(systemBroker), e.what());
273-
cursor = {};
243+
ALOGI("Exception when querying %s content resolver for %s: %s", getBrokerTypeName(systemBroker), contentDesc, e.what());
244+
out_cursor = {};
274245
return false;
275246
})
276247

277-
if (cursor.isNull()) {
278-
ALOGW("Null cursor when querying %s content resolver.", getBrokerTypeName(systemBroker));
279-
cursor = {};
248+
if (out_cursor.isNull()) {
249+
ALOGI("Null cursor when querying %s content resolver for %s.", getBrokerTypeName(systemBroker), contentDesc);
250+
out_cursor = {};
280251
return false;
281252
}
282-
if (cursor.getCount() < 1) {
283-
ALOGW("Non-null but empty cursor when querying %s content resolver.", getBrokerTypeName(systemBroker));
284-
cursor.close();
285-
cursor = {};
253+
if (out_cursor.getCount() < 1) {
254+
ALOGI("Non-null but empty cursor when querying %s content resolver for %s.", getBrokerTypeName(systemBroker), contentDesc);
255+
out_cursor.close();
256+
out_cursor = {};
286257
return false;
287258
}
288259
return true;
289260
}
290261

262+
static int populateFunctions(wrap::android::content::Context const &context, bool systemBroker, const std::string &packageName,
263+
JsonManifestBuilder &builder) {
264+
jni::Array<std::string> projection = makeArray({functions::Columns::FUNCTION_NAME, functions::Columns::SYMBOL_NAME});
265+
266+
auto uri = functions::makeRuntimeContentUri(systemBroker, XR_VERSION_MAJOR(XR_CURRENT_API_VERSION), packageName, ABI);
267+
ALOGI("populateFunctions: Querying URI: %s", uri.toString().c_str());
268+
Cursor cursor;
269+
if (!getCursor(context, projection, uri, systemBroker, "functions", cursor)) {
270+
return -1;
271+
}
272+
273+
auto functionIndex = cursor.getColumnIndex(functions::Columns::FUNCTION_NAME);
274+
auto symbolIndex = cursor.getColumnIndex(functions::Columns::SYMBOL_NAME);
275+
while (cursor.moveToNext()) {
276+
builder.function(cursor.getString(functionIndex), cursor.getString(symbolIndex));
277+
}
278+
279+
cursor.close();
280+
return 0;
281+
}
282+
283+
/// Get cursor for active runtime, parameterized by whether or not we use the system broker
284+
static bool getActiveRuntimeCursor(wrap::android::content::Context const &context, jni::Array<std::string> const &projection,
285+
bool systemBroker, Cursor &cursor) {
286+
auto uri = active_runtime::makeContentUri(systemBroker, XR_VERSION_MAJOR(XR_CURRENT_API_VERSION), ABI);
287+
ALOGI("getActiveRuntimeCursor: Querying URI: %s", uri.toString().c_str());
288+
return getCursor(context, projection, uri, systemBroker, "active runtime", cursor);
289+
}
290+
291291
int getActiveRuntimeVirtualManifest(wrap::android::content::Context const &context, Json::Value &virtualManifest) {
292292
jni::Array<std::string> projection = makeArray({active_runtime::Columns::PACKAGE_NAME, active_runtime::Columns::NATIVE_LIB_DIR,
293293
active_runtime::Columns::SO_FILENAME, active_runtime::Columns::HAS_FUNCTIONS});
@@ -303,7 +303,7 @@ int getActiveRuntimeVirtualManifest(wrap::android::content::Context const &conte
303303

304304
if (cursor.isNull()) {
305305
// Couldn't find either broker
306-
ALOGE("Could access neither the installable nor system runtime broker.");
306+
ALOGI("Could access neither the installable nor system runtime broker.");
307307
return -1;
308308
}
309309

0 commit comments

Comments
 (0)