/components/keyed_service/content/browser_context_dependency_manager.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 107 lines · 49 code · 20 blank · 38 comment · 0 complexity · 445db78862792c1e364f1589b35d287a 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_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
  5. #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
  6. #include "base/callback_forward.h"
  7. #include "base/callback_list.h"
  8. #include "base/memory/scoped_ptr.h"
  9. #include "components/keyed_service/core/dependency_manager.h"
  10. #include "components/keyed_service/core/keyed_service_export.h"
  11. class BrowserContextKeyedBaseFactory;
  12. namespace base {
  13. template <typename T>
  14. struct DefaultSingletonTraits;
  15. } // namespace base
  16. namespace content {
  17. class BrowserContext;
  18. }
  19. namespace user_prefs {
  20. class PrefRegistrySyncable;
  21. }
  22. // A singleton that listens for context destruction notifications and
  23. // rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order
  24. // based on the stated dependencies by each service.
  25. class KEYED_SERVICE_EXPORT BrowserContextDependencyManager
  26. : public DependencyManager {
  27. public:
  28. // Registers profile-specific preferences for all services via |registry|.
  29. // |context| should be the BrowserContext containing |registry| and is used as
  30. // a key to prevent multiple registrations on the same BrowserContext in
  31. // tests.
  32. void RegisterProfilePrefsForServices(
  33. content::BrowserContext* context,
  34. user_prefs::PrefRegistrySyncable* registry);
  35. // Called by each BrowserContext to alert us of its creation. Several
  36. // services want to be started when a context is created. If you want your
  37. // KeyedService to be started with the BrowserContext, override
  38. // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to
  39. // return true. This method also registers any service-related preferences
  40. // for non-incognito profiles.
  41. void CreateBrowserContextServices(content::BrowserContext* context);
  42. // Similar to CreateBrowserContextServices(), except this is used for creating
  43. // test BrowserContexts - these contexts will not create services for any
  44. // BrowserContextKeyedBaseFactories that return true from
  45. // ServiceIsNULLWhileTesting().
  46. void CreateBrowserContextServicesForTest(content::BrowserContext* context);
  47. // Called by each BrowserContext to alert us that we should destroy services
  48. // associated with it.
  49. void DestroyBrowserContextServices(content::BrowserContext* context);
  50. // Registers a |callback| that will be called just before executing
  51. // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
  52. // This can be useful in browser tests which wish to substitute test or mock
  53. // builders for the keyed services.
  54. scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription>
  55. RegisterWillCreateBrowserContextServicesCallbackForTesting(
  56. const base::Callback<void(content::BrowserContext*)>& callback);
  57. #ifndef NDEBUG
  58. // Debugging assertion called as part of GetServiceForBrowserContext in debug
  59. // mode. This will NOTREACHED() whenever the user is trying to access a stale
  60. // BrowserContext*.
  61. void AssertBrowserContextWasntDestroyed(content::BrowserContext* context);
  62. // Marks |context| as live (i.e., not stale). This method can be called as a
  63. // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off
  64. // due to |context| aliasing a BrowserContext instance from a prior test
  65. // (i.e., 0xWhatever might be created, be destroyed, and then a new
  66. // BrowserContext object might be created at 0xWhatever).
  67. void MarkBrowserContextLiveForTesting(content::BrowserContext* context);
  68. #endif // NDEBUG
  69. static BrowserContextDependencyManager* GetInstance();
  70. private:
  71. friend class BrowserContextDependencyManagerUnittests;
  72. friend struct base::DefaultSingletonTraits<BrowserContextDependencyManager>;
  73. // Helper function used by CreateBrowserContextServices[ForTest].
  74. void DoCreateBrowserContextServices(content::BrowserContext* context,
  75. bool is_testing_context);
  76. BrowserContextDependencyManager();
  77. ~BrowserContextDependencyManager() override;
  78. #ifndef NDEBUG
  79. // DependencyManager:
  80. void DumpContextDependencies(base::SupportsUserData* context) const final;
  81. #endif // NDEBUG
  82. // A list of callbacks to call just before executing
  83. // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
  84. base::CallbackList<void(content::BrowserContext*)>
  85. will_create_browser_context_services_callbacks_;
  86. };
  87. #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_