Home | History | Annotate | Download | only in safe_browsing
      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/safe_browsing/preference_validation_delegate.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/json/json_writer.h"
     12 #include "chrome/browser/prefs/pref_hash_store_transaction.h"
     13 #include "chrome/browser/prefs/tracked/tracked_preference_helper.h"
     14 #include "chrome/common/safe_browsing/csd.pb.h"
     15 
     16 namespace safe_browsing {
     17 
     18 namespace {
     19 
     20 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident TPIncident;
     21 typedef ClientIncidentReport_IncidentData_TrackedPreferenceIncident_ValueState
     22     TPIncident_ValueState;
     23 
     24 // Maps a PrefHashStoreTransaction::ValueState to a
     25 // TrackedPreferenceIncident::ValueState.
     26 TPIncident_ValueState MapValueState(
     27     PrefHashStoreTransaction::ValueState value_state) {
     28   switch (value_state) {
     29     case PrefHashStoreTransaction::CLEARED:
     30       return TPIncident::CLEARED;
     31     case PrefHashStoreTransaction::WEAK_LEGACY:
     32       return TPIncident::WEAK_LEGACY;
     33     case PrefHashStoreTransaction::CHANGED:
     34       return TPIncident::CHANGED;
     35     case PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE:
     36       return TPIncident::UNTRUSTED_UNKNOWN_VALUE;
     37     default:
     38       return TPIncident::UNKNOWN;
     39   }
     40 }
     41 
     42 }  // namespace
     43 
     44 PreferenceValidationDelegate::PreferenceValidationDelegate(
     45     const AddIncidentCallback& add_incident)
     46     : add_incident_(add_incident) {
     47 }
     48 
     49 PreferenceValidationDelegate::~PreferenceValidationDelegate() {
     50 }
     51 
     52 void PreferenceValidationDelegate::OnAtomicPreferenceValidation(
     53     const std::string& pref_path,
     54     const base::Value* value,
     55     PrefHashStoreTransaction::ValueState value_state,
     56     TrackedPreferenceHelper::ResetAction /* reset_action */) {
     57   TPIncident_ValueState proto_value_state = MapValueState(value_state);
     58   if (proto_value_state != TPIncident::UNKNOWN) {
     59     scoped_ptr<ClientIncidentReport_IncidentData> incident_data(
     60         new ClientIncidentReport_IncidentData());
     61     TPIncident* incident = incident_data->mutable_tracked_preference();
     62     incident->set_path(pref_path);
     63     if (!value ||
     64         (!value->GetAsString(incident->mutable_atomic_value()) &&
     65          !base::JSONWriter::Write(value, incident->mutable_atomic_value()))) {
     66       incident->clear_atomic_value();
     67     }
     68     incident->set_value_state(proto_value_state);
     69     add_incident_.Run(incident_data.Pass());
     70   }
     71 }
     72 
     73 void PreferenceValidationDelegate::OnSplitPreferenceValidation(
     74     const std::string& pref_path,
     75     const base::DictionaryValue* /* dict_value */,
     76     const std::vector<std::string>& invalid_keys,
     77     PrefHashStoreTransaction::ValueState value_state,
     78     TrackedPreferenceHelper::ResetAction /* reset_action */) {
     79   TPIncident_ValueState proto_value_state = MapValueState(value_state);
     80   if (proto_value_state != TPIncident::UNKNOWN) {
     81     scoped_ptr<ClientIncidentReport_IncidentData> incident_data(
     82         new ClientIncidentReport_IncidentData());
     83     TPIncident* incident = incident_data->mutable_tracked_preference();
     84     incident->set_path(pref_path);
     85     for (std::vector<std::string>::const_iterator scan(invalid_keys.begin());
     86          scan != invalid_keys.end();
     87          ++scan) {
     88       incident->add_split_key(*scan);
     89     }
     90     incident->set_value_state(proto_value_state);
     91     add_incident_.Run(incident_data.Pass());
     92   }
     93 }
     94 
     95 }  // namespace safe_browsing
     96