Commit f116d30a authored by spdonghao's avatar spdonghao Committed by Commit Bot

Add faviconId in SiteSuggestion.

Bug: 1061906
Change-Id: I798d592945cadcb5b375ee508369738224d78caa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2132330Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarCathy Li <chili@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarXi Han <hanxi@chromium.org>
Commit-Queue: Hao Dong <spdonghao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756384}
parent 8c308c1f
...@@ -1540,7 +1540,7 @@ chrome_java_sources = [ ...@@ -1540,7 +1540,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/suggestions/ThumbnailGradient.java", "java/src/org/chromium/chrome/browser/suggestions/ThumbnailGradient.java",
"java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSites.java", "java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSites.java",
"java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesBridge.java", "java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesBridge.java",
"java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesUtils.java", "java/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesMetadataUtils.java",
"java/src/org/chromium/chrome/browser/suggestions/tile/SiteSection.java", "java/src/org/chromium/chrome/browser/suggestions/tile/SiteSection.java",
"java/src/org/chromium/chrome/browser/suggestions/tile/SiteSectionViewHolder.java", "java/src/org/chromium/chrome/browser/suggestions/tile/SiteSectionViewHolder.java",
"java/src/org/chromium/chrome/browser/suggestions/tile/SuggestionsTileView.java", "java/src/org/chromium/chrome/browser/suggestions/tile/SuggestionsTileView.java",
......
...@@ -464,7 +464,7 @@ chrome_test_java_sources = [ ...@@ -464,7 +464,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/status_indicator/StatusIndicatorViewBinderTest.java", "javatests/src/org/chromium/chrome/browser/status_indicator/StatusIndicatorViewBinderTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/ContentSuggestionsTest.java", "javatests/src/org/chromium/chrome/browser/suggestions/ContentSuggestionsTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/NavigationRecorderTest.java", "javatests/src/org/chromium/chrome/browser/suggestions/NavigationRecorderTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesUtilsTest.java", "javatests/src/org/chromium/chrome/browser/suggestions/mostvisited/MostVisitedSitesMetadataUtilsTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGridLayoutTest.java", "javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGridLayoutTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGroupTest.java", "javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGroupTest.java",
"javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java", "javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java",
......
...@@ -14,12 +14,17 @@ import java.util.Date; ...@@ -14,12 +14,17 @@ import java.util.Date;
* Data class that holds the site suggestion data provided by the tiles component. * Data class that holds the site suggestion data provided by the tiles component.
*/ */
public class SiteSuggestion { public class SiteSuggestion {
public static final int INVALID_FAVICON_ID = -1;
/** Title of the suggested site. */ /** Title of the suggested site. */
public final String title; public final String title;
/** URL of the suggested site. */ /** URL of the suggested site. */
public final String url; public final String url;
/** Id of the favicon. It is optional and used when caching the favion on device. */
public int faviconId;
/** The path to the icon image file for whitelisted tile, empty string otherwise. */ /** The path to the icon image file for whitelisted tile, empty string otherwise. */
public final String whitelistIconPath; public final String whitelistIconPath;
...@@ -46,6 +51,7 @@ public class SiteSuggestion { ...@@ -46,6 +51,7 @@ public class SiteSuggestion {
int source, int sectionType, Date dataGenerationTime) { int source, int sectionType, Date dataGenerationTime) {
this.title = title; this.title = title;
this.url = url; this.url = url;
this.faviconId = INVALID_FAVICON_ID;
this.whitelistIconPath = whitelistIconPath; this.whitelistIconPath = whitelistIconPath;
this.source = source; this.source = source;
this.titleSource = titleSource; this.titleSource = titleSource;
......
...@@ -30,7 +30,7 @@ import java.util.List; ...@@ -30,7 +30,7 @@ import java.util.List;
/** /**
* This class provides methods to write/read most visited sites related info to devices. * This class provides methods to write/read most visited sites related info to devices.
*/ */
public class MostVisitedSitesUtils { public class MostVisitedSitesMetadataUtils {
private static final String TAG = "TopSites"; private static final String TAG = "TopSites";
/** Prevents two state directories from getting created simultaneously. */ /** Prevents two state directories from getting created simultaneously. */
private static final Object DIR_CREATION_LOCK = new Object(); private static final Object DIR_CREATION_LOCK = new Object();
...@@ -55,7 +55,7 @@ public class MostVisitedSitesUtils { ...@@ -55,7 +55,7 @@ public class MostVisitedSitesUtils {
try { try {
byte[] listData = serializeTopSitesData(topSitesInfo); byte[] listData = serializeTopSitesData(topSitesInfo);
saveSuggestionListsToFile( saveSuggestionListsToFile(
getOrCreateTopSitesStateDirectory(), sStateFileName, listData); getOrCreateTopSitesDirectory(), sStateFileName, listData);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Fail to save file."); Log.e(TAG, "Fail to save file.");
} }
...@@ -80,8 +80,8 @@ public class MostVisitedSitesUtils { ...@@ -80,8 +80,8 @@ public class MostVisitedSitesUtils {
public static List<SiteSuggestion> restoreFileToSuggestionLists() throws IOException { public static List<SiteSuggestion> restoreFileToSuggestionLists() throws IOException {
try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) { try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
List<SiteSuggestion> suggestions; List<SiteSuggestion> suggestions;
byte[] listData = restoreFileToSuggestionLists( byte[] listData =
getOrCreateTopSitesStateDirectory(), sStateFileName); restoreFileToSuggestionLists(getOrCreateTopSitesDirectory(), sStateFileName);
suggestions = deserializeTopSitesData(listData); suggestions = deserializeTopSitesData(listData);
return suggestions; return suggestions;
...@@ -102,6 +102,7 @@ public class MostVisitedSitesUtils { ...@@ -102,6 +102,7 @@ public class MostVisitedSitesUtils {
// Save top sites. // Save top sites.
for (int i = 0; i < topSitesCount; i++) { for (int i = 0; i < topSitesCount; i++) {
stream.writeInt(topSitesInfo.get(i).faviconId);
stream.writeUTF(topSitesInfo.get(i).title); stream.writeUTF(topSitesInfo.get(i).title);
stream.writeUTF(topSitesInfo.get(i).url); stream.writeUTF(topSitesInfo.get(i).url);
stream.writeUTF(topSitesInfo.get(i).whitelistIconPath); stream.writeUTF(topSitesInfo.get(i).whitelistIconPath);
...@@ -133,6 +134,7 @@ public class MostVisitedSitesUtils { ...@@ -133,6 +134,7 @@ public class MostVisitedSitesUtils {
// Restore top sites. // Restore top sites.
List<SiteSuggestion> suggestions = new ArrayList<>(); List<SiteSuggestion> suggestions = new ArrayList<>();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int faviconId = stream.readInt();
String title = stream.readUTF(); String title = stream.readUTF();
String url = stream.readUTF(); String url = stream.readUTF();
String whitelistIconPath = stream.readUTF(); String whitelistIconPath = stream.readUTF();
...@@ -140,8 +142,10 @@ public class MostVisitedSitesUtils { ...@@ -140,8 +142,10 @@ public class MostVisitedSitesUtils {
int source = stream.readInt(); int source = stream.readInt();
int sectionType = stream.readInt(); int sectionType = stream.readInt();
dataGenerationTime = new Date(stream.readLong()); dataGenerationTime = new Date(stream.readLong());
suggestions.add(new SiteSuggestion(title, url, whitelistIconPath, titleSource, source, SiteSuggestion newSite = new SiteSuggestion(title, url, whitelistIconPath, titleSource,
sectionType, dataGenerationTime)); source, sectionType, dataGenerationTime);
newSite.faviconId = faviconId;
suggestions.add(newSite);
} }
Log.d(TAG, "Deserializing top sites lists finished"); Log.d(TAG, "Deserializing top sites lists finished");
return suggestions; return suggestions;
...@@ -194,13 +198,15 @@ public class MostVisitedSitesUtils { ...@@ -194,13 +198,15 @@ public class MostVisitedSitesUtils {
return data; return data;
} }
protected static File getOrCreateTopSitesStateDirectory() { protected static File getOrCreateTopSitesDirectory() {
synchronized (DIR_CREATION_LOCK) { try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
if (sStateDirectory == null) { synchronized (DIR_CREATION_LOCK) {
sStateDirectory = ContextUtils.getApplicationContext().getDir( if (sStateDirectory == null) {
sStateDirName, Context.MODE_PRIVATE); sStateDirectory = ContextUtils.getApplicationContext().getDir(
sStateDirName, Context.MODE_PRIVATE);
}
} }
return sStateDirectory;
} }
return sStateDirectory;
} }
} }
...@@ -30,11 +30,11 @@ import java.util.Date; ...@@ -30,11 +30,11 @@ import java.util.Date;
import java.util.List; import java.util.List;
/** /**
* Instrumentation tests for {@link MostVisitedSitesUtils}. * Instrumentation tests for {@link MostVisitedSitesMetadataUtils}.
*/ */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class MostVisitedSitesUtilsTest { public class MostVisitedSitesMetadataUtilsTest {
@Rule @Rule
public ChromeTabbedActivityTestRule mTestSetupRule = new ChromeTabbedActivityTestRule(); public ChromeTabbedActivityTestRule mTestSetupRule = new ChromeTabbedActivityTestRule();
...@@ -51,16 +51,16 @@ public class MostVisitedSitesUtilsTest { ...@@ -51,16 +51,16 @@ public class MostVisitedSitesUtilsTest {
mExpectedSiteSuggestions = createFakeSiteSuggestions(); mExpectedSiteSuggestions = createFakeSiteSuggestions();
// Get old file and ensure to delete it. // Get old file and ensure to delete it.
File oldFile = MostVisitedSitesUtils.getOrCreateTopSitesStateDirectory(); File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory();
assertTrue(oldFile.delete() && !oldFile.exists()); assertTrue(oldFile.delete() && !oldFile.exists());
// Save suggestion lists to file. // Save suggestion lists to file.
MostVisitedSitesUtils.saveSuggestionListsToFile(mExpectedSiteSuggestions, () -> { MostVisitedSitesMetadataUtils.saveSuggestionListsToFile(mExpectedSiteSuggestions, () -> {
// Restore list from file after saving finished. // Restore list from file after saving finished.
List<SiteSuggestion> sitesAfterRestore = null; List<SiteSuggestion> sitesAfterRestore = null;
try { try {
sitesAfterRestore = MostVisitedSitesUtils.restoreFileToSuggestionLists(); sitesAfterRestore = MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -74,11 +74,11 @@ public class MostVisitedSitesUtilsTest { ...@@ -74,11 +74,11 @@ public class MostVisitedSitesUtilsTest {
@SmallTest @SmallTest
public void testRestoreException() throws IOException { public void testRestoreException() throws IOException {
// Get old file and ensure to delete it. // Get old file and ensure to delete it.
File oldFile = MostVisitedSitesUtils.getOrCreateTopSitesStateDirectory(); File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory();
assertTrue(oldFile.delete() || !oldFile.exists()); assertTrue(oldFile.delete() || !oldFile.exists());
// Call restore function and ensure it throws an IOException. // Call restore function and ensure it throws an IOException.
MostVisitedSitesUtils.restoreFileToSuggestionLists(); MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists();
} }
private static List<SiteSuggestion> createFakeSiteSuggestions() { private static List<SiteSuggestion> createFakeSiteSuggestions() {
...@@ -89,6 +89,7 @@ public class MostVisitedSitesUtilsTest { ...@@ -89,6 +89,7 @@ public class MostVisitedSitesUtilsTest {
siteSuggestions.add(new SiteSuggestion("1 WHITELIST", "https://www.bar.com", "", siteSuggestions.add(new SiteSuggestion("1 WHITELIST", "https://www.bar.com", "",
TileTitleSource.UNKNOWN, TileSource.WHITELIST, TileSectionType.PERSONALIZED, TileTitleSource.UNKNOWN, TileSource.WHITELIST, TileSectionType.PERSONALIZED,
new Date())); new Date()));
siteSuggestions.get(1).faviconId = 1;
return siteSuggestions; return siteSuggestions;
} }
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment