Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / chrome_password_manager_client.cc
1 // Copyright 2014 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/password_manager/chrome_password_manager_client.h"
6
7 #include "base/bind_helpers.h"
8 #include "base/command_line.h"
9 #include "base/memory/singleton.h"
10 #include "base/metrics/histogram.h"
11 #include "chrome/browser/password_manager/password_form_manager.h"
12 #include "chrome/browser/password_manager/password_manager.h"
13 #include "chrome/browser/password_manager/password_manager_util.h"
14 #include "chrome/browser/password_manager/password_store_factory.h"
15 #include "chrome/browser/password_manager/save_password_infobar_delegate.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
23 #include "content/public/browser/web_contents.h"
24
25 #if defined(OS_ANDROID)
26 #include "chrome/browser/android/password_authentication_manager.h"
27 #endif  // OS_ANDROID
28
29 namespace {
30
31 void ReportOsPassword() {
32   password_manager_util::OsPasswordStatus status =
33       password_manager_util::GetOsPasswordStatus();
34
35   UMA_HISTOGRAM_ENUMERATION("PasswordManager.OsPasswordStatus",
36                             status,
37                             password_manager_util::MAX_PASSWORD_STATUS);
38 }
39
40 }  // namespace
41
42 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromePasswordManagerClient);
43
44 ChromePasswordManagerClient::ChromePasswordManagerClient(
45     content::WebContents* web_contents)
46     : web_contents_(web_contents),
47       driver_(web_contents, this),
48       weak_factory_(this) {
49   // Avoid checking OS password until later on in browser startup
50   // since it calls a few Windows APIs.
51   base::MessageLoopProxy::current()->PostDelayedTask(
52       FROM_HERE,
53       base::Bind(&ReportOsPassword),
54       base::TimeDelta::FromSeconds(10));
55 }
56
57 ChromePasswordManagerClient::~ChromePasswordManagerClient() {}
58
59 void ChromePasswordManagerClient::PromptUserToSavePassword(
60     PasswordFormManager* form_to_save) {
61   if (CommandLine::ForCurrentProcess()->HasSwitch(
62           switches::kEnableSavePasswordBubble)) {
63     ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller =
64         ManagePasswordsBubbleUIController::FromWebContents(web_contents_);
65     if (manage_passwords_bubble_ui_controller) {
66       manage_passwords_bubble_ui_controller->OnPasswordSubmitted(form_to_save);
67     } else {
68       delete form_to_save;
69     }
70   } else {
71     std::string uma_histogram_suffix(
72         password_manager_metrics_util::GroupIdToString(
73             password_manager_metrics_util::MonitoredDomainGroupId(
74                 form_to_save->realm(), GetPrefs())));
75     SavePasswordInfoBarDelegate::Create(
76         web_contents_, form_to_save, uma_histogram_suffix);
77   }
78 }
79
80 void ChromePasswordManagerClient::PasswordWasAutofilled(
81     const autofill::PasswordFormMap& best_matches) const {
82   ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller =
83       ManagePasswordsBubbleUIController::FromWebContents(web_contents_);
84   if (manage_passwords_bubble_ui_controller &&
85       CommandLine::ForCurrentProcess()->HasSwitch(
86           switches::kEnableSavePasswordBubble)) {
87     manage_passwords_bubble_ui_controller->OnPasswordAutofilled(best_matches);
88   }
89 }
90
91 void ChromePasswordManagerClient::AuthenticateAutofillAndFillForm(
92       scoped_ptr<autofill::PasswordFormFillData> fill_data) {
93 #if defined(OS_ANDROID)
94   PasswordAuthenticationManager::AuthenticatePasswordAutofill(
95       web_contents_,
96       base::Bind(&ChromePasswordManagerClient::CommitFillPasswordForm,
97                  weak_factory_.GetWeakPtr(),
98                  base::Owned(fill_data.release())));
99 #else
100   // Additional authentication is currently only available for Android, so all
101   // other plaftorms should just fill the password form directly.
102   CommitFillPasswordForm(fill_data.get());
103 #endif  // OS_ANDROID
104 }
105
106 Profile* ChromePasswordManagerClient::GetProfile() {
107   return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
108 }
109
110 PrefService* ChromePasswordManagerClient::GetPrefs() {
111   return GetProfile()->GetPrefs();
112 }
113
114 PasswordStore* ChromePasswordManagerClient::GetPasswordStore() {
115   // Always use EXPLICIT_ACCESS as the password manager checks IsOffTheRecord
116   // itself when it shouldn't access the PasswordStore.
117   // TODO(gcasto): Is is safe to change this to Profile::IMPLICIT_ACCESS?
118   return PasswordStoreFactory::GetForProfile(GetProfile(),
119                                              Profile::EXPLICIT_ACCESS).get();
120 }
121
122 PasswordManagerDriver* ChromePasswordManagerClient::GetDriver() {
123   return &driver_;
124 }
125
126 base::FieldTrial::Probability
127 ChromePasswordManagerClient::GetProbabilityForExperiment(
128     const std::string& experiment_name) {
129   base::FieldTrial::Probability enabled_probability = 0;
130   if (experiment_name == PasswordManager::kOtherPossibleUsernamesExperiment) {
131     switch (chrome::VersionInfo::GetChannel()) {
132       case chrome::VersionInfo::CHANNEL_DEV:
133       case chrome::VersionInfo::CHANNEL_BETA:
134         enabled_probability = 50;
135         break;
136       default:
137         break;
138     }
139   }
140   return enabled_probability;
141 }
142
143 bool ChromePasswordManagerClient::IsPasswordSyncEnabled() {
144   ProfileSyncService* sync_service =
145       ProfileSyncServiceFactory::GetForProfile(GetProfile());
146   if (sync_service && sync_service->HasSyncSetupCompleted())
147     return sync_service->GetActiveDataTypes().Has(syncer::PASSWORDS);
148   return false;
149 }
150
151 // static
152 PasswordGenerationManager*
153 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
154     content::WebContents* contents) {
155   ChromePasswordManagerClient* client =
156       ChromePasswordManagerClient::FromWebContents(contents);
157   if (!client)
158     return NULL;
159   return client->GetDriver()->GetPasswordGenerationManager();
160 }
161
162 // static
163 PasswordManager* ChromePasswordManagerClient::GetManagerFromWebContents(
164     content::WebContents* contents) {
165   ChromePasswordManagerClient* client =
166       ChromePasswordManagerClient::FromWebContents(contents);
167   if (!client)
168     return NULL;
169   return client->GetDriver()->GetPasswordManager();
170 }
171
172 void ChromePasswordManagerClient::CommitFillPasswordForm(
173     autofill::PasswordFormFillData* data) {
174   driver_.FillPasswordForm(*data);
175 }