simple_key_map.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 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_SIMPLE_KEY_MAP_H_
  5. #define COMPONENTS_KEYED_SERVICE_CORE_SIMPLE_KEY_MAP_H_
  6. #include <map>
  7. #include "base/no_destructor.h"
  8. #include "components/keyed_service/core/keyed_service_export.h"
  9. namespace content {
  10. class BrowserContext;
  11. } // namespace content
  12. class SimpleFactoryKey;
  13. // Stores a mapping from BrowserContexts to SimpleFactoryKeys.
  14. //
  15. // Use this class to get the SimpleFactoryKey that is associated with a given
  16. // BrowserContext, when the BrowserContext is available and a
  17. // SimpleKeyedServiceFactory for that BrowserContext is needed. For example,
  18. // inside BuildServiceInstanceFor() in a BrowserContextKeyedServiceFactory that
  19. // depends on a SimpleKeyedServiceFactory.
  20. //
  21. // This mapping is not stored as a member in BrowserContext because
  22. // SimpleFactoryKeys are not a content layer concept, but a components level
  23. // concept.
  24. class KEYED_SERVICE_EXPORT SimpleKeyMap {
  25. public:
  26. static SimpleKeyMap* GetInstance();
  27. SimpleKeyMap(const SimpleKeyMap&) = delete;
  28. SimpleKeyMap& operator=(const SimpleKeyMap&) = delete;
  29. // When |browser_context| creates or takes ownership of a SimpleFactoryKey
  30. // |key|, it should register this association in this map.
  31. void Associate(content::BrowserContext* browser_context,
  32. SimpleFactoryKey* key);
  33. // When |browser_context| is destroyed or loses ownership of a
  34. // SimpleFactoryKey, it should erase its association from this map.
  35. void Dissociate(content::BrowserContext* browser_context);
  36. // Gets the SimpleFactoryKey associated with |browser_context|.
  37. SimpleFactoryKey* GetForBrowserContext(
  38. content::BrowserContext* browser_context);
  39. private:
  40. friend class base::NoDestructor<SimpleKeyMap>;
  41. SimpleKeyMap();
  42. ~SimpleKeyMap();
  43. std::map<content::BrowserContext*, SimpleFactoryKey*> mapping_;
  44. };
  45. #endif // COMPONENTS_KEYED_SERVICE_CORE_SIMPLE_KEY_MAP_H_