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