1 // Copyright 2014 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 "chrome/browser/prefs/tracked/tracked_atomic_preference.h" 6 7 #include "base/values.h" 8 #include "chrome/browser/prefs/pref_hash_store_transaction.h" 9 #include "chrome/browser/prefs/tracked/tracked_preference_validation_delegate.h" 10 11 TrackedAtomicPreference::TrackedAtomicPreference( 12 const std::string& pref_path, 13 size_t reporting_id, 14 size_t reporting_ids_count, 15 PrefHashFilter::EnforcementLevel enforcement_level, 16 TrackedPreferenceValidationDelegate* delegate) 17 : pref_path_(pref_path), 18 helper_(pref_path, reporting_id, reporting_ids_count, enforcement_level), 19 delegate_(delegate) { 20 } 21 22 void TrackedAtomicPreference::OnNewValue( 23 const base::Value* value, 24 PrefHashStoreTransaction* transaction) const { 25 transaction->StoreHash(pref_path_, value); 26 } 27 28 bool TrackedAtomicPreference::EnforceAndReport( 29 base::DictionaryValue* pref_store_contents, 30 PrefHashStoreTransaction* transaction) const { 31 const base::Value* value = NULL; 32 pref_store_contents->Get(pref_path_, &value); 33 PrefHashStoreTransaction::ValueState value_state = 34 transaction->CheckValue(pref_path_, value); 35 36 helper_.ReportValidationResult(value_state); 37 38 TrackedPreferenceHelper::ResetAction reset_action = 39 helper_.GetAction(value_state); 40 if (delegate_) { 41 delegate_->OnAtomicPreferenceValidation( 42 pref_path_, value, value_state, reset_action); 43 } 44 helper_.ReportAction(reset_action); 45 46 bool was_reset = false; 47 if (reset_action == TrackedPreferenceHelper::DO_RESET) { 48 pref_store_contents->RemovePath(pref_path_, NULL); 49 was_reset = true; 50 } 51 52 if (value_state != PrefHashStoreTransaction::UNCHANGED) { 53 // Store the hash for the new value (whether it was reset or not). 54 const base::Value* new_value = NULL; 55 pref_store_contents->Get(pref_path_, &new_value); 56 transaction->StoreHash(pref_path_, new_value); 57 } 58 59 return was_reset; 60 } 61