Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autofill/risk/fingerprint.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/cpu.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/prefs/public/pref_service_base.h" | |
| 12 #include "base/string_split.h" | |
| 13 #include "base/sys_info.h" | |
| 14 #include "base/time.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "chrome/browser/autofill/risk/proto/fingerprint.pb.h" | |
| 17 #include "chrome/browser/browser_process.h" | |
| 18 #include "chrome/browser/prefs/pref_service.h" | |
| 19 #include "chrome/common/chrome_version_info.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "content/public/browser/content_browser_client.h" | |
| 22 #include "content/public/browser/font_list_async.h" | |
| 23 #include "content/public/browser/gpu_data_manager.h" | |
| 24 #include "content/public/browser/gpu_data_manager_observer.h" | |
| 25 #include "content/public/browser/plugin_service.h" | |
| 26 #include "content/public/common/content_client.h" | |
| 27 #include "content/public/common/gpu_info.h" | |
| 28 #include "ui/gfx/rect.h" | |
| 29 #include "ui/gfx/screen.h" | |
| 30 #include "webkit/plugins/webplugininfo.h" | |
| 31 | |
| 32 namespace autofill { | |
| 33 namespace risk { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 const int32 kFingerprinterVersion = 1; | |
| 38 | |
| 39 // Returns the delta between |timestamp| and the Unix epoch, in milliseconds. | |
| 40 int64 GetMillisecondsSinceEpoch(const base::Time& timestamp) { | |
|
Evan Stade
2012/12/21 22:37:08
I think it would be preferable to have these time
Ilya Sherman
2012/12/22 01:22:00
I think the code was slightly cleaner with this he
| |
| 41 return (timestamp - base::Time::UnixEpoch()).InMilliseconds(); | |
| 42 } | |
| 43 | |
| 44 // Returns the time at which Chrome was installed, in milliseconds since the | |
| 45 // epoch. | |
| 46 int64 GetInstallTimeMs() { | |
| 47 // TODO(isherman): If we keep this implementation, the metric should probably | |
| 48 // be renamed; or at least the comments for it should be updated. | |
| 49 PrefServiceBase* prefs = g_browser_process->local_state(); | |
| 50 base::Time install_time = base::Time::FromTimeT( | |
| 51 prefs->GetInt64(prefs::kUninstallMetricsInstallDate)); | |
| 52 // The install date should always be available and initialized. | |
| 53 DCHECK(!install_time.is_null()); | |
| 54 return GetMillisecondsSinceEpoch(install_time); | |
| 55 } | |
| 56 | |
| 57 // Returns the delta between the local timezone and UTC, in milliseconds. | |
| 58 int64 GetTimezoneOffsetMs() { | |
| 59 base::Time now = base::Time::Now(); | |
| 60 | |
| 61 base::Time::Exploded utc; | |
| 62 now.UTCExplode(&utc); | |
| 63 | |
| 64 base::Time::Exploded local; | |
| 65 now.LocalExplode(&local); | |
| 66 | |
| 67 base::TimeDelta delta = | |
| 68 base::Time::FromUTCExploded(local) - base::Time::FromUTCExploded(utc); | |
|
Evan Stade
2012/12/21 22:37:08
can't you just use |now| for base::Time::FromUTCEx
Ilya Sherman
2012/12/22 01:22:00
Done.
| |
| 69 return delta.InMilliseconds(); | |
| 70 } | |
| 71 | |
| 72 // Returns the concatenation of the operating system name and version, e.g. | |
| 73 // "Mac OS X 10.6.8". | |
| 74 std::string GetOperatingSystemVersion() { | |
| 75 return | |
|
Evan Stade
2012/12/21 22:37:08
nit: looks slightly weird to me, consider:
retu
Ilya Sherman
2012/12/22 01:22:00
Done.
| |
| 76 base::SysInfo::OperatingSystemName() + " " + | |
| 77 base::SysInfo::OperatingSystemVersion(); | |
| 78 } | |
| 79 | |
| 80 // Adds the list of |fonts| to the |fingerprint|. | |
| 81 void AddFontsToFingerprint(const base::ListValue& fonts, | |
| 82 Fingerprint_MachineCharacteristics* fingerprint) { | |
| 83 for (base::ListValue::const_iterator it = fonts.begin(); | |
| 84 it != fonts.end(); ++it) { | |
| 85 // Each item in the list is a two-element list such that the first element | |
| 86 // is the font family and the second is the font name. | |
| 87 const base::ListValue* font_description; | |
| 88 bool success = (*it)->GetAsList(&font_description); | |
| 89 DCHECK(success); | |
| 90 | |
| 91 std::string font_name; | |
| 92 success = font_description->GetString(1, &font_name); | |
| 93 DCHECK(success); | |
| 94 | |
| 95 fingerprint->add_font(font_name); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 // Adds the list of |plugins| to the |fingerprint|. | |
| 100 void AddPluginsToFingerprint(const std::vector<webkit::WebPluginInfo>& plugins, | |
| 101 Fingerprint_MachineCharacteristics* fingerprint) { | |
| 102 for (std::vector<webkit::WebPluginInfo>::const_iterator it = plugins.begin(); | |
| 103 it != plugins.end(); ++it) { | |
| 104 Fingerprint_MachineCharacteristics_Plugin* plugin = | |
| 105 fingerprint->add_plugin(); | |
| 106 plugin->set_name(UTF16ToUTF8(it->name)); | |
| 107 plugin->set_description(UTF16ToUTF8(it->desc)); | |
| 108 for (std::vector<webkit::WebPluginMimeType>::const_iterator mime_type = | |
| 109 it->mime_types.begin(); | |
| 110 mime_type != it->mime_types.end(); ++mime_type) { | |
| 111 plugin->add_mime_type(mime_type->mime_type); | |
| 112 } | |
| 113 plugin->set_version(UTF16ToUTF8(it->version)); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 // Adds the list of HTTP accept languages in |prefs| to the |fingerprint|. | |
| 118 void AddAcceptLanguagesToFingerprint( | |
| 119 const std::string& accept_languages_str, | |
| 120 Fingerprint_MachineCharacteristics* fingerprint) { | |
| 121 std::vector<std::string> accept_languages; | |
| 122 base::SplitString(accept_languages_str, ',', &accept_languages); | |
| 123 for (std::vector<std::string>::const_iterator it = accept_languages.begin(); | |
| 124 it != accept_languages.end(); ++it) { | |
| 125 fingerprint->add_requested_language(*it); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 // Writes the number of screens and the primary display's screen size into the | |
| 130 // |fingerprint|. | |
| 131 void AddScreenInfoToFingerprint( | |
| 132 Fingerprint_MachineCharacteristics* fingerprint) { | |
| 133 // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312 | |
| 134 fingerprint->set_screen_count( | |
| 135 gfx::Screen::GetNativeScreen()->GetNumDisplays()); | |
| 136 | |
| 137 gfx::Size screen_size = | |
| 138 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel(); | |
| 139 fingerprint->mutable_screen_size()->set_width(screen_size.width()); | |
| 140 fingerprint->mutable_screen_size()->set_height(screen_size.height()); | |
| 141 } | |
| 142 | |
| 143 // Writes info about the machine's CPU into the |fingerprint|. | |
| 144 void AddCpuInfoToFingerprint(Fingerprint_MachineCharacteristics* fingerprint) { | |
| 145 base::CPU cpu; | |
| 146 fingerprint->mutable_cpu()->set_vendor_name(cpu.vendor_name()); | |
| 147 fingerprint->mutable_cpu()->set_brand(cpu.cpu_brand()); | |
| 148 } | |
| 149 | |
| 150 // Writes info about the machine's GPU into the |fingerprint|. | |
| 151 void AddGpuInfoToFingerprint(Fingerprint_MachineCharacteristics* fingerprint) { | |
| 152 const content::GPUInfo& gpu_info = | |
| 153 content::GpuDataManager::GetInstance()->GetGPUInfo(); | |
| 154 DCHECK(gpu_info.finalized); | |
| 155 | |
| 156 Fingerprint_MachineCharacteristics_Graphics* graphics = | |
| 157 fingerprint->mutable_graphics_card(); | |
| 158 graphics->set_vendor_id(gpu_info.gpu.vendor_id); | |
| 159 graphics->set_device_id(gpu_info.gpu.device_id); | |
| 160 graphics->set_driver_version(gpu_info.driver_version); | |
| 161 graphics->set_driver_date(gpu_info.driver_date); | |
| 162 | |
| 163 Fingerprint_MachineCharacteristics_Graphics_PerformanceStatistics* | |
| 164 gpu_performance = graphics->mutable_performance_statistics(); | |
| 165 gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics); | |
| 166 gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming); | |
| 167 gpu_performance->set_overall_score(gpu_info.performance_stats.overall); | |
| 168 } | |
| 169 | |
| 170 // Waits for all asynchronous data required for the fingerprint to be loaded; | |
| 171 // then fills out the fingerprint. | |
| 172 class FingerprintDataLoader : public content::GpuDataManagerObserver { | |
| 173 public: | |
| 174 FingerprintDataLoader( | |
| 175 int64 gaia_id, | |
| 176 const gfx::Rect& window_bounds, | |
| 177 const gfx::Rect& content_bounds, | |
| 178 const PrefServiceBase& prefs, | |
| 179 const base::Callback<void(scoped_ptr<Fingerprint>)>& callback); | |
| 180 | |
| 181 private: | |
| 182 virtual ~FingerprintDataLoader(); | |
| 183 | |
| 184 // content::GpuDataManagerObserver: | |
| 185 virtual void OnGpuInfoUpdate() OVERRIDE; | |
| 186 virtual void OnVideoMemoryUsageStatsUpdate( | |
| 187 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats) | |
| 188 OVERRIDE; | |
| 189 | |
| 190 // Callbacks for asynchronously loaded data. | |
| 191 void OnGotFonts(scoped_ptr<base::ListValue> fonts); | |
| 192 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); | |
| 193 | |
| 194 // If all of the asynchronous data has been loaded, calls |callback_| with | |
| 195 // the fingerprint data. | |
| 196 void MaybeFillFingerprint(); | |
| 197 | |
| 198 // Calls |callback_| with the fingerprint data. | |
| 199 void FillFingerprint(); | |
| 200 | |
| 201 // The GPU data provider. | |
| 202 content::GpuDataManager* const gpu_data_manager_; | |
| 203 | |
| 204 // Data that will be passed on to the next loading phase. | |
| 205 const int64 gaia_id_; | |
| 206 const gfx::Rect window_bounds_; | |
| 207 const gfx::Rect content_bounds_; | |
| 208 const std::string charset_; | |
| 209 const std::string accept_languages_; | |
| 210 | |
| 211 // Data that will be loaded asynchronously. | |
| 212 scoped_ptr<base::ListValue> fonts_; | |
| 213 std::vector<webkit::WebPluginInfo> plugins_; | |
| 214 bool has_loaded_plugins_; | |
| 215 | |
| 216 // The callback that will be called once all the data is available. | |
| 217 base::Callback<void(scoped_ptr<Fingerprint>)> callback_; | |
|
Evan Stade
2012/12/21 22:37:08
I think it's worth creating a typedef for this cal
Ilya Sherman
2012/12/22 01:22:00
Why? I'm not fond of typedefs, and I think this t
Evan Stade
2012/12/22 02:17:34
I would put this in the header file:
typedef base
| |
| 218 | |
| 219 DISALLOW_COPY_AND_ASSIGN(FingerprintDataLoader); | |
| 220 }; | |
| 221 | |
| 222 FingerprintDataLoader::FingerprintDataLoader( | |
| 223 int64 gaia_id, | |
| 224 const gfx::Rect& window_bounds, | |
| 225 const gfx::Rect& content_bounds, | |
| 226 const PrefServiceBase& prefs, | |
| 227 const base::Callback<void(scoped_ptr<Fingerprint>)>& callback) | |
| 228 : gpu_data_manager_(content::GpuDataManager::GetInstance()), | |
| 229 gaia_id_(gaia_id), | |
| 230 window_bounds_(window_bounds), | |
| 231 content_bounds_(content_bounds), | |
| 232 charset_(prefs.GetString(prefs::kDefaultCharset)), | |
| 233 accept_languages_(prefs.GetString(prefs::kAcceptLanguages)), | |
| 234 has_loaded_plugins_(false), | |
| 235 callback_(callback) { | |
| 236 // Load GPU data if needed. | |
| 237 if (!gpu_data_manager_->IsCompleteGpuInfoAvailable()) { | |
| 238 gpu_data_manager_->AddObserver(this); | |
| 239 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded(); | |
| 240 } | |
| 241 | |
| 242 // Load plugin data. | |
| 243 content::PluginService::GetInstance()->GetPlugins( | |
| 244 base::Bind(&FingerprintDataLoader::OnGotPlugins, base::Unretained(this))); | |
| 245 | |
| 246 // Load font data. | |
| 247 content::GetFontListAsync( | |
| 248 base::Bind(&FingerprintDataLoader::OnGotFonts, base::Unretained(this))); | |
| 249 } | |
| 250 | |
| 251 FingerprintDataLoader::~FingerprintDataLoader() { | |
| 252 } | |
| 253 | |
| 254 void FingerprintDataLoader::OnGpuInfoUpdate() { | |
| 255 if (!gpu_data_manager_->IsCompleteGpuInfoAvailable()) | |
| 256 return; | |
| 257 | |
| 258 gpu_data_manager_->RemoveObserver(this); | |
| 259 MaybeFillFingerprint(); | |
| 260 } | |
| 261 | |
| 262 void FingerprintDataLoader::OnVideoMemoryUsageStatsUpdate( | |
| 263 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats) { | |
| 264 } | |
| 265 | |
| 266 void FingerprintDataLoader::OnGotFonts(scoped_ptr<base::ListValue> fonts) { | |
| 267 DCHECK(!fonts_); | |
| 268 fonts_.reset(fonts.release()); | |
| 269 MaybeFillFingerprint(); | |
| 270 } | |
| 271 | |
| 272 void FingerprintDataLoader::OnGotPlugins( | |
| 273 const std::vector<webkit::WebPluginInfo>& plugins) { | |
| 274 DCHECK(!has_loaded_plugins_); | |
| 275 has_loaded_plugins_ = true; | |
| 276 plugins_ = plugins; | |
| 277 MaybeFillFingerprint(); | |
| 278 } | |
| 279 | |
| 280 void FingerprintDataLoader::MaybeFillFingerprint() { | |
| 281 // If all of the data has been loaded, fill the fingerprint and clean up. | |
| 282 if (gpu_data_manager_->IsCompleteGpuInfoAvailable() && | |
| 283 fonts_ && | |
| 284 has_loaded_plugins_) { | |
| 285 FillFingerprint(); | |
| 286 delete this; | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 void FingerprintDataLoader::FillFingerprint() { | |
| 291 scoped_ptr<Fingerprint> fingerprint(new Fingerprint); | |
| 292 Fingerprint_MachineCharacteristics* machine = | |
| 293 fingerprint->mutable_machine_characteristics(); | |
| 294 | |
| 295 machine->set_operating_system_build(GetOperatingSystemVersion()); | |
| 296 machine->set_browser_install_time_ms(GetInstallTimeMs()); | |
| 297 machine->set_utc_offset_ms(GetTimezoneOffsetMs()); | |
| 298 machine->set_browser_language( | |
| 299 content::GetContentClient()->browser()->GetApplicationLocale()); | |
| 300 machine->set_charset(charset_); | |
| 301 machine->set_user_agent(content::GetContentClient()->GetUserAgent()); | |
| 302 machine->set_ram(base::SysInfo::AmountOfPhysicalMemory()); | |
| 303 machine->set_browser_build(chrome::VersionInfo().Version()); | |
| 304 AddFontsToFingerprint(*fonts_.get(), machine); | |
|
Evan Stade
2012/12/21 22:37:08
you don't need the .get() part of *fonts_.get()
Ilya Sherman
2012/12/22 01:22:00
Done.
| |
| 305 AddPluginsToFingerprint(plugins_, machine); | |
| 306 AddAcceptLanguagesToFingerprint(accept_languages_, machine); | |
| 307 AddScreenInfoToFingerprint(machine); | |
| 308 AddCpuInfoToFingerprint(machine); | |
| 309 AddGpuInfoToFingerprint(machine); | |
| 310 | |
| 311 // TODO(isherman): Store the user's screen color depth by refactoring the code | |
| 312 // for RenderWidgetHostImpl::GetWebScreenInfo(). | |
| 313 // TODO(isherman): Store the user's unavailable screen size, likewise by | |
| 314 // fetching the WebScreenInfo that RenderWidgetHostImpl::GetWebScreenInfo() | |
| 315 // provides. | |
| 316 // TODO(isherman): Store the partition size of the hard drives? | |
| 317 | |
| 318 Fingerprint_TransientState* transient_state = | |
| 319 fingerprint->mutable_transient_state(); | |
| 320 Fingerprint_Dimension* inner_window_size = | |
| 321 transient_state->mutable_inner_window_size(); | |
| 322 inner_window_size->set_width(content_bounds_.width()); | |
| 323 inner_window_size->set_height(content_bounds_.height()); | |
| 324 Fingerprint_Dimension* outer_window_size = | |
| 325 transient_state->mutable_outer_window_size(); | |
| 326 outer_window_size->set_width(window_bounds_.width()); | |
| 327 outer_window_size->set_height(window_bounds_.height()); | |
| 328 | |
| 329 // TODO(isherman): Record network performance data, which is theoretically | |
| 330 // available to JS. | |
| 331 | |
| 332 // TODO(isherman): Record user behavior data. | |
| 333 | |
| 334 Fingerprint_Metadata* metadata = fingerprint->mutable_metadata(); | |
| 335 metadata->set_timestamp_ms(GetMillisecondsSinceEpoch(base::Time::Now())); | |
| 336 metadata->set_gaia_id(gaia_id_); | |
| 337 metadata->set_fingerprinter_version(kFingerprinterVersion); | |
| 338 | |
| 339 callback_.Run(fingerprint.Pass()); | |
| 340 } | |
| 341 | |
| 342 } // namespace | |
| 343 | |
| 344 void GetFingerprint( | |
| 345 int64 gaia_id, | |
| 346 const gfx::Rect& window_bounds, | |
| 347 const gfx::Rect& content_bounds, | |
| 348 const PrefServiceBase& prefs, | |
| 349 const base::Callback<void(scoped_ptr<Fingerprint>)>& callback) { | |
| 350 // TODO(isherman): Add a DCHECK that the ToS have been accepted prior to | |
| 351 // calling into this method. | |
| 352 | |
| 353 // Begin loading all of the data that we need to load asynchronously. | |
| 354 // This class is responsible for freeing its own memory. | |
| 355 new FingerprintDataLoader(gaia_id, window_bounds, content_bounds, prefs, | |
| 356 callback); | |
| 357 } | |
| 358 | |
| 359 } // namespace risk | |
| 360 } // namespace autofill | |
| OLD | NEW |