Commit 2f08b98c authored by Nicolas's avatar Nicolas Committed by Commit Bot

[BrowserSwitcher] Refresh sitelists every 30 minutes

Bug: 934992
Change-Id: I1a16b600e4ccbe5d690d0e8a433da97c427e6351
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1504263
Commit-Queue: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638197}
parent 21095442
...@@ -30,6 +30,9 @@ namespace { ...@@ -30,6 +30,9 @@ namespace {
// the sitelist fetch. // the sitelist fetch.
const base::TimeDelta kFetchSitelistDelay = base::TimeDelta::FromSeconds(60); const base::TimeDelta kFetchSitelistDelay = base::TimeDelta::FromSeconds(60);
// How long to wait after a fetch to re-fetch the sitelist to keep it fresh.
const base::TimeDelta kRefreshSitelistDelay = base::TimeDelta::FromMinutes(30);
// How many times to re-try fetching the XML file for the sitelist. // How many times to re-try fetching the XML file for the sitelist.
const int kFetchNumRetries = 1; const int kFetchNumRetries = 1;
...@@ -80,8 +83,7 @@ RulesetSource::~RulesetSource() = default; ...@@ -80,8 +83,7 @@ RulesetSource::~RulesetSource() = default;
XmlDownloader::XmlDownloader(Profile* profile, XmlDownloader::XmlDownloader(Profile* profile,
std::vector<RulesetSource> sources, std::vector<RulesetSource> sources,
base::OnceCallback<void()> all_done_callback, base::OnceCallback<void()> all_done_callback)
base::TimeDelta delay)
: sources_(std::move(sources)), : sources_(std::move(sources)),
all_done_callback_(std::move(all_done_callback)), all_done_callback_(std::move(all_done_callback)),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
...@@ -90,10 +92,7 @@ XmlDownloader::XmlDownloader(Profile* profile, ...@@ -90,10 +92,7 @@ XmlDownloader::XmlDownloader(Profile* profile,
other_url_factory_ = other_url_factory_ =
content::BrowserContext::GetDefaultStoragePartition(profile) content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess(); ->GetURLLoaderFactoryForBrowserProcess();
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( FetchXml();
FROM_HERE,
base::BindOnce(&XmlDownloader::FetchXml, weak_ptr_factory_.GetWeakPtr()),
delay);
} }
XmlDownloader::~XmlDownloader() = default; XmlDownloader::~XmlDownloader() = default;
...@@ -153,10 +152,11 @@ BrowserSwitcherService::BrowserSwitcherService(Profile* profile) ...@@ -153,10 +152,11 @@ BrowserSwitcherService::BrowserSwitcherService(Profile* profile)
driver_(new AlternativeBrowserDriverImpl(&prefs_)), driver_(new AlternativeBrowserDriverImpl(&prefs_)),
sitelist_(new BrowserSwitcherSitelistImpl(&prefs_)), sitelist_(new BrowserSwitcherSitelistImpl(&prefs_)),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::BindOnce(&BrowserSwitcherService::StartDownload, FROM_HERE,
weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&BrowserSwitcherService::StartDownload,
base::Unretained(profile))); weak_ptr_factory_.GetWeakPtr(), base::Unretained(profile)),
fetch_delay_);
} }
BrowserSwitcherService::~BrowserSwitcherService() = default; BrowserSwitcherService::~BrowserSwitcherService() = default;
...@@ -167,9 +167,14 @@ void BrowserSwitcherService::StartDownload(Profile* profile) { ...@@ -167,9 +167,14 @@ void BrowserSwitcherService::StartDownload(Profile* profile) {
sitelist_downloader_ = std::make_unique<XmlDownloader>( sitelist_downloader_ = std::make_unique<XmlDownloader>(
profile, std::move(sources), profile, std::move(sources),
base::BindOnce(&BrowserSwitcherService::OnAllRulesetsParsed, base::BindOnce(&BrowserSwitcherService::OnAllRulesetsParsed,
base::Unretained(this)), base::Unretained(this)));
fetch_delay_);
} }
// Refresh in 30 minutes, so the sitelists are never too stale.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&BrowserSwitcherService::StartDownload,
weak_ptr_factory_.GetWeakPtr(), base::Unretained(profile)),
refresh_delay_);
} }
void BrowserSwitcherService::Shutdown() { void BrowserSwitcherService::Shutdown() {
...@@ -225,10 +230,15 @@ void BrowserSwitcherService::OnExternalSitelistParsed(ParsedXml xml) { ...@@ -225,10 +230,15 @@ void BrowserSwitcherService::OnExternalSitelistParsed(ParsedXml xml) {
} }
base::TimeDelta BrowserSwitcherService::fetch_delay_ = kFetchSitelistDelay; base::TimeDelta BrowserSwitcherService::fetch_delay_ = kFetchSitelistDelay;
base::TimeDelta BrowserSwitcherService::refresh_delay_ = kRefreshSitelistDelay;
// static // static
void BrowserSwitcherService::SetFetchDelayForTesting(base::TimeDelta delay) { void BrowserSwitcherService::SetFetchDelayForTesting(base::TimeDelta delay) {
fetch_delay_ = delay; fetch_delay_ = delay;
} }
void BrowserSwitcherService::SetRefreshDelayForTesting(base::TimeDelta delay) {
refresh_delay_ = delay;
}
} // namespace browser_switcher } // namespace browser_switcher
...@@ -51,8 +51,7 @@ class XmlDownloader { ...@@ -51,8 +51,7 @@ class XmlDownloader {
// |all_done_callback| once all the rulesets have been processed. // |all_done_callback| once all the rulesets have been processed.
XmlDownloader(Profile* profile, XmlDownloader(Profile* profile,
std::vector<RulesetSource> sources, std::vector<RulesetSource> sources,
base::OnceCallback<void()> all_done_callback, base::OnceCallback<void()> all_done_callback);
base::TimeDelta delay);
virtual ~XmlDownloader(); virtual ~XmlDownloader();
private: private:
...@@ -93,6 +92,7 @@ class BrowserSwitcherService : public KeyedService { ...@@ -93,6 +92,7 @@ class BrowserSwitcherService : public KeyedService {
void SetSitelistForTesting(std::unique_ptr<BrowserSwitcherSitelist> sitelist); void SetSitelistForTesting(std::unique_ptr<BrowserSwitcherSitelist> sitelist);
static void SetFetchDelayForTesting(base::TimeDelta delay); static void SetFetchDelayForTesting(base::TimeDelta delay);
static void SetRefreshDelayForTesting(base::TimeDelta delay);
protected: protected:
// Return a platform-specific list of URLs to download 1 minute after startup, // Return a platform-specific list of URLs to download 1 minute after startup,
...@@ -103,6 +103,7 @@ class BrowserSwitcherService : public KeyedService { ...@@ -103,6 +103,7 @@ class BrowserSwitcherService : public KeyedService {
// Delay for the IEEM/external XML fetch tasks, launched from the constructor. // Delay for the IEEM/external XML fetch tasks, launched from the constructor.
static base::TimeDelta fetch_delay_; static base::TimeDelta fetch_delay_;
static base::TimeDelta refresh_delay_;
private: private:
void StartDownload(Profile* profile); void StartDownload(Profile* profile);
......
...@@ -46,11 +46,9 @@ const char kSitelistXml[] = ...@@ -46,11 +46,9 @@ const char kSitelistXml[] =
"<rules version=\"1\"><docMode><domain docMode=\"9\">" "<rules version=\"1\"><docMode><domain docMode=\"9\">"
"docs.google.com</domain></docMode></rules>"; "docs.google.com</domain></docMode></rules>";
#if defined(OS_WIN) const char kOtherSitelistXml[] =
const char kOtherSItelistXml[] =
"<rules version=\"1\"><docMode><domain docMode=\"9\">" "<rules version=\"1\"><docMode><domain docMode=\"9\">"
"yahoo.com</domain></docMode></rules>"; "yahoo.com</domain></docMode></rules>";
#endif
bool ReturnValidXml(content::URLLoaderInterceptor::RequestParams* params) { bool ReturnValidXml(content::URLLoaderInterceptor::RequestParams* params) {
std::string headers = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n"; std::string headers = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
...@@ -94,6 +92,8 @@ class BrowserSwitcherServiceTest : public InProcessBrowserTest { ...@@ -94,6 +92,8 @@ class BrowserSwitcherServiceTest : public InProcessBrowserTest {
.WillRepeatedly(testing::Return(true)); .WillRepeatedly(testing::Return(true));
policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
BrowserSwitcherService::SetFetchDelayForTesting(base::TimeDelta()); BrowserSwitcherService::SetFetchDelayForTesting(base::TimeDelta());
BrowserSwitcherService::SetRefreshDelayForTesting(
TestTimeouts::action_timeout() * 3 / 2);
#if defined(OS_WIN) #if defined(OS_WIN)
ASSERT_TRUE(fake_appdata_dir_.CreateUniqueTempDir()); ASSERT_TRUE(fake_appdata_dir_.CreateUniqueTempDir());
base::PathService::Override(base::DIR_LOCAL_APP_DATA, base::PathService::Override(base::DIR_LOCAL_APP_DATA,
...@@ -170,8 +170,21 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, ...@@ -170,8 +170,21 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
ExternalFetchAndParseAfterStartup) { ExternalFetchAndParseAfterStartup) {
SetExternalUrl(kAValidUrl); SetExternalUrl(kAValidUrl);
content::URLLoaderInterceptor interceptor( int counter = 0;
base::BindRepeating(ReturnValidXml)); content::URLLoaderInterceptor interceptor(base::BindRepeating(
[](int* counter, content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.spec() != kAValidUrl)
return false;
// Return a different sitelist on refresh.
const char* sitelist_xml =
(*counter == 0) ? kSitelistXml : kOtherSitelistXml;
std::string headers = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n";
content::URLLoaderInterceptor::WriteResponse(
headers, std::string(sitelist_xml), params->client.get());
(*counter)++;
return true;
},
&counter));
// Execute everything and make sure the rules are applied correctly. // Execute everything and make sure the rules are applied correctly.
auto* service = auto* service =
...@@ -180,13 +193,72 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, ...@@ -180,13 +193,72 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::BindOnce( base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) { [](BrowserSwitcherService* service) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/"))); EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/"))); EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
},
service),
TestTimeouts::action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://yahoo.com/")));
std::move(quit).Run(); std::move(quit).Run();
}, },
service, run_loop.QuitClosure()), service, run_loop.QuitClosure()),
TestTimeouts::action_timeout() * 2);
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
ExternalFirstFetchFailsButSecondWorks) {
SetExternalUrl(kAValidUrl);
int counter = 0;
content::URLLoaderInterceptor interceptor(base::BindRepeating(
[](int* counter, content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.spec() != kAValidUrl)
return false;
// First request fails, but second succeeds.
if (*counter == 0)
FailToDownload(params);
else
ReturnValidXml(params);
(*counter)++;
return true;
},
&counter));
// Execute everything and make sure the rules are applied correctly.
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
},
service),
TestTimeouts::action_timeout()); TestTimeouts::action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
TestTimeouts::action_timeout() * 2);
run_loop.Run(); run_loop.Run();
} }
...@@ -458,8 +530,8 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) { ...@@ -458,8 +530,8 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) {
base::FilePath external_sitelist_path = base::FilePath external_sitelist_path =
dir.GetPath().AppendASCII("external_sitelist.xml"); dir.GetPath().AppendASCII("external_sitelist.xml");
base::WriteFile(external_sitelist_path, kOtherSItelistXml, base::WriteFile(external_sitelist_path, kOtherSitelistXml,
strlen(kOtherSItelistXml)); strlen(kOtherSitelistXml));
policy::PolicyMap policies; policy::PolicyMap policies;
EnableBrowserSwitcher(&policies); EnableBrowserSwitcher(&policies);
......
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