• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Embedded Framework Authors.
2 // Portions copyright 2016 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include "libcef/browser/extensions/value_store/cef_value_store_factory.h"
7 
8 #include "libcef/browser/extensions/value_store/cef_value_store.h"
9 
10 #include "base/containers/contains.h"
11 #include "base/memory/ptr_util.h"
12 #include "components/value_store/leveldb_value_store.h"
13 
14 namespace {
15 
16 const char kUMAClientName[] = "Cef";
17 
18 }  // namespace
19 
20 namespace value_store {
21 
22 CefValueStoreFactory::CefValueStoreFactory() = default;
23 
CefValueStoreFactory(const base::FilePath & db_path)24 CefValueStoreFactory::CefValueStoreFactory(const base::FilePath& db_path)
25     : db_path_(db_path) {}
26 
27 CefValueStoreFactory::~CefValueStoreFactory() = default;
28 
CreateValueStore(const base::FilePath & directory,const std::string & uma_client_name)29 std::unique_ptr<ValueStore> CefValueStoreFactory::CreateValueStore(
30     const base::FilePath& directory,
31     const std::string& uma_client_name) {
32   std::unique_ptr<ValueStore> value_store(CreateStore());
33   // This factory is purposely keeping the raw pointers to each ValueStore
34   // created. Cefs using CefValueStoreFactory must be careful to keep
35   // those ValueStore's alive for the duration of their test.
36   value_store_map_[directory] = value_store.get();
37   return value_store;
38 }
39 
LastCreatedStore() const40 ValueStore* CefValueStoreFactory::LastCreatedStore() const {
41   return last_created_store_;
42 }
43 
DeleteValueStore(const base::FilePath & directory)44 void CefValueStoreFactory::DeleteValueStore(const base::FilePath& directory) {
45   value_store_map_.erase(directory);
46 }
47 
HasValueStore(const base::FilePath & directory)48 bool CefValueStoreFactory::HasValueStore(const base::FilePath& directory) {
49   return base::Contains(value_store_map_, directory);
50 }
51 
GetExisting(const base::FilePath & directory) const52 ValueStore* CefValueStoreFactory::GetExisting(
53     const base::FilePath& directory) const {
54   auto it = value_store_map_.find(directory);
55   DCHECK(it != value_store_map_.end());
56   return it->second;
57 }
58 
Reset()59 void CefValueStoreFactory::Reset() {
60   last_created_store_ = nullptr;
61   value_store_map_.clear();
62 }
63 
CreateStore()64 std::unique_ptr<ValueStore> CefValueStoreFactory::CreateStore() {
65   std::unique_ptr<ValueStore> store;
66   if (db_path_.empty())
67     store = std::make_unique<CefValueStore>();
68   else
69     store = std::make_unique<LeveldbValueStore>(kUMAClientName, db_path_);
70   last_created_store_ = store.get();
71   return store;
72 }
73 
74 }  // namespace value_store
75