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.
5 #include "components/user_prefs/tracked/tracked_split_preference.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
12 #include "components/user_prefs/tracked/tracked_preference_validation_delegate.h"
14 TrackedSplitPreference::TrackedSplitPreference(
15 const std::string
& pref_path
,
17 size_t reporting_ids_count
,
18 PrefHashFilter::EnforcementLevel enforcement_level
,
19 PrefHashFilter::ValueType value_type
,
20 TrackedPreferenceValidationDelegate
* delegate
)
21 : pref_path_(pref_path
),
30 void TrackedSplitPreference::OnNewValue(
31 const base::Value
* value
,
32 PrefHashStoreTransaction
* transaction
) const {
33 const base::DictionaryValue
* dict_value
= NULL
;
34 if (value
&& !value
->GetAsDictionary(&dict_value
)) {
38 transaction
->StoreSplitHash(pref_path_
, dict_value
);
41 bool TrackedSplitPreference::EnforceAndReport(
42 base::DictionaryValue
* pref_store_contents
,
43 PrefHashStoreTransaction
* transaction
) const {
44 base::DictionaryValue
* dict_value
= NULL
;
45 if (!pref_store_contents
->GetDictionary(pref_path_
, &dict_value
) &&
46 pref_store_contents
->Get(pref_path_
, NULL
)) {
47 // There should be a dictionary or nothing at |pref_path_|.
52 std::vector
<std::string
> invalid_keys
;
53 PrefHashStoreTransaction::ValueState value_state
=
54 transaction
->CheckSplitValue(pref_path_
, dict_value
, &invalid_keys
);
56 if (value_state
== PrefHashStoreTransaction::CHANGED
)
57 helper_
.ReportSplitPreferenceChangedCount(invalid_keys
.size());
59 helper_
.ReportValidationResult(value_state
);
61 TrackedPreferenceHelper::ResetAction reset_action
=
62 helper_
.GetAction(value_state
);
64 delegate_
->OnSplitPreferenceValidation(pref_path_
, dict_value
, invalid_keys
,
65 value_state
, helper_
.IsPersonal());
67 helper_
.ReportAction(reset_action
);
69 bool was_reset
= false;
70 if (reset_action
== TrackedPreferenceHelper::DO_RESET
) {
71 if (value_state
== PrefHashStoreTransaction::CHANGED
) {
72 DCHECK(!invalid_keys
.empty());
74 for (std::vector
<std::string
>::const_iterator it
= invalid_keys
.begin();
75 it
!= invalid_keys
.end(); ++it
) {
76 dict_value
->Remove(*it
, NULL
);
79 pref_store_contents
->RemovePath(pref_path_
, NULL
);
84 if (value_state
!= PrefHashStoreTransaction::UNCHANGED
) {
85 // Store the hash for the new value (whether it was reset or not).
86 const base::DictionaryValue
* new_dict_value
= NULL
;
87 pref_store_contents
->GetDictionary(pref_path_
, &new_dict_value
);
88 transaction
->StoreSplitHash(pref_path_
, new_dict_value
);