Commit 9d1b0158 authored by gab@chromium.org's avatar gab@chromium.org

Refactor SetClientID such that metrics rather than crash backs up the client id

in Google Update settings.

Consequentially, the backed up client_id now keeps its dashes and crash_keys
strips them at runtime rather than when backing it up
(https://codereview.chromium.org/372473004/ will add support for stripped
client_id backups for some time).

Also rename a lot of methods involved in setting the client id; having all of
them named "SetClientID" makes this series of calls very hard to follow!

BUG=391338
TBR=thestig

Review URL: https://codereview.chromium.org/365133005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282093 0039d316-1c4b-4281-b951-d872f2087c98
parent b8da79f3
......@@ -80,8 +80,9 @@ ChromeBreakpadClient::ChromeBreakpadClient() {}
ChromeBreakpadClient::~ChromeBreakpadClient() {}
void ChromeBreakpadClient::SetClientID(const std::string& client_id) {
crash_keys::SetClientID(client_id);
void ChromeBreakpadClient::SetBreakpadClientIdFromGUID(
const std::string& client_guid) {
crash_keys::SetCrashClientIdFromGUID(client_guid);
}
#if defined(OS_WIN)
......
......@@ -17,7 +17,8 @@ class ChromeBreakpadClient : public breakpad::BreakpadClient {
virtual ~ChromeBreakpadClient();
// breakpad::BreakpadClient implementation.
virtual void SetClientID(const std::string& client_id) OVERRIDE;
virtual void SetBreakpadClientIdFromGUID(
const std::string& client_guid) OVERRIDE;
#if defined(OS_WIN)
virtual bool GetAlternativeCrashDumpLocation(base::FilePath* crash_dir)
OVERRIDE;
......
......@@ -76,7 +76,7 @@ void GetPreReadPopulationAndGroup(double* population, double* group) {
// By default we use the metrics id for the user as stable pseudo-random
// input to a hash.
std::string metrics_id;
GoogleUpdateSettings::GetMetricsId(&metrics_id);
GoogleUpdateSettings::LoadMetricsClientId(&metrics_id);
// If this user has not metrics id, we fall back to a purely random value
// per browser session.
......
......@@ -1470,7 +1470,7 @@ void ChromeContentBrowserClient::AppendExtraCommandLineSwitches(
#if defined(OS_POSIX)
if (breakpad::IsCrashReporterEnabled()) {
std::string enable_crash_reporter;
GoogleUpdateSettings::GetMetricsId(&enable_crash_reporter);
GoogleUpdateSettings::LoadMetricsClientId(&enable_crash_reporter);
command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
enable_crash_reporter);
}
......
......@@ -14,8 +14,9 @@
namespace {
base::LazyInstance<std::string>::Leaky g_posix_guid = LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<base::Lock>::Leaky g_posix_guid_lock =
base::LazyInstance<std::string>::Leaky g_posix_client_id =
LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<base::Lock>::Leaky g_posix_client_id_lock =
LAZY_INSTANCE_INITIALIZER;
// File name used in the user data dir to indicate consent.
......@@ -28,11 +29,15 @@ bool GoogleUpdateSettings::GetCollectStatsConsent() {
base::FilePath consent_file;
PathService::Get(chrome::DIR_USER_DATA, &consent_file);
consent_file = consent_file.Append(kConsentToSendStats);
if (!base::DirectoryExists(consent_file.DirName()))
return false;
std::string tmp_guid;
bool consented = base::ReadFileToString(consent_file, &tmp_guid);
if (consented) {
base::AutoLock lock(g_posix_guid_lock.Get());
g_posix_guid.Get().assign(tmp_guid);
base::AutoLock lock(g_posix_client_id_lock.Get());
g_posix_client_id.Get().assign(tmp_guid);
}
return consented;
}
......@@ -44,44 +49,40 @@ bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
if (!base::DirectoryExists(consent_dir))
return false;
base::AutoLock lock(g_posix_guid_lock.Get());
base::AutoLock lock(g_posix_client_id_lock.Get());
base::FilePath consent_file = consent_dir.AppendASCII(kConsentToSendStats);
if (consented) {
if ((!base::PathExists(consent_file)) ||
(base::PathExists(consent_file) && !g_posix_guid.Get().empty())) {
const char* c_str = g_posix_guid.Get().c_str();
int size = g_posix_guid.Get().size();
(base::PathExists(consent_file) && !g_posix_client_id.Get().empty())) {
const char* c_str = g_posix_client_id.Get().c_str();
int size = g_posix_client_id.Get().size();
return base::WriteFile(consent_file, c_str, size) == size;
}
} else {
g_posix_guid.Get().clear();
g_posix_client_id.Get().clear();
return base::DeleteFile(consent_file, false);
}
return true;
}
// static
bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
base::AutoLock lock(g_posix_guid_lock.Get());
*metrics_id = g_posix_guid.Get();
bool GoogleUpdateSettings::LoadMetricsClientId(std::string* metrics_id) {
base::AutoLock lock(g_posix_client_id_lock.Get());
*metrics_id = g_posix_client_id.Get();
return true;
}
// static
bool GoogleUpdateSettings::SetMetricsId(const std::string& client_id) {
bool GoogleUpdateSettings::StoreMetricsClientId(const std::string& client_id) {
// Make sure that user has consented to send crashes.
base::FilePath consent_dir;
PathService::Get(chrome::DIR_USER_DATA, &consent_dir);
if (!base::DirectoryExists(consent_dir) ||
!GoogleUpdateSettings::GetCollectStatsConsent()) {
if (!GoogleUpdateSettings::GetCollectStatsConsent())
return false;
}
{
// Since user has consented, write the metrics id to the file.
base::AutoLock lock(g_posix_guid_lock.Get());
g_posix_guid.Get() = client_id;
base::AutoLock lock(g_posix_client_id_lock.Get());
g_posix_client_id.Get() = client_id;
}
return GoogleUpdateSettings::SetCollectStatsConsent(true);
}
......
......@@ -35,6 +35,7 @@
#include "chrome/common/crash_keys.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "chrome/installer/util/google_update_settings.h"
#include "components/metrics/metrics_service.h"
#include "components/metrics/net/net_metrics_log_uploader.h"
#include "content/public/browser/browser_thread.h"
......@@ -161,8 +162,12 @@ void ChromeMetricsServiceClient::RegisterPrefs(PrefRegistrySimple* registry) {
#endif // defined(ENABLE_PLUGINS)
}
void ChromeMetricsServiceClient::SetClientID(const std::string& client_id) {
crash_keys::SetClientID(client_id);
void ChromeMetricsServiceClient::SetMetricsClientId(
const std::string& client_id) {
crash_keys::SetCrashClientIdFromGUID(client_id);
// Store a backup of the client id in Google Update settings.
GoogleUpdateSettings::StoreMetricsClientId(client_id);
}
bool ChromeMetricsServiceClient::IsOffTheRecordSessionActive() {
......
......@@ -52,7 +52,7 @@ class ChromeMetricsServiceClient
static void RegisterPrefs(PrefRegistrySimple* registry);
// metrics::MetricsServiceClient:
virtual void SetClientID(const std::string& client_id) OVERRIDE;
virtual void SetMetricsClientId(const std::string& client_id) OVERRIDE;
virtual bool IsOffTheRecordSessionActive() OVERRIDE;
virtual std::string GetApplicationLocale() OVERRIDE;
virtual bool GetBrand(std::string* brand_code) OVERRIDE;
......
......@@ -65,12 +65,13 @@ void Init() {
base::debug::SetCrashKeyReportingFunctions(
&SetCrashKeyValueTrampoline, &ClearCrashKeyValueTrampoline);
// This would be handled by BreakpadClient::SetClientID(), but because of the
// aforementioned issue, crash keys aren't ready yet at the time of Breakpad
// initialization.
std::string client_id;
if (GoogleUpdateSettings::GetMetricsId(&client_id))
base::debug::SetCrashKeyValue(crash_keys::kClientID, client_id);
// This would be handled by BreakpadClient::SetCrashClientIdFromGUID(), but
// because of the aforementioned issue, crash keys aren't ready yet at the
// time of Breakpad initialization, load the client id backed up in Google
// Update settings instead.
std::string client_guid;
if (GoogleUpdateSettings::LoadMetricsClientId(&client_guid))
crash_keys::SetCrashClientIdFromGUID(client_guid);
}
} // namespace child_process_logging
......@@ -11,7 +11,6 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/installer/util/google_update_settings.h"
#if defined(OS_MACOSX)
#include "breakpad/src/common/simple_string_dictionary.h"
......@@ -56,7 +55,7 @@ COMPILE_ASSERT(kMediumSize <= kSingleChunkLength,
mac_has_medium_size_crash_key_chunks);
#endif
const char kClientID[] = "guid";
const char kClientId[] = "guid";
const char kChannel[] = "channel";
......@@ -119,7 +118,7 @@ size_t RegisterChromeCrashKeys() {
// The following keys may be chunked by the underlying crash logging system,
// but ultimately constitute a single key-value pair.
base::debug::CrashKey fixed_keys[] = {
{ kClientID, kSmallSize },
{ kClientId, kSmallSize },
{ kChannel, kSmallSize },
{ kActiveURL, kLargeSize },
{ kNumSwitches, kSmallSize },
......@@ -225,15 +224,14 @@ size_t RegisterChromeCrashKeys() {
kSingleChunkLength);
}
void SetClientID(const std::string& client_id) {
std::string guid(client_id);
void SetCrashClientIdFromGUID(const std::string& client_guid) {
std::string stripped_guid(client_guid);
// Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
ReplaceSubstringsAfterOffset(&guid, 0, "-", "");
if (guid.empty())
ReplaceSubstringsAfterOffset(&stripped_guid, 0, "-", "");
if (stripped_guid.empty())
return;
base::debug::SetCrashKeyValue(kClientID, guid);
GoogleUpdateSettings::SetMetricsId(guid);
base::debug::SetCrashKeyValue(kClientId, stripped_guid);
}
static bool IsBoringSwitch(const std::string& flag) {
......
......@@ -21,8 +21,11 @@ namespace crash_keys {
// reporting server. Returns the size of the union of all keys.
size_t RegisterChromeCrashKeys();
// Sets the GUID by which this crash reporting client can be identified.
void SetClientID(const std::string& client_id);
// Sets the ID (based on |client_guid| which may either be a full GUID or a
// GUID that was already stripped from its dashes -- in either cases this method
// will strip remaining dashes before setting the crash key) by which this crash
// reporting client can be identified.
void SetCrashClientIdFromGUID(const std::string& client_guid);
// Sets the kSwitch and kNumSwitches keys based on the given |command_line|.
void SetSwitchesFromCommandLine(const base::CommandLine* command_line);
......
......@@ -296,14 +296,14 @@ bool GoogleUpdateSettings::SetCollectStatsConsentAtLevel(bool system_install,
return (result == ERROR_SUCCESS);
}
bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
bool GoogleUpdateSettings::LoadMetricsClientId(std::string* metrics_id) {
base::string16 metrics_id16;
bool rv = ReadGoogleUpdateStrKey(google_update::kRegMetricsId, &metrics_id16);
*metrics_id = base::UTF16ToUTF8(metrics_id16);
return rv;
}
bool GoogleUpdateSettings::SetMetricsId(const std::string& metrics_id) {
bool GoogleUpdateSettings::StoreMetricsClientId(const std::string& metrics_id) {
base::string16 metrics_id16 = base::UTF8ToUTF16(metrics_id);
return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id16);
}
......
......@@ -86,12 +86,12 @@ class GoogleUpdateSettings {
bool consented);
#endif
// Returns the metrics id set in the registry (that can be used in crash
// reports). If none found, returns empty string.
static bool GetMetricsId(std::string* metrics_id);
// Returns the metrics client id backed up in the registry. If none found,
// returns empty string.
static bool LoadMetricsClientId(std::string* metrics_id);
// Sets the metrics id to be used in crash reports.
static bool SetMetricsId(const std::string& metrics_id);
// Stores a backup of the metrics client id in the registry.
static bool StoreMetricsClientId(const std::string& metrics_id);
// Sets the machine-wide EULA consented flag required on OEM installs.
// Returns false if the setting could not be recorded.
......
......@@ -27,7 +27,8 @@ BreakpadClient* GetBreakpadClient() {
BreakpadClient::BreakpadClient() {}
BreakpadClient::~BreakpadClient() {}
void BreakpadClient::SetClientID(const std::string& client_id) {
void BreakpadClient::SetBreakpadClientIdFromGUID(
const std::string& client_guid) {
}
#if defined(OS_WIN)
......
......@@ -46,7 +46,9 @@ class BreakpadClient {
// Sets the Breakpad client ID, which is a unique identifier for the client
// that is sending crash reports. After it is set, it should not be changed.
virtual void SetClientID(const std::string& client_id);
// |client_guid| may either be a full GUID or a GUID that was already stripped
// from its dashes.
virtual void SetBreakpadClientIdFromGUID(const std::string& client_guid);
#if defined(OS_WIN)
// Returns true if an alternative location to store the minidump files was
......
......@@ -206,7 +206,7 @@ void SetClientIdFromCommandLine(const CommandLine& command_line) {
// Get the guid from the command line switch.
std::string switch_value =
command_line.GetSwitchValueASCII(switches::kEnableCrashReporter);
GetBreakpadClient()->SetClientID(switch_value);
GetBreakpadClient()->SetBreakpadClientIdFromGUID(switch_value);
}
// MIME substrings.
......
......@@ -238,9 +238,9 @@ void InitCrashReporter(const std::string& process_type) {
if (!is_browser) {
// Get the guid from the command line switch.
std::string guid =
std::string client_guid =
command_line->GetSwitchValueASCII(switches::kEnableCrashReporter);
GetBreakpadClient()->SetClientID(guid);
GetBreakpadClient()->SetBreakpadClientIdFromGUID(client_guid);
}
logging::SetLogMessageHandler(&FatalMessageHandler);
......
......@@ -404,7 +404,7 @@ void MetricsService::EnableRecording() {
recording_active_ = true;
state_manager_->ForceClientIdCreation();
client_->SetClientID(state_manager_->client_id());
client_->SetMetricsClientId(state_manager_->client_id());
if (!log_manager_.current_log())
OpenNewLog();
......
......@@ -22,9 +22,9 @@ class MetricsServiceClient {
public:
virtual ~MetricsServiceClient() {}
// Register the client id with other services (e.g. crash reporting), called
// Registers the client id with other services (e.g. crash reporting), called
// when metrics recording gets enabled.
virtual void SetClientID(const std::string& client_id) = 0;
virtual void SetMetricsClientId(const std::string& client_id) = 0;
// Whether there's an "off the record" (aka "Incognito") session active.
virtual bool IsOffTheRecordSessionActive() = 0;
......
......@@ -19,7 +19,8 @@ TestMetricsServiceClient::TestMetricsServiceClient()
TestMetricsServiceClient::~TestMetricsServiceClient() {
}
void TestMetricsServiceClient::SetClientID(const std::string& client_id) {
void TestMetricsServiceClient::SetMetricsClientId(
const std::string& client_id) {
client_id_ = client_id;
}
......
......@@ -21,7 +21,7 @@ class TestMetricsServiceClient : public MetricsServiceClient {
virtual ~TestMetricsServiceClient();
// MetricsServiceClient:
virtual void SetClientID(const std::string& client_id) OVERRIDE;
virtual void SetMetricsClientId(const std::string& client_id) OVERRIDE;
virtual bool IsOffTheRecordSessionActive() OVERRIDE;
virtual std::string GetApplicationLocale() OVERRIDE;
virtual bool GetBrand(std::string* brand_code) OVERRIDE;
......
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