Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 10 import android.os.Handler; | 10 import android.os.Handler; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 public UrlManager() { | 79 public UrlManager() { |
| 80 Context context = ContextUtils.getApplicationContext(); | 80 Context context = ContextUtils.getApplicationContext(); |
| 81 mPwsClient = new PwsClientImpl(context); | 81 mPwsClient = new PwsClientImpl(context); |
| 82 mObservers = new ObserverList<Listener>(); | 82 mObservers = new ObserverList<Listener>(); |
| 83 mNearbyUrls = new HashSet<>(); | 83 mNearbyUrls = new HashSet<>(); |
| 84 mUrlInfoMap = new HashMap<>(); | 84 mUrlInfoMap = new HashMap<>(); |
| 85 mPwsResultMap = new HashMap<>(); | 85 mPwsResultMap = new HashMap<>(); |
| 86 mUrlsSortedByTimestamp = new PriorityQueue<String>(1, new Comparator<Str ing>() { | 86 mUrlsSortedByTimestamp = new PriorityQueue<String>(1, new Comparator<Str ing>() { |
| 87 @Override | 87 @Override |
| 88 public int compare(String url1, String url2) { | 88 public int compare(String url1, String url2) { |
| 89 Long scanTimestamp1 = Long.valueOf(mUrlInfoMap.get(url1).getScan Timestamp()); | 89 Long timestamp1 = Long.valueOf(mUrlInfoMap.get(url1).getLastSeen Timestamp()); |
| 90 Long scanTimestamp2 = Long.valueOf(mUrlInfoMap.get(url2).getScan Timestamp()); | 90 Long timestamp2 = Long.valueOf(mUrlInfoMap.get(url2).getLastSeen Timestamp()); |
| 91 return scanTimestamp1.compareTo(scanTimestamp2); | 91 return timestamp1.compareTo(timestamp2); |
| 92 } | 92 } |
| 93 }); | 93 }); |
| 94 initSharedPreferences(); | 94 initSharedPreferences(); |
| 95 registerNativeInitStartupCallback(); | 95 registerNativeInitStartupCallback(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // "Initialization on demand holder idiom" | 98 // "Initialization on demand holder idiom" |
| 99 private static class LazyHolder { | 99 private static class LazyHolder { |
| 100 private static final UrlManager INSTANCE = new UrlManager(); | 100 private static final UrlManager INSTANCE = new UrlManager(); |
| 101 } | 101 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 * which will receive the list of nearby URL metadata. | 244 * which will receive the list of nearby URL metadata. |
| 245 */ | 245 */ |
| 246 @CalledByNative | 246 @CalledByNative |
| 247 public void getPwCollection(long nativePhysicalWebCollection) { | 247 public void getPwCollection(long nativePhysicalWebCollection) { |
| 248 List<UrlInfo> nearbyUrlInfos = getUrlInfoList(mNearbyUrls); | 248 List<UrlInfo> nearbyUrlInfos = getUrlInfoList(mNearbyUrls); |
| 249 for (UrlInfo urlInfo : nearbyUrlInfos) { | 249 for (UrlInfo urlInfo : nearbyUrlInfos) { |
| 250 String requestUrl = urlInfo.getUrl(); | 250 String requestUrl = urlInfo.getUrl(); |
| 251 PwsResult pwsResult = mPwsResultMap.get(requestUrl); | 251 PwsResult pwsResult = mPwsResultMap.get(requestUrl); |
| 252 if (pwsResult != null) { | 252 if (pwsResult != null) { |
| 253 nativeAppendMetadataItem(nativePhysicalWebCollection, requestUrl , | 253 nativeAppendMetadataItem(nativePhysicalWebCollection, requestUrl , |
| 254 urlInfo.getDistance(), urlInfo.getScanTimestamp(), pwsRe sult.siteUrl, | 254 urlInfo.getDistance(), urlInfo.getLastSeenTimestamp(), p wsResult.siteUrl, |
| 255 pwsResult.iconUrl, pwsResult.title, pwsResult.descriptio n, | 255 pwsResult.iconUrl, pwsResult.title, pwsResult.descriptio n, |
| 256 pwsResult.groupId); | 256 pwsResult.groupId); |
| 257 } | 257 } |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 /** | 261 /** |
| 262 * Forget all stored URLs. | 262 * Forget all stored URLs. |
| 263 */ | 263 */ |
| 264 public void clearAllUrls() { | 264 public void clearAllUrls() { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 416 } catch (JSONException e) { | 416 } catch (JSONException e) { |
| 417 Log.e(TAG, "Could not serialize PwsResult", e); | 417 Log.e(TAG, "Could not serialize PwsResult", e); |
| 418 } | 418 } |
| 419 } | 419 } |
| 420 | 420 |
| 421 setStringSetInSharedPreferences(PREFS_PWS_RESULTS_KEY, serializedPwsResu lts); | 421 setStringSetInSharedPreferences(PREFS_PWS_RESULTS_KEY, serializedPwsResu lts); |
| 422 } | 422 } |
| 423 | 423 |
| 424 /** | 424 /** |
| 425 * Updates a cache entry with new information. | 425 * Updates a cache entry with new information. |
| 426 * When we reencounter a URL, a subset of its metadata should update. Only distance and | 426 * When we reencounter a URL, a subset of its metadata should update. Only distance fall |
|
mattreynolds
2017/02/22 23:33:42
How about: "When we reencounter a URL, only its di
Ran
2017/02/23 19:44:14
Done.
| |
| 427 * scanTimestamp fall into this category. | 427 * into this category. |
| 428 * @param urlInfo This should be a freshly discovered UrlInfo, though it doe s not have to be | 428 * @param urlInfo This should be a freshly discovered UrlInfo, though it doe s not have to be |
| 429 * previously undiscovered. | 429 * previously undiscovered. |
| 430 * @return The updated cache entry | 430 * @return The updated cache entry |
| 431 */ | 431 */ |
| 432 private UrlInfo updateCacheEntry(UrlInfo urlInfo) { | 432 private UrlInfo updateCacheEntry(UrlInfo urlInfo) { |
| 433 UrlInfo currentUrlInfo = mUrlInfoMap.get(urlInfo.getUrl()); | 433 UrlInfo currentUrlInfo = mUrlInfoMap.get(urlInfo.getUrl()); |
| 434 if (currentUrlInfo == null) { | 434 if (currentUrlInfo == null) { |
| 435 mUrlInfoMap.put(urlInfo.getUrl(), urlInfo); | 435 mUrlInfoMap.put(urlInfo.getUrl(), urlInfo); |
| 436 currentUrlInfo = urlInfo; | 436 currentUrlInfo = urlInfo; |
| 437 } else { | 437 } else { |
| 438 mUrlsSortedByTimestamp.remove(urlInfo.getUrl()); | 438 mUrlsSortedByTimestamp.remove(urlInfo.getUrl()); |
| 439 currentUrlInfo.setScanTimestamp(urlInfo.getScanTimestamp()); | |
| 440 if (urlInfo.getDistance() > 0.0) { | 439 if (urlInfo.getDistance() > 0.0) { |
| 441 currentUrlInfo.setDistance(urlInfo.getDistance()); | 440 currentUrlInfo.setDistance(urlInfo.getDistance()); |
| 442 } | 441 } |
| 443 if (urlInfo.getDeviceAddress() != null) { | 442 if (urlInfo.getDeviceAddress() != null) { |
| 444 currentUrlInfo.setDeviceAddress(urlInfo.getDeviceAddress()); | 443 currentUrlInfo.setDeviceAddress(urlInfo.getDeviceAddress()); |
| 445 } | 444 } |
| 446 } | 445 } |
| 447 mUrlsSortedByTimestamp.add(urlInfo.getUrl()); | 446 mUrlsSortedByTimestamp.add(urlInfo.getUrl()); |
| 448 return currentUrlInfo; | 447 return currentUrlInfo; |
| 449 } | 448 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 safeNotifyNativeListenersOnFound(urlInfo.getUrl()); | 484 safeNotifyNativeListenersOnFound(urlInfo.getUrl()); |
| 486 if (urlInfo.getDistance() >= 0.0) { | 485 if (urlInfo.getDistance() >= 0.0) { |
| 487 safeNotifyNativeListenersOnDistanceChanged(urlInfo.getUrl(), urlInfo .getDistance()); | 486 safeNotifyNativeListenersOnDistanceChanged(urlInfo.getUrl(), urlInfo .getDistance()); |
| 488 } | 487 } |
| 489 } | 488 } |
| 490 | 489 |
| 491 private void garbageCollect() { | 490 private void garbageCollect() { |
| 492 for (String url = mUrlsSortedByTimestamp.peek(); url != null; | 491 for (String url = mUrlsSortedByTimestamp.peek(); url != null; |
| 493 url = mUrlsSortedByTimestamp.peek()) { | 492 url = mUrlsSortedByTimestamp.peek()) { |
| 494 UrlInfo urlInfo = mUrlInfoMap.get(url); | 493 UrlInfo urlInfo = mUrlInfoMap.get(url); |
| 495 if ((System.currentTimeMillis() - urlInfo.getScanTimestamp() <= MAX_ CACHE_TIME | 494 if ((System.currentTimeMillis() - urlInfo.getLastSeenTimestamp() <= MAX_CACHE_TIME |
| 496 && mUrlsSortedByTimestamp.size() <= MAX_CACHE_SIZE) | 495 && mUrlsSortedByTimestamp.size() <= MAX_CACHE_SIZE) |
| 497 || mNearbyUrls.contains(url)) { | 496 || mNearbyUrls.contains(url)) { |
| 498 Log.d(TAG, "Not garbage collecting: ", urlInfo); | 497 Log.d(TAG, "Not garbage collecting: ", urlInfo); |
| 499 break; | 498 break; |
| 500 } | 499 } |
| 501 Log.d(TAG, "Garbage collecting: ", urlInfo); | 500 Log.d(TAG, "Garbage collecting: ", urlInfo); |
| 502 // The min value cannot have changed at this point, so it's OK to ju st remove via | 501 // The min value cannot have changed at this point, so it's OK to ju st remove via |
| 503 // poll(). | 502 // poll(). |
| 504 mUrlsSortedByTimestamp.poll(); | 503 mUrlsSortedByTimestamp.poll(); |
| 505 mUrlInfoMap.remove(url); | 504 mUrlInfoMap.remove(url); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 || mUrlsSortedByTimestamp.contains(url); | 635 || mUrlsSortedByTimestamp.contains(url); |
| 637 } | 636 } |
| 638 | 637 |
| 639 @VisibleForTesting | 638 @VisibleForTesting |
| 640 int getMaxCacheSize() { | 639 int getMaxCacheSize() { |
| 641 return MAX_CACHE_SIZE; | 640 return MAX_CACHE_SIZE; |
| 642 } | 641 } |
| 643 | 642 |
| 644 private native long nativeInit(); | 643 private native long nativeInit(); |
| 645 private native void nativeAppendMetadataItem(long nativePhysicalWebCollectio n, | 644 private native void nativeAppendMetadataItem(long nativePhysicalWebCollectio n, |
| 646 String requestUrl, double distanceEstimate, long scanTimestamp, Stri ng siteUrl, | 645 String requestUrl, double distanceEstimate, long lastSeenTimestamp, String siteUrl, |
| 647 String iconUrl, String title, String description, String groupId); | 646 String iconUrl, String title, String description, String groupId); |
| 648 private native void nativeOnFound(long nativePhysicalWebDataSourceAndroid, S tring url); | 647 private native void nativeOnFound(long nativePhysicalWebDataSourceAndroid, S tring url); |
| 649 private native void nativeOnLost(long nativePhysicalWebDataSourceAndroid, St ring url); | 648 private native void nativeOnLost(long nativePhysicalWebDataSourceAndroid, St ring url); |
| 650 private native void nativeOnDistanceChanged( | 649 private native void nativeOnDistanceChanged( |
| 651 long nativePhysicalWebDataSourceAndroid, String url, double distance Changed); | 650 long nativePhysicalWebDataSourceAndroid, String url, double distance Changed); |
| 652 } | 651 } |
| OLD | NEW |