summaryrefslogtreecommitdiff
path: root/chromium/components/user_prefs/tracked/tracked_split_preference.cc
blob: 2094d97b71ec6b8ee4dea42dd030ffdedf9ec44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/user_prefs/tracked/tracked_split_preference.h"

#include <vector>

#include "base/logging.h"
#include "base/values.h"
#include "components/user_prefs/tracked/pref_hash_store_transaction.h"
#include "components/user_prefs/tracked/tracked_preference_validation_delegate.h"

TrackedSplitPreference::TrackedSplitPreference(
    const std::string& pref_path,
    size_t reporting_id,
    size_t reporting_ids_count,
    PrefHashFilter::EnforcementLevel enforcement_level,
    PrefHashFilter::ValueType value_type,
    TrackedPreferenceValidationDelegate* delegate)
    : pref_path_(pref_path),
      helper_(pref_path,
              reporting_id,
              reporting_ids_count,
              enforcement_level,
              value_type),
      delegate_(delegate) {
}

TrackedPreferenceType TrackedSplitPreference::GetType() const {
  return TrackedPreferenceType::SPLIT;
}

void TrackedSplitPreference::OnNewValue(
    const base::Value* value,
    PrefHashStoreTransaction* transaction) const {
  const base::DictionaryValue* dict_value = NULL;
  if (value && !value->GetAsDictionary(&dict_value)) {
    NOTREACHED();
    return;
  }
  transaction->StoreSplitHash(pref_path_, dict_value);
}

bool TrackedSplitPreference::EnforceAndReport(
    base::DictionaryValue* pref_store_contents,
    PrefHashStoreTransaction* transaction,
    PrefHashStoreTransaction* external_validation_transaction) const {
  base::DictionaryValue* dict_value = NULL;
  if (!pref_store_contents->GetDictionary(pref_path_, &dict_value) &&
      pref_store_contents->Get(pref_path_, NULL)) {
    // There should be a dictionary or nothing at |pref_path_|.
    NOTREACHED();
    return false;
  }

  std::vector<std::string> invalid_keys;
  PrefHashStoreTransaction::ValueState value_state =
      transaction->CheckSplitValue(pref_path_, dict_value, &invalid_keys);

  if (value_state == PrefHashStoreTransaction::CHANGED)
    helper_.ReportSplitPreferenceChangedCount(invalid_keys.size());

  helper_.ReportValidationResult(value_state, transaction->GetStoreUMASuffix());

  PrefHashStoreTransaction::ValueState external_validation_value_state =
      PrefHashStoreTransaction::UNSUPPORTED;
  std::vector<std::string> external_validation_invalid_keys;
  if (external_validation_transaction) {
    external_validation_value_state =
        external_validation_transaction->CheckSplitValue(
            pref_path_, dict_value, &external_validation_invalid_keys);
    helper_.ReportValidationResult(
        external_validation_value_state,
        external_validation_transaction->GetStoreUMASuffix());
  }

  if (delegate_) {
    delegate_->OnSplitPreferenceValidation(
        pref_path_, dict_value, invalid_keys, external_validation_invalid_keys,
        value_state, external_validation_value_state, helper_.IsPersonal());
  }
  TrackedPreferenceHelper::ResetAction reset_action =
      helper_.GetAction(value_state);
  helper_.ReportAction(reset_action);

  bool was_reset = false;
  if (reset_action == TrackedPreferenceHelper::DO_RESET) {
    if (value_state == PrefHashStoreTransaction::CHANGED) {
      DCHECK(!invalid_keys.empty());

      for (std::vector<std::string>::const_iterator it = invalid_keys.begin();
           it != invalid_keys.end(); ++it) {
        dict_value->Remove(*it, NULL);
      }
    } else {
      pref_store_contents->RemovePath(pref_path_, NULL);
    }
    was_reset = true;
  }

  if (value_state != PrefHashStoreTransaction::UNCHANGED) {
    // Store the hash for the new value (whether it was reset or not).
    const base::DictionaryValue* new_dict_value = NULL;
    pref_store_contents->GetDictionary(pref_path_, &new_dict_value);
    transaction->StoreSplitHash(pref_path_, new_dict_value);
  }

  // Update MACs in the external store if there is one and there either was a
  // reset or external validation failed.
  if (external_validation_transaction &&
      (was_reset ||
       external_validation_value_state !=
           PrefHashStoreTransaction::UNCHANGED)) {
    const base::DictionaryValue* new_dict_value = nullptr;
    pref_store_contents->GetDictionary(pref_path_, &new_dict_value);
    external_validation_transaction->StoreSplitHash(pref_path_, new_dict_value);
  }

  return was_reset;
}