mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 878670 - Version profile information cache file. r=nalexander
This commit is contained in:
parent
f68cb852ba
commit
4454d00df9
@ -26,6 +26,7 @@ import org.mozilla.gecko.background.healthreport.EnvironmentBuilder.ProfileInfor
|
|||||||
public class ProfileInformationCache implements ProfileInformationProvider {
|
public class ProfileInformationCache implements ProfileInformationProvider {
|
||||||
private static final String LOG_TAG = "GeckoProfileInfo";
|
private static final String LOG_TAG = "GeckoProfileInfo";
|
||||||
private static final String CACHE_FILE = "profile_info_cache.json";
|
private static final String CACHE_FILE = "profile_info_cache.json";
|
||||||
|
public static final int FORMAT_VERSION = 1;
|
||||||
|
|
||||||
protected boolean initialized = false;
|
protected boolean initialized = false;
|
||||||
protected boolean needsWrite = false;
|
protected boolean needsWrite = false;
|
||||||
@ -51,6 +52,7 @@ public class ProfileInformationCache implements ProfileInformationProvider {
|
|||||||
public JSONObject toJSON() {
|
public JSONObject toJSON() {
|
||||||
JSONObject object = new JSONObject();
|
JSONObject object = new JSONObject();
|
||||||
try {
|
try {
|
||||||
|
object.put("version", FORMAT_VERSION);
|
||||||
object.put("blocklist", blocklistEnabled);
|
object.put("blocklist", blocklistEnabled);
|
||||||
object.put("telemetry", telemetryEnabled);
|
object.put("telemetry", telemetryEnabled);
|
||||||
object.put("profileCreated", profileCreationTime);
|
object.put("profileCreated", profileCreationTime);
|
||||||
@ -63,11 +65,50 @@ public class ProfileInformationCache implements ProfileInformationProvider {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fromJSON(JSONObject object) throws JSONException {
|
/**
|
||||||
blocklistEnabled = object.getBoolean("blocklist");
|
* Attempt to restore this object from a JSON blob.
|
||||||
telemetryEnabled = object.getBoolean("telemetry");
|
*
|
||||||
profileCreationTime = object.getLong("profileCreated");
|
* @return false if there's a version mismatch or an error, true on success.
|
||||||
addons = object.optJSONObject("addons");
|
*/
|
||||||
|
private boolean fromJSON(JSONObject object) throws JSONException {
|
||||||
|
int version = object.optInt("version", 1);
|
||||||
|
switch (version) {
|
||||||
|
case FORMAT_VERSION:
|
||||||
|
blocklistEnabled = object.getBoolean("blocklist");
|
||||||
|
telemetryEnabled = object.getBoolean("telemetry");
|
||||||
|
profileCreationTime = object.getLong("profileCreated");
|
||||||
|
addons = object.optJSONObject("addons");
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
Logger.warn(LOG_TAG, "Unable to restore from version " + version + " PIC file: expecting " + FORMAT_VERSION);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected JSONObject readFromFile() throws FileNotFoundException, JSONException {
|
||||||
|
Scanner scanner = null;
|
||||||
|
try {
|
||||||
|
scanner = new Scanner(file, "UTF-8");
|
||||||
|
final String contents = scanner.useDelimiter("\\A").next();
|
||||||
|
return new JSONObject(contents);
|
||||||
|
} finally {
|
||||||
|
if (scanner != null) {
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void writeToFile(JSONObject object) throws IOException {
|
||||||
|
Logger.debug(LOG_TAG, "Writing profile information.");
|
||||||
|
Logger.pii(LOG_TAG, "Writing to file: " + file.getAbsolutePath());
|
||||||
|
FileOutputStream stream = new FileOutputStream(file);
|
||||||
|
OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
|
||||||
|
try {
|
||||||
|
writer.append(object.toString());
|
||||||
|
needsWrite = false;
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,16 +127,7 @@ public class ProfileInformationCache implements ProfileInformationProvider {
|
|||||||
throw new IOException("Couldn't serialize JSON.");
|
throw new IOException("Couldn't serialize JSON.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.debug(LOG_TAG, "Writing profile information.");
|
writeToFile(object);
|
||||||
Logger.pii(LOG_TAG, "Writing to file: " + file.getAbsolutePath());
|
|
||||||
FileOutputStream stream = new FileOutputStream(file);
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
|
|
||||||
try {
|
|
||||||
writer.append(object.toString());
|
|
||||||
needsWrite = false;
|
|
||||||
} finally {
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -118,11 +150,11 @@ public class ProfileInformationCache implements ProfileInformationProvider {
|
|||||||
Logger.info(LOG_TAG, "Restoring ProfileInformationCache from file.");
|
Logger.info(LOG_TAG, "Restoring ProfileInformationCache from file.");
|
||||||
Logger.pii(LOG_TAG, "Restoring from file: " + file.getAbsolutePath());
|
Logger.pii(LOG_TAG, "Restoring from file: " + file.getAbsolutePath());
|
||||||
|
|
||||||
Scanner scanner = null;
|
|
||||||
try {
|
try {
|
||||||
scanner = new Scanner(file, "UTF-8");
|
if (!fromJSON(readFromFile())) {
|
||||||
final String contents = scanner.useDelimiter("\\A").next();
|
// No need to blow away the file; the caller can eventually overwrite it.
|
||||||
fromJSON(new JSONObject(contents));
|
return false;
|
||||||
|
}
|
||||||
initialized = true;
|
initialized = true;
|
||||||
needsWrite = false;
|
needsWrite = false;
|
||||||
return true;
|
return true;
|
||||||
@ -130,10 +162,6 @@ public class ProfileInformationCache implements ProfileInformationProvider {
|
|||||||
return false;
|
return false;
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
|
||||||
if (scanner != null) {
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user