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/safe_browsing/incident_reporting/preference_validation_delegate.h"
10 #include "base/json/json_writer.h"
11 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h"
12 #include "chrome/browser/safe_browsing/incident_reporting/tracked_preference_incident.h"
13 #include "chrome/common/safe_browsing/csd.pb.h"
14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
15 #include "components/user_prefs/tracked/tracked_preference_helper.h"
17 namespace safe_browsing
{
21 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident
;
22 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState
23 TPIncident_ValueState
;
25 // Maps a PrefHashStoreTransaction::ValueState to a
26 // TrackedPreferenceIncident::ValueState.
27 TPIncident_ValueState
MapValueState(
28 PrefHashStoreTransaction::ValueState value_state
) {
29 switch (value_state
) {
30 case PrefHashStoreTransaction::CLEARED
:
31 return TPIncident::CLEARED
;
32 case PrefHashStoreTransaction::CHANGED
:
33 return TPIncident::CHANGED
;
34 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE
:
35 return TPIncident::UNTRUSTED_UNKNOWN_VALUE
;
37 return TPIncident::UNKNOWN
;
43 PreferenceValidationDelegate::PreferenceValidationDelegate(
45 scoped_ptr
<IncidentReceiver
> incident_receiver
)
47 incident_receiver_(incident_receiver
.Pass()) {
50 PreferenceValidationDelegate::~PreferenceValidationDelegate() {
53 void PreferenceValidationDelegate::OnAtomicPreferenceValidation(
54 const std::string
& pref_path
,
55 const base::Value
* value
,
56 PrefHashStoreTransaction::ValueState value_state
,
58 TPIncident_ValueState proto_value_state
= MapValueState(value_state
);
59 if (proto_value_state
!= TPIncident::UNKNOWN
) {
60 scoped_ptr
<TPIncident
> incident(
61 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident());
62 incident
->set_path(pref_path
);
64 (!value
->GetAsString(incident
->mutable_atomic_value()) &&
65 !base::JSONWriter::Write(*value
, incident
->mutable_atomic_value()))) {
66 incident
->clear_atomic_value();
68 incident
->set_value_state(proto_value_state
);
69 incident_receiver_
->AddIncidentForProfile(
70 profile_
, make_scoped_ptr(new TrackedPreferenceIncident(incident
.Pass(),
75 void PreferenceValidationDelegate::OnSplitPreferenceValidation(
76 const std::string
& pref_path
,
77 const base::DictionaryValue
* /* dict_value */,
78 const std::vector
<std::string
>& invalid_keys
,
79 PrefHashStoreTransaction::ValueState value_state
,
81 TPIncident_ValueState proto_value_state
= MapValueState(value_state
);
82 if (proto_value_state
!= TPIncident::UNKNOWN
) {
83 scoped_ptr
<ClientIncidentReport_IncidentData_TrackedPreferenceIncident
>
85 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident());
86 incident
->set_path(pref_path
);
87 for (std::vector
<std::string
>::const_iterator
scan(invalid_keys
.begin());
88 scan
!= invalid_keys
.end();
90 incident
->add_split_key(*scan
);
92 incident
->set_value_state(proto_value_state
);
93 incident_receiver_
->AddIncidentForProfile(
94 profile_
, make_scoped_ptr(new TrackedPreferenceIncident(incident
.Pass(),
99 } // namespace safe_browsing