Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/passwords/password_generation_agent.h" | 5 #import "ios/chrome/browser/passwords/password_generation_agent.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #import "base/ios/weak_nsobject.h" | |
| 10 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
| 12 #include "base/mac/scoped_nsobject.h" | |
| 13 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/autofill/core/browser/password_generator.h" | 13 #include "components/autofill/core/browser/password_generator.h" |
| 16 #include "components/autofill/core/common/form_data.h" | 14 #include "components/autofill/core/common/form_data.h" |
| 17 #include "components/autofill/core/common/password_form.h" | 15 #include "components/autofill/core/common/password_form.h" |
| 18 #include "components/autofill/core/common/password_generation_util.h" | 16 #include "components/autofill/core/common/password_generation_util.h" |
| 19 #import "components/autofill/ios/browser/js_suggestion_manager.h" | 17 #import "components/autofill/ios/browser/js_suggestion_manager.h" |
| 20 #include "components/password_manager/core/browser/password_manager.h" | 18 #include "components/password_manager/core/browser/password_manager.h" |
| 21 #include "google_apis/gaia/gaia_urls.h" | 19 #include "google_apis/gaia/gaia_urls.h" |
| 22 #import "ios/chrome/browser/autofill/form_input_accessory_view_controller.h" | 20 #import "ios/chrome/browser/autofill/form_input_accessory_view_controller.h" |
| 23 #include "ios/chrome/browser/experimental_flags.h" | 21 #include "ios/chrome/browser/experimental_flags.h" |
| 24 #import "ios/chrome/browser/passwords/js_password_manager.h" | 22 #import "ios/chrome/browser/passwords/js_password_manager.h" |
| 25 #import "ios/chrome/browser/passwords/password_generation_edit_view.h" | 23 #import "ios/chrome/browser/passwords/password_generation_edit_view.h" |
| 26 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" | 24 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 27 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | 25 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 28 #include "ios/web/public/url_scheme_util.h" | 26 #include "ios/web/public/url_scheme_util.h" |
| 29 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 27 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 30 #include "ios/web/public/web_state/web_state.h" | 28 #include "ios/web/public/web_state/web_state.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 33 | 31 |
| 32 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 33 #error "This file requires ARC support." | |
| 34 #endif | |
| 35 | |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 // Target length of generated passwords. | 38 // Target length of generated passwords. |
| 37 const int kGeneratedPasswordLength = 20; | 39 const int kGeneratedPasswordLength = 20; |
| 38 | 40 |
| 39 // The minimum number of text fields that a form needs to be considered as | 41 // The minimum number of text fields that a form needs to be considered as |
| 40 // an account creation form. | 42 // an account creation form. |
| 41 const size_t kMinimumTextFieldsForAccountCreation = 3; | 43 const size_t kMinimumTextFieldsForAccountCreation = 3; |
| 42 | 44 |
| 43 // Returns true if |urls| contains |url|. | 45 // Returns true if |urls| contains |url|. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 // Stores the account creation form we detected on the page. | 128 // Stores the account creation form we detected on the page. |
| 127 std::unique_ptr<autofill::PasswordForm> _possibleAccountCreationForm; | 129 std::unique_ptr<autofill::PasswordForm> _possibleAccountCreationForm; |
| 128 | 130 |
| 129 // Password fields found in |_possibleAccountCreationForm|. | 131 // Password fields found in |_possibleAccountCreationForm|. |
| 130 std::vector<autofill::FormFieldData> _passwordFields; | 132 std::vector<autofill::FormFieldData> _passwordFields; |
| 131 | 133 |
| 132 // The password field that triggers the password generation UI. | 134 // The password field that triggers the password generation UI. |
| 133 std::unique_ptr<autofill::FormFieldData> _passwordGenerationField; | 135 std::unique_ptr<autofill::FormFieldData> _passwordGenerationField; |
| 134 | 136 |
| 135 // Wrapper for suggestion JavaScript. Used for form navigation. | 137 // Wrapper for suggestion JavaScript. Used for form navigation. |
| 136 base::scoped_nsobject<JsSuggestionManager> _JSSuggestionManager; | 138 JsSuggestionManager* _JSSuggestionManager; |
| 137 | 139 |
| 138 // Wrapper for passwords JavaScript. Used for form filling. | 140 // Wrapper for passwords JavaScript. Used for form filling. |
| 139 base::scoped_nsobject<JsPasswordManager> _JSPasswordManager; | 141 JsPasswordManager* _JSPasswordManager; |
| 140 | 142 |
| 141 // Driver that is passed to PasswordManager when a password is generated. | 143 // Driver that is passed to PasswordManager when a password is generated. |
| 142 password_manager::PasswordManagerDriver* _passwordManagerDriver; | 144 password_manager::PasswordManagerDriver* _passwordManagerDriver; |
| 143 | 145 |
| 144 // PasswordManager to inform when a password is generated. | 146 // PasswordManager to inform when a password is generated. |
| 145 password_manager::PasswordManager* _passwordManager; | 147 password_manager::PasswordManager* _passwordManager; |
| 146 | 148 |
| 147 // Callback to update the custom keyboard accessory view. Will be non-nil when | 149 // Callback to update the custom keyboard accessory view. Will be non-nil when |
| 148 // this PasswordGenerationAgent controls the keyboard accessory view. | 150 // this PasswordGenerationAgent controls the keyboard accessory view. |
| 149 base::mac::ScopedBlock<AccessoryViewReadyCompletion> | 151 AccessoryViewReadyCompletion _accessoryViewReadyCompletion; |
| 150 _accessoryViewReadyCompletion; | |
| 151 | 152 |
| 152 // The delegate for controlling the password generation UI. | 153 // The delegate for controlling the password generation UI. |
| 153 base::scoped_nsprotocol<id<PasswordsUiDelegate>> _passwords_ui_delegate; | 154 id<PasswordsUiDelegate> _passwords_ui_delegate; |
| 154 | 155 |
| 155 // The password that was generated and accepted by the user. | 156 // The password that was generated and accepted by the user. |
| 156 base::scoped_nsobject<NSString> _generatedPassword; | 157 NSString* _generatedPassword; |
| 157 } | 158 } |
| 158 | 159 |
| 159 - (instancetype)init { | 160 - (instancetype)init { |
| 160 NOTREACHED(); | 161 NOTREACHED(); |
| 161 return nil; | 162 return nil; |
| 162 } | 163 } |
| 163 | 164 |
| 164 - (instancetype) | 165 - (instancetype) |
| 165 initWithWebState:(web::WebState*)webState | 166 initWithWebState:(web::WebState*)webState |
| 166 passwordManager:(password_manager::PasswordManager*)passwordManager | 167 passwordManager:(password_manager::PasswordManager*)passwordManager |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 188 JSPasswordManager:(JsPasswordManager*)JSPasswordManager | 189 JSPasswordManager:(JsPasswordManager*)JSPasswordManager |
| 189 JSSuggestionManager:(JsSuggestionManager*)JSSuggestionManager | 190 JSSuggestionManager:(JsSuggestionManager*)JSSuggestionManager |
| 190 passwordsUiDelegate:(id<PasswordsUiDelegate>)UIDelegate { | 191 passwordsUiDelegate:(id<PasswordsUiDelegate>)UIDelegate { |
| 191 DCHECK([NSThread isMainThread]); | 192 DCHECK([NSThread isMainThread]); |
| 192 DCHECK(webState); | 193 DCHECK(webState); |
| 193 DCHECK_EQ([self class], [PasswordGenerationAgent class]); | 194 DCHECK_EQ([self class], [PasswordGenerationAgent class]); |
| 194 self = [super init]; | 195 self = [super init]; |
| 195 if (self) { | 196 if (self) { |
| 196 _passwordManager = passwordManager; | 197 _passwordManager = passwordManager; |
| 197 _passwordManagerDriver = driver; | 198 _passwordManagerDriver = driver; |
| 198 _JSPasswordManager.reset([JSPasswordManager retain]); | 199 _JSPasswordManager = nil; |
|
vabr (Chromium)
2017/06/13 15:23:52
For my own education -- what does the "retain" mes
marq (ping after 24h)
2017/06/13 17:39:04
These aren't classes (where there typically isn't
| |
| 199 _JSSuggestionManager.reset([JSSuggestionManager retain]); | 200 _JSSuggestionManager = nil; |
| 200 _webStateObserverBridge.reset( | 201 _webStateObserverBridge.reset( |
| 201 new web::WebStateObserverBridge(webState, self)); | 202 new web::WebStateObserverBridge(webState, self)); |
| 202 _passwords_ui_delegate.reset([UIDelegate retain]); | 203 _passwords_ui_delegate = nil; |
| 203 } | 204 } |
| 204 return self; | 205 return self; |
| 205 } | 206 } |
| 206 | 207 |
| 207 - (void)dealloc { | 208 - (void)dealloc { |
| 208 DCHECK([NSThread isMainThread]); | 209 DCHECK([NSThread isMainThread]); |
| 209 [super dealloc]; | 210 ; |
|
vabr (Chromium)
2017/06/13 15:23:52
nit: Remove this line.
marq (ping after 24h)
2017/06/13 17:39:04
Done.
| |
| 210 } | 211 } |
| 211 | 212 |
| 212 - (autofill::PasswordForm*)possibleAccountCreationForm { | 213 - (autofill::PasswordForm*)possibleAccountCreationForm { |
| 213 return _possibleAccountCreationForm.get(); | 214 return _possibleAccountCreationForm.get(); |
| 214 } | 215 } |
| 215 | 216 |
| 216 - (const std::vector<autofill::FormFieldData>&)passwordFields { | 217 - (const std::vector<autofill::FormFieldData>&)passwordFields { |
| 217 return _passwordFields; | 218 return _passwordFields; |
| 218 } | 219 } |
| 219 | 220 |
| 220 - (autofill::FormFieldData*)passwordGenerationField { | 221 - (autofill::FormFieldData*)passwordGenerationField { |
| 221 return _passwordGenerationField.get(); | 222 return _passwordGenerationField.get(); |
| 222 } | 223 } |
| 223 | 224 |
| 224 - (void)clearState { | 225 - (void)clearState { |
| 225 [self hideAlert]; | 226 [self hideAlert]; |
| 226 _allowedGenerationFormOrigins.clear(); | 227 _allowedGenerationFormOrigins.clear(); |
| 227 _possibleAccountCreationForm.reset(); | 228 _possibleAccountCreationForm.reset(); |
| 228 _passwordFields.clear(); | 229 _passwordFields.clear(); |
| 229 _passwordGenerationField.reset(); | 230 _passwordGenerationField.reset(); |
| 230 _generatedPassword.reset(); | 231 _generatedPassword = nil; |
| 231 } | 232 } |
| 232 | 233 |
| 233 - (BOOL)formHasGAIARealm:(const autofill::PasswordForm&)form { | 234 - (BOOL)formHasGAIARealm:(const autofill::PasswordForm&)form { |
| 234 // Do not generate password for GAIA since it is used to retrieve the | 235 // Do not generate password for GAIA since it is used to retrieve the |
| 235 // generated paswords. | 236 // generated paswords. |
| 236 return GURL(form.signon_realm) == | 237 return GURL(form.signon_realm) == |
| 237 GaiaUrls::GetInstance()->gaia_login_form_realm(); | 238 GaiaUrls::GetInstance()->gaia_login_form_realm(); |
| 238 } | 239 } |
| 239 | 240 |
| 240 - (BOOL)formHasEnoughTextFieldsForAccountCreation: | 241 - (BOOL)formHasEnoughTextFieldsForAccountCreation: |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 - (NSString*)passwordGenerationFormName { | 319 - (NSString*)passwordGenerationFormName { |
| 319 return base::SysUTF16ToNSString(_possibleAccountCreationForm->form_data.name); | 320 return base::SysUTF16ToNSString(_possibleAccountCreationForm->form_data.name); |
| 320 } | 321 } |
| 321 | 322 |
| 322 - (void)hideAlert { | 323 - (void)hideAlert { |
| 323 [_passwords_ui_delegate hideGenerationAlert]; | 324 [_passwords_ui_delegate hideGenerationAlert]; |
| 324 } | 325 } |
| 325 | 326 |
| 326 - (UIView*)currentAccessoryView { | 327 - (UIView*)currentAccessoryView { |
| 327 return [_generatedPassword length] > 0 | 328 return [_generatedPassword length] > 0 |
| 328 ? [[[PasswordGenerationEditView alloc] | 329 ? [[PasswordGenerationEditView alloc] |
| 329 initWithPassword:_generatedPassword] autorelease] | 330 initWithPassword:_generatedPassword] |
| 330 : [[[PasswordGenerationOfferView alloc] initWithDelegate:self] | 331 : [[PasswordGenerationOfferView alloc] initWithDelegate:self]; |
| 331 autorelease]; | |
| 332 } | 332 } |
| 333 | 333 |
| 334 #pragma mark - | 334 #pragma mark - |
| 335 #pragma mark CRWWebStateObserver | 335 #pragma mark CRWWebStateObserver |
| 336 | 336 |
| 337 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | 337 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 338 [self clearState]; | 338 [self clearState]; |
| 339 } | 339 } |
| 340 | 340 |
| 341 - (void)webStateDestroyed:(web::WebState*)webState { | 341 - (void)webStateDestroyed:(web::WebState*)webState { |
| 342 [self clearState]; | 342 [self clearState]; |
| 343 _webStateObserverBridge.reset(); | 343 _webStateObserverBridge.reset(); |
| 344 } | 344 } |
| 345 | 345 |
| 346 #pragma mark - | 346 #pragma mark - |
| 347 #pragma mark PasswordGenerationPromptDelegate | 347 #pragma mark PasswordGenerationPromptDelegate |
| 348 | 348 |
| 349 - (void)acceptPasswordGeneration:(id)sender { | 349 - (void)acceptPasswordGeneration:(id)sender { |
| 350 [self hideAlert]; | 350 [self hideAlert]; |
| 351 base::WeakNSObject<PasswordGenerationAgent> weakSelf(self); | 351 __weak PasswordGenerationAgent* weakSelf = self; |
| 352 id completionHandler = ^(BOOL success) { | 352 id completionHandler = ^(BOOL success) { |
| 353 if (!success) | 353 if (!success) |
| 354 return; | 354 return; |
| 355 base::scoped_nsobject<PasswordGenerationAgent> strongSelf( | 355 PasswordGenerationAgent* strongSelf = weakSelf; |
| 356 [weakSelf retain]); | |
| 357 if (!strongSelf) | 356 if (!strongSelf) |
| 358 return; | 357 return; |
| 359 if (strongSelf.get()->_passwordManager) { | 358 if (strongSelf->_passwordManager) { |
| 360 // Might be null in tests. | 359 // Might be null in tests. |
| 361 strongSelf.get()->_passwordManager->SetHasGeneratedPasswordForForm( | 360 strongSelf->_passwordManager->SetHasGeneratedPasswordForForm( |
| 362 strongSelf.get()->_passwordManagerDriver, | 361 strongSelf->_passwordManagerDriver, |
| 363 *strongSelf.get()->_possibleAccountCreationForm, true); | 362 *strongSelf->_possibleAccountCreationForm, true); |
| 364 } | 363 } |
| 365 if (strongSelf.get()->_accessoryViewReadyCompletion) { | 364 if (strongSelf->_accessoryViewReadyCompletion) { |
| 366 strongSelf.get()->_accessoryViewReadyCompletion.get()( | 365 strongSelf->_accessoryViewReadyCompletion( |
| 367 [strongSelf currentAccessoryView], strongSelf); | 366 [strongSelf currentAccessoryView], strongSelf); |
| 368 } | 367 } |
| 369 }; | 368 }; |
| 370 [_JSPasswordManager fillPasswordForm:[self passwordGenerationFormName] | 369 [_JSPasswordManager fillPasswordForm:[self passwordGenerationFormName] |
| 371 withGeneratedPassword:_generatedPassword | 370 withGeneratedPassword:_generatedPassword |
| 372 completionHandler:completionHandler]; | 371 completionHandler:completionHandler]; |
| 373 } | 372 } |
| 374 | 373 |
| 375 - (void)showSavedPasswords:(id)sender { | 374 - (void)showSavedPasswords:(id)sender { |
| 376 [self hideAlert]; | 375 [self hideAlert]; |
| 377 base::scoped_nsobject<GenericChromeCommand> command( | 376 GenericChromeCommand* command = [[GenericChromeCommand alloc] |
| 378 [[GenericChromeCommand alloc] | 377 initWithTag:IDC_SHOW_SAVE_PASSWORDS_SETTINGS]; |
| 379 initWithTag:IDC_SHOW_SAVE_PASSWORDS_SETTINGS]); | |
| 380 [command executeOnMainWindow]; | 378 [command executeOnMainWindow]; |
| 381 } | 379 } |
| 382 | 380 |
| 383 #pragma mark - | 381 #pragma mark - |
| 384 #pragma mark PasswordGenerationOfferDelegate | 382 #pragma mark PasswordGenerationOfferDelegate |
| 385 | 383 |
| 386 - (void)generatePassword { | 384 - (void)generatePassword { |
| 387 _generatedPassword.reset([base::SysUTF8ToNSString( | 385 _generatedPassword = [base::SysUTF8ToNSString( |
| 388 autofill::PasswordGenerator(kGeneratedPasswordLength).Generate()) copy]); | 386 autofill::PasswordGenerator(kGeneratedPasswordLength).Generate()) copy]; |
| 389 [_passwords_ui_delegate showGenerationAlertWithPassword:_generatedPassword | 387 [_passwords_ui_delegate showGenerationAlertWithPassword:_generatedPassword |
| 390 andPromptDelegate:self]; | 388 andPromptDelegate:self]; |
| 391 } | 389 } |
| 392 | 390 |
| 393 #pragma mark - | 391 #pragma mark - |
| 394 #pragma mark FormInputAccessoryViewProvider | 392 #pragma mark FormInputAccessoryViewProvider |
| 395 | 393 |
| 396 - (id<FormInputAccessoryViewDelegate>)accessoryViewDelegate { | 394 - (id<FormInputAccessoryViewDelegate>)accessoryViewDelegate { |
| 397 return nil; | 395 return nil; |
| 398 } | 396 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 416 | 414 |
| 417 - (void)retrieveAccessoryViewForFormNamed:(const std::string&)formName | 415 - (void)retrieveAccessoryViewForFormNamed:(const std::string&)formName |
| 418 fieldName:(const std::string&)fieldName | 416 fieldName:(const std::string&)fieldName |
| 419 value:(const std::string&)value | 417 value:(const std::string&)value |
| 420 type:(const std::string&)type | 418 type:(const std::string&)type |
| 421 webState:(web::WebState*)webState | 419 webState:(web::WebState*)webState |
| 422 accessoryViewUpdateBlock: | 420 accessoryViewUpdateBlock: |
| 423 (AccessoryViewReadyCompletion)accessoryViewUpdateBlock { | 421 (AccessoryViewReadyCompletion)accessoryViewUpdateBlock { |
| 424 DCHECK(!_accessoryViewReadyCompletion); | 422 DCHECK(!_accessoryViewReadyCompletion); |
| 425 if ([_generatedPassword length] > 0) | 423 if ([_generatedPassword length] > 0) |
| 426 _generatedPassword.reset([base::SysUTF8ToNSString(value) copy]); | 424 _generatedPassword = [base::SysUTF8ToNSString(value) copy]; |
| 427 accessoryViewUpdateBlock([self currentAccessoryView], self); | 425 accessoryViewUpdateBlock([self currentAccessoryView], self); |
| 428 _accessoryViewReadyCompletion.reset([accessoryViewUpdateBlock copy]); | 426 _accessoryViewReadyCompletion = [accessoryViewUpdateBlock copy]; |
| 429 } | 427 } |
| 430 | 428 |
| 431 - (void)inputAccessoryViewControllerDidReset: | 429 - (void)inputAccessoryViewControllerDidReset: |
| 432 (FormInputAccessoryViewController*)controller { | 430 (FormInputAccessoryViewController*)controller { |
| 433 [self hideAlert]; | 431 [self hideAlert]; |
| 434 DCHECK(_accessoryViewReadyCompletion); | 432 DCHECK(_accessoryViewReadyCompletion); |
| 435 _accessoryViewReadyCompletion.reset(); | 433 _accessoryViewReadyCompletion = nil; |
| 436 } | 434 } |
| 437 | 435 |
| 438 - (void)resizeAccessoryView { | 436 - (void)resizeAccessoryView { |
| 439 DCHECK(_accessoryViewReadyCompletion); | 437 DCHECK(_accessoryViewReadyCompletion); |
| 440 _accessoryViewReadyCompletion.get()([self currentAccessoryView], self); | 438 _accessoryViewReadyCompletion([self currentAccessoryView], self); |
| 441 } | 439 } |
| 442 | 440 |
| 443 - (BOOL)getLogKeyboardAccessoryMetrics { | 441 - (BOOL)getLogKeyboardAccessoryMetrics { |
| 444 // Only store metrics for regular Autofill, not passwords. | 442 // Only store metrics for regular Autofill, not passwords. |
| 445 return NO; | 443 return NO; |
| 446 } | 444 } |
| 447 | 445 |
| 448 @end | 446 @end |
| OLD | NEW |