keyed_service_shutdown_notifier.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. #ifndef COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_SHUTDOWN_NOTIFIER_H_
  5. #define COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_SHUTDOWN_NOTIFIER_H_
  6. #include "base/callback_list.h"
  7. #include "components/keyed_service/core/keyed_service.h"
  8. // This is a helper class for objects that depend on one or more keyed services,
  9. // but which cannot be keyed services themselves, for example because they don't
  10. // correspond 1:1 to a context, or because they have a different lifetime.
  11. //
  12. // To use this class, add a factory class and declare the dependencies there.
  13. // This class (being a KeyedService itself) will be shut down before its
  14. // dependencies and notify its observers.
  15. class KEYED_SERVICE_EXPORT KeyedServiceShutdownNotifier : public KeyedService {
  16. public:
  17. KeyedServiceShutdownNotifier();
  18. KeyedServiceShutdownNotifier(const KeyedServiceShutdownNotifier&) = delete;
  19. KeyedServiceShutdownNotifier& operator=(const KeyedServiceShutdownNotifier&) =
  20. delete;
  21. ~KeyedServiceShutdownNotifier() override;
  22. // Subscribe for a notification when the keyed services this object depends on
  23. // (as defined by its factory) are shut down. The subscription can be
  24. // destroyed to unsubscribe.
  25. base::CallbackListSubscription Subscribe(base::OnceClosure callback);
  26. private:
  27. // KeyedService implementation:
  28. void Shutdown() override;
  29. base::OnceClosureList closure_list_;
  30. };
  31. #endif // COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_SHUTDOWN_NOTIFIER_H_