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 = [
"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/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/SiteSectionViewHolder.java",
"java/src/org/chromium/chrome/browser/suggestions/tile/SuggestionsTileView.java",
......
......@@ -464,7 +464,7 @@ chrome_test_java_sources = [
"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/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/TileGroupTest.java",
"javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java",
......
......@@ -14,12 +14,17 @@ import java.util.Date;
* Data class that holds the site suggestion data provided by the tiles component.
*/
public class SiteSuggestion {
public static final int INVALID_FAVICON_ID = -1;
/** Title of the suggested site. */
public final String title;
/** URL of the suggested site. */
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. */
public final String whitelistIconPath;
......@@ -46,6 +51,7 @@ public class SiteSuggestion {
int source, int sectionType, Date dataGenerationTime) {
this.title = title;
this.url = url;
this.faviconId = INVALID_FAVICON_ID;
this.whitelistIconPath = whitelistIconPath;
this.source = source;
this.titleSource = titleSource;
......
......@@ -30,7 +30,7 @@ import java.util.List;
/**
* 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";
/** Prevents two state directories from getting created simultaneously. */
private static final Object DIR_CREATION_LOCK = new Object();
......@@ -55,7 +55,7 @@ public class MostVisitedSitesUtils {
try {
byte[] listData = serializeTopSitesData(topSitesInfo);
saveSuggestionListsToFile(
getOrCreateTopSitesStateDirectory(), sStateFileName, listData);
getOrCreateTopSitesDirectory(), sStateFileName, listData);
} catch (IOException e) {
Log.e(TAG, "Fail to save file.");
}
......@@ -80,8 +80,8 @@ public class MostVisitedSitesUtils {
public static List<SiteSuggestion> restoreFileToSuggestionLists() throws IOException {
try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
List<SiteSuggestion> suggestions;
byte[] listData = restoreFileToSuggestionLists(
getOrCreateTopSitesStateDirectory(), sStateFileName);
byte[] listData =
restoreFileToSuggestionLists(getOrCreateTopSitesDirectory(), sStateFileName);
suggestions = deserializeTopSitesData(listData);
return suggestions;
......@@ -102,6 +102,7 @@ public class MostVisitedSitesUtils {
// Save top sites.
for (int i = 0; i < topSitesCount; i++) {
stream.writeInt(topSitesInfo.get(i).faviconId);
stream.writeUTF(topSitesInfo.get(i).title);
stream.writeUTF(topSitesInfo.get(i).url);
stream.writeUTF(topSitesInfo.get(i).whitelistIconPath);
......@@ -133,6 +134,7 @@ public class MostVisitedSitesUtils {
// Restore top sites.
List<SiteSuggestion> suggestions = new ArrayList<>();
for (int i = 0; i < count; i++) {
int faviconId = stream.readInt();
String title = stream.readUTF();
String url = stream.readUTF();
String whitelistIconPath = stream.readUTF();
......@@ -140,8 +142,10 @@ public class MostVisitedSitesUtils {
int source = stream.readInt();
int sectionType = stream.readInt();
dataGenerationTime = new Date(stream.readLong());
suggestions.add(new SiteSuggestion(title, url, whitelistIconPath, titleSource, source,
sectionType, dataGenerationTime));
SiteSuggestion newSite = new SiteSuggestion(title, url, whitelistIconPath, titleSource,
source, sectionType, dataGenerationTime);
newSite.faviconId = faviconId;
suggestions.add(newSite);
}
Log.d(TAG, "Deserializing top sites lists finished");
return suggestions;
......@@ -194,13 +198,15 @@ public class MostVisitedSitesUtils {
return data;
}
protected static File getOrCreateTopSitesStateDirectory() {
synchronized (DIR_CREATION_LOCK) {
if (sStateDirectory == null) {
sStateDirectory = ContextUtils.getApplicationContext().getDir(
sStateDirName, Context.MODE_PRIVATE);
protected static File getOrCreateTopSitesDirectory() {
try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
synchronized (DIR_CREATION_LOCK) {
if (sStateDirectory == null) {
sStateDirectory = ContextUtils.getApplicationContext().getDir(
sStateDirName, Context.MODE_PRIVATE);
}
}
return sStateDirectory;
}
return sStateDirectory;
}
}
......@@ -30,11 +30,11 @@ import java.util.Date;
import java.util.List;
/**
* Instrumentation tests for {@link MostVisitedSitesUtils}.
* Instrumentation tests for {@link MostVisitedSitesMetadataUtils}.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class MostVisitedSitesUtilsTest {
public class MostVisitedSitesMetadataUtilsTest {
@Rule
public ChromeTabbedActivityTestRule mTestSetupRule = new ChromeTabbedActivityTestRule();
......@@ -51,16 +51,16 @@ public class MostVisitedSitesUtilsTest {
mExpectedSiteSuggestions = createFakeSiteSuggestions();
// Get old file and ensure to delete it.
File oldFile = MostVisitedSitesUtils.getOrCreateTopSitesStateDirectory();
File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory();
assertTrue(oldFile.delete() && !oldFile.exists());
// Save suggestion lists to file.
MostVisitedSitesUtils.saveSuggestionListsToFile(mExpectedSiteSuggestions, () -> {
MostVisitedSitesMetadataUtils.saveSuggestionListsToFile(mExpectedSiteSuggestions, () -> {
// Restore list from file after saving finished.
List<SiteSuggestion> sitesAfterRestore = null;
try {
sitesAfterRestore = MostVisitedSitesUtils.restoreFileToSuggestionLists();
sitesAfterRestore = MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists();
} catch (IOException e) {
e.printStackTrace();
}
......@@ -74,11 +74,11 @@ public class MostVisitedSitesUtilsTest {
@SmallTest
public void testRestoreException() throws IOException {
// Get old file and ensure to delete it.
File oldFile = MostVisitedSitesUtils.getOrCreateTopSitesStateDirectory();
File oldFile = MostVisitedSitesMetadataUtils.getOrCreateTopSitesDirectory();
assertTrue(oldFile.delete() || !oldFile.exists());
// Call restore function and ensure it throws an IOException.
MostVisitedSitesUtils.restoreFileToSuggestionLists();
MostVisitedSitesMetadataUtils.restoreFileToSuggestionLists();
}
private static List<SiteSuggestion> createFakeSiteSuggestions() {
......@@ -89,6 +89,7 @@ public class MostVisitedSitesUtilsTest {
siteSuggestions.add(new SiteSuggestion("1 WHITELIST", "https://www.bar.com", "",
TileTitleSource.UNKNOWN, TileSource.WHITELIST, TileSectionType.PERSONALIZED,
new Date()));
siteSuggestions.get(1).faviconId = 1;
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