From 47b5b4c4a4f614b910527a1f0e235c9d3af6f918 Mon Sep 17 00:00:00 2001 From: Bhavin Modi Date: Fri, 20 Mar 2020 18:54:21 -0700 Subject: [PATCH] Switch SoLoader implementation to use NativeLoader Summary: Changed the way .so's are loaded. Using the NativeLoader which takes in a delegate and requires init on app start. Existing SoLoader init already calls NativeLoader init. Oculus apps can now call NativeLoader init directly with the provided SystemDelegate. This now enabled us to remove the SoLoader dep for all oculus apps. Reviewed By: swiftcoder Differential Revision: D20301277 fbshipit-source-id: 43a1fb69c9a7161d5cd0d1180c382b1bffa6463a --- .../NativeLoaderToSoLoaderDelegate.java | 11 ++++++ .../soloader/nativeloader/NativeLoader.java | 39 +++++++++++++++++++ .../nativeloader/NativeLoaderDelegate.java | 6 +++ .../soloader/nativeloader/SystemDelegate.java | 10 +++++ 4 files changed, 66 insertions(+) diff --git a/java/com/facebook/soloader/NativeLoaderToSoLoaderDelegate.java b/java/com/facebook/soloader/NativeLoaderToSoLoaderDelegate.java index 6f7ed4e..83d951c 100644 --- a/java/com/facebook/soloader/NativeLoaderToSoLoaderDelegate.java +++ b/java/com/facebook/soloader/NativeLoaderToSoLoaderDelegate.java @@ -17,6 +17,7 @@ package com.facebook.soloader; import com.facebook.soloader.nativeloader.NativeLoaderDelegate; +import java.io.IOException; /** Class that connects SoLoader to NativeLoader */ public class NativeLoaderToSoLoaderDelegate implements NativeLoaderDelegate { @@ -24,4 +25,14 @@ public class NativeLoaderToSoLoaderDelegate implements NativeLoaderDelegate { public boolean loadLibrary(final String shortName) { return SoLoader.loadLibrary(shortName); } + + @Override + public String getLibraryPath(String libName) throws IOException { + return SoLoader.getLibraryPath(libName); + } + + @Override + public int getSoSourcesVersion() { + return SoLoader.getSoSourcesVersion(); + } } diff --git a/java/com/facebook/soloader/nativeloader/NativeLoader.java b/java/com/facebook/soloader/nativeloader/NativeLoader.java index 9fec765..1e83247 100644 --- a/java/com/facebook/soloader/nativeloader/NativeLoader.java +++ b/java/com/facebook/soloader/nativeloader/NativeLoader.java @@ -16,6 +16,8 @@ package com.facebook.soloader.nativeloader; +import java.io.IOException; + /** Facade to load native libraries for android */ public class NativeLoader { @@ -44,6 +46,43 @@ public static boolean loadLibrary(String shortName) { return sDelegate.loadLibrary(shortName); } + /** + * Get the path of a loaded shared library. + * + * @param shortName Name of library to find, without "lib" prefix or ".so" suffix + * @return The path for the loaded library, null otherwise. + */ + public static String getLibraryPath(String shortName) throws IOException { + synchronized (NativeLoader.class) { + if (sDelegate == null) { + throw new IllegalStateException( + "NativeLoader has not been initialized. " + + "To use standard native library loading, call " + + "NativeLoader.init(new SystemDelegate())."); + } + } + + return sDelegate.getLibraryPath(shortName); + } + + /** + * Get the version of the loader used. + * + * @return The version number for the loader. + */ + public static int getSoSourcesVersion() { + synchronized (NativeLoader.class) { + if (sDelegate == null) { + throw new IllegalStateException( + "NativeLoader has not been initialized. " + + "To use standard native library loading, call " + + "NativeLoader.init(new SystemDelegate())."); + } + } + + return sDelegate.getSoSourcesVersion(); + } + /** * Initializes native code loading for this app. Should be called only once, before any calls to * {@link #loadLibrary(String)}. diff --git a/java/com/facebook/soloader/nativeloader/NativeLoaderDelegate.java b/java/com/facebook/soloader/nativeloader/NativeLoaderDelegate.java index 790687e..0686325 100644 --- a/java/com/facebook/soloader/nativeloader/NativeLoaderDelegate.java +++ b/java/com/facebook/soloader/nativeloader/NativeLoaderDelegate.java @@ -16,8 +16,14 @@ package com.facebook.soloader.nativeloader; +import java.io.IOException; + /** Interface used to connect chosen loader of native libraries to NativeLoader */ public interface NativeLoaderDelegate { /** @see com.facebook.soloader.nativeloader.NativeLoader#loadLibrary(String) */ boolean loadLibrary(String shortName); + /** @see com.facebook.soloader.nativeloader.NativeLoader#getLibraryPath(String) */ + String getLibraryPath(String libName) throws IOException; + /** @see com.facebook.soloader.nativeloader.NativeLoader#getSoSourcesVersion() */ + int getSoSourcesVersion(); } diff --git a/java/com/facebook/soloader/nativeloader/SystemDelegate.java b/java/com/facebook/soloader/nativeloader/SystemDelegate.java index c085d3a..08ee0f9 100644 --- a/java/com/facebook/soloader/nativeloader/SystemDelegate.java +++ b/java/com/facebook/soloader/nativeloader/SystemDelegate.java @@ -25,4 +25,14 @@ public boolean loadLibrary(final String shortName) { // so let's just always assume it was. return true; } + + @Override + public String getLibraryPath(String libName) { + return null; + } + + @Override + public int getSoSourcesVersion() { + return 0; + } }