Commit 9063d39b authored by Nicolas Ouellet-Payeur's avatar Nicolas Ouellet-Payeur Committed by Commit Bot

[BrowserSwitcher] Update cache.dat after loading rules from prefs

Previously, cache.dat was only updated once on startup (before loading
XML sitelists from prefs), and then after all the XML sitelists were
done downloading & parsing.

This left a 1-minute period of time where cache.dat and Chrome's state
didn't match. This would cause some back-and-forth switching between
the two.

Bug: 993054
Change-Id: Idb4da1c102a86392b88d7060664b16e1680c2deb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1798526
Commit-Queue: Julian Pastarmov <pastarmovj@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Auto-Submit: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695964}
parent eef4ee07
...@@ -790,6 +790,77 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) { ...@@ -790,6 +790,77 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) {
run_loop.Run(); run_loop.Run();
} }
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
PRE_CacheFileCorrectOnStartup) {
SetUseIeSitelist(true);
BrowserSwitcherServiceWin::SetIeemSitelistUrlForTesting(kAValidUrl);
content::URLLoaderInterceptor interceptor(
base::BindRepeating(&ReturnValidXml));
// Execute everything and check "cache.dat" file contents.
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::PathExists(base::FilePath(cache_file_path)));
std::move(quit).Run();
},
cache_file_path(), run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, CacheFileCorrectOnStartup) {
SetUseIeSitelist(true);
// Never refresh the sitelist. We want to check the state of cache.dat after
// startup, not after the sitelist is downloaded.
BrowserSwitcherServiceWin::SetFetchDelayForTesting(
base::TimeDelta::FromHours(24));
BrowserSwitcherServiceWin::SetIeemSitelistUrlForTesting(kAValidUrl);
content::URLLoaderInterceptor interceptor(
base::BindRepeating(&ReturnValidXml));
// Execute everything and check "cache.dat" file contents.
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::File file(cache_file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
ASSERT_TRUE(file.IsValid());
const char expected_output[] =
"1\n"
"\n"
"\n"
"\n"
"\n"
"1\n"
"docs.google.com\n"
"0\n";
std::unique_ptr<char[]> buffer(new char[file.GetLength() + 1]);
buffer.get()[file.GetLength()] = '\0';
file.Read(0, buffer.get(), file.GetLength());
EXPECT_EQ(std::string(expected_output), std::string(buffer.get()));
std::move(quit).Run();
},
cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
DeletesSitelistCacheOnStartup) { DeletesSitelistCacheOnStartup) {
base::ScopedAllowBlockingForTesting allow_blocking; base::ScopedAllowBlockingForTesting allow_blocking;
......
...@@ -192,6 +192,9 @@ void BrowserSwitcherServiceWin::LoadRulesFromPrefs() { ...@@ -192,6 +192,9 @@ void BrowserSwitcherServiceWin::LoadRulesFromPrefs() {
if (prefs().UseIeSitelist()) if (prefs().UseIeSitelist())
sitelist()->SetIeemSitelist( sitelist()->SetIeemSitelist(
ParsedXml(prefs().GetCachedIeemSitelist(), base::nullopt)); ParsedXml(prefs().GetCachedIeemSitelist(), base::nullopt));
if (!prefs().IsEnabled())
return;
SavePrefsToFile();
} }
void BrowserSwitcherServiceWin::OnAllRulesetsParsed() { void BrowserSwitcherServiceWin::OnAllRulesetsParsed() {
......
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