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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// Copyright 2015 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 "chrome/browser/safe_browsing/incident_reporting/state_store.h"
#include <utility>
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/incident_reporting/incident.h"
#include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
namespace safe_browsing {
// StateStore::Transaction -----------------------------------------------------
StateStore::Transaction::Transaction(StateStore* store) : store_(store) {
#if DCHECK_IS_ON()
DCHECK(!store_->has_transaction_);
store_->has_transaction_ = true;
#endif
}
StateStore::Transaction::~Transaction() {
#if DCHECK_IS_ON()
store_->has_transaction_ = false;
#endif
if (pref_update_)
platform_state_store::Store(store_->profile_, store_->incidents_sent_);
}
void StateStore::Transaction::MarkAsReported(IncidentType type,
const std::string& key,
IncidentDigest digest) {
std::string type_string(base::IntToString(static_cast<int32_t>(type)));
base::DictionaryValue* incidents_sent = GetPrefDict();
base::DictionaryValue* type_dict = nullptr;
if (!incidents_sent->GetDictionaryWithoutPathExpansion(type_string,
&type_dict)) {
type_dict = new base::DictionaryValue();
incidents_sent->SetWithoutPathExpansion(type_string, type_dict);
}
type_dict->SetStringWithoutPathExpansion(key, base::UintToString(digest));
}
void StateStore::Transaction::Clear(IncidentType type, const std::string& key) {
// Nothing to do if the pref dict does not exist.
if (!store_->incidents_sent_)
return;
// Use the read-only view on the preference to figure out if there is a value
// to remove before committing to making a change since any use of GetPrefDict
// will result in a full serialize-and-write operation on the preferences
// store.
std::string type_string(base::IntToString(static_cast<int32_t>(type)));
const base::DictionaryValue* const_type_dict = nullptr;
if (store_->incidents_sent_->GetDictionaryWithoutPathExpansion(
type_string, &const_type_dict) &&
const_type_dict->GetWithoutPathExpansion(key, nullptr)) {
base::DictionaryValue* type_dict = nullptr;
GetPrefDict()->GetDictionaryWithoutPathExpansion(type_string, &type_dict);
type_dict->RemoveWithoutPathExpansion(key, nullptr);
}
}
void StateStore::Transaction::ClearForType(IncidentType type) {
// Nothing to do if the pref dict does not exist.
if (!store_->incidents_sent_)
return;
// Use the read-only view on the preference to figure out if there is a value
// to remove before committing to making a change since any use of GetPrefDict
// will result in a full serialize-and-write operation on the preferences
// store.
std::string type_string(base::IntToString(static_cast<int32_t>(type)));
const base::DictionaryValue* type_dict = nullptr;
if (store_->incidents_sent_->GetDictionaryWithoutPathExpansion(type_string,
&type_dict)) {
GetPrefDict()->RemoveWithoutPathExpansion(type_string, nullptr);
}
}
void StateStore::Transaction::ClearAll() {
// Clear the preference if it exists and contains any values.
if (store_->incidents_sent_ && !store_->incidents_sent_->empty())
GetPrefDict()->Clear();
}
base::DictionaryValue* StateStore::Transaction::GetPrefDict() {
if (!pref_update_) {
pref_update_.reset(new DictionaryPrefUpdate(
store_->profile_->GetPrefs(), prefs::kSafeBrowsingIncidentsSent));
// Getting the dict will cause it to be created if it doesn't exist.
// Unconditionally refresh the store's read-only view on the preference so
// that it will always be correct.
store_->incidents_sent_ = pref_update_->Get();
}
return pref_update_->Get();
}
void StateStore::Transaction::ReplacePrefDict(
std::unique_ptr<base::DictionaryValue> pref_dict) {
GetPrefDict()->Swap(pref_dict.get());
}
// StateStore ------------------------------------------------------------------
StateStore::StateStore(Profile* profile)
: profile_(profile),
incidents_sent_(nullptr)
#if DCHECK_IS_ON()
,
has_transaction_(false)
#endif
{
// Cache a read-only view of the preference.
const base::Value* value =
profile_->GetPrefs()->GetUserPrefValue(prefs::kSafeBrowsingIncidentsSent);
if (value)
value->GetAsDictionary(&incidents_sent_);
// Apply the platform data.
Transaction transaction(this);
std::unique_ptr<base::DictionaryValue> value_dict(
platform_state_store::Load(profile_));
InitializationResult state_store_init_result = PSS_MATCHES;
if (!value_dict) {
state_store_init_result = PSS_NULL;
} else if (value_dict->empty()) {
if (incidents_sent_ && !incidents_sent_->empty())
state_store_init_result = PSS_EMPTY;
transaction.ClearAll();
} else if (!incidents_sent_ || !incidents_sent_->Equals(value_dict.get())) {
state_store_init_result = PSS_DIFFERS;
transaction.ReplacePrefDict(std::move(value_dict));
}
UMA_HISTOGRAM_ENUMERATION("SBIRS.StateStoreInit", state_store_init_result,
NUM_INITIALIZATION_RESULTS);
if (incidents_sent_)
CleanLegacyValues(&transaction);
}
StateStore::~StateStore() {
#if DCHECK_IS_ON()
DCHECK(!has_transaction_);
#endif
}
bool StateStore::HasBeenReported(IncidentType type,
const std::string& key,
IncidentDigest digest) {
const base::DictionaryValue* type_dict = nullptr;
std::string digest_string;
return (incidents_sent_ &&
incidents_sent_->GetDictionaryWithoutPathExpansion(
base::IntToString(static_cast<int32_t>(type)), &type_dict) &&
type_dict->GetStringWithoutPathExpansion(key, &digest_string) &&
digest_string == base::UintToString(digest));
}
void StateStore::CleanLegacyValues(Transaction* transaction) {
static const IncidentType kLegacyTypes[] = {
// TODO(grt): remove in M44 (crbug.com/451173).
IncidentType::OMNIBOX_INTERACTION,
};
for (IncidentType type : kLegacyTypes)
transaction->ClearForType(type);
}
} // namespace safe_browsing
|