/components/keyed_service/ios/browser_state_dependency_manager.h

https://github.com/chromium/chromium · C Header · 85 lines · 40 code · 18 blank · 27 comment · 0 complexity · e3d16ef71fe7b366a22f9f6cc2c46f00 MD5 · raw file

  1. // Copyright 2014 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. #ifndef COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_DEPENDENCY_MANAGER_H_
  5. #define COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_DEPENDENCY_MANAGER_H_
  6. #include "base/callback_list.h"
  7. #include "components/keyed_service/core/dependency_manager.h"
  8. #include "components/keyed_service/core/keyed_service_export.h"
  9. namespace base {
  10. template <typename T>
  11. struct DefaultSingletonTraits;
  12. } // namespace base
  13. namespace web {
  14. class BrowserState;
  15. }
  16. namespace user_prefs {
  17. class PrefRegistrySyncable;
  18. }
  19. // A singleton that listens for context destruction notifications and
  20. // rebroadcasts them to each KeyedServiceBaseFactory in a safe order
  21. // based on the stated dependencies by each service.
  22. class KEYED_SERVICE_EXPORT BrowserStateDependencyManager
  23. : public DependencyManager {
  24. public:
  25. static BrowserStateDependencyManager* GetInstance();
  26. BrowserStateDependencyManager(const BrowserStateDependencyManager&) = delete;
  27. BrowserStateDependencyManager& operator=(
  28. const BrowserStateDependencyManager&) = delete;
  29. // Registers context-specific preferences for all services via |registry|.
  30. void RegisterBrowserStatePrefsForServices(
  31. user_prefs::PrefRegistrySyncable* registry);
  32. // Called by each BrowserState to alert us of its creation. Service that
  33. // want to be started when BrowserState is created should override the
  34. // ServiceIsCreatedWithBrowserState() method in their factory. Preferences
  35. // registration also happens during that method call.
  36. void CreateBrowserStateServices(web::BrowserState* context);
  37. // Similar to CreateBrowserStateServices(), except this is used for creating
  38. // test BrowserStates - these contexts will not create services for any
  39. // BrowserStateKeyedBaseFactories that return true from
  40. // ServiceIsNULLWhileTesting().
  41. void CreateBrowserStateServicesForTest(web::BrowserState* context);
  42. // Called by each BrowserState to alert us that we should destroy services
  43. // associated with it.
  44. void DestroyBrowserStateServices(web::BrowserState* context);
  45. // Runtime assertion called as a part of GetServiceForBrowserState() to check
  46. // if |context| is considered stale. This will NOTREACHED() or
  47. // base::debug::DumpWithoutCrashing() depending on the DCHECK_IS_ON() value.
  48. void AssertBrowserStateWasntDestroyed(web::BrowserState* context) const;
  49. // Marks |context| as live (i.e., not stale). This method can be called as a
  50. // safeguard against |AssertBrowserStateWasntDestroyed()| checks going off
  51. // due to |context| aliasing a BrowserState instance from a prior construction
  52. // (i.e., 0xWhatever might be created, be destroyed, and then a new
  53. // BrowserState object might be created at 0xWhatever).
  54. void MarkBrowserStateLive(web::BrowserState* context);
  55. private:
  56. friend struct base::DefaultSingletonTraits<BrowserStateDependencyManager>;
  57. BrowserStateDependencyManager();
  58. ~BrowserStateDependencyManager() override;
  59. // Helper function used by CreateBrowserStateServices[ForTest].
  60. void DoCreateBrowserStateServices(web::BrowserState* context,
  61. bool is_testing_context);
  62. #ifndef NDEBUG
  63. // DependencyManager:
  64. void DumpContextDependencies(void* context) const final;
  65. #endif // NDEBUG
  66. };
  67. #endif // COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_DEPENDENCY_MANAGER_H_