Bug 1357783 - infer: Avoid CONTEXT_LEAK in stumbler r=sebastian

This solves 2 issues:
- We keep a reference to Tracker (which is implemented by a Service) forever,
  which is solved by keeping a WeakReference.
- We kept another reference to the Service by using it as a Context - we can
  avoid that by using the ApplicationContext instead.

MozReview-Commit-ID: 6UNSkZx12an

--HG--
extra : rebase_source : bfb50d02246e4377ef23179747987bc82731fd54
This commit is contained in:
Andrzej Hunt 2017-04-18 23:18:41 -07:00
Родитель 1bc56c5a1f
Коммит a1a692f7f5
1 изменённых файлов: 14 добавлений и 6 удалений

Просмотреть файл

@ -15,6 +15,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Timer;
@ -63,7 +64,7 @@ public class DataStorageManager {
private final ReportBatchBuilder mCurrentReports = new ReportBatchBuilder();
private final File mReportsDir;
private final File mStatsFile;
private final StorageIsEmptyTracker mTracker;
private final WeakReference<StorageIsEmptyTracker> mTrackerWeakReference;
private static DataStorageManager sInstance;
@ -215,6 +216,10 @@ public class DataStorageManager {
return dir.getPath();
}
/*
* Setup a new global instance. A WeakReference will be kept to the supplied tracker, the caller
* is reponsible for the tracker lifetime.
*/
public static synchronized void createGlobalInstance(Context context, StorageIsEmptyTracker tracker) {
DataStorageManager.createGlobalInstance(context, tracker,
DEFAULT_MAX_BYTES_STORED_ON_DISK, DEFAULT_MAX_WEEKS_DATA_ON_DISK);
@ -222,7 +227,9 @@ public class DataStorageManager {
public static synchronized void createGlobalInstance(Context context, StorageIsEmptyTracker tracker,
long maxBytesStoredOnDisk, int maxWeeksDataStored) {
if (sInstance != null) {
// Only create if no instance already exists. If there's an instance but the tracker it was associated
// with has died, then treat the old instance as dead.
if (sInstance != null && sInstance.mTrackerWeakReference.get() != null) {
return;
}
sInstance = new DataStorageManager(context, tracker, maxBytesStoredOnDisk, maxWeeksDataStored);
@ -236,8 +243,8 @@ public class DataStorageManager {
long maxBytesStoredOnDisk, int maxWeeksDataStored) {
mMaxBytesDiskStorage = maxBytesStoredOnDisk;
mMaxWeeksStored = maxWeeksDataStored;
mTracker = tracker;
final String baseDir = getStorageDir(c);
mTrackerWeakReference = new WeakReference<>(tracker);
final String baseDir = getStorageDir(c.getApplicationContext());
mStatsFile = new File(baseDir, "upload_stats.ini");
mReportsDir = new File(baseDir + "/reports");
if (!mReportsDir.exists()) {
@ -464,8 +471,9 @@ public class DataStorageManager {
}
private void notifyStorageIsEmpty(boolean isEmpty) {
if (mTracker != null) {
mTracker.notifyStorageStateEmpty(isEmpty);
final StorageIsEmptyTracker tracker = mTrackerWeakReference.get();
if (tracker != null) {
tracker.notifyStorageStateEmpty(isEmpty);
}
}