Fix crash in adsmanager login by nolonger assuming only one react context exists at once

Reviewed By: lexs

Differential Revision: D3252624

fb-gh-sync-id: e1d1c914be4e0caab953478f12441b440bd1f894
fbshipit-source-id: e1d1c914be4e0caab953478f12441b440bd1f894
This commit is contained in:
Charles Dick 2016-05-03 13:27:10 -07:00 committed by Facebook Github Bot 2
parent 34ec6a91a9
commit 0a7a228fbb
2 changed files with 39 additions and 18 deletions

View File

@ -16,6 +16,7 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -310,12 +311,14 @@ public class DevSupportManagerImpl implements DevSupportManager {
@Override
public void onOptionSelected() {
try {
String heapDumpPath = mApplicationContext.getCacheDir() + "/heapdump.json";
JSCHeapCapture.captureHeap(heapDumpPath, 60000);
String heapDumpPath = mApplicationContext.getCacheDir().getPath();
List<String> captureFiles = JSCHeapCapture.captureHeap(heapDumpPath, 60000);
for (String captureFile : captureFiles) {
Toast.makeText(
mCurrentContext,
"Heap captured to " + heapDumpPath,
"Heap captured to " + captureFile,
Toast.LENGTH_LONG).show();
}
} catch (JSCHeapCapture.CaptureException e) {
showNewJavaError(e.getMessage(), e);
}

View File

@ -11,6 +11,11 @@ package com.facebook.react.devsupport;
import javax.annotation.Nullable;
import java.io.File;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@ -33,28 +38,41 @@ public class JSCHeapCapture extends ReactContextBaseJavaModule {
private int mOperationToken;
private @Nullable String mOperationError;
private static @Nullable JSCHeapCapture sJSCHeapCapture = null;
private static final HashSet<JSCHeapCapture> sRegisteredDumpers = new HashSet<>();
private static synchronized void registerHeapCapture(JSCHeapCapture dumper) {
if (sJSCHeapCapture != null) {
if (sRegisteredDumpers.contains(dumper)) {
throw new RuntimeException(
"JSCHeapCapture already registered. Are you running more than one JSC?");
"a JSCHeapCapture registered more than once");
}
sJSCHeapCapture = dumper;
sRegisteredDumpers.add(dumper);
}
private static synchronized void unregisterHeapCapture(JSCHeapCapture dumper) {
if (sJSCHeapCapture != dumper) {
throw new RuntimeException("Can't unregister JSCHeapCapture that is not registered.");
}
sJSCHeapCapture = null;
sRegisteredDumpers.remove(dumper);
}
public static synchronized void captureHeap(String path, long timeout) throws CaptureException {
if (sJSCHeapCapture == null) {
throw new CaptureException("No JSC registered.");
public static synchronized List<String> captureHeap(String path, long timeout)
throws CaptureException {
LinkedList<String> captureFiles = new LinkedList<>();
if (sRegisteredDumpers.isEmpty()) {
throw new CaptureException("No JSC registered");
}
sJSCHeapCapture.captureHeapHelper(path, timeout);
int disambiguate = 0;
File f = new File(path + "/capture" + Integer.toString(disambiguate) + ".json");
while (f.delete()) {
disambiguate++;
f = new File(path + "/capture" + Integer.toString(disambiguate) + ".json");
}
disambiguate = 0;
for (JSCHeapCapture dumper : sRegisteredDumpers) {
String file = path + "/capture" + Integer.toString(disambiguate) + ".json";
dumper.captureHeapHelper(file, timeout);
captureFiles.add(file);
}
return captureFiles;
}
public JSCHeapCapture(ReactApplicationContext reactContext) {