credential_provider_migrator_app_agent.mm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 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. #import "ios/chrome/app/credential_provider_migrator_app_agent.h"
  5. #include "components/keyed_service/core/service_access_type.h"
  6. #import "ios/chrome/app/application_delegate/app_state.h"
  7. #import "ios/chrome/browser/credential_provider/credential_provider_migrator.h"
  8. #include "ios/chrome/browser/passwords/ios_chrome_password_store_factory.h"
  9. #import "ios/chrome/browser/ui/main/browser_interface_provider.h"
  10. #import "ios/chrome/browser/ui/main/scene_state.h"
  11. #include "ios/chrome/common/app_group/app_group_constants.h"
  12. #import "ios/chrome/common/credential_provider/constants.h"
  13. #if !defined(__has_feature) || !__has_feature(objc_arc)
  14. #error "This file requires ARC support."
  15. #endif
  16. @interface CredentialProviderAppAgent ()
  17. // `migrator` is in charge of migrating the password when Chrome comes to
  18. // foreground.
  19. @property(nonatomic, strong) CredentialProviderMigrator* migrator;
  20. @end
  21. @implementation CredentialProviderAppAgent
  22. - (void)appDidEnterForeground {
  23. if (self.migrator) {
  24. return;
  25. }
  26. NSString* key = AppGroupUserDefaultsCredentialProviderNewCredentials();
  27. SceneState* anyScene = self.appState.foregroundScenes.firstObject;
  28. DCHECK(anyScene);
  29. ChromeBrowserState* browserState =
  30. anyScene.interfaceProvider.mainInterface.browserState;
  31. DCHECK(browserState);
  32. scoped_refptr<password_manager::PasswordStoreInterface> store =
  33. IOSChromePasswordStoreFactory::GetForBrowserState(
  34. browserState, ServiceAccessType::IMPLICIT_ACCESS);
  35. NSUserDefaults* userDefaults = app_group::GetGroupUserDefaults();
  36. self.migrator =
  37. [[CredentialProviderMigrator alloc] initWithUserDefaults:userDefaults
  38. key:key
  39. passwordStore:store];
  40. __weak __typeof__(self) weakSelf = self;
  41. [self.migrator startMigrationWithCompletion:^(BOOL success, NSError* error) {
  42. DCHECK(success);
  43. weakSelf.migrator = nil;
  44. }];
  45. }
  46. @end