remove global list in JscHeapCapture

Differential Revision: D4620810

fbshipit-source-id: 88ddadb1a2111408bc0188512dd818b7a7bf31c7
This commit is contained in:
Charles Dick 2017-02-28 11:12:37 -08:00 committed by Facebook Github Bot
parent 80225fb9e7
commit 31628f3aa4
3 changed files with 39 additions and 112 deletions

View File

@ -702,7 +702,11 @@ public class DevSupportManagerImpl implements
}
private void handleCaptureHeap() {
JSCHeapCapture.captureHeap(
if (mCurrentContext == null) {
return;
}
JSCHeapCapture heapCapture = mCurrentContext.getNativeModule(JSCHeapCapture.class);
heapCapture.captureHeap(
mApplicationContext.getCacheDir().getPath(),
JSCHeapUpload.captureCallback(mDevServerHelper.getHeapCaptureUploadUrl()));
}

View File

@ -12,9 +12,6 @@ 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;
@ -40,88 +37,32 @@ public class JSCHeapCapture extends ReactContextBaseJavaModule {
}
public interface CaptureCallback {
void onComplete(List<File> captures, List<CaptureException> failures);
}
private interface PerCaptureCallback {
void onSuccess(File capture);
void onFailure(CaptureException cause);
void onFailure(CaptureException error);
}
private @Nullable HeapCapture mHeapCapture;
private @Nullable PerCaptureCallback mCaptureInProgress;
private static final HashSet<JSCHeapCapture> sRegisteredDumpers = new HashSet<>();
private static synchronized void registerHeapCapture(JSCHeapCapture dumper) {
if (sRegisteredDumpers.contains(dumper)) {
throw new RuntimeException(
"a JSCHeapCapture registered more than once");
}
sRegisteredDumpers.add(dumper);
}
private static synchronized void unregisterHeapCapture(JSCHeapCapture dumper) {
sRegisteredDumpers.remove(dumper);
}
public static synchronized void captureHeap(String path, final CaptureCallback callback) {
final LinkedList<File> captureFiles = new LinkedList<>();
final LinkedList<CaptureException> captureFailures = new LinkedList<>();
if (sRegisteredDumpers.isEmpty()) {
captureFailures.add(new CaptureException("No JSC registered"));
callback.onComplete(captureFiles, captureFailures);
return;
}
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");
}
final int numRegisteredDumpers = sRegisteredDumpers.size();
disambiguate = 0;
for (JSCHeapCapture dumper : sRegisteredDumpers) {
File file = new File(path + "/capture" + Integer.toString(disambiguate) + ".json");
dumper.captureHeapHelper(file, new PerCaptureCallback() {
@Override
public void onSuccess(File capture) {
captureFiles.add(capture);
if (captureFiles.size() + captureFailures.size() == numRegisteredDumpers) {
callback.onComplete(captureFiles, captureFailures);
}
}
@Override
public void onFailure(CaptureException cause) {
captureFailures.add(cause);
if (captureFiles.size() + captureFailures.size() == numRegisteredDumpers) {
callback.onComplete(captureFiles, captureFailures);
}
}
});
}
}
private @Nullable CaptureCallback mCaptureInProgress;
public JSCHeapCapture(ReactApplicationContext reactContext) {
super(reactContext);
mHeapCapture = null;
mCaptureInProgress = null;
}
private synchronized void captureHeapHelper(File file, PerCaptureCallback callback) {
if (mHeapCapture == null) {
callback.onFailure(new CaptureException("HeapCapture.js module not connected"));
public synchronized void captureHeap(String path, final CaptureCallback callback) {
if (mCaptureInProgress != null) {
callback.onFailure(new CaptureException("Heap capture already in progress."));
return;
}
if (mCaptureInProgress != null) {
callback.onFailure(new CaptureException("Heap capture already in progress"));
File f = new File(path + "/capture.json");
f.delete();
HeapCapture heapCapture = getReactApplicationContext().getJSModule(HeapCapture.class);
if (heapCapture == null) {
callback.onFailure(new CaptureException("Heap capture js module not registered."));
return;
}
mCaptureInProgress = callback;
mHeapCapture.captureHeap(file.getPath());
heapCapture.captureHeap(f.getPath());
}
@ReactMethod
@ -140,18 +81,4 @@ public class JSCHeapCapture extends ReactContextBaseJavaModule {
public String getName() {
return "JSCHeapCapture";
}
@Override
public void initialize() {
super.initialize();
mHeapCapture = getReactApplicationContext().getJSModule(HeapCapture.class);
registerHeapCapture(this);
}
@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
unregisterHeapCapture(this);
mHeapCapture = null;
}
}

View File

@ -28,36 +28,32 @@ public class JSCHeapUpload {
public static JSCHeapCapture.CaptureCallback captureCallback(final String uploadUrl) {
return new JSCHeapCapture.CaptureCallback() {
@Override
public void onComplete(
List<File> captures,
List<JSCHeapCapture.CaptureException> failures) {
for (JSCHeapCapture.CaptureException e : failures) {
Log.e("JSCHeapCapture", e.getMessage());
}
public void onSuccess(File capture) {
OkHttpClient httpClient = new OkHttpClient.Builder().build();
RequestBody body = RequestBody.create(MediaType.parse("application/json"), capture);
Request request = new Request.Builder()
.url(uploadUrl)
.method("POST", body)
.build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("JSCHeapCapture", "Upload of heap capture failed: " + e.toString());
}
for (File path : captures) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), path);
Request request = new Request.Builder()
.url(uploadUrl)
.method("POST", body)
.build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("JSCHeapCapture", "Upload of heap capture failed: " + e.toString());
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
Log.e("JSCHeapCapture", "Upload of heap capture failed with code: " + Integer.toString(response.code()));
}
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
Log.e("JSCHeapCapture", "Upload of heap capture failed with code: " + Integer.toString(response.code()));
}
}
});
}
@Override
public void onFailure(JSCHeapCapture.CaptureException e) {
Log.e("JSCHeapCapture", "Heap capture failed: " + e.toString());
}
};
}