| 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 "chrome/browser/safe_browsing/incident_reporting/preference_validation_
delegate.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/preference_validation_
delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" | 13 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" |
| 14 #include "chrome/browser/safe_browsing/incident_reporting/tracked_preference_inc
ident.h" | 14 #include "chrome/browser/safe_browsing/incident_reporting/tracked_preference_inc
ident.h" |
| 15 #include "chrome/common/safe_browsing/csd.pb.h" | 15 #include "chrome/common/safe_browsing/csd.pb.h" |
| 16 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 16 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
| 17 #include "components/user_prefs/tracked/tracked_preference_helper.h" | 17 #include "components/user_prefs/tracked/tracked_preference_helper.h" |
| 18 | 18 |
| 19 namespace safe_browsing { | 19 namespace safe_browsing { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; | 23 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident; |
| 24 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState | 24 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState |
| 25 TPIncident_ValueState; | 25 TPIncident_ValueState; |
| 26 | 26 |
| 27 // Maps a primary PrefHashStoreTransaction::ValueState and an external | 27 // Maps a PrefHashStoreTransaction::ValueState to a |
| 28 // validation state to a TrackedPreferenceIncident::ValueState. | 28 // TrackedPreferenceIncident::ValueState. |
| 29 TPIncident_ValueState MapValueState( | 29 TPIncident_ValueState MapValueState( |
| 30 PrefHashStoreTransaction::ValueState value_state, | 30 PrefHashStoreTransaction::ValueState value_state) { |
| 31 PrefHashStoreTransaction::ValueState external_validation_value_state) { | |
| 32 switch (value_state) { | 31 switch (value_state) { |
| 33 case PrefHashStoreTransaction::CLEARED: | 32 case PrefHashStoreTransaction::CLEARED: |
| 34 return TPIncident::CLEARED; | 33 return TPIncident::CLEARED; |
| 35 case PrefHashStoreTransaction::CHANGED: | 34 case PrefHashStoreTransaction::CHANGED: |
| 36 return TPIncident::CHANGED; | 35 return TPIncident::CHANGED; |
| 37 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: | 36 case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE: |
| 38 return TPIncident::UNTRUSTED_UNKNOWN_VALUE; | 37 return TPIncident::UNTRUSTED_UNKNOWN_VALUE; |
| 39 default: | 38 default: |
| 40 switch (external_validation_value_state) { | 39 return TPIncident::UNKNOWN; |
| 41 case PrefHashStoreTransaction::CLEARED: | |
| 42 return TPIncident::BYPASS_CLEARED; | |
| 43 case PrefHashStoreTransaction::CHANGED: | |
| 44 return TPIncident::BYPASS_CHANGED; | |
| 45 default: | |
| 46 return TPIncident::UNKNOWN; | |
| 47 } | |
| 48 } | 40 } |
| 49 } | 41 } |
| 50 | 42 |
| 51 } // namespace | 43 } // namespace |
| 52 | 44 |
| 53 PreferenceValidationDelegate::PreferenceValidationDelegate( | 45 PreferenceValidationDelegate::PreferenceValidationDelegate( |
| 54 Profile* profile, | 46 Profile* profile, |
| 55 std::unique_ptr<IncidentReceiver> incident_receiver) | 47 std::unique_ptr<IncidentReceiver> incident_receiver) |
| 56 : profile_(profile), incident_receiver_(std::move(incident_receiver)) {} | 48 : profile_(profile), incident_receiver_(std::move(incident_receiver)) {} |
| 57 | 49 |
| 58 PreferenceValidationDelegate::~PreferenceValidationDelegate() { | 50 PreferenceValidationDelegate::~PreferenceValidationDelegate() { |
| 59 } | 51 } |
| 60 | 52 |
| 61 void PreferenceValidationDelegate::OnAtomicPreferenceValidation( | 53 void PreferenceValidationDelegate::OnAtomicPreferenceValidation( |
| 62 const std::string& pref_path, | 54 const std::string& pref_path, |
| 63 const base::Value* value, | 55 const base::Value* value, |
| 64 PrefHashStoreTransaction::ValueState value_state, | 56 PrefHashStoreTransaction::ValueState value_state, |
| 65 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 66 bool is_personal) { | 57 bool is_personal) { |
| 67 TPIncident_ValueState proto_value_state = | 58 TPIncident_ValueState proto_value_state = MapValueState(value_state); |
| 68 MapValueState(value_state, external_validation_value_state); | |
| 69 if (proto_value_state != TPIncident::UNKNOWN) { | 59 if (proto_value_state != TPIncident::UNKNOWN) { |
| 70 std::unique_ptr<TPIncident> incident( | 60 std::unique_ptr<TPIncident> incident( |
| 71 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident()); | 61 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident()); |
| 72 incident->set_path(pref_path); | 62 incident->set_path(pref_path); |
| 73 if (!value || | 63 if (!value || |
| 74 (!value->GetAsString(incident->mutable_atomic_value()) && | 64 (!value->GetAsString(incident->mutable_atomic_value()) && |
| 75 !base::JSONWriter::Write(*value, incident->mutable_atomic_value()))) { | 65 !base::JSONWriter::Write(*value, incident->mutable_atomic_value()))) { |
| 76 incident->clear_atomic_value(); | 66 incident->clear_atomic_value(); |
| 77 } | 67 } |
| 78 incident->set_value_state(proto_value_state); | 68 incident->set_value_state(proto_value_state); |
| 79 incident_receiver_->AddIncidentForProfile( | 69 incident_receiver_->AddIncidentForProfile( |
| 80 profile_, base::MakeUnique<TrackedPreferenceIncident>( | 70 profile_, base::MakeUnique<TrackedPreferenceIncident>( |
| 81 std::move(incident), is_personal)); | 71 std::move(incident), is_personal)); |
| 82 } | 72 } |
| 83 } | 73 } |
| 84 | 74 |
| 85 void PreferenceValidationDelegate::OnSplitPreferenceValidation( | 75 void PreferenceValidationDelegate::OnSplitPreferenceValidation( |
| 86 const std::string& pref_path, | 76 const std::string& pref_path, |
| 87 const base::DictionaryValue* /* dict_value */, | 77 const base::DictionaryValue* /* dict_value */, |
| 88 const std::vector<std::string>& invalid_keys, | 78 const std::vector<std::string>& invalid_keys, |
| 89 PrefHashStoreTransaction::ValueState value_state, | 79 PrefHashStoreTransaction::ValueState value_state, |
| 90 PrefHashStoreTransaction::ValueState external_validation_value_state, | |
| 91 bool is_personal) { | 80 bool is_personal) { |
| 92 TPIncident_ValueState proto_value_state = | 81 TPIncident_ValueState proto_value_state = MapValueState(value_state); |
| 93 MapValueState(value_state, external_validation_value_state); | |
| 94 if (proto_value_state != TPIncident::UNKNOWN) { | 82 if (proto_value_state != TPIncident::UNKNOWN) { |
| 95 std::unique_ptr<ClientIncidentReport_IncidentData_TrackedPreferenceIncident> | 83 std::unique_ptr<ClientIncidentReport_IncidentData_TrackedPreferenceIncident> |
| 96 incident( | 84 incident( |
| 97 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident()); | 85 new ClientIncidentReport_IncidentData_TrackedPreferenceIncident()); |
| 98 incident->set_path(pref_path); | 86 incident->set_path(pref_path); |
| 99 for (std::vector<std::string>::const_iterator scan(invalid_keys.begin()); | 87 for (std::vector<std::string>::const_iterator scan(invalid_keys.begin()); |
| 100 scan != invalid_keys.end(); | 88 scan != invalid_keys.end(); |
| 101 ++scan) { | 89 ++scan) { |
| 102 incident->add_split_key(*scan); | 90 incident->add_split_key(*scan); |
| 103 } | 91 } |
| 104 incident->set_value_state(proto_value_state); | 92 incident->set_value_state(proto_value_state); |
| 105 incident_receiver_->AddIncidentForProfile( | 93 incident_receiver_->AddIncidentForProfile( |
| 106 profile_, base::MakeUnique<TrackedPreferenceIncident>( | 94 profile_, base::MakeUnique<TrackedPreferenceIncident>( |
| 107 std::move(incident), is_personal)); | 95 std::move(incident), is_personal)); |
| 108 } | 96 } |
| 109 } | 97 } |
| 110 | 98 |
| 111 } // namespace safe_browsing | 99 } // namespace safe_browsing |
| OLD | NEW |