/ios/chrome/browser/bookmarks/managed_bookmark_service_factory.mm

https://github.com/chromium/chromium · Objective C++ · 82 lines · 60 code · 15 blank · 7 comment · 4 complexity · 3ceb760fd11b3ddb18468ac369614e66 MD5 · raw file

  1. // Copyright 2020 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/browser/bookmarks/managed_bookmark_service_factory.h"
  5. #include "base/no_destructor.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #include "components/bookmarks/managed/managed_bookmark_service.h"
  8. #include "components/keyed_service/ios/browser_state_dependency_manager.h"
  9. #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
  10. #include "ios/chrome/browser/signin/authentication_service.h"
  11. #include "ios/chrome/browser/signin/authentication_service_factory.h"
  12. #import "ios/public/provider/chrome/browser/chrome_browser_provider.h"
  13. #import "ios/public/provider/chrome/browser/signin/chrome_identity.h"
  14. #include "ios/public/provider/chrome/browser/signin/chrome_identity_service.h"
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. namespace {
  19. std::string GetManagedBookmarksDomain(ChromeBrowserState* browser_state) {
  20. AuthenticationService* auth_service =
  21. AuthenticationServiceFactory::GetForBrowserState(browser_state);
  22. if (!auth_service)
  23. return std::string();
  24. ChromeIdentity* identity =
  25. auth_service->GetPrimaryIdentity(signin::ConsentLevel::kSignin);
  26. if (!identity)
  27. return std::string();
  28. ios::ChromeIdentityService* identity_service =
  29. ios::GetChromeBrowserProvider().GetChromeIdentityService();
  30. if (!identity_service)
  31. return std::string();
  32. return base::SysNSStringToUTF8(
  33. identity_service->GetCachedHostedDomainForIdentity(identity));
  34. }
  35. } // namespace
  36. // static
  37. bookmarks::ManagedBookmarkService*
  38. ManagedBookmarkServiceFactory::GetForBrowserState(
  39. ChromeBrowserState* browser_state) {
  40. return static_cast<bookmarks::ManagedBookmarkService*>(
  41. GetInstance()->GetServiceForBrowserState(browser_state, true));
  42. }
  43. // static
  44. ManagedBookmarkServiceFactory* ManagedBookmarkServiceFactory::GetInstance() {
  45. static base::NoDestructor<ManagedBookmarkServiceFactory> instance;
  46. return instance.get();
  47. }
  48. ManagedBookmarkServiceFactory::ManagedBookmarkServiceFactory()
  49. : BrowserStateKeyedServiceFactory(
  50. "ManagedBookmarkService",
  51. BrowserStateDependencyManager::GetInstance()) {}
  52. ManagedBookmarkServiceFactory::~ManagedBookmarkServiceFactory() {}
  53. std::unique_ptr<KeyedService>
  54. ManagedBookmarkServiceFactory::BuildServiceInstanceFor(
  55. web::BrowserState* context) const {
  56. ChromeBrowserState* browser_state =
  57. ChromeBrowserState::FromBrowserState(context);
  58. // base::Unretained is safe because ManagedBookmarkService will
  59. // be destroyed before the browser_state it is attached to.
  60. return std::make_unique<bookmarks::ManagedBookmarkService>(
  61. browser_state->GetPrefs(),
  62. base::BindRepeating(&GetManagedBookmarksDomain,
  63. base::Unretained(browser_state)));
  64. }
  65. bool ManagedBookmarkServiceFactory::ServiceIsNULLWhileTesting() const {
  66. return true;
  67. }