Chromium Code Reviews| Index: chrome/browser/autofill/risk/machine_fingerprint.cc |
| diff --git a/chrome/browser/autofill/risk/machine_fingerprint.cc b/chrome/browser/autofill/risk/machine_fingerprint.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3a7195debb0490e0bb87fc5cd466c638c68427cf |
| --- /dev/null |
| +++ b/chrome/browser/autofill/risk/machine_fingerprint.cc |
| @@ -0,0 +1,225 @@ |
| +// Copyright (c) 2012 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/autofill/risk/machine_fingerprint.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/cpu.h" |
| +#include "base/logging.h" |
| +#include "base/string_split.h" |
| +#include "base/sys_info.h" |
| +#include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/autofill/risk/proto/machine_fingerprint.pb.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/chrome_version_info.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "content/public/browser/content_browser_client.h" |
| +#include "content/public/browser/font_list_async.h" |
| +#include "content/public/browser/gpu_data_manager.h" |
| +#include "content/public/browser/plugin_service.h" |
| +#include "content/public/common/content_client.h" |
| +#include "content/public/common/gpu_info.h" |
| +#include "ui/gfx/rect.h" |
| +#include "ui/gfx/screen.h" |
| +#include "webkit/plugins/webplugininfo.h" |
| + |
| +namespace autofill { |
| +namespace risk { |
| + |
| +namespace { |
| + |
| +const int32 kFingerprinterVersion = 1; |
| + |
| +// Returns the delta between |timestamp| and the Unix epoch, in milliseconds. |
| +int64 GetMillisecondsSinceEpoch(const base::Time& timestamp) { |
|
Albert Bodenhamer
2012/12/18 17:42:22
Should this live in a util module somewhere?
Evan Stade
2012/12/18 22:14:34
yea, this seems similar to ToJsTime
Ilya Sherman
2012/12/19 01:50:08
I'll check with Brett whether he's ok with moving
brettw
2012/12/19 06:18:13
I think the code you have here is short and quite
|
| + return (timestamp - base::Time::UnixEpoch()).InMilliseconds(); |
| +} |
| + |
| +// Returns the time at which Chrome was installed, in milliseconds since the |
| +// epoch. |
| +int64 GetInstallTimeMs() { |
| + // TODO(isherman): If we keep this implementation, the metric should probably |
| + // be renamed; or at least the comments for it should be updated. |
| + PrefService* prefs = g_browser_process->local_state(); |
| + base::Time install_time = base::Time::FromTimeT( |
| + prefs->GetInt64(prefs::kUninstallMetricsInstallDate)); |
| + // The install date should always be available and initialized. |
| + DCHECK(!install_time.is_null()); |
| + return GetMillisecondsSinceEpoch(install_time); |
| +} |
| + |
| +// Returns the delta between the local timezone and UTC, in milliseconds. |
| +int64 GetTimezoneOffsetMs() { |
| + base::Time now = base::Time::Now(); |
| + |
| + base::Time::Exploded utc; |
| + now.UTCExplode(&utc); |
| + |
| + base::Time::Exploded local; |
| + now.LocalExplode(&local); |
| + |
| + base::TimeDelta delta = |
| + base::Time::FromUTCExploded(local) - base::Time::FromUTCExploded(utc); |
| + return delta.InMilliseconds(); |
| +} |
| + |
| +void GetMachineFingerprintImpl( |
| + int64 gaia_id, |
| + const gfx::Rect& window_rect, |
| + const gfx::Rect& content_rect, |
| + scoped_ptr<BrowserNativeFingerprinting> data, |
| + const std::vector<webkit::WebPluginInfo>& plugins, |
| + scoped_ptr<base::ListValue> fonts) { |
| + const PrefService* prefs = ProfileManager::GetLastUsedProfile()->GetPrefs(); |
| + |
| + BrowserNativeFingerprinting_Fingerprint* fingerprint = |
| + data->mutable_fingerprint(); |
| + |
| + // TODO(isherman): Confirm the format for this and other strings in the |
| + // fingerprint. |
| + fingerprint->set_operating_system_build( |
| + base::SysInfo::OperatingSystemVersion()); |
| + |
| + fingerprint->set_browser_install_time_ms(GetInstallTimeMs()); |
| + |
| + for (base::ListValue::const_iterator it = fonts->begin(); |
|
Evan Stade
2012/12/18 22:14:34
dunno if you're looking for this level of review y
Ilya Sherman
2012/12/19 01:50:08
Done.
|
| + it != fonts->end(); ++it) { |
| + // Each item in the list is a two-element list such that the first element |
| + // is the font family and the second is the font name. |
| + const base::ListValue* font_description; |
| + bool success = (*it)->GetAsList(&font_description); |
| + DCHECK(success); |
| + |
| + std::string font_name; |
| + success = font_description->GetString(1, &font_name); |
| + DCHECK(success); |
| + |
| + fingerprint->add_font(font_name); |
| + } |
| + |
| + for (std::vector<webkit::WebPluginInfo>::const_iterator it = plugins.begin(); |
| + it != plugins.end(); ++it) { |
| + BrowserNativeFingerprinting_Fingerprint_Plugin* plugin = |
| + fingerprint->add_plugin(); |
| + plugin->set_name(UTF16ToUTF8(it->name)); |
| + plugin->set_description(UTF16ToUTF8(it->desc)); |
| + for (std::vector<webkit::WebPluginMimeType>::const_iterator mime_type = |
| + it->mime_types.begin(); |
| + mime_type != it->mime_types.end(); ++mime_type) { |
| + plugin->add_mime_type(mime_type->mime_type); |
| + } |
| + plugin->set_version(UTF16ToUTF8(it->version)); |
| + } |
| + |
| + fingerprint->set_utc_offset_ms(GetTimezoneOffsetMs()); |
| + |
| + fingerprint->set_browser_language( |
| + content::GetContentClient()->browser()->GetApplicationLocale()); |
| + |
| + std::vector<std::string> accept_languages; |
| + base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ',', |
| + &accept_languages); |
| + for (std::vector<std::string>::const_iterator it = accept_languages.begin(); |
| + it != accept_languages.end(); ++it) { |
| + fingerprint->add_requested_language(*it); |
| + } |
| + |
| + fingerprint->set_charset(prefs->GetString(prefs::kDefaultCharset)); |
| + |
| + // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312 |
| + fingerprint->set_screen_count( |
| + gfx::Screen::GetNativeScreen()->GetNumDisplays()); |
| + |
| + gfx::Size screen_size = |
| + gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); |
| + fingerprint->mutable_screen_size()->set_width(screen_size.width()); |
| + fingerprint->mutable_screen_size()->set_height(screen_size.height()); |
| + |
| + // TODO(isherman): Store the user's screen color depth by refactoring the code |
| + // for RenderWidgetHostImpl::GetWebScreenInfo(). |
| + // TODO(isherman): Store the user's unavailable screen size, likewise by |
| + // fetching the WebScreenInfo that RenderWidgetHostImpl::GetWebScreenInfo() |
| + // provides. |
| + |
| + fingerprint->set_user_agent(content::GetContentClient()->GetUserAgent()); |
| + |
| + // TODO(isherman): Store the partition size of the hard drives? |
| + |
| + base::CPU cpu; |
| + fingerprint->set_cpu(cpu.vendor_name() + "; " + cpu.cpu_brand()); |
| + |
| + fingerprint->set_ram(base::SysInfo::AmountOfPhysicalMemory()); |
| + |
| + const content::GPUInfo& gpu_info = |
| + content::GpuDataManager::GetInstance()->GetGPUInfo(); |
| + BrowserNativeFingerprinting_Fingerprint_Graphics* graphics = |
| + fingerprint->mutable_graphics_card(); |
| + graphics->set_vendor_id(gpu_info.gpu.vendor_id); |
| + graphics->set_device_id(gpu_info.gpu.device_id); |
| + // TODO(isherman): The driver version might not be available yet. We need to |
| + // register ourselves as an observer, and wait for the info to come back to |
| + // us... |
| + graphics->set_driver_version(gpu_info.driver_version); |
| + graphics->set_driver_date(gpu_info.driver_date); |
| + BrowserNativeFingerprinting_Fingerprint_Graphics_PerformanceStatistics* |
| + gpu_performance = graphics->mutable_performance_statistics(); |
| + gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics); |
| + gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming); |
| + gpu_performance->set_overall_score(gpu_info.performance_stats.overall); |
| + |
| + fingerprint->set_browser_build(chrome::VersionInfo().Version()); |
| + |
| + BrowserNativeFingerprinting_State* transient_state = data->mutable_state(); |
| + BrowserNativeFingerprinting_Dimension* inner_window_size = |
| + transient_state->mutable_inner_window_size(); |
| + inner_window_size->set_width(window_rect.width()); |
| + inner_window_size->set_height(window_rect.height()); |
| + BrowserNativeFingerprinting_Dimension* outer_window_size = |
| + transient_state->mutable_outer_window_size(); |
| + outer_window_size->set_width(content_rect.width()); |
| + outer_window_size->set_height(content_rect.height()); |
| + |
| + // TODO(isherman): Record network performance data, which is theoretically |
| + // available to JS. |
| + |
| + // TODO(isherman): Record user behavior data. |
| + |
| + BrowserNativeFingerprinting_Metadata* metadata = data->mutable_metadata(); |
| + metadata->set_timestamp_ms(GetMillisecondsSinceEpoch(base::Time::Now())); |
| + metadata->set_gaia_id(gaia_id); |
| + metadata->set_fingerprinter_version(kFingerprinterVersion); |
| + |
| + // TODO(isherman): REMOVEME |
|
Albert Bodenhamer
2012/12/18 17:42:22
Remove
Ilya Sherman
2012/12/19 01:50:08
This is useful for debugging; I'll remove once the
|
| + DLOG(WARNING) << "\n" << data->DebugString(); |
| +} |
| + |
| +void OnGotPlugins(int64 gaia_id, |
| + const gfx::Rect& window_rect, |
| + const gfx::Rect& content_rect, |
| + scoped_ptr<BrowserNativeFingerprinting> data, |
| + const std::vector<webkit::WebPluginInfo>& plugins) { |
| + content::GetFontListAsync( |
| + base::Bind(&GetMachineFingerprintImpl, gaia_id, window_rect, content_rect, |
| + base::Passed(&data), plugins)); |
| +} |
| + |
| + |
| +} // namespace |
| + |
| +// TODO(isherman): The variable |data| could use a more descriptive name. Any |
| +// suggestions? |
| +void GetMachineFingerprint(int64 gaia_id, |
| + const gfx::Rect& window_rect, |
| + const gfx::Rect& content_rect, |
| + scoped_ptr<BrowserNativeFingerprinting> data) { |
|
Albert Bodenhamer
2012/12/18 17:42:22
|fingerprint| ?
Ilya Sherman
2012/12/19 01:50:08
Unfortunately, fingerprint is one of the fields wi
Evan Stade
2012/12/19 03:13:27
imo give the field within the data structure a bet
Ilya Sherman
2012/12/20 04:06:49
Done.
|
| + content::PluginService::GetInstance()->GetPlugins( |
| + base::Bind(&OnGotPlugins, gaia_id, window_rect, content_rect, |
| + base::Passed(&data))); |
| +} |
| + |
| +} // namespace risk |
| +} // namespace autofill |