Bug 1370111 - Post: Use a helper function for grabbing monotonic time r=nalexander draft
authorGrigory Kruglov <gkruglov@mozilla.com>
Mon, 05 Jun 2017 19:52:58 -0400
changeset 589264 126e36be93363c46d009ac64e6060c1505925211
parent 589263 f014bd3b40b239649ad68c218f091f3763c61062
child 631818 38fbb0ae5093dd5a97cb8348af9b5e15d5364be1
push id62306
push userbmo:gkruglov@mozilla.com
push dateMon, 05 Jun 2017 23:54:50 +0000
reviewersnalexander
bugs1370111
milestone55.0a1
Bug 1370111 - Post: Use a helper function for grabbing monotonic time r=nalexander MozReview-Commit-ID: H3kqLMMpLjq
mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
@@ -195,17 +195,17 @@ public class FxAccountSyncAdapter extend
       // Note to future maintainers: while there are reasons, other than 'backoff', this method
       // might be called, in practice that _is_ the only reason it gets called at the moment of
       // writing this. If this changes, please do expand this telemetry handling.
       this.telemetryCollector.setError(TelemetryCollector.KEY_ERROR_INTERNAL, "backoff");
       recordTelemetry();
     }
 
     private void recordTelemetry() {
-      telemetryCollector.setFinished(SystemClock.elapsedRealtime());
+      telemetryCollector.setFinished(now());
       final Intent telemetryIntent = new Intent();
       telemetryIntent.setAction(ACTION_BACKGROUND_TELEMETRY);
       telemetryIntent.putExtra(TelemetryContract.KEY_TYPE, TelemetryContract.KEY_TYPE_SYNC);
       telemetryIntent.putExtra(TelemetryContract.KEY_TELEMETRY, this.telemetryCollector.build());
       localBroadcastManager.sendBroadcast(telemetryIntent);
     }
   }
 
@@ -472,17 +472,17 @@ public class FxAccountSyncAdapter extend
         // value for token server scheduling.
         tokenBackoffHandler.setEarliestNextRequest(delay(backoffSeconds * 1000));
       }
 
       private long delay(long delay) {
         return System.currentTimeMillis() + delay;
       }
     };
-    telemetryCollector.setStarted(SystemClock.elapsedRealtime());
+    telemetryCollector.setStarted(now());
     TokenServerClient tokenServerclient = new TokenServerClient(tokenServerEndpointURI, executor);
     tokenServerclient.getTokenFromBrowserIDAssertion(assertion, true, clientState, delegate);
   }
 
   public void maybeRegisterDevice(Context context, AndroidFxAccount fxAccount) {
     // Register the device if necessary (asynchronous, in another thread).
     // As part of device registration, we obtain a PushSubscription, register our push endpoint
     // with FxA, and update account data with fxaDeviceId, which is part of our synced
@@ -516,35 +516,33 @@ public class FxAccountSyncAdapter extend
   @Override
   public void onPerformSync(final Account account, final Bundle extras, final String authority, ContentProviderClient provider, final SyncResult syncResult) {
     Logger.setThreadLogTag(FxAccountConstants.GLOBAL_LOG_TAG);
     Logger.resetLogging();
 
     final Context context = getContext();
     final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
 
-    // NB: we use elapsedRealtime which is time since boot, to ensure our clock is monotonic and isn't
-    // paused while CPU is in the power-saving mode.
-    final long syncDeadline = SystemClock.elapsedRealtime() + SYNC_DEADLINE_DELTA_MILLIS;
+    final long syncDeadline = now() + SYNC_DEADLINE_DELTA_MILLIS;
 
     Logger.info(LOG_TAG, "Syncing FxAccount" +
         " account named like " + Utils.obfuscateEmail(account.name) +
         " for authority " + authority +
         " with instance " + this + ".");
 
     Logger.info(LOG_TAG, "Account last synced at: " + fxAccount.getLastSyncedTimestamp());
 
     if (FxAccountUtils.LOG_PERSONAL_INFORMATION) {
       fxAccount.dump();
     }
 
     FirefoxAccounts.logSyncOptions(extras);
 
     if (this.lastSyncRealtimeMillis > 0L &&
-        (this.lastSyncRealtimeMillis + MINIMUM_SYNC_DELAY_MILLIS) > SystemClock.elapsedRealtime() &&
+        (this.lastSyncRealtimeMillis + MINIMUM_SYNC_DELAY_MILLIS) > now() &&
             !extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)) {
       Logger.info(LOG_TAG, "Not syncing FxAccount " + Utils.obfuscateEmail(account.name) +
                            ": minimum interval not met.");
       return;
     }
 
     // Pickle in a background thread to avoid strict mode warnings.
     ThreadPool.run(new Runnable() {
@@ -701,17 +699,17 @@ public class FxAccountSyncAdapter extend
       offeredResult = latch.take();
     } catch (Exception e) {
       Logger.error(LOG_TAG, "Got error syncing.", e);
       syncDelegate.handleError(e);
     } finally {
       fxAccount.releaseSharedAccountStateLock();
     }
 
-    lastSyncRealtimeMillis = SystemClock.elapsedRealtime();
+    lastSyncRealtimeMillis = now();
 
     // We got to this point without being offered a result, and so it's unwise to proceed with
     // trying to sync stages again. Nothing else we can do but log an error.
     if (offeredResult == null) {
       Logger.error(LOG_TAG, "Did not receive a sync result from the delegate.");
       return;
     }
 
@@ -741,9 +739,15 @@ public class FxAccountSyncAdapter extend
       Logger.info(LOG_TAG, "Syncing done.");
       return;
     }
 
     // If there are any other stages marked as incomplete, request that they're synced again.
     Logger.info(LOG_TAG, "Syncing done. Requesting an immediate follow-up sync.");
     fxAccount.requestImmediateSync(stagesToSyncAgain, null);
   }
+
+  private static long now() {
+    // NB: we use elapsedRealtime which is time since boot, to ensure our clock is monotonic and isn't
+    // paused while CPU is in the power-saving mode.
+    return SystemClock.elapsedRealtime();
+  }
 }