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 SyncDisableObserver::SyncDisableObserver() |
| 12 : sync_observer_(this), all_profiles_enabled_(false) {} |
| 13 |
| 14 SyncDisableObserver::~SyncDisableObserver() {} |
| 15 |
| 16 // static |
| 17 SyncDisableObserver::SyncState SyncDisableObserver::GetSyncState( |
| 18 syncer::SyncService* sync_service) { |
| 19 return SyncDisableObserver::SyncState{ |
| 20 .history_enabled = sync_service->GetPreferredDataTypes().Has( |
| 21 syncer::HISTORY_DELETE_DIRECTIVES), |
| 22 .initialized = sync_service->IsEngineInitialized(), |
| 23 .passphrase_protected = sync_service->IsUsingSecondaryPassphrase(), |
| 24 }; |
| 25 } |
| 26 |
| 27 void SyncDisableObserver::ObserveServiceForSyncDisables( |
| 28 syncer::SyncService* sync_service) { |
| 29 previous_states_[sync_service] = GetSyncState(sync_service); |
| 30 sync_observer_.Add(sync_service); |
| 31 UpdateAllProfileEnabled(false); |
| 32 } |
| 33 |
| 34 void SyncDisableObserver::UpdateAllProfileEnabled(bool must_purge) { |
| 35 bool all_enabled = AreAllProfilesEnabled(); |
| 36 if (must_purge || (all_enabled != all_profiles_enabled_)) { |
| 37 all_profiles_enabled_ = all_enabled; |
| 38 OnSyncPrefsChanged(must_purge); |
| 39 } |
| 40 } |
| 41 |
| 42 bool SyncDisableObserver::AreAllProfilesEnabled() { |
| 43 if (previous_states_.empty()) |
| 44 return false; |
| 45 for (const auto& kv : previous_states_) { |
| 46 const SyncDisableObserver::SyncState& state = kv.second; |
| 47 if (!state.history_enabled || !state.initialized || |
| 48 state.passphrase_protected) |
| 49 return false; |
| 50 } |
| 51 return true; |
| 52 } |
| 53 |
| 54 void SyncDisableObserver::OnStateChanged(syncer::SyncService* sync) { |
| 55 DCHECK(base::ContainsKey(previous_states_, sync)); |
| 56 SyncDisableObserver::SyncState state = GetSyncState(sync); |
| 57 const SyncDisableObserver::SyncState& previous_state = previous_states_[sync]; |
| 58 bool must_purge = |
| 59 // Trigger a purge if history sync was disabled. |
| 60 (previous_state.history_enabled && !state.history_enabled) || |
| 61 // Trigger a purge if the user added a passphrase. Since we can't detect |
| 62 // the use of a passphrase while the engine is not initialized, we may |
| 63 // miss the transition if the user adds a passphrase in this state. |
| 64 (previous_state.initialized && state.initialized && |
| 65 !previous_state.passphrase_protected && state.passphrase_protected); |
| 66 previous_states_[sync] = state; |
| 67 UpdateAllProfileEnabled(must_purge); |
| 68 } |
| 69 |
| 70 void SyncDisableObserver::OnSyncShutdown(syncer::SyncService* sync) { |
| 71 DCHECK(base::ContainsKey(previous_states_, sync)); |
| 72 sync_observer_.Remove(sync); |
| 73 previous_states_.erase(sync); |
| 74 UpdateAllProfileEnabled(false); |
| 75 } |
| 76 |
| 77 bool SyncDisableObserver::IsHistorySyncEnabledOnAllProfiles() { |
| 78 return all_profiles_enabled_; |
| 79 } |
| 80 |
| 81 } // namespace ukm |
OLD | NEW |