Commit 33cf2a4a authored by adamta's avatar adamta Committed by Commit Bot

Provide hardware model name for ClonedInstallDetector

When running on a non-windows platform, MachineIdProvider will provide the hardware model name.

Bug: 981408
Change-Id: I1edae511b1e79cb2360630c1253c4e7209aeec3b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2088531
Commit-Queue: Adam Trudeau-Arcaro <adamta@google.com>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747763}
parent 68335744
...@@ -74,7 +74,7 @@ jumbo_static_library("metrics") { ...@@ -74,7 +74,7 @@ jumbo_static_library("metrics") {
"log_decoder.h", "log_decoder.h",
"log_store.h", "log_store.h",
"machine_id_provider.h", "machine_id_provider.h",
"machine_id_provider_stub.cc", "machine_id_provider_nonwin.cc",
"machine_id_provider_win.cc", "machine_id_provider_win.cc",
"metrics_log.cc", "metrics_log.cc",
"metrics_log.h", "metrics_log.h",
...@@ -165,7 +165,7 @@ jumbo_static_library("metrics") { ...@@ -165,7 +165,7 @@ jumbo_static_library("metrics") {
} }
if (is_win) { if (is_win) {
sources -= [ "machine_id_provider_stub.cc" ] sources -= [ "machine_id_provider_nonwin.cc" ]
deps += [ "//components/browser_watcher:stability_client" ] deps += [ "//components/browser_watcher:stability_client" ]
libs = [ "wevtapi.lib" ] libs = [ "wevtapi.lib" ]
} }
...@@ -404,7 +404,7 @@ source_set("unit_tests") { ...@@ -404,7 +404,7 @@ source_set("unit_tests") {
"field_trials_provider_unittest.cc", "field_trials_provider_unittest.cc",
"file_metrics_provider_unittest.cc", "file_metrics_provider_unittest.cc",
"histogram_encoder_unittest.cc", "histogram_encoder_unittest.cc",
"machine_id_provider_win_unittest.cc", "machine_id_provider_nonwin_unittest.cc",
"metrics_log_manager_unittest.cc", "metrics_log_manager_unittest.cc",
"metrics_log_store_unittest.cc", "metrics_log_store_unittest.cc",
"metrics_log_unittest.cc", "metrics_log_unittest.cc",
...@@ -450,6 +450,11 @@ source_set("unit_tests") { ...@@ -450,6 +450,11 @@ source_set("unit_tests") {
"//ui/gfx/geometry", "//ui/gfx/geometry",
] ]
if (is_win) {
sources -= [ "machine_id_provider_nonwin_unittest.cc" ]
sources += [ "machine_id_provider_win_unittest.cc" ]
}
if (is_linux) { if (is_linux) {
sources += [ "serialization/serialization_utils_unittest.cc" ] sources += [ "serialization/serialization_utils_unittest.cc" ]
deps += [ ":serialization" ] deps += [ ":serialization" ]
......
...@@ -55,6 +55,9 @@ ClonedInstallDetector::~ClonedInstallDetector() { ...@@ -55,6 +55,9 @@ ClonedInstallDetector::~ClonedInstallDetector() {
} }
void ClonedInstallDetector::CheckForClonedInstall(PrefService* local_state) { void ClonedInstallDetector::CheckForClonedInstall(PrefService* local_state) {
if (!MachineIdProvider::HasId())
return;
base::ThreadPool::PostTaskAndReplyWithResult( base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, FROM_HERE,
{base::MayBlock(), base::TaskPriority::BEST_EFFORT, {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
......
...@@ -23,8 +23,9 @@ class MachineIdProvider { ...@@ -23,8 +23,9 @@ class MachineIdProvider {
static bool HasId(); static bool HasId();
// Get a string containing machine characteristics, to be used as a machine // Get a string containing machine characteristics, to be used as a machine
// id. The implementation is platform specific, with a default implementation // id. The implementation is split into Windows and non-Windows. The former
// returning an empty string. // returns the drive serial number and the latter returns the hardware
// model name. Should not be called if HasId() returns false.
// The return value should not be stored to disk or transmitted. // The return value should not be stored to disk or transmitted.
static std::string GetMachineId(); static std::string GetMachineId();
......
// Copyright 2014 The Chromium Authors. All rights reserved. // Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/metrics/machine_id_provider.h" #include "components/metrics/machine_id_provider.h"
#include "base/logging.h" #include <stdint.h>
#include "base/system/sys_info.h"
namespace metrics { namespace metrics {
// static // Checks if hardware model name is available.
bool MachineIdProvider::HasId() { bool MachineIdProvider::HasId() {
return false; return !base::SysInfo::HardwareModelName().empty();
} }
// static // On non-windows, the machine id is based on the hardware model name.
// This will suffice as users are unlikely to change to the same machine model.
std::string MachineIdProvider::GetMachineId() { std::string MachineIdProvider::GetMachineId() {
NOTREACHED(); // Gets hardware model name. (e.g. 'Macbook Pro 16,1', 'iPhone 9,3')
return std::string(); std::string hardware_model_name = base::SysInfo::HardwareModelName();
}
// This function should not be called if hardware model name is unavailable.
DCHECK(!hardware_model_name.empty());
return hardware_model_name;
}
} // namespace metrics } // namespace metrics
// Copyright 2020 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/metrics/machine_id_provider.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace metrics {
TEST(MachineIdProviderNonWinTest, GetId) {
const bool has_machine_name = !base::SysInfo::HardwareModelName().empty();
#if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_IOS)
DCHECK(has_machine_name);
#endif
// Should only return a machine ID if the hardware model name is available.
if (has_machine_name) {
const std::string id1 = MachineIdProvider::GetMachineId();
EXPECT_TRUE(MachineIdProvider::HasId());
EXPECT_NE(std::string(), id1);
const std::string id2 = MachineIdProvider::GetMachineId();
EXPECT_EQ(id1, id2);
} else {
EXPECT_FALSE(MachineIdProvider::HasId());
}
}
} // namespace metrics
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace metrics { namespace metrics {
TEST(MachineIdProviderTest, GetId) { TEST(MachineIdProviderWinTest, GetId) {
EXPECT_TRUE(MachineIdProvider::HasId()); EXPECT_TRUE(MachineIdProvider::HasId());
const std::string id1 = MachineIdProvider::GetMachineId(); const std::string id1 = MachineIdProvider::GetMachineId();
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "components/metrics/cloned_install_detector.h" #include "components/metrics/cloned_install_detector.h"
#include "components/metrics/enabled_state_provider.h" #include "components/metrics/enabled_state_provider.h"
#include "components/metrics/entropy_state.h" #include "components/metrics/entropy_state.h"
#include "components/metrics/machine_id_provider.h"
#include "components/metrics/metrics_log.h" #include "components/metrics/metrics_log.h"
#include "components/metrics/metrics_pref_names.h" #include "components/metrics/metrics_pref_names.h"
#include "components/metrics/metrics_provider.h" #include "components/metrics/metrics_provider.h"
...@@ -292,9 +291,6 @@ void MetricsStateManager::ForceClientIdCreation() { ...@@ -292,9 +291,6 @@ void MetricsStateManager::ForceClientIdCreation() {
} }
void MetricsStateManager::CheckForClonedInstall() { void MetricsStateManager::CheckForClonedInstall() {
if (!MachineIdProvider::HasId())
return;
cloned_install_detector_.CheckForClonedInstall(local_state_); cloned_install_detector_.CheckForClonedInstall(local_state_);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment