This source file includes following definitions.
- HashRawId
- LogMachineIdState
- weak_ptr_factory_
- CheckForClonedInstall
- SaveMachineId
- RegisterPrefs
#include "chrome/browser/metrics/cloned_install_detector.h"
#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/metrics/cloned_install_detector.h"
#include "chrome/browser/metrics/machine_id_provider.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/metrics_hashes.h"
#include "content/public/browser/browser_thread.h"
namespace metrics {
namespace {
uint32 HashRawId(const std::string& value) {
uint64 hash = metrics::HashMetricName(value);
return hash & ((1 << 24) - 1);
}
enum MachineIdState {
ID_GENERATION_FAILED,
ID_NO_STORED_VALUE,
ID_CHANGED,
ID_UNCHANGED,
ID_ENUM_SIZE
};
void LogMachineIdState(MachineIdState state) {
UMA_HISTOGRAM_ENUMERATION("UMA.MachineIdState", state, ID_ENUM_SIZE);
}
}
ClonedInstallDetector::ClonedInstallDetector(MachineIdProvider* raw_id_provider)
: raw_id_provider_(raw_id_provider),
weak_ptr_factory_(this) {}
ClonedInstallDetector::~ClonedInstallDetector() {}
void ClonedInstallDetector::CheckForClonedInstall(PrefService* local_state) {
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(&metrics::MachineIdProvider::GetMachineId,
raw_id_provider_),
base::Bind(&metrics::ClonedInstallDetector::SaveMachineId,
weak_ptr_factory_.GetWeakPtr(),
local_state));
}
void ClonedInstallDetector::SaveMachineId(
PrefService* local_state,
std::string raw_id) {
if (raw_id.empty()) {
LogMachineIdState(ID_GENERATION_FAILED);
local_state->ClearPref(prefs::kMetricsMachineId);
return;
}
int hashed_id = HashRawId(raw_id);
MachineIdState id_state = ID_NO_STORED_VALUE;
if (local_state->HasPrefPath(prefs::kMetricsMachineId)) {
if (local_state->GetInteger(prefs::kMetricsMachineId) != hashed_id) {
id_state = ID_CHANGED;
local_state->SetBoolean(prefs::kMetricsResetIds, true);
} else {
id_state = ID_UNCHANGED;
}
}
LogMachineIdState(id_state);
local_state->SetInteger(prefs::kMetricsMachineId, hashed_id);
}
void ClonedInstallDetector::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(prefs::kMetricsMachineId, 0);
}
}