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

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: Cleanup, removed spurious files. 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]; 324 ;
vabr (Chromium) 2017/06/13 15:23:52 nit: Should this whole line be deleted?
marq (ping after 24h) 2017/06/13 17:39:04 Done.
320 } 325 }
321 326
322 - (ios::ChromeBrowserState*)browserState { 327 - (ios::ChromeBrowserState*)browserState {
323 return webState_ ? ios::ChromeBrowserState::FromBrowserState( 328 return webState_ ? ios::ChromeBrowserState::FromBrowserState(
324 webState_->GetBrowserState()) 329 webState_->GetBrowserState())
325 : nullptr; 330 : nullptr;
326 } 331 }
327 332
328 - (const GURL&)lastCommittedURL { 333 - (const GURL&)lastCommittedURL {
329 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 334 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
330 return GURL::EmptyGURL(); 335 return GURL::EmptyGURL();
331 return webStateObserverBridge_->web_state()->GetLastCommittedURL(); 336 return webStateObserverBridge_->web_state()->GetLastCommittedURL();
332 } 337 }
333 338
334 - (void)detach { 339 - (void)detach {
335 webState_ = nullptr; 340 webState_ = nullptr;
336 webStateObserverBridge_.reset(); 341 webStateObserverBridge_.reset();
337 passwordGenerationAgent_.reset(); 342 passwordGenerationAgent_ = nil;
338 passwordGenerationManager_.reset(); 343 passwordGenerationManager_.reset();
339 passwordManagerDriver_.reset(); 344 passwordManagerDriver_.reset();
340 passwordManager_.reset(); 345 passwordManager_.reset();
341 passwordManagerClient_.reset(); 346 passwordManagerClient_.reset();
342 } 347 }
343 348
344 - (void)findAndFillPasswordForms:(NSString*)username 349 - (void)findAndFillPasswordForms:(NSString*)username
345 password:(NSString*)password 350 password:(NSString*)password
346 completionHandler:(void (^)(BOOL))completionHandler { 351 completionHandler:(void (^)(BOOL))completionHandler {
347 [self findPasswordFormsWithCompletionHandler:^( 352 [self findPasswordFormsWithCompletionHandler:^(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 passwordManager_->DidNavigateMainFrame(); 389 passwordManager_->DidNavigateMainFrame();
385 390
386 if (!webState->ContentIsHTML()) { 391 if (!webState->ContentIsHTML()) {
387 // If the current page is not HTML, it does not contain any HTML forms. 392 // If the current page is not HTML, it does not contain any HTML forms.
388 [self 393 [self
389 didFinishPasswordFormExtraction:std::vector<autofill::PasswordForm>()]; 394 didFinishPasswordFormExtraction:std::vector<autofill::PasswordForm>()];
390 } 395 }
391 396
392 // Read all password forms from the page and send them to the password 397 // Read all password forms from the page and send them to the password
393 // manager. 398 // manager.
394 base::WeakNSObject<PasswordController> weakSelf(self); 399 __weak PasswordController* weakSelf = self;
395 [self findPasswordFormsWithCompletionHandler:^( 400 [self findPasswordFormsWithCompletionHandler:^(
396 const std::vector<autofill::PasswordForm>& forms) { 401 const std::vector<autofill::PasswordForm>& forms) {
397 [weakSelf didFinishPasswordFormExtraction:forms]; 402 [weakSelf didFinishPasswordFormExtraction:forms];
398 }]; 403 }];
399 } 404 }
400 405
401 - (void)webState:(web::WebState*)webState 406 - (void)webState:(web::WebState*)webState
402 didSubmitDocumentWithFormNamed:(const std::string&)formName 407 didSubmitDocumentWithFormNamed:(const std::string&)formName
403 userInitiated:(BOOL)userInitiated { 408 userInitiated:(BOOL)userInitiated {
404 base::WeakNSObject<PasswordController> weakSelf(self); 409 __weak PasswordController* weakSelf = self;
405 // This code is racing against the new page loading and will not get the 410 // 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 411 // password form data if the page has changed. In most cases this code wins
407 // the race. 412 // the race.
408 // TODO(crbug.com/418827): Fix this by passing in more data from the JS side. 413 // TODO(crbug.com/418827): Fix this by passing in more data from the JS side.
409 id completionHandler = ^(BOOL found, const autofill::PasswordForm& form) { 414 id completionHandler = ^(BOOL found, const autofill::PasswordForm& form) {
410 if (weakSelf && ![weakSelf isWebStateDestroyed]) { 415 PasswordController* strongSelf = weakSelf;
411 weakSelf.get()->passwordManager_->OnPasswordFormSubmitted( 416 if (strongSelf && ![strongSelf isWebStateDestroyed]) {
412 weakSelf.get()->passwordManagerDriver_.get(), form); 417 strongSelf.passwordManager->OnPasswordFormSubmitted(
418 strongSelf.passwordManagerDriver, form);
413 } 419 }
414 }; 420 };
415 [self extractSubmittedPasswordForm:formName 421 [self extractSubmittedPasswordForm:formName
416 completionHandler:completionHandler]; 422 completionHandler:completionHandler];
417 } 423 }
418 424
419 - (void)webStateDestroyed:(web::WebState*)webState { 425 - (void)webStateDestroyed:(web::WebState*)webState {
420 isWebStateDestroyed_ = YES; 426 isWebStateDestroyed_ = YES;
421 [self detach]; 427 [self detach];
422 } 428 }
423 429
424 - (void)findPasswordFormsWithCompletionHandler: 430 - (void)findPasswordFormsWithCompletionHandler:
425 (void (^)(const std::vector<autofill::PasswordForm>&))completionHandler { 431 (void (^)(const std::vector<autofill::PasswordForm>&))completionHandler {
426 DCHECK(completionHandler); 432 DCHECK(completionHandler);
427 433
428 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 434 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
429 return; 435 return;
430 436
431 GURL pageURL; 437 GURL pageURL;
432 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(), 438 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
433 &pageURL)) { 439 &pageURL)) {
434 return; 440 return;
435 } 441 }
436 442
437 base::WeakNSObject<PasswordController> weakSelf(self); 443 __weak PasswordController* weakSelf = self;
vabr (Chromium) 2017/06/13 15:23:52 Just wondering why we use "__weak T* var" instead
marq (ping after 24h) 2017/06/13 17:39:04 I prefer having it as a prefix, so it's positioned
vabr (Chromium) 2017/06/13 17:49:52 Acknowledged. Since this goes against Apple's advi
438 [passwordJsManager_ findPasswordFormsWithCompletionHandler:^( 444 [passwordJsManager_ findPasswordFormsWithCompletionHandler:^(
439 NSString* jsonString) { 445 NSString* jsonString) {
440 std::vector<autofill::PasswordForm> forms; 446 std::vector<autofill::PasswordForm> forms;
441 [weakSelf getPasswordForms:&forms fromFormsJSON:jsonString pageURL:pageURL]; 447 [weakSelf getPasswordForms:&forms fromFormsJSON:jsonString pageURL:pageURL];
442 completionHandler(forms); 448 completionHandler(forms);
443 }]; 449 }];
444 } 450 }
445 451
446 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms 452 - (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms
447 fromFormsJSON:(NSString*)JSONNSString 453 fromFormsJSON:(NSString*)JSONNSString
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state()) 495 if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
490 return; 496 return;
491 497
492 GURL pageURL; 498 GURL pageURL;
493 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(), 499 if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
494 &pageURL)) { 500 &pageURL)) {
495 completionHandler(NO, autofill::PasswordForm()); 501 completionHandler(NO, autofill::PasswordForm());
496 return; 502 return;
497 } 503 }
498 504
499 base::WeakNSObject<PasswordController> weakSelf(self); 505 __weak PasswordController* weakSelf = self;
500 id extractSubmittedFormCompletionHandler = ^(NSString* jsonString) { 506 id extractSubmittedFormCompletionHandler = ^(NSString* jsonString) {
501 autofill::PasswordForm form; 507 autofill::PasswordForm form;
502 BOOL found = [weakSelf getPasswordForm:&form 508 BOOL found = [weakSelf getPasswordForm:&form
503 fromPasswordFormJSON:jsonString 509 fromPasswordFormJSON:jsonString
504 pageURL:pageURL]; 510 pageURL:pageURL];
505 completionHandler(found, form); 511 completionHandler(found, form);
506 }; 512 };
507 513
508 [passwordJsManager_ extractForm:base::SysUTF8ToNSString(formName) 514 [passwordJsManager_ extractForm:base::SysUTF8ToNSString(formName)
509 completionHandler:extractSubmittedFormCompletionHandler]; 515 completionHandler:extractSubmittedFormCompletionHandler];
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 return; 826 return;
821 } 827 }
822 828
823 [self fillPasswordForm:*formData_ 829 [self fillPasswordForm:*formData_
824 withUsername:formData_->username_field.value 830 withUsername:formData_->username_field.value
825 password:formData_->password_field.value 831 password:formData_->password_field.value
826 completionHandler:completionHandler]; 832 completionHandler:completionHandler];
827 } 833 }
828 834
829 - (PasswordGenerationAgent*)passwordGenerationAgent { 835 - (PasswordGenerationAgent*)passwordGenerationAgent {
830 return passwordGenerationAgent_.get(); 836 return passwordGenerationAgent_;
831 } 837 }
832 838
833 - (PasswordGenerationManager*)passwordGenerationManager { 839 - (PasswordGenerationManager*)passwordGenerationManager {
834 return passwordGenerationManager_.get(); 840 return passwordGenerationManager_.get();
835 } 841 }
836 842
837 - (PasswordManagerClient*)passwordManagerClient { 843 - (PasswordManagerClient*)passwordManagerClient {
838 return passwordManagerClient_.get(); 844 return passwordManagerClient_.get();
839 } 845 }
840 846
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 break; 881 break;
876 882
877 case PasswordInfoBarType::UPDATE: 883 case PasswordInfoBarType::UPDATE:
878 IOSChromeUpdatePasswordInfoBarDelegate::Create( 884 IOSChromeUpdatePasswordInfoBarDelegate::Create(
879 isSmartLockBrandingEnabled, infoBarManager, std::move(form)); 885 isSmartLockBrandingEnabled, infoBarManager, std::move(form));
880 break; 886 break;
881 } 887 }
882 } 888 }
883 889
884 @end 890 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698