Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/autofill/risk/machine_fingerprint.cc

Issue 11612017: [Autofill] Add protobuf for Google Wallet risk parameters and a utility function for filling it out. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clean it up! Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/machine_fingerprint.h"
6
7 #include "base/bind.h"
8 #include "base/cpu.h"
9 #include "base/logging.h"
10 #include "base/string_split.h"
11 #include "base/sys_info.h"
12 #include "base/time.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/autofill/risk/proto/machine_fingerprint.pb.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "chrome/common/pref_names.h"
20 #include "content/public/browser/content_browser_client.h"
21 #include "content/public/browser/font_list_async.h"
22 #include "content/public/browser/gpu_data_manager.h"
23 #include "content/public/browser/gpu_data_manager_observer.h"
24 #include "content/public/browser/plugin_service.h"
25 #include "content/public/common/content_client.h"
26 #include "content/public/common/gpu_info.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/gfx/screen.h"
29 #include "webkit/plugins/webplugininfo.h"
30
31 namespace autofill {
32 namespace risk {
33
34 namespace {
35
36 const int32 kFingerprinterVersion = 1;
37
38 // Returns the delta between |timestamp| and the Unix epoch, in milliseconds.
39 int64 GetMillisecondsSinceEpoch(const base::Time& timestamp) {
40 return (timestamp - base::Time::UnixEpoch()).InMilliseconds();
41 }
42
43 // Returns the time at which Chrome was installed, in milliseconds since the
44 // epoch.
45 int64 GetInstallTimeMs() {
46 // TODO(isherman): If we keep this implementation, the metric should probably
47 // be renamed; or at least the comments for it should be updated.
48 PrefService* prefs = g_browser_process->local_state();
49 base::Time install_time = base::Time::FromTimeT(
50 prefs->GetInt64(prefs::kUninstallMetricsInstallDate));
51 // The install date should always be available and initialized.
52 DCHECK(!install_time.is_null());
53 return GetMillisecondsSinceEpoch(install_time);
54 }
55
56 // Returns the delta between the local timezone and UTC, in milliseconds.
57 int64 GetTimezoneOffsetMs() {
58 base::Time now = base::Time::Now();
59
60 base::Time::Exploded utc;
61 now.UTCExplode(&utc);
62
63 base::Time::Exploded local;
64 now.LocalExplode(&local);
65
66 base::TimeDelta delta =
67 base::Time::FromUTCExploded(local) - base::Time::FromUTCExploded(utc);
68 return delta.InMilliseconds();
69 }
70
71 // Returns the concatenation of the operating system name and version, e.g.
72 // "Mac OS X 10.6.8".
73 std::string GetOperatingSystemVersion() {
74 return
75 base::SysInfo::OperatingSystemName() + " " +
76 base::SysInfo::OperatingSystemVersion();
77 }
78
79 // Adds the list of |fonts| to the |fingerprint|.
80 void AddFontsToFingerprint(
81 const base::ListValue& fonts,
82 BrowserNativeFingerprinting_Fingerprint* 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(
101 const std::vector<webkit::WebPluginInfo>& plugins,
102 BrowserNativeFingerprinting_Fingerprint* fingerprint) {
103 for (std::vector<webkit::WebPluginInfo>::const_iterator it = plugins.begin();
104 it != plugins.end(); ++it) {
105 BrowserNativeFingerprinting_Fingerprint_Plugin* plugin =
106 fingerprint->add_plugin();
107 plugin->set_name(UTF16ToUTF8(it->name));
108 plugin->set_description(UTF16ToUTF8(it->desc));
109 for (std::vector<webkit::WebPluginMimeType>::const_iterator mime_type =
110 it->mime_types.begin();
111 mime_type != it->mime_types.end(); ++mime_type) {
112 plugin->add_mime_type(mime_type->mime_type);
113 }
114 plugin->set_version(UTF16ToUTF8(it->version));
115 }
116 }
117
118 // Adds the list of HTTP accept languages in |prefs| to the |fingerprint|.
119 void AddAcceptLanguagesToFingerprint(
120 const PrefService& prefs,
121 BrowserNativeFingerprinting_Fingerprint* fingerprint) {
122 std::vector<std::string> accept_languages;
123 base::SplitString(prefs.GetString(prefs::kAcceptLanguages), ',',
124 &accept_languages);
125 for (std::vector<std::string>::const_iterator it = accept_languages.begin();
126 it != accept_languages.end(); ++it) {
127 fingerprint->add_requested_language(*it);
128 }
129 }
130
131 // Writes the number of screens and the primary display's screen size into the
132 // |fingerprint|.
133 void AddScreenInfoToFingerprint(
134 BrowserNativeFingerprinting_Fingerprint* fingerprint) {
135 // TODO(scottmg): NativeScreen maybe wrong. http://crbug.com/133312
136 fingerprint->set_screen_count(
137 gfx::Screen::GetNativeScreen()->GetNumDisplays());
138
139 gfx::Size screen_size =
140 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
141 fingerprint->mutable_screen_size()->set_width(screen_size.width());
142 fingerprint->mutable_screen_size()->set_height(screen_size.height());
143 }
144
145 // Writes info about the machine's CPU into the |fingerprint|.
146 void AddCpuInfoToFingerprint(
147 BrowserNativeFingerprinting_Fingerprint* fingerprint) {
148 base::CPU cpu;
149 fingerprint->mutable_cpu()->set_vendor_name(cpu.vendor_name());
150 fingerprint->mutable_cpu()->set_vendor_name(cpu.cpu_brand());
151 }
152
153 // Writes info about the machine's GPU into the |fingerprint|.
154 void AddGpuInfoToFingerprint(
155 BrowserNativeFingerprinting_Fingerprint* fingerprint) {
156 const content::GPUInfo& gpu_info =
157 content::GpuDataManager::GetInstance()->GetGPUInfo();
158 DCHECK(gpu_info.finalized);
159
160 BrowserNativeFingerprinting_Fingerprint_Graphics* graphics =
161 fingerprint->mutable_graphics_card();
162 graphics->set_vendor_id(gpu_info.gpu.vendor_id);
163 graphics->set_device_id(gpu_info.gpu.device_id);
164 graphics->set_driver_version(gpu_info.driver_version);
165 graphics->set_driver_date(gpu_info.driver_date);
166
167 BrowserNativeFingerprinting_Fingerprint_Graphics_PerformanceStatistics*
168 gpu_performance = graphics->mutable_performance_statistics();
169 gpu_performance->set_graphics_score(gpu_info.performance_stats.graphics);
170 gpu_performance->set_gaming_score(gpu_info.performance_stats.gaming);
171 gpu_performance->set_overall_score(gpu_info.performance_stats.overall);
172 }
173
174 void GetMachineFingerprintImpl(
175 int64 gaia_id,
176 const gfx::Rect& window_rect,
177 const gfx::Rect& content_rect,
178 scoped_ptr<BrowserNativeFingerprinting> data,
179 scoped_ptr<base::ListValue> fonts,
180 const std::vector<webkit::WebPluginInfo>& plugins) {
181 // TODO(isherman): We should not rely on the assumption that the last used
182 // profile is the correct profile. I need to figure out how to pass in a
183 // pointer to the correct profile such that the pointer is guaranteed not to
184 // be dead by the time I look at it.
Evan Stade 2012/12/19 03:13:27 I think you'd need to listen for PROFILE_DESTROYED
Ilya Sherman 2012/12/20 04:06:49 Done.
185 const PrefService* prefs = ProfileManager::GetLastUsedProfile()->GetPrefs();
186
187 BrowserNativeFingerprinting_Fingerprint* fingerprint =
188 data->mutable_fingerprint();
189
190 fingerprint->set_operating_system_build(GetOperatingSystemVersion());
191 fingerprint->set_browser_install_time_ms(GetInstallTimeMs());
192 fingerprint->set_utc_offset_ms(GetTimezoneOffsetMs());
193 fingerprint->set_browser_language(
194 content::GetContentClient()->browser()->GetApplicationLocale());
195 fingerprint->set_charset(prefs->GetString(prefs::kDefaultCharset));
196 fingerprint->set_user_agent(content::GetContentClient()->GetUserAgent());
197 fingerprint->set_ram(base::SysInfo::AmountOfPhysicalMemory());
198 fingerprint->set_browser_build(chrome::VersionInfo().Version());
199 AddFontsToFingerprint(*fonts.get(), fingerprint);
200 AddPluginsToFingerprint(plugins, fingerprint);
201 AddAcceptLanguagesToFingerprint(*prefs, fingerprint);
202 AddScreenInfoToFingerprint(fingerprint);
203 AddCpuInfoToFingerprint(fingerprint);
204 AddGpuInfoToFingerprint(fingerprint);
205
206 // TODO(isherman): Store the user's screen color depth by refactoring the code
207 // for RenderWidgetHostImpl::GetWebScreenInfo().
208 // TODO(isherman): Store the user's unavailable screen size, likewise by
209 // fetching the WebScreenInfo that RenderWidgetHostImpl::GetWebScreenInfo()
210 // provides.
211 // TODO(isherman): Store the partition size of the hard drives?
212
213 BrowserNativeFingerprinting_State* transient_state = data->mutable_state();
214 BrowserNativeFingerprinting_Dimension* inner_window_size =
215 transient_state->mutable_inner_window_size();
216 inner_window_size->set_width(window_rect.width());
217 inner_window_size->set_height(window_rect.height());
218 BrowserNativeFingerprinting_Dimension* outer_window_size =
219 transient_state->mutable_outer_window_size();
220 outer_window_size->set_width(content_rect.width());
221 outer_window_size->set_height(content_rect.height());
222
223 // TODO(isherman): Record network performance data, which is theoretically
224 // available to JS.
225
226 // TODO(isherman): Record user behavior data.
227
228 BrowserNativeFingerprinting_Metadata* metadata = data->mutable_metadata();
229 metadata->set_timestamp_ms(GetMillisecondsSinceEpoch(base::Time::Now()));
230 metadata->set_gaia_id(gaia_id);
231 metadata->set_fingerprinter_version(kFingerprinterVersion);
232
233 // TODO(isherman): REMOVEME
234 DLOG(WARNING) << "\n" << data->DebugString();
235 }
236
237 void OnGotFonts(int64 gaia_id,
238 const gfx::Rect& window_rect,
239 const gfx::Rect& content_rect,
240 scoped_ptr<BrowserNativeFingerprinting> data,
241 scoped_ptr<base::ListValue> fonts) {
242 content::PluginService::GetInstance()->GetPlugins(
Evan Stade 2012/12/19 03:13:27 it seems like you could create one class that does
Ilya Sherman 2012/12/20 04:06:49 Done.
243 base::Bind(&GetMachineFingerprintImpl, gaia_id, window_rect, content_rect,
244 base::Passed(&data), base::Passed(&fonts)));
245 }
246
247 // Waits for GPU data to be loaded; then fires off a task to continue loading
248 // other data needed for fingerprinting.
249 class GpuDataLoader : public content::GpuDataManagerObserver {
250 public:
251 GpuDataLoader(int64 gaia_id,
252 const gfx::Rect& window_rect,
253 const gfx::Rect& content_rect,
254 scoped_ptr<BrowserNativeFingerprinting> data);
255
256 private:
257 virtual ~GpuDataLoader();
258
259 // content::GpuDataManagerObserver:
260 virtual void OnGpuInfoUpdate() OVERRIDE;
261 virtual void OnVideoMemoryUsageStatsUpdate(
262 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats)
263 OVERRIDE;
264
265 // Continues on to the next loading phase for fingerprinting: loading fonts.
266 void LoadFonts();
267
268 // The GPU data provider.
269 content::GpuDataManager* const gpu_data_manager_;
270
271 // Data that will be passed on to the next loading phase.
Evan Stade 2012/12/19 03:13:27 it would make more sense to me if you passed this
Ilya Sherman 2012/12/20 04:06:49 moot.
272 const int64 gaia_id_;
273 const gfx::Rect window_rect_;
274 const gfx::Rect content_rect_;
275 scoped_ptr<BrowserNativeFingerprinting> data_;
276
277 DISALLOW_COPY_AND_ASSIGN(GpuDataLoader);
278 };
279
280 GpuDataLoader::GpuDataLoader(int64 gaia_id,
281 const gfx::Rect& window_rect,
282 const gfx::Rect& content_rect,
283 scoped_ptr<BrowserNativeFingerprinting> data)
284 : gpu_data_manager_(content::GpuDataManager::GetInstance()),
285 gaia_id_(gaia_id),
286 window_rect_(window_rect),
287 content_rect_(content_rect),
288 data_(data.Pass()) {
289 if (gpu_data_manager_->IsCompleteGpuInfoAvailable()) {
290 LoadFonts();
291 } else {
292 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded();
293 gpu_data_manager_->AddObserver(this);
294 }
295 }
296
297 GpuDataLoader::~GpuDataLoader() {
298 gpu_data_manager_->RemoveObserver(this);
299 }
300
301 void GpuDataLoader::OnGpuInfoUpdate() {
302 if (gpu_data_manager_->IsCompleteGpuInfoAvailable())
303 LoadFonts();
304 }
305
306 void GpuDataLoader::OnVideoMemoryUsageStatsUpdate(
307 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats) {
308 }
309
310 void GpuDataLoader::LoadFonts() {
311 content::GetFontListAsync(
312 base::Bind(&OnGotFonts, gaia_id_, window_rect_, content_rect_,
313 base::Passed(&data_)));
314
315 delete this;
316 }
317
318
319 } // namespace
320
321 // TODO(isherman): The variable |data| could use a more descriptive name. Any
322 // suggestions?
323 void GetMachineFingerprint(int64 gaia_id,
324 const gfx::Rect& window_rect,
325 const gfx::Rect& content_rect,
326 scoped_ptr<BrowserNativeFingerprinting> data) {
327 // Begin loading all of the data that we need to load asynchronously.
328 // This class is responsible for freeing its own memory.
329 new GpuDataLoader(gaia_id, window_rect, content_rect, data.Pass());
330 }
331
332 } // namespace risk
333 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698