Bug 1246209 - Add getProfileCreationDate, implement from filestystem, & add stencil code. r=mfinkle draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 08 Feb 2016 17:10:26 -0800
changeset 329739 4480a9c3e27f1973b2f8ae8fbc2edff642563c3d
parent 329738 629f11457149f2ea21fef6fa9f4c7990e986e9bd
child 329740 3eb383b940891a868b2cfe32837411ab965fcc90
push id10587
push usermichael.l.comella@gmail.com
push dateTue, 09 Feb 2016 01:21:17 +0000
reviewersmfinkle
bugs1246209, 1246816
milestone47.0a1
Bug 1246209 - Add getProfileCreationDate, implement from filestystem, & add stencil code. r=mfinkle Retrieving the profile creation date from the filesystem is not strictly necessary to upload this data and returns -1 until it is implemented. If the decision is r+'d here, it will be implemented in bug 1246816.
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
@@ -46,16 +46,19 @@ import android.util.Log;
 public final class GeckoProfile {
     private static final String LOGTAG = "GeckoProfile";
 
     // The path in the profile to the file containing the client ID.
     private static final String CLIENT_ID_FILE_PATH = "datareporting/state.json";
     // In the client ID file, the attribute title in the JSON object containing the client ID value.
     private static final String CLIENT_ID_JSON_ATTR = "clientID";
 
+    private static final String TIMES_PATH = "times.json";
+    private static final String PROFILE_CREATION_DATE_JSON_ATTR = "created";
+
     // Only tests should need to do this.
     // We can default this to AppConstants.RELEASE_BUILD once we fix Bug 1069687.
     private static volatile boolean sAcceptDirectoryChanges = true;
 
     @RobocopTarget
     public static void enableDirectoryChanges() {
         Log.w(LOGTAG, "Directory changes should only be enabled for tests. And even then it's a bad idea.");
         sAcceptDirectoryChanges = true;
@@ -618,16 +621,50 @@ public final class GeckoProfile {
             return obj.getString(CLIENT_ID_JSON_ATTR);
         } catch (final JSONException e) {
             // Don't log to avoid leaking data in JSONObject.
             throw new IOException("Client ID does not exist in JSONObject");
         }
     }
 
     /**
+     * @return the profile creation date in the format returned by {@link System#currentTimeMillis()} or -1 if the value
+     *         was not found.
+     */
+    @WorkerThread
+    public long getProfileCreationDate() {
+        try {
+            return getProfileCreationDateFromTimesFile();
+        } catch (final IOException e) {
+            return getAndPersistProfileCreationDateFromFilesystem();
+        }
+    }
+
+    @WorkerThread
+    private long getProfileCreationDateFromTimesFile() throws IOException {
+        final JSONObject obj = readJSONObjectFromFile(TIMES_PATH);
+        try {
+            return obj.getLong(PROFILE_CREATION_DATE_JSON_ATTR);
+        } catch (final JSONException e) {
+            // Don't log to avoid leaking data in JSONObject.
+            throw new IOException("Profile creation does not exist in JSONObject");
+        }
+    }
+
+    /**
+     * TODO (bug 1246816): Implement ProfileAge.jsm - getOldestProfileTimestamp. Persist results to times.json.
+     * Update comment in getProfileCreationDate too.
+     * @return -1 until implemented.
+     */
+    @WorkerThread
+    private long getAndPersistProfileCreationDateFromFilesystem() {
+        return -1;
+    }
+
+    /**
      * 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
      * from last time"). Normally, sessionstore.js is moved to sessionstore.bak
      * on a clean quit, but this doesn't happen if Fennec crashed. Thus, this
      * method should be called after a crash so sessionstore.bak correctly
      * holds the previous session.