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_split_preference.h" | 5 #include "components/user_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 "components/user_prefs/tracked/pref_hash_store_transaction.h" | 11 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
12 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h
" | 12 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h
" |
13 | 13 |
14 TrackedSplitPreference::TrackedSplitPreference( | 14 TrackedSplitPreference::TrackedSplitPreference( |
15 const std::string& pref_path, | 15 const std::string& pref_path, |
16 size_t reporting_id, | 16 size_t reporting_id, |
17 size_t reporting_ids_count, | 17 size_t reporting_ids_count, |
18 PrefHashFilter::EnforcementLevel enforcement_level, | 18 PrefHashFilter::EnforcementLevel enforcement_level, |
19 PrefHashFilter::ValueType value_type, | 19 PrefHashFilter::ValueType value_type, |
20 TrackedPreferenceValidationDelegate* delegate) | 20 TrackedPreferenceValidationDelegate* delegate) |
21 : pref_path_(pref_path), | 21 : pref_path_(pref_path), |
22 helper_(pref_path, | 22 helper_(pref_path, |
23 reporting_id, | 23 reporting_id, |
24 reporting_ids_count, | 24 reporting_ids_count, |
25 enforcement_level, | 25 enforcement_level, |
26 value_type), | 26 value_type), |
27 delegate_(delegate) { | 27 delegate_(delegate) { |
28 } | 28 } |
29 | 29 |
30 TrackedPreferenceType TrackedSplitPreference::GetType() const { | |
31 return TrackedPreferenceType::SPLIT; | |
32 } | |
33 | |
34 void TrackedSplitPreference::OnNewValue( | 30 void TrackedSplitPreference::OnNewValue( |
35 const base::Value* value, | 31 const base::Value* value, |
36 PrefHashStoreTransaction* transaction) const { | 32 PrefHashStoreTransaction* transaction) const { |
37 const base::DictionaryValue* dict_value = NULL; | 33 const base::DictionaryValue* dict_value = NULL; |
38 if (value && !value->GetAsDictionary(&dict_value)) { | 34 if (value && !value->GetAsDictionary(&dict_value)) { |
39 NOTREACHED(); | 35 NOTREACHED(); |
40 return; | 36 return; |
41 } | 37 } |
42 transaction->StoreSplitHash(pref_path_, dict_value); | 38 transaction->StoreSplitHash(pref_path_, dict_value); |
43 } | 39 } |
44 | 40 |
45 bool TrackedSplitPreference::EnforceAndReport( | 41 bool TrackedSplitPreference::EnforceAndReport( |
46 base::DictionaryValue* pref_store_contents, | 42 base::DictionaryValue* pref_store_contents, |
47 PrefHashStoreTransaction* transaction, | 43 PrefHashStoreTransaction* transaction) const { |
48 PrefHashStoreTransaction* external_validation_transaction) const { | |
49 base::DictionaryValue* dict_value = NULL; | 44 base::DictionaryValue* dict_value = NULL; |
50 if (!pref_store_contents->GetDictionary(pref_path_, &dict_value) && | 45 if (!pref_store_contents->GetDictionary(pref_path_, &dict_value) && |
51 pref_store_contents->Get(pref_path_, NULL)) { | 46 pref_store_contents->Get(pref_path_, NULL)) { |
52 // There should be a dictionary or nothing at |pref_path_|. | 47 // There should be a dictionary or nothing at |pref_path_|. |
53 NOTREACHED(); | 48 NOTREACHED(); |
54 return false; | 49 return false; |
55 } | 50 } |
56 | 51 |
57 std::vector<std::string> invalid_keys; | 52 std::vector<std::string> invalid_keys; |
58 PrefHashStoreTransaction::ValueState value_state = | 53 PrefHashStoreTransaction::ValueState value_state = |
59 transaction->CheckSplitValue(pref_path_, dict_value, &invalid_keys); | 54 transaction->CheckSplitValue(pref_path_, dict_value, &invalid_keys); |
60 | 55 |
61 if (value_state == PrefHashStoreTransaction::CHANGED) | 56 if (value_state == PrefHashStoreTransaction::CHANGED) |
62 helper_.ReportSplitPreferenceChangedCount(invalid_keys.size()); | 57 helper_.ReportSplitPreferenceChangedCount(invalid_keys.size()); |
63 | 58 |
64 helper_.ReportValidationResult(value_state, transaction->GetStoreUMASuffix()); | 59 helper_.ReportValidationResult(value_state, transaction->GetStoreUMASuffix()); |
65 | 60 |
66 PrefHashStoreTransaction::ValueState external_validation_value_state = | 61 TrackedPreferenceHelper::ResetAction reset_action = |
67 PrefHashStoreTransaction::UNCHANGED; | 62 helper_.GetAction(value_state); |
68 if (external_validation_transaction) { | |
69 std::vector<std::string> invalid_external_validation_keys; | |
70 external_validation_value_state = | |
71 external_validation_transaction->CheckSplitValue( | |
72 pref_path_, dict_value, &invalid_external_validation_keys); | |
73 helper_.ReportValidationResult( | |
74 external_validation_value_state, | |
75 external_validation_transaction->GetStoreUMASuffix()); | |
76 | |
77 // TODO(proberge): Call delegate_->OnSplitPreferenceValidation. | |
78 } | |
79 | |
80 if (delegate_) { | 63 if (delegate_) { |
81 delegate_->OnSplitPreferenceValidation(pref_path_, dict_value, invalid_keys, | 64 delegate_->OnSplitPreferenceValidation(pref_path_, dict_value, invalid_keys, |
82 value_state, helper_.IsPersonal()); | 65 value_state, helper_.IsPersonal()); |
83 } | 66 } |
84 TrackedPreferenceHelper::ResetAction reset_action = | |
85 helper_.GetAction(value_state); | |
86 helper_.ReportAction(reset_action); | 67 helper_.ReportAction(reset_action); |
87 | 68 |
88 bool was_reset = false; | 69 bool was_reset = false; |
89 if (reset_action == TrackedPreferenceHelper::DO_RESET) { | 70 if (reset_action == TrackedPreferenceHelper::DO_RESET) { |
90 if (value_state == PrefHashStoreTransaction::CHANGED) { | 71 if (value_state == PrefHashStoreTransaction::CHANGED) { |
91 DCHECK(!invalid_keys.empty()); | 72 DCHECK(!invalid_keys.empty()); |
92 | 73 |
93 for (std::vector<std::string>::const_iterator it = invalid_keys.begin(); | 74 for (std::vector<std::string>::const_iterator it = invalid_keys.begin(); |
94 it != invalid_keys.end(); ++it) { | 75 it != invalid_keys.end(); ++it) { |
95 dict_value->Remove(*it, NULL); | 76 dict_value->Remove(*it, NULL); |
96 } | 77 } |
97 } else { | 78 } else { |
98 pref_store_contents->RemovePath(pref_path_, NULL); | 79 pref_store_contents->RemovePath(pref_path_, NULL); |
99 } | 80 } |
100 was_reset = true; | 81 was_reset = true; |
101 } | 82 } |
102 | 83 |
103 if (value_state != PrefHashStoreTransaction::UNCHANGED) { | 84 if (value_state != PrefHashStoreTransaction::UNCHANGED) { |
104 // Store the hash for the new value (whether it was reset or not). | 85 // Store the hash for the new value (whether it was reset or not). |
105 const base::DictionaryValue* new_dict_value = NULL; | 86 const base::DictionaryValue* new_dict_value = NULL; |
106 pref_store_contents->GetDictionary(pref_path_, &new_dict_value); | 87 pref_store_contents->GetDictionary(pref_path_, &new_dict_value); |
107 transaction->StoreSplitHash(pref_path_, new_dict_value); | 88 transaction->StoreSplitHash(pref_path_, new_dict_value); |
108 } | 89 } |
109 | 90 |
110 // Update MACs in the external store if there is one and there either was a | |
111 // reset or external validation failed. | |
112 if (external_validation_transaction && | |
113 (was_reset || | |
114 external_validation_value_state != | |
115 PrefHashStoreTransaction::UNCHANGED)) { | |
116 const base::DictionaryValue* new_dict_value = nullptr; | |
117 pref_store_contents->GetDictionary(pref_path_, &new_dict_value); | |
118 external_validation_transaction->StoreSplitHash(pref_path_, new_dict_value); | |
119 } | |
120 | |
121 return was_reset; | 91 return was_reset; |
122 } | 92 } |
OLD | NEW |