Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / autofill / risk_util.cc
blob026fdc138951ecf7dbb2a416c37880101ca5dca6
1 // Copyright 2015 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.
5 #include "chrome/browser/autofill/risk_util.h"
7 #include "base/base64.h"
8 #include "base/callback.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/apps/app_window_registry_util.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/common/chrome_content_client.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/autofill/content/browser/risk/fingerprint.h"
18 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
19 #include "components/metrics/metrics_service.h"
20 #include "components/version_info/version_info.h"
21 #include "content/public/browser/web_contents.h"
23 #if !defined(OS_ANDROID)
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "extensions/browser/app_window/app_window.h"
27 #include "extensions/browser/app_window/native_app_window.h"
28 #include "ui/base/base_window.h"
29 #endif
31 namespace autofill {
33 namespace {
35 void PassRiskData(const base::Callback<void(const std::string&)>& callback,
36 scoped_ptr<risk::Fingerprint> fingerprint) {
37 std::string proto_data, risk_data;
38 fingerprint->SerializeToString(&proto_data);
39 base::Base64Encode(proto_data, &risk_data);
40 callback.Run(risk_data);
43 #if !defined(OS_ANDROID)
44 // Returns the containing window for the given |web_contents|. The containing
45 // window might be a browser window for a Chrome tab, or it might be an app
46 // window for a platform app.
47 ui::BaseWindow* GetBaseWindowForWebContents(
48 content::WebContents* web_contents) {
49 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
50 if (browser)
51 return browser->window();
53 gfx::NativeWindow native_window = web_contents->GetTopLevelNativeWindow();
54 extensions::AppWindow* app_window =
55 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
56 native_window);
57 return app_window->GetBaseWindow();
59 #endif
61 } // namespace
63 void LoadRiskData(uint64 obfuscated_gaia_id,
64 content::WebContents* web_contents,
65 const base::Callback<void(const std::string&)>& callback) {
66 // No easy way to get window bounds on Android, and that signal isn't very
67 // useful anyway (given that we're also including the bounds of the web
68 // contents).
69 gfx::Rect window_bounds;
70 #if !defined(OS_ANDROID)
71 window_bounds = GetBaseWindowForWebContents(web_contents)->GetBounds();
72 #endif
74 const PrefService* user_prefs =
75 Profile::FromBrowserContext(web_contents->GetBrowserContext())
76 ->GetPrefs();
77 std::string charset = user_prefs->GetString(::prefs::kDefaultCharset);
78 std::string accept_languages =
79 user_prefs->GetString(::prefs::kAcceptLanguages);
80 base::Time install_time = base::Time::FromTimeT(
81 g_browser_process->metrics_service()->GetInstallDate());
83 risk::GetFingerprint(obfuscated_gaia_id, window_bounds, web_contents,
84 version_info::GetVersionNumber(), charset,
85 accept_languages, install_time,
86 g_browser_process->GetApplicationLocale(),
87 GetUserAgent(), base::Bind(PassRiskData, callback));
90 } // namespace autofill