simple_key_map.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. #include "components/keyed_service/core/simple_key_map.h"
  5. #include "base/check.h"
  6. #include "base/no_destructor.h"
  7. SimpleKeyMap::SimpleKeyMap() = default;
  8. SimpleKeyMap::~SimpleKeyMap() = default;
  9. // static
  10. SimpleKeyMap* SimpleKeyMap::GetInstance() {
  11. static base::NoDestructor<SimpleKeyMap> provider;
  12. return provider.get();
  13. }
  14. void SimpleKeyMap::Associate(content::BrowserContext* browser_context,
  15. SimpleFactoryKey* key) {
  16. DCHECK(browser_context);
  17. DCHECK(key);
  18. DCHECK(mapping_.find(browser_context) == mapping_.end());
  19. mapping_[browser_context] = key;
  20. }
  21. SimpleFactoryKey* SimpleKeyMap::GetForBrowserContext(
  22. content::BrowserContext* browser_context) {
  23. const auto& it = mapping_.find(browser_context);
  24. if (it == mapping_.end()) {
  25. DCHECK(false);
  26. return nullptr;
  27. }
  28. return it->second;
  29. }
  30. void SimpleKeyMap::Dissociate(content::BrowserContext* browser_context) {
  31. DCHECK(mapping_.find(browser_context) != mapping_.end());
  32. mapping_.erase(browser_context);
  33. }