Index: chrome/android/java/src/org/chromium/chrome/browser/omnibox/geo/VisibleNetworksTracker.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/omnibox/geo/VisibleNetworksTracker.java b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/geo/VisibleNetworksTracker.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8d947d367b25c9053f07983e5469ffb2059c018c |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/omnibox/geo/VisibleNetworksTracker.java |
@@ -0,0 +1,133 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.omnibox.geo; |
+ |
+import android.content.Context; |
+import android.os.AsyncTask; |
+ |
+import org.chromium.base.Log; |
+import org.chromium.base.VisibleForTesting; |
+ |
+import javax.annotation.Nullable; |
+import javax.annotation.concurrent.GuardedBy; |
+ |
+/** |
+ * VisibleNetworksTracker keeps track of the visible networks. |
+ */ |
+public class VisibleNetworksTracker { |
+ private static final String TAG = "VNTracker"; |
+ |
+ static TimeProvider sTimeProvider = new TimeProvider(); |
+ private static final int AGE_THRESHOLD = 5 * 60 * 1000; // 5 min |
+ |
+ @GuardedBy("this") |
Ted C
2017/05/18 02:13:33
As these are statics, it isn't guarded by this bec
lbargu
2017/05/18 14:15:29
Sorry, fixed throughout.
|
+ @Nullable |
+ private static VisibleNetworks sVisibleNetworks; |
dougt
2017/05/18 02:59:03
Sorry, I am not sure what synchronized on |this| m
lbargu
2017/05/18 14:15:29
Removed locking. Using UI thread.
|
+ @GuardedBy("this") |
+ private static long sVisibleNetworksTime = Long.MAX_VALUE; |
+ |
+ private static VisibleNetworks sVisibleNetworksForTesting; |
+ private static boolean sUseVisibleNetworksForTesting; |
+ |
+ /** |
+ * Returns last known visible networks. It returns the cached value if the cache is valid or it |
+ * computes the visibleNetworks, caches the value and then returns it. |
+ */ |
+ public static synchronized VisibleNetworks getLastKnownVisibleNetworks(Context context) { |
Ted C
2017/05/18 02:13:33
What threads do we expect this to run on?
We rare
dougt
2017/05/18 02:59:03
This method is pretty odd. It sometimes returns a
lbargu
2017/05/18 14:15:29
Decided to keep the logic here but simpler.
Redoin
lbargu
2017/05/18 14:15:29
Done.
|
+ if (sUseVisibleNetworksForTesting) return sVisibleNetworksForTesting; |
+ |
+ if (isValidCachedVisibleNetworks()) return getCachedVisibleNetworks(); |
+ |
+ VisibleNetworks visibleNetworks = null; |
+ try { |
+ // Include only the connected cell/wifi to minimize latency and compute the simplest |
+ // visible networks possible. |
+ visibleNetworks = PlatformNetworksManager.computeVisibleNetworks( |
+ context, false /* includeAllVisibleNotConnectedNetworks */); |
+ } catch (Exception e) { |
+ Log.e(TAG, "Failed to get the visible networks. Error: ", e.toString()); |
+ } |
+ setCachedVisibleNetworks(visibleNetworks); |
+ return sVisibleNetworks; |
+ } |
+ |
+ /** |
+ * Refreshes the visible networks cache if the cache is not valid by starting a background task |
Ted C
2017/05/18 02:13:33
this describes a bit too much about the implementa
lbargu
2017/05/18 14:15:29
Done.
|
+ * to compute the visible networks including all visible wifis/cells. Caches the returned value |
+ * when the background task completes. |
+ */ |
+ public static void refreshVisibleNetworks(final Context context) { |
+ if (isValidCachedVisibleNetworks()) { |
+ return; |
+ } |
+ new AsyncTask<Void, Void, VisibleNetworks>() { |
Ted C
2017/05/18 02:13:33
How expensive is this calculation? Looks like it
lbargu
2017/05/18 14:15:29
Done. Good point, it might be not that cheap on so
|
+ @Override |
+ protected VisibleNetworks doInBackground(Void... params) { |
+ VisibleNetworks visibleNetworks = null; |
+ try { |
+ // Include all visible wifis and cells. |
+ visibleNetworks = PlatformNetworksManager.computeVisibleNetworks( |
+ context, true /* includeAllVisibleNotConnectedNetworks */); |
+ } catch (Exception e) { |
+ Log.e(TAG, "Failed to get the visible networks. Error: ", e.toString()); |
+ } |
+ return visibleNetworks; |
+ } |
+ |
+ @Override |
+ protected void onPostExecute(VisibleNetworks visibleNetworks) { |
+ setCachedVisibleNetworks(visibleNetworks); |
+ } |
+ } |
+ .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
+ } |
+ |
+ @Nullable |
+ @VisibleForTesting |
+ static synchronized VisibleNetworks getCachedVisibleNetworks() { |
+ return sVisibleNetworks; |
+ } |
+ |
+ @VisibleForTesting |
+ static synchronized long getCachedVisibleNetworksTime() { |
+ return sVisibleNetworksTime; |
+ } |
+ |
+ @VisibleForTesting |
+ static synchronized void clearCache() { |
+ setCachedVisibleNetworks(null); |
+ sVisibleNetworksTime = Long.MAX_VALUE; |
+ } |
+ |
+ @VisibleForTesting |
+ static void setVisibleNetworksForTesting(VisibleNetworks visibleNetworksForTesting) { |
+ sVisibleNetworksForTesting = visibleNetworksForTesting; |
+ sUseVisibleNetworksForTesting = true; |
+ } |
+ |
+ private static synchronized void setCachedVisibleNetworks(VisibleNetworks visibleNetworks) { |
+ sVisibleNetworks = visibleNetworks; |
+ sVisibleNetworksTime = sTimeProvider.getCurrentTime(); |
+ } |
+ |
+ private static synchronized boolean isValidCachedVisibleNetworks() { |
+ return sVisibleNetworks != null && sVisibleNetworksTime != Long.MAX_VALUE |
+ && !sVisibleNetworks.isEmpty() |
+ && sTimeProvider.getCurrentTime() - sVisibleNetworksTime < AGE_THRESHOLD; |
+ } |
+ |
+ /** |
+ * Wrapper around static time providers that allows us to mock the implementation in |
+ * tests. |
+ */ |
+ public static class TimeProvider { |
+ /** |
+ * Get current time in milliseconds. |
+ */ |
+ public long getCurrentTime() { |
+ return System.currentTimeMillis(); |
+ } |
+ } |
+} |