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

Side by Side Diff: ios/chrome/browser/passwords/password_controller.mm

Issue 2933873002: [ObjC ARC] Converts ios/chrome/browser/passwords:passwords to ARC. (Closed)
Patch Set: fix names and nil assignments. Created 3 years, 6 months 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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_controller.h" 5 #import "ios/chrome/browser/passwords/password_controller.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #import "base/ios/weak_nsobject.h"
16 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
17 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
18 #include "base/mac/foundation_util.h" 17 #include "base/mac/foundation_util.h"
19 #include "base/mac/scoped_nsobject.h"
20 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
21 #include "base/strings/string16.h" 19 #include "base/strings/string16.h"
22 #include "base/strings/sys_string_conversions.h" 20 #include "base/strings/sys_string_conversions.h"
23 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
24 #include "base/values.h" 22 #include "base/values.h"
25 #include "components/autofill/core/common/password_form.h" 23 #include "components/autofill/core/common/password_form.h"
26 #include "components/autofill/core/common/password_form_fill_data.h" 24 #include "components/autofill/core/common/password_form_fill_data.h"
27 #include "components/browser_sync/profile_sync_service.h" 25 #include "components/browser_sync/profile_sync_service.h"
28 #include "components/infobars/core/infobar_manager.h" 26 #include "components/infobars/core/infobar_manager.h"
29 #include "components/password_manager/core/browser/password_bubble_experiment.h" 27 #include "components/password_manager/core/browser/password_bubble_experiment.h"
30 #include "components/password_manager/core/browser/password_generation_manager.h " 28 #include "components/password_manager/core/browser/password_generation_manager.h "
31 #include "components/password_manager/core/browser/password_manager.h" 29 #include "components/password_manager/core/browser/password_manager.h"
32 #include "components/password_manager/core/browser/password_manager_client.h" 30 #include "components/password_manager/core/browser/password_manager_client.h"
33 #include "components/password_manager/core/browser/password_manager_driver.h" 31 #include "components/password_manager/core/browser/password_manager_driver.h"
34 #include "components/sync/driver/sync_service.h" 32 #include "components/sync/driver/sync_service.h"
35 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 33 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
36 #include "ios/chrome/browser/experimental_flags.h" 34 #include "ios/chrome/browser/experimental_flags.h"
37 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" 35 #include "ios/chrome/browser/infobars/infobar_manager_impl.h"
38 #import "ios/chrome/browser/passwords/ios_chrome_save_password_infobar_delegate. h" 36 #import "ios/chrome/browser/passwords/ios_chrome_save_password_infobar_delegate. h"
39 #import "ios/chrome/browser/passwords/ios_chrome_update_password_infobar_delegat e.h" 37 #import "ios/chrome/browser/passwords/ios_chrome_update_password_infobar_delegat e.h"
40 #import "ios/chrome/browser/passwords/js_password_manager.h" 38 #import "ios/chrome/browser/passwords/js_password_manager.h"
41 #import "ios/chrome/browser/passwords/password_generation_agent.h" 39 #import "ios/chrome/browser/passwords/password_generation_agent.h"
42 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" 40 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h"
43 #import "ios/web/public/origin_util.h" 41 #import "ios/web/public/origin_util.h"
44 #include "ios/web/public/url_scheme_util.h" 42 #include "ios/web/public/url_scheme_util.h"
45 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" 43 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
46 #import "ios/web/public/web_state/web_state.h" 44 #import "ios/web/public/web_state/web_state.h"
47 #include "url/gurl.h" 45 #include "url/gurl.h"
48 46
47 #if !defined(__has_feature) || !__has_feature(objc_arc)
48 #error "This file requires ARC support."
49 #endif
50
49 using password_manager::PasswordFormManager; 51 using password_manager::PasswordFormManager;
50 using password_manager::PasswordGenerationManager; 52 using password_manager::PasswordGenerationManager;
51 using password_manager::PasswordManager; 53 using password_manager::PasswordManager;
52 using password_manager::PasswordManagerClient; 54 using password_manager::PasswordManagerClient;
53 using password_manager::PasswordManagerDriver; 55 using password_manager::PasswordManagerDriver;
54 56
55 namespace { 57 namespace {
56 // Types of password infobars to display. 58 // Types of password infobars to display.
57 enum class PasswordInfoBarType { SAVE, UPDATE }; 59 enum class PasswordInfoBarType { SAVE, UPDATE };
58 } 60 }
59 61
60 @interface PasswordController () 62 @interface PasswordController ()
61 63
62 // This is set to YES as soon as the associated WebState is destroyed. 64 // This is set to YES as soon as the associated WebState is destroyed.
63 @property(readonly) BOOL isWebStateDestroyed; 65 @property(readonly) BOOL isWebStateDestroyed;
64 66
67 // Accessor for property inside block.
68 @property(readonly) PasswordManager* passwordManager;
69
65 @end 70 @end
66 71
67 @interface PasswordController ()<FormSuggestionProvider> 72 @interface PasswordController ()<FormSuggestionProvider>
68 73
69 // Parses the |jsonString| which contatins the password forms found on a web 74 // Parses the |jsonString| which contatins the password forms found on a web
70 // page to populate the |forms| vector. 75 // page to populate the |forms| vector.
71 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms 76 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms
72 fromFormsJSON:(NSString*)jsonString 77 fromFormsJSON:(NSString*)jsonString
73 pageURL:(const GURL&)pageURL; 78 pageURL:(const GURL&)pageURL;
74 79
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 return trustLevel == web::URLVerificationTrustLevel::kAbsolute; 251 return trustLevel == web::URLVerificationTrustLevel::kAbsolute;
247 } 252 }
248 253
249 } // namespace 254 } // namespace
250 255
251 @implementation PasswordController { 256 @implementation PasswordController {
252 std::unique_ptr<PasswordManager> passwordManager_; 257 std::unique_ptr<PasswordManager> passwordManager_;
253 std::unique_ptr<PasswordGenerationManager> passwordGenerationManager_; 258 std::unique_ptr<PasswordGenerationManager> passwordGenerationManager_;
254 std::unique_ptr<PasswordManagerClient> passwordManagerClient_; 259 std::unique_ptr<PasswordManagerClient> passwordManagerClient_;
255 std::unique_ptr<PasswordManagerDriver> passwordManagerDriver_; 260 std::unique_ptr<PasswordManagerDriver> passwordManagerDriver_;
256 base::scoped_nsobject<PasswordGenerationAgent> passwordGenerationAgent_; 261 PasswordGenerationAgent* passwordGenerationAgent_;
257 262
258 JsPasswordManager* passwordJsManager_; // weak 263 JsPasswordManager* passwordJsManager_; // weak
259 web::WebState* webState_; // weak 264 web::WebState* webState_; // weak
260 265
261 // The pending form data. 266 // The pending form data.
262 std::unique_ptr<autofill::PasswordFormFillData> formData_; 267 std::unique_ptr<autofill::PasswordFormFillData> formData_;
263 268
264 // Bridge to observe WebState from Objective-C. 269 // Bridge to observe WebState from Objective-C.
265 std::unique_ptr<web::WebStateObserverBridge> webStateObserverBridge_; 270 std::unique_ptr<web::WebStateObserverBridge> webStateObserverBridge_;
266 } 271 }
(...skipping 19 matching lines...) Expand all
286 if (passwordManagerClient) 291 if (passwordManagerClient)
287 passwordManagerClient_ = std::move(passwordManagerClient); 292 passwordManagerClient_ = std::move(passwordManagerClient);
288 else 293 else
289 passwordManagerClient_.reset(new IOSChromePasswordManagerClient(self)); 294 passwordManagerClient_.reset(new IOSChromePasswordManagerClient(self));
290 passwordManager_.reset(new PasswordManager(passwordManagerClient_.get())); 295 passwordManager_.reset(new PasswordManager(passwordManagerClient_.get()));
291 passwordManagerDriver_.reset(new IOSChromePasswordManagerDriver(self)); 296 passwordManagerDriver_.reset(new IOSChromePasswordManagerDriver(self));
292 if (experimental_flags::IsPasswordGenerationEnabled() && 297 if (experimental_flags::IsPasswordGenerationEnabled() &&
293 !passwordManagerClient_->IsIncognito()) { 298 !passwordManagerClient_->IsIncognito()) {
294 passwordGenerationManager_.reset(new PasswordGenerationManager( 299 passwordGenerationManager_.reset(new PasswordGenerationManager(
295 passwordManagerClient_.get(), passwordManagerDriver_.get())); 300 passwordManagerClient_.get(), passwordManagerDriver_.get()));
296 passwordGenerationAgent_.reset([[PasswordGenerationAgent alloc] 301 passwordGenerationAgent_ = [[PasswordGenerationAgent alloc]
297 initWithWebState:webState 302 initWithWebState:webState
298 passwordManager:passwordManager_.get() 303 passwordManager:passwordManager_.get()
299 passwordManagerDriver:passwordManagerDriver_.get() 304 passwordManagerDriver:passwordManagerDriver_.get()
300 passwordsUiDelegate:UIDelegate]); 305 passwordsUiDelegate:UIDelegate];
301 } 306 }
302 307
303 passwordJsManager_ = base::mac::ObjCCastStrict<JsPasswordManager>( 308 passwordJsManager_ = base::mac::ObjCCastStrict<JsPasswordManager>(
304 [webState->GetJSInjectionReceiver() 309 [webState->GetJSInjectionReceiver()
305 instanceOfClass:[JsPasswordManager class]]); 310 instanceOfClass:[JsPasswordManager class]]);
306 webStateObserverBridge_.reset( 311 webStateObserverBridge_.reset(
307 new web::WebStateObserverBridge(webState, self)); 312 new web::WebStateObserverBridge(webState, self));
308 } 313 }
309 return self; 314 return self;
310 } 315 }
311 316
312 - (instancetype)init { 317 - (instancetype)init {
313 NOTREACHED(); 318 NOTREACHED();
314 return nil; 319 return nil;
315 } 320 }
316 321
317 - (void)dealloc { 322 - (void)dealloc {
318 [self detach]; 323 [self detach];
319 [super dealloc];
320 } 324 }
321 325
322 - (ios::ChromeBrowserState*)browserState { 326 - (ios::ChromeBrowserState*)browserState {
323 return webState_ ? ios::ChromeBrowserState::FromBrowserState( 327 return webState_ ? ios::ChromeBrowserState::FromBrowserState(
324 webState_->GetBrowserState()) 328 webState_->GetBrowserState())
325 : nullptr; 329 : nullptr;
326 } 330 }
327 331
328 - (const GURL&)lastCommittedURL { 332 - (const GURL&)lastCommittedURL {
329 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 333 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
330 return GURL::EmptyGURL(); 334 return GURL::EmptyGURL();
331 return webStateObserverBridge_->web_state()->GetLastCommittedURL(); 335 return webStateObserverBridge_->web_state()->GetLastCommittedURL();
332 } 336 }
333 337
334 - (void)detach { 338 - (void)detach {
335 webState_ = nullptr; 339 webState_ = nullptr;
336 webStateObserverBridge_.reset(); 340 webStateObserverBridge_.reset();
337 passwordGenerationAgent_.reset(); 341 passwordGenerationAgent_ = nil;
338 passwordGenerationManager_.reset(); 342 passwordGenerationManager_.reset();
339 passwordManagerDriver_.reset(); 343 passwordManagerDriver_.reset();
340 passwordManager_.reset(); 344 passwordManager_.reset();
341 passwordManagerClient_.reset(); 345 passwordManagerClient_.reset();
342 } 346 }
343 347
344 - (void)findAndFillPasswordForms:(NSString*)username 348 - (void)findAndFillPasswordForms:(NSString*)username
345 password:(NSString*)password 349 password:(NSString*)password
346 completionHandler:(void (^)(BOOL))completionHandler { 350 completionHandler:(void (^)(BOOL))completionHandler {
347 [self findPasswordFormsWithCompletionHandler:^( 351 [self findPasswordFormsWithCompletionHandler:^(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 passwordManager_->DidNavigateMainFrame(); 388 passwordManager_->DidNavigateMainFrame();
385 389
386 if (!webState->ContentIsHTML()) { 390 if (!webState->ContentIsHTML()) {
387 // If the current page is not HTML, it does not contain any HTML forms. 391 // If the current page is not HTML, it does not contain any HTML forms.
388 [self 392 [self
389 didFinishPasswordFormExtraction:std::vector<autofill::PasswordForm>()]; 393 didFinishPasswordFormExtraction:std::vector<autofill::PasswordForm>()];
390 } 394 }
391 395
392 // Read all password forms from the page and send them to the password 396 // Read all password forms from the page and send them to the password
393 // manager. 397 // manager.
394 base::WeakNSObject<PasswordController> weakSelf(self); 398 __weak PasswordController* weakSelf = self;
395 [self findPasswordFormsWithCompletionHandler:^( 399 [self findPasswordFormsWithCompletionHandler:^(
396 const std::vector<autofill::PasswordForm>& forms) { 400 const std::vector<autofill::PasswordForm>& forms) {
397 [weakSelf didFinishPasswordFormExtraction:forms]; 401 [weakSelf didFinishPasswordFormExtraction:forms];
398 }]; 402 }];
399 } 403 }
400 404
401 - (void)webState:(web::WebState*)webState 405 - (void)webState:(web::WebState*)webState
402 didSubmitDocumentWithFormNamed:(const std::string&)formName 406 didSubmitDocumentWithFormNamed:(const std::string&)formName
403 userInitiated:(BOOL)userInitiated { 407 userInitiated:(BOOL)userInitiated {
404 base::WeakNSObject<PasswordController> weakSelf(self); 408 __weak PasswordController* weakSelf = self;
405 // This code is racing against the new page loading and will not get the 409 // This code is racing against the new page loading and will not get the
406 // password form data if the page has changed. In most cases this code wins 410 // password form data if the page has changed. In most cases this code wins
407 // the race. 411 // the race.
408 // TODO(crbug.com/418827): Fix this by passing in more data from the JS side. 412 // TODO(crbug.com/418827): Fix this by passing in more data from the JS side.
409 id completionHandler = ^(BOOL found, const autofill::PasswordForm& form) { 413 id completionHandler = ^(BOOL found, const autofill::PasswordForm& form) {
410 if (weakSelf && ![weakSelf isWebStateDestroyed]) { 414 PasswordController* strongSelf = weakSelf;
411 weakSelf.get()->passwordManager_->OnPasswordFormSubmitted( 415 if (strongSelf && ![strongSelf isWebStateDestroyed]) {
412 weakSelf.get()->passwordManagerDriver_.get(), form); 416 strongSelf.passwordManager->OnPasswordFormSubmitted(
417 strongSelf.passwordManagerDriver, form);
413 } 418 }
414 }; 419 };
415 [self extractSubmittedPasswordForm:formName 420 [self extractSubmittedPasswordForm:formName
416 completionHandler:completionHandler]; 421 completionHandler:completionHandler];
417 } 422 }
418 423
419 - (void)webStateDestroyed:(web::WebState*)webState { 424 - (void)webStateDestroyed:(web::WebState*)webState {
420 isWebStateDestroyed_ = YES; 425 isWebStateDestroyed_ = YES;
421 [self detach]; 426 [self detach];
422 } 427 }
423 428
424 - (void)findPasswordFormsWithCompletionHandler: 429 - (void)findPasswordFormsWithCompletionHandler:
425 (void (^)(const std::vector<autofill::PasswordForm>&))completionHandler { 430 (void (^)(const std::vector<autofill::PasswordForm>&))completionHandler {
426 DCHECK(completionHandler); 431 DCHECK(completionHandler);
427 432
428 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 433 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
429 return; 434 return;
430 435
431 GURL pageURL; 436 GURL pageURL;
432 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(), 437 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
433 &pageURL)) { 438 &pageURL)) {
434 return; 439 return;
435 } 440 }
436 441
437 base::WeakNSObject<PasswordController> weakSelf(self); 442 __weak PasswordController* weakSelf = self;
438 [passwordJsManager_ findPasswordFormsWithCompletionHandler:^( 443 [passwordJsManager_ findPasswordFormsWithCompletionHandler:^(
439 NSString* jsonString) { 444 NSString* jsonString) {
440 std::vector<autofill::PasswordForm> forms; 445 std::vector<autofill::PasswordForm> forms;
441 [weakSelf getPasswordForms:&forms fromFormsJSON:jsonString pageURL:pageURL]; 446 [weakSelf getPasswordForms:&forms fromFormsJSON:jsonString pageURL:pageURL];
442 completionHandler(forms); 447 completionHandler(forms);
443 }]; 448 }];
444 } 449 }
445 450
446 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms 451 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms
447 fromFormsJSON:(NSString*)JSONNSString 452 fromFormsJSON:(NSString*)JSONNSString
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 494 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
490 return; 495 return;
491 496
492 GURL pageURL; 497 GURL pageURL;
493 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(), 498 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
494 &pageURL)) { 499 &pageURL)) {
495 completionHandler(NO, autofill::PasswordForm()); 500 completionHandler(NO, autofill::PasswordForm());
496 return; 501 return;
497 } 502 }
498 503
499 base::WeakNSObject<PasswordController> weakSelf(self); 504 __weak PasswordController* weakSelf = self;
500 id extractSubmittedFormCompletionHandler = ^(NSString* jsonString) { 505 id extractSubmittedFormCompletionHandler = ^(NSString* jsonString) {
501 autofill::PasswordForm form; 506 autofill::PasswordForm form;
502 BOOL found = [weakSelf getPasswordForm:&form 507 BOOL found = [weakSelf getPasswordForm:&form
503 fromPasswordFormJSON:jsonString 508 fromPasswordFormJSON:jsonString
504 pageURL:pageURL]; 509 pageURL:pageURL];
505 completionHandler(found, form); 510 completionHandler(found, form);
506 }; 511 };
507 512
508 [passwordJsManager_ extractForm:base::SysUTF8ToNSString(formName) 513 [passwordJsManager_ extractForm:base::SysUTF8ToNSString(formName)
509 completionHandler:extractSubmittedFormCompletionHandler]; 514 completionHandler:extractSubmittedFormCompletionHandler];
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 return; 825 return;
821 } 826 }
822 827
823 [self fillPasswordForm:*formData_ 828 [self fillPasswordForm:*formData_
824 withUsername:formData_->username_field.value 829 withUsername:formData_->username_field.value
825 password:formData_->password_field.value 830 password:formData_->password_field.value
826 completionHandler:completionHandler]; 831 completionHandler:completionHandler];
827 } 832 }
828 833
829 - (PasswordGenerationAgent*)passwordGenerationAgent { 834 - (PasswordGenerationAgent*)passwordGenerationAgent {
830 return passwordGenerationAgent_.get(); 835 return passwordGenerationAgent_;
831 } 836 }
832 837
833 - (PasswordGenerationManager*)passwordGenerationManager { 838 - (PasswordGenerationManager*)passwordGenerationManager {
834 return passwordGenerationManager_.get(); 839 return passwordGenerationManager_.get();
835 } 840 }
836 841
837 - (PasswordManagerClient*)passwordManagerClient { 842 - (PasswordManagerClient*)passwordManagerClient {
838 return passwordManagerClient_.get(); 843 return passwordManagerClient_.get();
839 } 844 }
840 845
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 break; 880 break;
876 881
877 case PasswordInfoBarType::UPDATE: 882 case PasswordInfoBarType::UPDATE:
878 IOSChromeUpdatePasswordInfoBarDelegate::Create( 883 IOSChromeUpdatePasswordInfoBarDelegate::Create(
879 isSmartLockBrandingEnabled, infoBarManager, std::move(form)); 884 isSmartLockBrandingEnabled, infoBarManager, std::move(form));
880 break; 885 break;
881 } 886 }
882 } 887 }
883 888
884 @end 889 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/passwords/js_password_manager.mm ('k') | ios/chrome/browser/passwords/password_generation_agent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698