Bug 1246209 - Extract readJSONObjectFromFile out of getClientId. r=mfinkle draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 08 Feb 2016 15:03:21 -0800
changeset 329738 629f11457149f2ea21fef6fa9f4c7990e986e9bd
parent 329332 103f2dcf2778d0c22db916e96a962bc0d4eb7ac0
child 329739 4480a9c3e27f1973b2f8ae8fbc2edff642563c3d
push id10587
push usermichael.l.comella@gmail.com
push dateTue, 09 Feb 2016 01:21:17 +0000
reviewersmfinkle
bugs1246209
milestone47.0a1
Bug 1246209 - Extract readJSONObjectFromFile out of getClientId. r=mfinkle
mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
--- 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();
         }