Merge "DO NOT MERGE Libnativebridge: Temporarily change back to late dlopen" into lmp-dev

This commit is contained in:
Andreas Gampe 2014-09-10 05:09:58 +00:00 committed by Android (Google) Code Review
commit 4d1365b4d3
2 changed files with 65 additions and 30 deletions

View file

@ -29,6 +29,7 @@ static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
enum class NativeBridgeState {
kNotSetup, // Initial state.
kOpened, // After successful dlopen.
// Temporary meaning: string copied. TODO: remove. b/17440362
kInitialized, // After successful initialization.
kClosed // Closed or errors.
};
@ -60,6 +61,9 @@ static NativeBridgeState state = NativeBridgeState::kNotSetup;
// Whether we had an error at some point.
static bool had_error = false;
// Native bridge filename. TODO: Temporary, remove. b/17440362
static const char* native_bridge_filename;
// Handle of the loaded library.
static void* native_bridge_handle = nullptr;
// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
@ -131,28 +135,32 @@ bool LoadNativeBridge(const char* nb_library_filename,
state = NativeBridgeState::kClosed;
had_error = true;
} else {
// Try to open the library.
void* handle = dlopen(nb_library_filename, RTLD_LAZY);
if (handle != nullptr) {
callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
kNativeBridgeInterfaceSymbol));
if (callbacks != nullptr) {
// Store the handle for later.
native_bridge_handle = handle;
} else {
dlclose(handle);
}
}
// Two failure conditions: could not find library (dlopen failed), or could not find native
// bridge interface (dlsym failed). Both are an error and close the native bridge.
if (callbacks == nullptr) {
had_error = true;
state = NativeBridgeState::kClosed;
} else {
runtime_callbacks = runtime_cbs;
state = NativeBridgeState::kOpened;
}
// Save the name. TODO: Remove this, return to old flow. b/17440362
native_bridge_filename = nb_library_filename;
runtime_callbacks = runtime_cbs;
state = NativeBridgeState::kOpened;
// // Try to open the library.
// void* handle = dlopen(nb_library_filename, RTLD_LAZY);
// if (handle != nullptr) {
// callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
// kNativeBridgeInterfaceSymbol));
// if (callbacks != nullptr) {
// // Store the handle for later.
// native_bridge_handle = handle;
// } else {
// dlclose(handle);
// }
// }
//
// // Two failure conditions: could not find library (dlopen failed), or could not find native
// // bridge interface (dlsym failed). Both are an error and close the native bridge.
// if (callbacks == nullptr) {
// had_error = true;
// state = NativeBridgeState::kClosed;
// } else {
// runtime_callbacks = runtime_cbs;
// state = NativeBridgeState::kOpened;
// }
}
return state == NativeBridgeState::kOpened;
}
@ -163,15 +171,38 @@ bool InitializeNativeBridge() {
// point we are not multi-threaded, so we do not need locking here.
if (state == NativeBridgeState::kOpened) {
// Try to initialize.
if (callbacks->initialize(runtime_callbacks)) {
state = NativeBridgeState::kInitialized;
// Open and initialize. TODO: Temporary, remove. b/17440362
void* handle = dlopen(native_bridge_filename, RTLD_LAZY);
if (handle != nullptr) {
callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
kNativeBridgeInterfaceSymbol));
if (callbacks != nullptr) {
if (callbacks->initialize(runtime_callbacks)) {
state = NativeBridgeState::kInitialized;
native_bridge_handle = handle;
} else {
callbacks = nullptr;
}
}
if (callbacks == nullptr) {
state = NativeBridgeState::kClosed;
had_error = true;
dlclose(handle);
}
} else {
// Unload the library.
dlclose(native_bridge_handle);
had_error = true;
state = NativeBridgeState::kClosed;
had_error = true;
}
// // Try to initialize.
// if (callbacks->initialize(runtime_callbacks)) {
// state = NativeBridgeState::kInitialized;
// } else {
// // Unload the library.
// dlclose(native_bridge_handle);
// had_error = true;
// state = NativeBridgeState::kClosed;
// }
} else {
had_error = true;
state = NativeBridgeState::kClosed;
@ -185,7 +216,7 @@ void UnloadNativeBridge() {
// point we are not multi-threaded, so we do not need locking here.
switch(state) {
case NativeBridgeState::kOpened:
// case NativeBridgeState::kOpened: // TODO: Re-add this. b/17440362
case NativeBridgeState::kInitialized:
// Unload.
dlclose(native_bridge_handle);
@ -196,6 +227,7 @@ void UnloadNativeBridge() {
had_error = true;
break;
case NativeBridgeState::kOpened: // TODO: Remove this. b/17440362
case NativeBridgeState::kClosed:
// Ignore.
break;

View file

@ -27,9 +27,12 @@ TEST_F(NativeBridgeTest, ValidName) {
// Now check what happens on LoadNativeBridge.
EXPECT_EQ(false, NativeBridgeError());
LoadNativeBridge(kTestName, nullptr);
// TODO: Remove this call. b/17440362
InitializeNativeBridge();
// This will lead to an error as the library doesn't exist.
EXPECT_EQ(true, NativeBridgeError());
EXPECT_EQ(false, NativeBridgeAvailable());
// TODO: Test again. b/17440362
// EXPECT_EQ(false, NativeBridgeAvailable());
}
} // namespace android