[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: Julian Pastarmov <pastarmovj@chromium.org>
Auto-Submit: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695964}
diff --git a/chrome/browser/browser_switcher/browser_switcher_service_browsertest.cc b/chrome/browser/browser_switcher/browser_switcher_service_browsertest.cc
index 360310c..bcbe6bb 100644
--- a/chrome/browser/browser_switcher/browser_switcher_service_browsertest.cc
+++ b/chrome/browser/browser_switcher/browser_switcher_service_browsertest.cc
@@ -791,6 +791,77 @@
 }
 
 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,
                        DeletesSitelistCacheOnStartup) {
   base::ScopedAllowBlockingForTesting allow_blocking;
 
diff --git a/chrome/browser/browser_switcher/browser_switcher_service_win.cc b/chrome/browser/browser_switcher/browser_switcher_service_win.cc
index 27414d9..196725c6 100644
--- a/chrome/browser/browser_switcher/browser_switcher_service_win.cc
+++ b/chrome/browser/browser_switcher/browser_switcher_service_win.cc
@@ -192,6 +192,9 @@
   if (prefs().UseIeSitelist())
     sitelist()->SetIeemSitelist(
         ParsedXml(prefs().GetCachedIeemSitelist(), base::nullopt));
+  if (!prefs().IsEnabled())
+    return;
+  SavePrefsToFile();
 }
 
 void BrowserSwitcherServiceWin::OnAllRulesetsParsed() {