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 "chrome/browser/prefs/tracked/tracked_split_preference.h"
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"
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
),
22 helper_(pref_path
, reporting_id
, reporting_ids_count
, enforcement_level
,
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
)) {
35 transaction
->StoreSplitHash(pref_path_
, dict_value
);
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_|.
49 std::vector
<std::string
> invalid_keys
;
50 PrefHashStoreTransaction::ValueState value_state
=
51 transaction
->CheckSplitValue(pref_path_
, dict_value
, &invalid_keys
);
53 if (value_state
== PrefHashStoreTransaction::CHANGED
)
54 helper_
.ReportSplitPreferenceChangedCount(invalid_keys
.size());
56 helper_
.ReportValidationResult(value_state
);
58 TrackedPreferenceHelper::ResetAction reset_action
=
59 helper_
.GetAction(value_state
);
61 delegate_
->OnSplitPreferenceValidation(pref_path_
, dict_value
, invalid_keys
,
62 value_state
, helper_
.IsPersonal());
64 helper_
.ReportAction(reset_action
);
66 bool was_reset
= false;
67 if (reset_action
== TrackedPreferenceHelper::DO_RESET
) {
68 if (value_state
== PrefHashStoreTransaction::CHANGED
) {
69 DCHECK(!invalid_keys
.empty());
71 for (std::vector
<std::string
>::const_iterator it
=
72 invalid_keys
.begin(); it
!= invalid_keys
.end(); ++it
) {
73 dict_value
->Remove(*it
, NULL
);
76 pref_store_contents
->RemovePath(pref_path_
, NULL
);
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
);