Index: components/ukm/observers/sync_disable_observer.cc |
diff --git a/components/ukm/observers/sync_disable_observer.cc b/components/ukm/observers/sync_disable_observer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0e69147fc1aac237655fe633044f1f5d9e10008f |
--- /dev/null |
+++ b/components/ukm/observers/sync_disable_observer.cc |
@@ -0,0 +1,87 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/ukm/observers/sync_disable_observer.h" |
+ |
+#include "base/stl_util.h" |
+ |
+namespace ukm { |
+ |
+namespace { |
+ |
+// Check that the map contains at least one profile and all profiles are |
+// enabled. |
+bool AllProfilesEnabled( |
+ const std::map<syncer::SyncService*, bool>& enabled_map) { |
+ if (enabled_map.empty()) |
+ return false; |
+ for (const auto& kv : enabled_map) { |
+ if (!kv.second) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+SyncDisableObserver::SyncDisableObserver() |
+ : sync_observer_(this), all_profiles_enabled_(false) {} |
+ |
+SyncDisableObserver::~SyncDisableObserver() {} |
+ |
+// static |
+bool SyncDisableObserver::IsHistorySyncEnabled( |
+ syncer::SyncService* sync_service) { |
+ return sync_service && |
+ // We can't be sure of the state until after the engine is initialized, |
+ // so assume it is disabled until then. This should not trigger a |
+ // purge, since data is only purged when an enabled service changes to |
+ // a disabled state, and this should only impact services initial |
+ // state. |
+ sync_service->IsEngineInitialized() && |
+ // User must not be using a passphrase to encrypt synced data. |
+ !sync_service->IsUsingSecondaryPassphrase() && |
+ // User must have history sync option enabled. |
+ sync_service->GetPreferredDataTypes().Has( |
+ syncer::HISTORY_DELETE_DIRECTIVES); |
+} |
+ |
+void SyncDisableObserver::ObserveServiceForSyncDisables( |
+ syncer::SyncService* sync_service) { |
+ was_enabled_map_[sync_service] = IsHistorySyncEnabled(sync_service); |
+ sync_observer_.Add(sync_service); |
+ UpdateAllProfileEnabled(false); |
+} |
+ |
+void SyncDisableObserver::UpdateAllProfileEnabled(bool must_purge) { |
+ bool all_enabled = AllProfilesEnabled(was_enabled_map_); |
+ if (must_purge || (all_enabled != all_profiles_enabled_)) { |
+ all_profiles_enabled_ = all_enabled; |
+ OnSyncPrefsChanged(must_purge); |
+ } |
+} |
+ |
+void SyncDisableObserver::OnStateChanged(syncer::SyncService* sync) { |
+ DCHECK(base::ContainsKey(was_enabled_map_, sync)); |
+ bool is_enabled = IsHistorySyncEnabled(sync); |
+ // Only trigger a purge of a service state transitions from enabled to |
+ // disabled. Adding a disabled profile should simply disable recording new |
+ // data. |
+ bool must_purge = was_enabled_map_[sync] && !is_enabled; |
+ was_enabled_map_[sync] = is_enabled; |
+ UpdateAllProfileEnabled(must_purge); |
+} |
+ |
+void SyncDisableObserver::OnSyncShutdown(syncer::SyncService* sync) { |
+ DCHECK(base::ContainsKey(was_enabled_map_, sync)); |
+ sync_observer_.Remove(sync); |
+ was_enabled_map_.erase(sync); |
+ UpdateAllProfileEnabled(false); |
+} |
+ |
+bool SyncDisableObserver::IsHistorySyncEnabledOnAllProfiles() { |
+ return all_profiles_enabled_; |
+} |
+ |
+} // namespace ukm |