1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include <utility>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/autofill/risk_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/signin_promo_util.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
#include "chrome/browser/ui/autofill/create_card_unmask_prompt_view.h"
#include "chrome/browser/ui/autofill/credit_card_scanner_controller.h"
#include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/web_data_service_factory.h"
#include "chrome/common/url_constants.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/ui/card_unmask_prompt_view.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/password_manager/content/browser/content_password_manager_driver.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/profile_identity_provider.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "components/signin/core/browser/signin_metrics.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/ssl_status.h"
#include "ui/gfx/geometry/rect.h"
#if defined(OS_ANDROID)
#include "base/android/context_utils.h"
#include "chrome/browser/android/chrome_application.h"
#include "chrome/browser/android/signin/signin_promo_util_android.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/android/autofill/autofill_logger_android.h"
#include "chrome/browser/ui/android/infobars/autofill_credit_card_filling_infobar.h"
#include "components/autofill/core/browser/autofill_credit_card_filling_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/autofill_save_card_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/autofill_save_card_infobar_mobile.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/android/content_view_core.h"
#else // !OS_ANDROID
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "components/zoom/zoom_controller.h"
#endif
DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::ChromeAutofillClient);
namespace autofill {
ChromeAutofillClient::ChromeAutofillClient(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
unmask_controller_(
user_prefs::UserPrefs::Get(web_contents->GetBrowserContext()),
Profile::FromBrowserContext(web_contents->GetBrowserContext())
->IsOffTheRecord()) {
DCHECK(web_contents);
#if !defined(OS_ANDROID)
// Since ZoomController is also a WebContentsObserver, we need to be careful
// about disconnecting from it since the relative order of destruction of
// WebContentsObservers is not guaranteed. ZoomController silently clears
// its ZoomObserver list during WebContentsDestroyed() so there's no need
// to explicitly remove ourselves on destruction.
zoom::ZoomController* zoom_controller =
zoom::ZoomController::FromWebContents(web_contents);
// There may not always be a ZoomController, e.g. in tests.
if (zoom_controller)
zoom_controller->AddObserver(this);
#endif
}
ChromeAutofillClient::~ChromeAutofillClient() {
// NOTE: It is too late to clean up the autofill popup; that cleanup process
// requires that the WebContents instance still be valid and it is not at
// this point (in particular, the WebContentsImpl destructor has already
// finished running and we are now in the base class destructor).
DCHECK(!popup_controller_);
}
PersonalDataManager* ChromeAutofillClient::GetPersonalDataManager() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return PersonalDataManagerFactory::GetForProfile(
profile->GetOriginalProfile());
}
scoped_refptr<AutofillWebDataService> ChromeAutofillClient::GetDatabase() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return WebDataServiceFactory::GetAutofillWebDataForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS);
}
PrefService* ChromeAutofillClient::GetPrefs() {
return Profile::FromBrowserContext(web_contents()->GetBrowserContext())
->GetPrefs();
}
syncer::SyncService* ChromeAutofillClient::GetSyncService() {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
return ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
}
IdentityProvider* ChromeAutofillClient::GetIdentityProvider() {
if (!identity_provider_) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext())
->GetOriginalProfile();
base::Closure login_callback;
#if !defined(OS_ANDROID)
login_callback =
LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile);
#endif
identity_provider_.reset(new ProfileIdentityProvider(
SigninManagerFactory::GetForProfile(profile),
ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
login_callback));
}
return identity_provider_.get();
}
rappor::RapporServiceImpl* ChromeAutofillClient::GetRapporServiceImpl() {
return g_browser_process->rappor_service();
}
void ChromeAutofillClient::ShowAutofillSettings() {
#if defined(OS_ANDROID)
chrome::android::ChromeApplication::ShowAutofillSettings();
#else
Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
if (browser)
chrome::ShowSettingsSubPage(browser, chrome::kAutofillSubPage);
#endif // #if defined(OS_ANDROID)
}
void ChromeAutofillClient::ShowUnmaskPrompt(
const CreditCard& card,
UnmaskCardReason reason,
base::WeakPtr<CardUnmaskDelegate> delegate) {
unmask_controller_.ShowPrompt(
CreateCardUnmaskPromptView(&unmask_controller_, web_contents()),
card, reason, delegate);
}
void ChromeAutofillClient::OnUnmaskVerificationResult(
PaymentsRpcResult result) {
unmask_controller_.OnVerificationResult(result);
}
void ChromeAutofillClient::ConfirmSaveCreditCardLocally(
const CreditCard& card,
const base::Closure& callback) {
#if defined(OS_ANDROID)
InfoBarService::FromWebContents(web_contents())
->AddInfoBar(CreateSaveCardInfoBarMobile(
base::MakeUnique<AutofillSaveCardInfoBarDelegateMobile>(
false, card, std::unique_ptr<base::DictionaryValue>(nullptr),
callback)));
#else
// Do lazy initialization of SaveCardBubbleControllerImpl.
autofill::SaveCardBubbleControllerImpl::CreateForWebContents(
web_contents());
autofill::SaveCardBubbleControllerImpl* controller =
autofill::SaveCardBubbleControllerImpl::FromWebContents(web_contents());
controller->ShowBubbleForLocalSave(card, callback);
#endif
}
void ChromeAutofillClient::ConfirmSaveCreditCardToCloud(
const CreditCard& card,
std::unique_ptr<base::DictionaryValue> legal_message,
const base::Closure& callback) {
#if defined(OS_ANDROID)
InfoBarService::FromWebContents(web_contents())
->AddInfoBar(CreateSaveCardInfoBarMobile(
base::MakeUnique<AutofillSaveCardInfoBarDelegateMobile>(
true, card, std::move(legal_message), callback)));
#else
// Do lazy initialization of SaveCardBubbleControllerImpl.
autofill::SaveCardBubbleControllerImpl::CreateForWebContents(web_contents());
autofill::SaveCardBubbleControllerImpl* controller =
autofill::SaveCardBubbleControllerImpl::FromWebContents(web_contents());
controller->ShowBubbleForUpload(card, std::move(legal_message), callback);
#endif
}
void ChromeAutofillClient::ConfirmCreditCardFillAssist(
const CreditCard& card,
const base::Closure& callback) {
#if defined(OS_ANDROID)
auto infobar_delegate =
base::MakeUnique<AutofillCreditCardFillingInfoBarDelegateMobile>(
card, callback);
auto* raw_delegate = infobar_delegate.get();
if (InfoBarService::FromWebContents(web_contents())->AddInfoBar(
base::MakeUnique<AutofillCreditCardFillingInfoBar>(
std::move(infobar_delegate)))) {
raw_delegate->set_was_shown();
}
#endif
}
void ChromeAutofillClient::LoadRiskData(
const base::Callback<void(const std::string&)>& callback) {
::autofill::LoadRiskData(0, web_contents(), callback);
}
bool ChromeAutofillClient::HasCreditCardScanFeature() {
return CreditCardScannerController::HasCreditCardScanFeature();
}
void ChromeAutofillClient::ScanCreditCard(
const CreditCardScanCallback& callback) {
CreditCardScannerController::ScanCreditCard(web_contents(), callback);
}
void ChromeAutofillClient::ShowAutofillPopup(
const gfx::RectF& element_bounds,
base::i18n::TextDirection text_direction,
const std::vector<autofill::Suggestion>& suggestions,
base::WeakPtr<AutofillPopupDelegate> delegate) {
// Convert element_bounds to be in screen space.
gfx::Rect client_area = web_contents()->GetContainerBounds();
gfx::RectF element_bounds_in_screen_space =
element_bounds + client_area.OffsetFromOrigin();
// Will delete or reuse the old |popup_controller_|.
popup_controller_ =
AutofillPopupControllerImpl::GetOrCreate(popup_controller_,
delegate,
web_contents(),
web_contents()->GetNativeView(),
element_bounds_in_screen_space,
text_direction);
popup_controller_->Show(suggestions);
}
void ChromeAutofillClient::UpdateAutofillPopupDataListValues(
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
if (popup_controller_.get())
popup_controller_->UpdateDataListValues(values, labels);
}
void ChromeAutofillClient::HideAutofillPopup() {
if (popup_controller_.get())
popup_controller_->Hide();
// Password generation popups behave in the same fashion and should also
// be hidden.
ChromePasswordManagerClient* password_client =
ChromePasswordManagerClient::FromWebContents(web_contents());
if (password_client)
password_client->HidePasswordGenerationPopup();
}
bool ChromeAutofillClient::IsAutocompleteEnabled() {
// For browser, Autocomplete is always enabled as part of Autofill.
return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
}
void ChromeAutofillClient::MainFrameWasResized(bool width_changed) {
#if defined(OS_ANDROID)
// Ignore virtual keyboard showing and hiding a strip of suggestions.
if (!width_changed)
return;
#endif
HideAutofillPopup();
}
void ChromeAutofillClient::WebContentsDestroyed() {
HideAutofillPopup();
}
void ChromeAutofillClient::OnZoomChanged(
const zoom::ZoomController::ZoomChangedEventData& data) {
HideAutofillPopup();
}
void ChromeAutofillClient::PropagateAutofillPredictions(
content::RenderFrameHost* rfh,
const std::vector<autofill::FormStructure*>& forms) {
password_manager::ContentPasswordManagerDriver* driver =
password_manager::ContentPasswordManagerDriver::GetForRenderFrameHost(
rfh);
if (driver) {
driver->GetPasswordGenerationManager()->DetectFormsEligibleForGeneration(
forms);
driver->GetPasswordManager()->ProcessAutofillPredictions(driver, forms);
}
}
void ChromeAutofillClient::DidFillOrPreviewField(
const base::string16& autofilled_value,
const base::string16& profile_full_name) {
#if defined(OS_ANDROID)
AutofillLoggerAndroid::DidFillOrPreviewField(autofilled_value,
profile_full_name);
#endif // defined(OS_ANDROID)
}
void ChromeAutofillClient::OnFirstUserGestureObserved() {
ContentAutofillDriverFactory* factory =
ContentAutofillDriverFactory::FromWebContents(web_contents());
DCHECK(factory);
for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) {
// No need to notify non-live frames.
// And actually they have no corresponding drivers in the factory's map.
if (!frame->IsRenderFrameLive())
continue;
ContentAutofillDriver* driver = factory->DriverForFrame(frame);
DCHECK(driver);
driver->NotifyFirstUserGestureObservedInTab();
}
}
bool ChromeAutofillClient::IsContextSecure() {
content::SSLStatus ssl_status;
content::NavigationEntry* navigation_entry =
web_contents()->GetController().GetLastCommittedEntry();
if (!navigation_entry)
return false;
ssl_status = navigation_entry->GetSSL();
// Note: If changing the implementation below, also change
// AwAutofillClient::IsContextSecure. See crbug.com/505388
return navigation_entry->GetURL().SchemeIsCryptographic() &&
ssl_status.certificate &&
(!net::IsCertStatusError(ssl_status.cert_status) ||
net::IsCertStatusMinorError(ssl_status.cert_status)) &&
!(ssl_status.content_status &
content::SSLStatus::RAN_INSECURE_CONTENT);
}
bool ChromeAutofillClient::ShouldShowSigninPromo() {
#if !defined(OS_ANDROID)
// Determine if we are in a valid context (on desktop platforms, we could be
// in an app window with no Browser).
if (!chrome::FindBrowserWithWebContents(web_contents()))
return false;
#endif
return signin::ShouldShowPromo(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
}
void ChromeAutofillClient::StartSigninFlow() {
#if defined(OS_ANDROID)
chrome::android::SigninPromoUtilAndroid::StartAccountSigninActivityForPromo(
content::ContentViewCore::FromWebContents(web_contents()),
signin_metrics::AccessPoint::ACCESS_POINT_AUTOFILL_DROPDOWN);
#else
chrome::FindBrowserWithWebContents(web_contents())
->window()
->ShowAvatarBubbleFromAvatarButton(
BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN,
signin::ManageAccountsParams(),
signin_metrics::AccessPoint::ACCESS_POINT_AUTOFILL_DROPDOWN);
#endif
}
void ChromeAutofillClient::ShowHttpNotSecureExplanation() {
#if !defined(OS_ANDROID)
// On desktop platforms, open Page Info, which briefly explains the HTTP
// warning message and provides a link to the Help Center for more details.
Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
if (browser && chrome::ShowWebsiteSettings(browser, web_contents()))
return;
// Otherwise fall through to the section below that opens the URL directly.
#endif
// On Android, where Page Info does not (yet) contain a link to the Help
// Center (https://crbug.com/679532), or in corner cases where Page Info is
// not shown (for example, no navigation entry), just launch the Help topic
// directly.
const GURL kSecurityIndicatorHelpCenterUrl(
"https://support.google.com/chrome/?p=ui_security_indicator");
web_contents()->OpenURL(content::OpenURLParams(
GURL(kSecurityIndicatorHelpCenterUrl), content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK,
false /* is_renderer_initiated */));
}
} // namespace autofill
|