Restart HistoryCounter when the history sync state changes.

When the history sync is turned on (off), history deletion requests will (not) be synced as well, but the history counter result does not change. This can be misleading in that the result only mentions local visits, while history visits would be deleted as well. In this CL, we restart counter anytime the history sync state changes.

Update: Tested manually and added a browsertest for this behavior.

BUG=510028

Review URL: https://codereview.chromium.org/1415873009

Cr-Commit-Position: refs/heads/master@{#358605}
diff --git a/chrome/browser/browsing_data/history_counter.h b/chrome/browser/browsing_data/history_counter.h
index 4061cdb..026da3a8 100644
--- a/chrome/browser/browsing_data/history_counter.h
+++ b/chrome/browser/browsing_data/history_counter.h
@@ -10,8 +10,12 @@
 #include "chrome/browser/browsing_data/browsing_data_counter.h"
 #include "components/history/core/browser/history_service.h"
 #include "components/history/core/browser/web_history_service.h"
+#include "components/sync_driver/sync_service_observer.h"
 
-class HistoryCounter: public BrowsingDataCounter {
+class ProfileSyncService;
+
+class HistoryCounter: public BrowsingDataCounter,
+                      public sync_driver::SyncServiceObserver {
  public:
   class HistoryResult : public FinishedResult {
    public:
@@ -29,6 +33,7 @@
   HistoryCounter();
   ~HistoryCounter() override;
 
+  void OnInitialized() override;
   const std::string& GetPrefName() const override;
 
   // Whether there are counting tasks in progress. Only used for testing.
@@ -55,6 +60,9 @@
 
   base::ThreadChecker thread_checker_;
 
+  ProfileSyncService* sync_service_;
+  bool history_sync_enabled_;
+
   void Count() override;
 
   void OnGetLocalHistoryCount(history::HistoryCountResult result);
@@ -62,6 +70,9 @@
                             const base::DictionaryValue* result);
   void OnWebHistoryTimeout();
   void MergeResults();
+
+  // SyncServiceObserver implementation.
+  void OnStateChanged() override;
 };
 
 #endif  // CHROME_BROWSER_BROWSING_DATA_HISTORY_COUNTER_H_