OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ukm/observers/sync_disable_observer.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 |
| 9 namespace ukm { |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Check that the map contains at least one profile and all profiles are |
| 14 // enabled. |
| 15 bool AllProfilesEnabled( |
| 16 const std::map<syncer::SyncService*, bool>& enabled_map) { |
| 17 if (enabled_map.empty()) |
| 18 return false; |
| 19 for (const auto& kv : enabled_map) { |
| 20 if (!kv.second) |
| 21 return false; |
| 22 } |
| 23 return true; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 SyncDisableObserver::SyncDisableObserver() |
| 29 : sync_observer_(this), all_profiles_enabled_(false) {} |
| 30 |
| 31 SyncDisableObserver::~SyncDisableObserver() {} |
| 32 |
| 33 // static |
| 34 bool SyncDisableObserver::IsHistorySyncEnabled( |
| 35 syncer::SyncService* sync_service) { |
| 36 return sync_service && |
| 37 // We can't be sure of the state until after the engine is initialized, |
| 38 // so assume it is disabled until then. This should not trigger a |
| 39 // purge, since data is only purged when an enabled service changes to |
| 40 // a disabled state, and this should only impact services initial |
| 41 // state. |
| 42 sync_service->IsEngineInitialized() && |
| 43 // User must not be using a passphrase to encrypt synced data. |
| 44 !sync_service->IsUsingSecondaryPassphrase() && |
| 45 // User must have history sync option enabled. |
| 46 sync_service->GetPreferredDataTypes().Has( |
| 47 syncer::HISTORY_DELETE_DIRECTIVES); |
| 48 } |
| 49 |
| 50 void SyncDisableObserver::ObserveServiceForSyncDisables( |
| 51 syncer::SyncService* sync_service) { |
| 52 was_enabled_map_[sync_service] = IsHistorySyncEnabled(sync_service); |
| 53 sync_observer_.Add(sync_service); |
| 54 UpdateAllProfileEnabled(false); |
| 55 } |
| 56 |
| 57 void SyncDisableObserver::UpdateAllProfileEnabled(bool must_purge) { |
| 58 bool all_enabled = AllProfilesEnabled(was_enabled_map_); |
| 59 if (must_purge || (all_enabled != all_profiles_enabled_)) { |
| 60 all_profiles_enabled_ = all_enabled; |
| 61 OnSyncPrefsChanged(must_purge); |
| 62 } |
| 63 } |
| 64 |
| 65 void SyncDisableObserver::OnStateChanged(syncer::SyncService* sync) { |
| 66 DCHECK(base::ContainsKey(was_enabled_map_, sync)); |
| 67 bool is_enabled = IsHistorySyncEnabled(sync); |
| 68 // Only trigger a purge of a service state transitions from enabled to |
| 69 // disabled. Adding a disabled profile should simply disable recording new |
| 70 // data. |
| 71 bool must_purge = was_enabled_map_[sync] && !is_enabled; |
| 72 was_enabled_map_[sync] = is_enabled; |
| 73 UpdateAllProfileEnabled(must_purge); |
| 74 } |
| 75 |
| 76 void SyncDisableObserver::OnSyncShutdown(syncer::SyncService* sync) { |
| 77 DCHECK(base::ContainsKey(was_enabled_map_, sync)); |
| 78 sync_observer_.Remove(sync); |
| 79 was_enabled_map_.erase(sync); |
| 80 UpdateAllProfileEnabled(false); |
| 81 } |
| 82 |
| 83 bool SyncDisableObserver::IsHistorySyncEnabledOnAllProfiles() { |
| 84 return all_profiles_enabled_; |
| 85 } |
| 86 |
| 87 } // namespace ukm |
OLD | NEW |