/ios/chrome/browser/passwords/ios_chrome_password_store_factory.cc

https://gitlab.com/0072016/Facebook-SDK- · C++ · 105 lines · 81 code · 13 blank · 11 comment · 3 complexity · d195e9c96ae1abc01972b8245d21b14d MD5 · raw file

  1. // Copyright 2015 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. #include "ios/chrome/browser/passwords/ios_chrome_password_store_factory.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/command_line.h"
  8. #include "base/memory/singleton.h"
  9. #include "components/browser_sync/browser/profile_sync_service.h"
  10. #include "components/keyed_service/core/service_access_type.h"
  11. #include "components/keyed_service/ios/browser_state_dependency_manager.h"
  12. #include "components/password_manager/core/browser/affiliated_match_helper.h"
  13. #include "components/password_manager/core/browser/affiliation_service.h"
  14. #include "components/password_manager/core/browser/affiliation_utils.h"
  15. #include "components/password_manager/core/browser/login_database.h"
  16. #include "components/password_manager/core/browser/password_store_default.h"
  17. #include "components/password_manager/core/browser/password_store_factory_util.h"
  18. #include "components/sync_driver/sync_service.h"
  19. #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
  20. #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
  21. #include "ios/chrome/browser/sync/glue/sync_start_util.h"
  22. #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h"
  23. #include "ios/chrome/browser/web_data_service_factory.h"
  24. #include "ios/web/public/web_thread.h"
  25. // static
  26. scoped_refptr<password_manager::PasswordStore>
  27. IOSChromePasswordStoreFactory::GetForBrowserState(
  28. ios::ChromeBrowserState* browser_state,
  29. ServiceAccessType access_type) {
  30. // |profile| gets always redirected to a non-Incognito profile below, so
  31. // Incognito & IMPLICIT_ACCESS means that incognito browsing session would
  32. // result in traces in the normal profile without the user knowing it.
  33. if (access_type == ServiceAccessType::IMPLICIT_ACCESS &&
  34. browser_state->IsOffTheRecord())
  35. return nullptr;
  36. return make_scoped_refptr(static_cast<password_manager::PasswordStore*>(
  37. GetInstance()->GetServiceForBrowserState(browser_state, true).get()));
  38. }
  39. // static
  40. IOSChromePasswordStoreFactory* IOSChromePasswordStoreFactory::GetInstance() {
  41. return base::Singleton<IOSChromePasswordStoreFactory>::get();
  42. }
  43. // static
  44. void IOSChromePasswordStoreFactory::OnPasswordsSyncedStatePotentiallyChanged(
  45. ios::ChromeBrowserState* browser_state) {
  46. scoped_refptr<password_manager::PasswordStore> password_store =
  47. GetForBrowserState(browser_state, ServiceAccessType::EXPLICIT_ACCESS);
  48. sync_driver::SyncService* sync_service =
  49. IOSChromeProfileSyncServiceFactory::GetForBrowserStateIfExists(
  50. browser_state);
  51. net::URLRequestContextGetter* request_context_getter =
  52. browser_state->GetRequestContext();
  53. password_manager::ToggleAffiliationBasedMatchingBasedOnPasswordSyncedState(
  54. password_store.get(), sync_service, request_context_getter,
  55. browser_state->GetStatePath(),
  56. web::WebThread::GetTaskRunnerForThread(web::WebThread::DB));
  57. }
  58. IOSChromePasswordStoreFactory::IOSChromePasswordStoreFactory()
  59. : RefcountedBrowserStateKeyedServiceFactory(
  60. "PasswordStore",
  61. BrowserStateDependencyManager::GetInstance()) {
  62. DependsOn(ios::WebDataServiceFactory::GetInstance());
  63. }
  64. IOSChromePasswordStoreFactory::~IOSChromePasswordStoreFactory() {}
  65. scoped_refptr<RefcountedKeyedService>
  66. IOSChromePasswordStoreFactory::BuildServiceInstanceFor(
  67. web::BrowserState* context) const {
  68. std::unique_ptr<password_manager::LoginDatabase> login_db(
  69. password_manager::CreateLoginDatabase(context->GetStatePath()));
  70. scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner(
  71. base::ThreadTaskRunnerHandle::Get());
  72. scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner(
  73. web::WebThread::GetTaskRunnerForThread(web::WebThread::DB));
  74. scoped_refptr<password_manager::PasswordStore> store =
  75. new password_manager::PasswordStoreDefault(
  76. main_thread_runner, db_thread_runner, std::move(login_db));
  77. if (!store->Init(ios::sync_start_util::GetFlareForSyncableService(
  78. context->GetStatePath()))) {
  79. // TODO(crbug.com/479725): Remove the LOG once this error is visible in the
  80. // UI.
  81. LOG(WARNING) << "Could not initialize password store.";
  82. return nullptr;
  83. }
  84. return store;
  85. }
  86. web::BrowserState* IOSChromePasswordStoreFactory::GetBrowserStateToUse(
  87. web::BrowserState* context) const {
  88. return GetBrowserStateRedirectedInIncognito(context);
  89. }
  90. bool IOSChromePasswordStoreFactory::ServiceIsNULLWhileTesting() const {
  91. return true;
  92. }