Skip to content

Lazy initialize dataFile in PersistedInstallation #3314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @hide
*/
public class PersistedInstallation {
private final File dataFile;
private File dataFile;
@NonNull private final FirebaseApp firebaseApp;

// Registration Status of each persisted fid entry
Expand Down Expand Up @@ -77,15 +77,27 @@ public enum RegistrationStatus {
private static final String FIS_ERROR_KEY = "FisError";

public PersistedInstallation(@NonNull FirebaseApp firebaseApp) {
// Different FirebaseApp in the same Android application should have the same application
// context and same dir path
dataFile =
new File(
firebaseApp.getApplicationContext().getFilesDir(),
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
this.firebaseApp = firebaseApp;
}

private File getDataFile() {

if (dataFile == null) {
synchronized (this) {
if (dataFile == null) {
// Different FirebaseApp in the same Android application should have the same application
// context and same dir path
dataFile =
new File(
firebaseApp.getApplicationContext().getFilesDir(),
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
}
}
}

return dataFile;
}

@NonNull
public PersistedInstallationEntry readPersistedInstallationEntryValue() {
JSONObject json = readJSONFromFile();
Expand Down Expand Up @@ -114,7 +126,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
private JSONObject readJSONFromFile() {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] tmpBuf = new byte[16 * 1024];
try (FileInputStream fis = new FileInputStream(dataFile)) {
try (FileInputStream fis = new FileInputStream(getDataFile())) {
while (true) {
int numRead = fis.read(tmpBuf, 0, tmpBuf.length);
if (numRead < 0) {
Expand Down Expand Up @@ -156,7 +168,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
fos.close();

// Snapshot the temp file to the actual file
if (!tmpFile.renameTo(dataFile)) {
if (!tmpFile.renameTo(getDataFile())) {
throw new IOException("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX);
}
} catch (JSONException | IOException e) {
Expand All @@ -173,6 +185,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(

/** Sets the state to ATTEMPT_MIGRATION. */
public void clearForTesting() {
dataFile.delete();
getDataFile().delete();
}
}