| 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 "chrome/browser/prefs/tracked/tracked_split_preference.h" | 5 #include "chrome/browser/prefs/tracked/tracked_split_preference.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/prefs/pref_hash_store_transaction.h" | 11 #include "chrome/browser/prefs/pref_hash_store_transaction.h" |
| 12 #include "chrome/browser/prefs/tracked/tracked_preference_validation_observer.h" |
| 12 | 13 |
| 13 TrackedSplitPreference::TrackedSplitPreference( | 14 TrackedSplitPreference::TrackedSplitPreference( |
| 14 const std::string& pref_path, | 15 const std::string& pref_path, |
| 15 size_t reporting_id, | 16 size_t reporting_id, |
| 16 size_t reporting_ids_count, | 17 size_t reporting_ids_count, |
| 17 PrefHashFilter::EnforcementLevel enforcement_level) | 18 PrefHashFilter::EnforcementLevel enforcement_level) |
| 18 : pref_path_(pref_path), | 19 : pref_path_(pref_path), |
| 19 helper_(pref_path, reporting_id, reporting_ids_count, enforcement_level) { | 20 helper_(pref_path, reporting_id, reporting_ids_count, enforcement_level) { |
| 20 } | 21 } |
| 21 | 22 |
| 22 void TrackedSplitPreference::OnNewValue( | 23 void TrackedSplitPreference::OnNewValue( |
| 23 const base::Value* value, | 24 const base::Value* value, |
| 24 PrefHashStoreTransaction* transaction) const { | 25 PrefHashStoreTransaction* transaction) const { |
| 25 const base::DictionaryValue* dict_value = NULL; | 26 const base::DictionaryValue* dict_value = NULL; |
| 26 if (value && !value->GetAsDictionary(&dict_value)) { | 27 if (value && !value->GetAsDictionary(&dict_value)) { |
| 27 NOTREACHED(); | 28 NOTREACHED(); |
| 28 return; | 29 return; |
| 29 } | 30 } |
| 30 transaction->StoreSplitHash(pref_path_, dict_value); | 31 transaction->StoreSplitHash(pref_path_, dict_value); |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool TrackedSplitPreference::EnforceAndReport( | 34 bool TrackedSplitPreference::EnforceAndReport( |
| 34 base::DictionaryValue* pref_store_contents, | 35 base::DictionaryValue* pref_store_contents, |
| 35 PrefHashStoreTransaction* transaction) const { | 36 PrefHashStoreTransaction* transaction, |
| 37 TrackedPreferenceValidationObserver* observer) const { |
| 36 base::DictionaryValue* dict_value = NULL; | 38 base::DictionaryValue* dict_value = NULL; |
| 37 if (!pref_store_contents->GetDictionary(pref_path_, &dict_value) && | 39 if (!pref_store_contents->GetDictionary(pref_path_, &dict_value) && |
| 38 pref_store_contents->Get(pref_path_, NULL)) { | 40 pref_store_contents->Get(pref_path_, NULL)) { |
| 39 // There should be a dictionary or nothing at |pref_path_|. | 41 // There should be a dictionary or nothing at |pref_path_|. |
| 40 NOTREACHED(); | 42 NOTREACHED(); |
| 41 return false; | 43 return false; |
| 42 } | 44 } |
| 43 | 45 |
| 44 std::vector<std::string> invalid_keys; | 46 std::vector<std::string> invalid_keys; |
| 45 PrefHashStoreTransaction::ValueState value_state = | 47 PrefHashStoreTransaction::ValueState value_state = |
| 46 transaction->CheckSplitValue(pref_path_, dict_value, &invalid_keys); | 48 transaction->CheckSplitValue(pref_path_, dict_value, &invalid_keys); |
| 47 | 49 |
| 48 if (value_state == PrefHashStoreTransaction::CHANGED) | 50 if (value_state == PrefHashStoreTransaction::CHANGED) |
| 49 helper_.ReportSplitPreferenceChangedCount(invalid_keys.size()); | 51 helper_.ReportSplitPreferenceChangedCount(invalid_keys.size()); |
| 50 | 52 |
| 51 helper_.ReportValidationResult(value_state); | 53 helper_.ReportValidationResult(value_state); |
| 52 | 54 |
| 53 TrackedPreferenceHelper::ResetAction reset_action = | 55 TrackedPreferenceHelper::ResetAction reset_action = |
| 54 helper_.GetAction(value_state); | 56 helper_.GetAction(value_state); |
| 57 if (observer) { |
| 58 observer->OnSplitPreferenceValidation( |
| 59 pref_path_, dict_value, invalid_keys, value_state, reset_action); |
| 60 } |
| 55 helper_.ReportAction(reset_action); | 61 helper_.ReportAction(reset_action); |
| 56 | 62 |
| 57 bool was_reset = false; | 63 bool was_reset = false; |
| 58 if (reset_action == TrackedPreferenceHelper::DO_RESET) { | 64 if (reset_action == TrackedPreferenceHelper::DO_RESET) { |
| 59 if (value_state == PrefHashStoreTransaction::CHANGED) { | 65 if (value_state == PrefHashStoreTransaction::CHANGED) { |
| 60 DCHECK(!invalid_keys.empty()); | 66 DCHECK(!invalid_keys.empty()); |
| 61 | 67 |
| 62 for (std::vector<std::string>::const_iterator it = | 68 for (std::vector<std::string>::const_iterator it = |
| 63 invalid_keys.begin(); it != invalid_keys.end(); ++it) { | 69 invalid_keys.begin(); it != invalid_keys.end(); ++it) { |
| 64 dict_value->Remove(*it, NULL); | 70 dict_value->Remove(*it, NULL); |
| 65 } | 71 } |
| 66 } else { | 72 } else { |
| 67 pref_store_contents->RemovePath(pref_path_, NULL); | 73 pref_store_contents->RemovePath(pref_path_, NULL); |
| 68 } | 74 } |
| 69 was_reset = true; | 75 was_reset = true; |
| 70 } | 76 } |
| 71 | 77 |
| 72 if (value_state != PrefHashStoreTransaction::UNCHANGED) { | 78 if (value_state != PrefHashStoreTransaction::UNCHANGED) { |
| 73 // Store the hash for the new value (whether it was reset or not). | 79 // Store the hash for the new value (whether it was reset or not). |
| 74 const base::DictionaryValue* new_dict_value = NULL; | 80 const base::DictionaryValue* new_dict_value = NULL; |
| 75 pref_store_contents->GetDictionary(pref_path_, &new_dict_value); | 81 pref_store_contents->GetDictionary(pref_path_, &new_dict_value); |
| 76 transaction->StoreSplitHash(pref_path_, new_dict_value); | 82 transaction->StoreSplitHash(pref_path_, new_dict_value); |
| 77 } | 83 } |
| 78 | 84 |
| 79 return was_reset; | 85 return was_reset; |
| 80 } | 86 } |
| OLD | NEW |