| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/user_prefs/tracked/tracked_atomic_preference.h" | 5 #include "components/user_prefs/tracked/tracked_atomic_preference.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 8 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
| 9 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h
" | 9 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h
" |
| 10 | 10 |
| 11 TrackedAtomicPreference::TrackedAtomicPreference( | 11 TrackedAtomicPreference::TrackedAtomicPreference( |
| 12 const std::string& pref_path, | 12 const std::string& pref_path, |
| 13 size_t reporting_id, | 13 size_t reporting_id, |
| 14 size_t reporting_ids_count, | 14 size_t reporting_ids_count, |
| 15 PrefHashFilter::EnforcementLevel enforcement_level, | 15 PrefHashFilter::EnforcementLevel enforcement_level, |
| 16 PrefHashFilter::ValueType value_type, | 16 PrefHashFilter::ValueType value_type, |
| 17 TrackedPreferenceValidationDelegate* delegate) | 17 TrackedPreferenceValidationDelegate* delegate) |
| 18 : pref_path_(pref_path), | 18 : pref_path_(pref_path), |
| 19 helper_(pref_path, | 19 helper_(pref_path, |
| 20 reporting_id, | 20 reporting_id, |
| 21 reporting_ids_count, | 21 reporting_ids_count, |
| 22 enforcement_level, | 22 enforcement_level, |
| 23 value_type), | 23 value_type), |
| 24 delegate_(delegate) { | 24 delegate_(delegate) { |
| 25 } | 25 } |
| 26 | 26 |
| 27 TrackedPreferenceType TrackedAtomicPreference::GetType() const { | |
| 28 return TrackedPreferenceType::ATOMIC; | |
| 29 } | |
| 30 | |
| 31 void TrackedAtomicPreference::OnNewValue( | 27 void TrackedAtomicPreference::OnNewValue( |
| 32 const base::Value* value, | 28 const base::Value* value, |
| 33 PrefHashStoreTransaction* transaction) const { | 29 PrefHashStoreTransaction* transaction) const { |
| 34 transaction->StoreHash(pref_path_, value); | 30 transaction->StoreHash(pref_path_, value); |
| 35 } | 31 } |
| 36 | 32 |
| 37 bool TrackedAtomicPreference::EnforceAndReport( | 33 bool TrackedAtomicPreference::EnforceAndReport( |
| 38 base::DictionaryValue* pref_store_contents, | 34 base::DictionaryValue* pref_store_contents, |
| 39 PrefHashStoreTransaction* transaction, | 35 PrefHashStoreTransaction* transaction) const { |
| 40 PrefHashStoreTransaction* external_validation_transaction) const { | |
| 41 const base::Value* value = NULL; | 36 const base::Value* value = NULL; |
| 42 pref_store_contents->Get(pref_path_, &value); | 37 pref_store_contents->Get(pref_path_, &value); |
| 43 PrefHashStoreTransaction::ValueState value_state = | 38 PrefHashStoreTransaction::ValueState value_state = |
| 44 transaction->CheckValue(pref_path_, value); | 39 transaction->CheckValue(pref_path_, value); |
| 40 |
| 45 helper_.ReportValidationResult(value_state, transaction->GetStoreUMASuffix()); | 41 helper_.ReportValidationResult(value_state, transaction->GetStoreUMASuffix()); |
| 46 | 42 |
| 47 PrefHashStoreTransaction::ValueState external_validation_value_state = | 43 TrackedPreferenceHelper::ResetAction reset_action = |
| 48 PrefHashStoreTransaction::UNCHANGED; | 44 helper_.GetAction(value_state); |
| 49 if (external_validation_transaction) { | |
| 50 external_validation_value_state = | |
| 51 external_validation_transaction->CheckValue(pref_path_, value); | |
| 52 helper_.ReportValidationResult( | |
| 53 external_validation_value_state, | |
| 54 external_validation_transaction->GetStoreUMASuffix()); | |
| 55 | |
| 56 // TODO(proberge): Call delegate_->OnAtomicPreferenceValidation. | |
| 57 } | |
| 58 | |
| 59 if (delegate_) { | 45 if (delegate_) { |
| 60 delegate_->OnAtomicPreferenceValidation(pref_path_, value, value_state, | 46 delegate_->OnAtomicPreferenceValidation(pref_path_, value, value_state, |
| 61 helper_.IsPersonal()); | 47 helper_.IsPersonal()); |
| 62 } | 48 } |
| 63 TrackedPreferenceHelper::ResetAction reset_action = | |
| 64 helper_.GetAction(value_state); | |
| 65 helper_.ReportAction(reset_action); | 49 helper_.ReportAction(reset_action); |
| 66 | 50 |
| 67 bool was_reset = false; | 51 bool was_reset = false; |
| 68 if (reset_action == TrackedPreferenceHelper::DO_RESET) { | 52 if (reset_action == TrackedPreferenceHelper::DO_RESET) { |
| 69 pref_store_contents->RemovePath(pref_path_, NULL); | 53 pref_store_contents->RemovePath(pref_path_, NULL); |
| 70 was_reset = true; | 54 was_reset = true; |
| 71 } | 55 } |
| 72 | 56 |
| 73 if (value_state != PrefHashStoreTransaction::UNCHANGED) { | 57 if (value_state != PrefHashStoreTransaction::UNCHANGED) { |
| 74 // Store the hash for the new value (whether it was reset or not). | 58 // Store the hash for the new value (whether it was reset or not). |
| 75 const base::Value* new_value = NULL; | 59 const base::Value* new_value = NULL; |
| 76 pref_store_contents->Get(pref_path_, &new_value); | 60 pref_store_contents->Get(pref_path_, &new_value); |
| 77 transaction->StoreHash(pref_path_, new_value); | 61 transaction->StoreHash(pref_path_, new_value); |
| 78 } | 62 } |
| 79 | 63 |
| 80 // Update MACs in the external store if there is one and there either was a | |
| 81 // reset or external validation failed. | |
| 82 if (external_validation_transaction && | |
| 83 (was_reset || | |
| 84 external_validation_value_state != | |
| 85 PrefHashStoreTransaction::UNCHANGED)) { | |
| 86 const base::Value* new_value = nullptr; | |
| 87 pref_store_contents->Get(pref_path_, &new_value); | |
| 88 external_validation_transaction->StoreHash(pref_path_, new_value); | |
| 89 } | |
| 90 | |
| 91 return was_reset; | 64 return was_reset; |
| 92 } | 65 } |
| OLD | NEW |