Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.omnibox.geo; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.AsyncTask; | |
| 9 | |
| 10 import org.chromium.base.Log; | |
| 11 import org.chromium.base.VisibleForTesting; | |
| 12 | |
| 13 import javax.annotation.Nullable; | |
| 14 import javax.annotation.concurrent.GuardedBy; | |
| 15 | |
| 16 /** | |
| 17 * VisibleNetworksTracker keeps track of the visible networks. | |
| 18 */ | |
| 19 public class VisibleNetworksTracker { | |
| 20 private static final String TAG = "VNTracker"; | |
| 21 | |
| 22 static TimeProvider sTimeProvider = new TimeProvider(); | |
| 23 private static final int AGE_THRESHOLD = 5 * 60 * 1000; // 5 min | |
| 24 | |
| 25 @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.
| |
| 26 @Nullable | |
| 27 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.
| |
| 28 @GuardedBy("this") | |
| 29 private static long sVisibleNetworksTime = Long.MAX_VALUE; | |
| 30 | |
| 31 private static VisibleNetworks sVisibleNetworksForTesting; | |
| 32 private static boolean sUseVisibleNetworksForTesting; | |
| 33 | |
| 34 /** | |
| 35 * Returns last known visible networks. It returns the cached value if the c ache is valid or it | |
| 36 * computes the visibleNetworks, caches the value and then returns it. | |
| 37 */ | |
| 38 public static synchronized VisibleNetworks getLastKnownVisibleNetworks(Conte xt 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.
| |
| 39 if (sUseVisibleNetworksForTesting) return sVisibleNetworksForTesting; | |
| 40 | |
| 41 if (isValidCachedVisibleNetworks()) return getCachedVisibleNetworks(); | |
| 42 | |
| 43 VisibleNetworks visibleNetworks = null; | |
| 44 try { | |
| 45 // Include only the connected cell/wifi to minimize latency and comp ute the simplest | |
| 46 // visible networks possible. | |
| 47 visibleNetworks = PlatformNetworksManager.computeVisibleNetworks( | |
| 48 context, false /* includeAllVisibleNotConnectedNetworks */); | |
| 49 } catch (Exception e) { | |
| 50 Log.e(TAG, "Failed to get the visible networks. Error: ", e.toString ()); | |
| 51 } | |
| 52 setCachedVisibleNetworks(visibleNetworks); | |
| 53 return sVisibleNetworks; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Refreshes the visible networks cache if the cache is not valid by startin g 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.
| |
| 58 * to compute the visible networks including all visible wifis/cells. Caches the returned value | |
| 59 * when the background task completes. | |
| 60 */ | |
| 61 public static void refreshVisibleNetworks(final Context context) { | |
| 62 if (isValidCachedVisibleNetworks()) { | |
| 63 return; | |
| 64 } | |
| 65 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
| |
| 66 @Override | |
| 67 protected VisibleNetworks doInBackground(Void... params) { | |
| 68 VisibleNetworks visibleNetworks = null; | |
| 69 try { | |
| 70 // Include all visible wifis and cells. | |
| 71 visibleNetworks = PlatformNetworksManager.computeVisibleNetw orks( | |
| 72 context, true /* includeAllVisibleNotConnectedNetwor ks */); | |
| 73 } catch (Exception e) { | |
| 74 Log.e(TAG, "Failed to get the visible networks. Error: ", e. toString()); | |
| 75 } | |
| 76 return visibleNetworks; | |
| 77 } | |
| 78 | |
| 79 @Override | |
| 80 protected void onPostExecute(VisibleNetworks visibleNetworks) { | |
| 81 setCachedVisibleNetworks(visibleNetworks); | |
| 82 } | |
| 83 } | |
| 84 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | |
| 85 } | |
| 86 | |
| 87 @Nullable | |
| 88 @VisibleForTesting | |
| 89 static synchronized VisibleNetworks getCachedVisibleNetworks() { | |
| 90 return sVisibleNetworks; | |
| 91 } | |
| 92 | |
| 93 @VisibleForTesting | |
| 94 static synchronized long getCachedVisibleNetworksTime() { | |
| 95 return sVisibleNetworksTime; | |
| 96 } | |
| 97 | |
| 98 @VisibleForTesting | |
| 99 static synchronized void clearCache() { | |
| 100 setCachedVisibleNetworks(null); | |
| 101 sVisibleNetworksTime = Long.MAX_VALUE; | |
| 102 } | |
| 103 | |
| 104 @VisibleForTesting | |
| 105 static void setVisibleNetworksForTesting(VisibleNetworks visibleNetworksForT esting) { | |
| 106 sVisibleNetworksForTesting = visibleNetworksForTesting; | |
| 107 sUseVisibleNetworksForTesting = true; | |
| 108 } | |
| 109 | |
| 110 private static synchronized void setCachedVisibleNetworks(VisibleNetworks vi sibleNetworks) { | |
| 111 sVisibleNetworks = visibleNetworks; | |
| 112 sVisibleNetworksTime = sTimeProvider.getCurrentTime(); | |
| 113 } | |
| 114 | |
| 115 private static synchronized boolean isValidCachedVisibleNetworks() { | |
| 116 return sVisibleNetworks != null && sVisibleNetworksTime != Long.MAX_VALU E | |
| 117 && !sVisibleNetworks.isEmpty() | |
| 118 && sTimeProvider.getCurrentTime() - sVisibleNetworksTime < AGE_T HRESHOLD; | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * Wrapper around static time providers that allows us to mock the implement ation in | |
| 123 * tests. | |
| 124 */ | |
| 125 public static class TimeProvider { | |
| 126 /** | |
| 127 * Get current time in milliseconds. | |
| 128 */ | |
| 129 public long getCurrentTime() { | |
| 130 return System.currentTimeMillis(); | |
| 131 } | |
| 132 } | |
| 133 } | |
| OLD | NEW |