Bug 1246209 - Extract readJSONObjectFromFile out of getClientId. r=mfinkle
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
@@ -16,16 +16,17 @@ import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONArray;
+import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException;
import org.mozilla.gecko.GeckoProfileDirectories.NoSuchProfileException;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.LocalBrowserDB;
import org.mozilla.gecko.db.StubBrowserDB;
import org.mozilla.gecko.distribution.Distribution;
import org.mozilla.gecko.mozglue.ContextUtils;
@@ -607,30 +608,22 @@ public final class GeckoProfile {
* this code to fail.
*
* TODO: Write tests to prevent regressions. Mention them here. Test both file location and file format.
*
* [1]: https://mxr.mozilla.org/mozilla-central/source/toolkit/modules/ClientID.jsm
*/
@WorkerThread
public String getClientId() throws IOException {
- final String clientIdFileContents;
+ final JSONObject obj = readJSONObjectFromFile(CLIENT_ID_FILE_PATH);
try {
- clientIdFileContents = readFile(CLIENT_ID_FILE_PATH);
- } catch (final IOException e) {
- // Don't log exception to avoid leaking profile path.
- throw new IOException("Could not read client ID file to retrieve client ID");
- }
-
- try {
- final org.json.JSONObject json = new org.json.JSONObject(clientIdFileContents);
- return json.getString(CLIENT_ID_JSON_ATTR);
+ return obj.getString(CLIENT_ID_JSON_ATTR);
} catch (final JSONException e) {
- // Don't log exception to avoid leaking profile path.
- throw new IOException("Could not parse JSON to retrieve client ID");
+ // Don't log to avoid leaking data in JSONObject.
+ throw new IOException("Client ID does not exist in JSONObject");
}
}
/**
* Moves the session file to the backup session file.
*
* sessionstore.js should hold the current session, and sessionstore.bak
* should hold the previous session (where it is used to read the "tabs
@@ -686,16 +679,34 @@ public final class GeckoProfile {
bufferedWriter.close();
}
} catch (IOException e) {
Log.e(LOGTAG, "Error closing writer while writing to file", e);
}
}
}
+ @WorkerThread
+ public JSONObject readJSONObjectFromFile(final String filename) throws IOException {
+ final String fileContents;
+ try {
+ fileContents = readFile(filename);
+ } catch (final IOException e) {
+ // Don't log exception to avoid leaking profile path.
+ throw new IOException("Could not access given file to retrieve JSONObject");
+ }
+
+ try {
+ return new JSONObject(fileContents);
+ } catch (final JSONException e) {
+ // Don't log exception to avoid leaking profile path.
+ throw new IOException("Could not parse JSON to retrieve JSONObject");
+ }
+ }
+
public JSONArray readJSONArrayFromFile(final String filename) {
String fileContent;
try {
fileContent = readFile(filename);
} catch (IOException expected) {
return new JSONArray();
}