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