| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.os.SystemClock; | 7 import android.os.SystemClock; |
| 8 import android.util.LruCache; | 8 import android.util.LruCache; |
| 9 import android.webkit.URLUtil; | 9 import android.webkit.URLUtil; |
| 10 | 10 |
| 11 import org.chromium.base.Callback; |
| 11 import org.chromium.base.SysUtils; | 12 import org.chromium.base.SysUtils; |
| 12 import org.chromium.base.VisibleForTesting; | 13 import org.chromium.base.VisibleForTesting; |
| 13 import org.chromium.blink.mojom.document_metadata.CopylessPaste; | 14 import org.chromium.blink.mojom.document_metadata.CopylessPaste; |
| 14 import org.chromium.blink.mojom.document_metadata.WebPage; | 15 import org.chromium.blink.mojom.document_metadata.WebPage; |
| 15 import org.chromium.chrome.browser.historyreport.AppIndexingReporter; | 16 import org.chromium.chrome.browser.historyreport.AppIndexingReporter; |
| 16 import org.chromium.chrome.browser.tab.Tab; | 17 import org.chromium.chrome.browser.tab.Tab; |
| 17 import org.chromium.content_public.browser.RenderFrameHost; | 18 import org.chromium.content_public.browser.RenderFrameHost; |
| 18 import org.chromium.content_public.browser.WebContents; | 19 import org.chromium.content_public.browser.WebContents; |
| 19 import org.chromium.services.service_manager.InterfaceProvider; | 20 import org.chromium.services.service_manager.InterfaceProvider; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * This is the top-level CopylessPaste metadata extraction for AppIndexing. | 23 * This is the top-level CopylessPaste metadata extraction for AppIndexing. |
| 23 */ | 24 */ |
| 24 public class AppIndexingUtil { | 25 public class AppIndexingUtil { |
| 25 private static final int CACHE_SIZE = 100; | 26 private static final int CACHE_SIZE = 100; |
| 26 private static final int CACHE_VISIT_CUTOFF_MS = 60 * 60 * 1000; // 1 hour | 27 private static final int CACHE_VISIT_CUTOFF_MS = 60 * 60 * 1000; // 1 hour |
| 27 // Cache of recently seen urls. If a url is among the CACHE_SIZE most recent
pages visited, and | 28 // Cache of recently seen urls. If a url is among the CACHE_SIZE most recent
pages visited, and |
| 28 // the parse was in the last CACHE_VISIT_CUTOFF_MS milliseconds, then we don
't parse the page, | 29 // the parse was in the last CACHE_VISIT_CUTOFF_MS milliseconds, then we don
't parse the page, |
| 29 // and instead just report the view (not the content) to App Indexing. | 30 // and instead just report the view (not the content) to App Indexing. |
| 30 private LruCache<String, CacheEntry> mPageCache; | 31 private LruCache<String, CacheEntry> mPageCache; |
| 31 | 32 |
| 33 private static Callback<WebPage> sCallbackForTesting; |
| 34 |
| 32 /** | 35 /** |
| 33 * Extracts entities from document metadata and reports it to on-device App
Indexing. | 36 * Extracts entities from document metadata and reports it to on-device App
Indexing. |
| 34 * This call can cache entities from recently parsed webpages, in which case
, only the url and | 37 * This call can cache entities from recently parsed webpages, in which case
, only the url and |
| 35 * title of the page is reported to App Indexing. | 38 * title of the page is reported to App Indexing. |
| 36 */ | 39 */ |
| 37 public void extractCopylessPasteMetadata(final Tab tab) { | 40 public void extractCopylessPasteMetadata(final Tab tab) { |
| 38 final String url = tab.getUrl(); | 41 final String url = tab.getUrl(); |
| 39 boolean isHttpOrHttps = URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url
); | 42 boolean isHttpOrHttps = URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url
); |
| 40 if (!isEnabledForDevice() || tab.isIncognito() || !isHttpOrHttps) { | 43 if (!isEnabledForDevice() || tab.isIncognito() || !isHttpOrHttps) { |
| 41 return; | 44 return; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 55 } else { | 58 } else { |
| 56 // Condition 3 | 59 // Condition 3 |
| 57 CopylessPaste copylessPaste = getCopylessPasteInterface(tab); | 60 CopylessPaste copylessPaste = getCopylessPasteInterface(tab); |
| 58 if (copylessPaste == null) { | 61 if (copylessPaste == null) { |
| 59 return; | 62 return; |
| 60 } | 63 } |
| 61 copylessPaste.getEntities(new CopylessPaste.GetEntitiesResponse() { | 64 copylessPaste.getEntities(new CopylessPaste.GetEntitiesResponse() { |
| 62 @Override | 65 @Override |
| 63 public void call(WebPage webpage) { | 66 public void call(WebPage webpage) { |
| 64 putCacheEntry(url, webpage != null); | 67 putCacheEntry(url, webpage != null); |
| 68 if (sCallbackForTesting != null) { |
| 69 sCallbackForTesting.onResult(webpage); |
| 70 } |
| 65 if (webpage == null) return; | 71 if (webpage == null) return; |
| 66 getAppIndexingReporter().reportWebPage(webpage); | 72 getAppIndexingReporter().reportWebPage(webpage); |
| 67 } | 73 } |
| 68 }); | 74 }); |
| 69 } | 75 } |
| 70 } | 76 } |
| 71 | 77 |
| 78 @VisibleForTesting |
| 79 public static void setCallbackForTesting(Callback<WebPage> callback) { |
| 80 sCallbackForTesting = callback; |
| 81 } |
| 82 |
| 72 private boolean wasPageVisitedRecently(String url) { | 83 private boolean wasPageVisitedRecently(String url) { |
| 73 if (url == null) { | 84 if (url == null) { |
| 74 return false; | 85 return false; |
| 75 } | 86 } |
| 76 CacheEntry entry = getPageCache().get(url); | 87 CacheEntry entry = getPageCache().get(url); |
| 77 if (entry == null || (getElapsedTime() - entry.lastSeenTimeMs > CACHE_VI
SIT_CUTOFF_MS)) { | 88 if (entry == null || (getElapsedTime() - entry.lastSeenTimeMs > CACHE_VI
SIT_CUTOFF_MS)) { |
| 78 return false; | 89 return false; |
| 79 } | 90 } |
| 80 return true; | 91 return true; |
| 81 } | 92 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 mPageCache = new LruCache<String, CacheEntry>(CACHE_SIZE); | 147 mPageCache = new LruCache<String, CacheEntry>(CACHE_SIZE); |
| 137 } | 148 } |
| 138 return mPageCache; | 149 return mPageCache; |
| 139 } | 150 } |
| 140 | 151 |
| 141 private static class CacheEntry { | 152 private static class CacheEntry { |
| 142 public long lastSeenTimeMs; | 153 public long lastSeenTimeMs; |
| 143 public boolean containedEntity; | 154 public boolean containedEntity; |
| 144 } | 155 } |
| 145 } | 156 } |
| OLD | NEW |