Skip to content

Commit

Permalink
Observer SoFileLoader calls
Browse files Browse the repository at this point in the history
Summary:
Add implementation of SoFileLoader interface that delegates all calls and
notifies `Observer`s

Reviewed By: adicatana, danjin250

Differential Revision: D48149670

fbshipit-source-id: 76151f80adf224080e301e8a2228cb11e349f8e1
  • Loading branch information
michalgr authored and facebook-github-bot committed Jan 25, 2024
1 parent ad4dcc1 commit c96d478
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
59 changes: 59 additions & 0 deletions java/com/facebook/soloader/InstrumentedSoFileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/

package com.facebook.soloader;

import android.annotation.SuppressLint;
import com.facebook.soloader.observer.ObserverHolder;
import javax.annotation.Nullable;

public class InstrumentedSoFileLoader implements SoFileLoader {
private final SoFileLoader mDelegate;

public InstrumentedSoFileLoader(SoFileLoader delegate) {
mDelegate = delegate;
}

@Override
@SuppressLint({"CatchGeneralException", "EmptyCatchBlock"})
public void load(String pathToSoFile, int loadFlags) {
@Nullable Throwable failure = null;
ObserverHolder.onSoFileLoaderLoadStart(mDelegate, "load", loadFlags);
try {
mDelegate.load(pathToSoFile, loadFlags);
} catch (Throwable t) {
failure = t;
throw t;
} finally {
ObserverHolder.onSoFileLoaderLoadEnd(failure);
}
}

@Override
@SuppressLint({"CatchGeneralException", "EmptyCatchBlock"})
public void loadBytes(String pathName, ElfByteChannel bytes, int loadFlags) {
@Nullable Throwable failure = null;
ObserverHolder.onSoFileLoaderLoadStart(mDelegate, "loadBytes", loadFlags);
try {
mDelegate.loadBytes(pathName, bytes, loadFlags);
} catch (Throwable t) {
failure = t;
throw t;
} finally {
ObserverHolder.onSoFileLoaderLoadEnd(failure);
}
}
}
2 changes: 1 addition & 1 deletion java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private static synchronized void initSoLoader(
return;
}

sSoFileLoader = new SoFileLoaderImpl();
sSoFileLoader = new InstrumentedSoFileLoader(new SoFileLoaderImpl());
}

private static int getAppType(Context context) {
Expand Down
5 changes: 5 additions & 0 deletions java/com/facebook/soloader/observer/Observer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.soloader.observer;

import com.facebook.soloader.SoFileLoader;
import com.facebook.soloader.SoSource;
import com.facebook.soloader.recovery.RecoveryStrategy;
import javax.annotation.Nullable;
Expand All @@ -40,4 +41,8 @@ public interface Observer {
void onGetDependenciesStart();

void onGetDependenciesEnd(@Nullable Throwable t);

void onSoFileLoaderLoadStart(SoFileLoader soFileLoader, String method, int flags);

void onSoFileLoaderLoadEnd(@Nullable Throwable t);
}
19 changes: 19 additions & 0 deletions java/com/facebook/soloader/observer/ObserverHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.soloader.observer;

import com.facebook.soloader.SoFileLoader;
import com.facebook.soloader.SoSource;
import com.facebook.soloader.recovery.RecoveryStrategy;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -133,4 +134,22 @@ public static void onGetDependenciesEnd(@Nullable Throwable t) {
}
}
}

public static void onSoFileLoaderLoadStart(SoFileLoader soFileLoader, String method, int flags) {
Observer[] observers = sObservers.get();
if (observers != null) {
for (Observer observer : observers) {
observer.onSoFileLoaderLoadStart(soFileLoader, method, flags);
}
}
}

public static void onSoFileLoaderLoadEnd(@Nullable Throwable t) {
Observer[] observers = sObservers.get();
if (observers != null) {
for (Observer observer : observers) {
observer.onSoFileLoaderLoadEnd(t);
}
}
}
}

0 comments on commit c96d478

Please sign in to comment.