2017-09-11 20:42:30 +02:00
|
|
|
// Copyright 2017 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/extensions/value_store/cef_value_store_factory.h"
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
#include "libcef/browser/extensions/value_store/cef_value_store.h"
|
|
|
|
|
|
|
|
#include "base/containers/contains.h"
|
2017-09-11 20:42:30 +02:00
|
|
|
#include "base/memory/ptr_util.h"
|
2021-10-19 00:17:16 +02:00
|
|
|
#include "components/value_store/leveldb_value_store.h"
|
2017-09-11 20:42:30 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kUMAClientName[] = "Cef";
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
namespace value_store {
|
2017-09-11 20:42:30 +02:00
|
|
|
|
|
|
|
CefValueStoreFactory::CefValueStoreFactory() = default;
|
|
|
|
|
|
|
|
CefValueStoreFactory::CefValueStoreFactory(const base::FilePath& db_path)
|
|
|
|
: db_path_(db_path) {}
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
CefValueStoreFactory::~CefValueStoreFactory() = default;
|
2017-09-11 20:42:30 +02:00
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
std::unique_ptr<ValueStore> CefValueStoreFactory::CreateValueStore(
|
|
|
|
const base::FilePath& directory,
|
|
|
|
const std::string& uma_client_name) {
|
|
|
|
std::unique_ptr<ValueStore> value_store(CreateStore());
|
|
|
|
// This factory is purposely keeping the raw pointers to each ValueStore
|
|
|
|
// created. Cefs using CefValueStoreFactory must be careful to keep
|
|
|
|
// those ValueStore's alive for the duration of their test.
|
|
|
|
value_store_map_[directory] = value_store.get();
|
|
|
|
return value_store;
|
2017-09-11 20:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ValueStore* CefValueStoreFactory::LastCreatedStore() const {
|
|
|
|
return last_created_store_;
|
|
|
|
}
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
void CefValueStoreFactory::DeleteValueStore(const base::FilePath& directory) {
|
|
|
|
value_store_map_.erase(directory);
|
2017-09-11 20:42:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
bool CefValueStoreFactory::HasValueStore(const base::FilePath& directory) {
|
|
|
|
return base::Contains(value_store_map_, directory);
|
2017-09-11 20:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ValueStore* CefValueStoreFactory::GetExisting(
|
2021-09-20 11:06:23 +02:00
|
|
|
const base::FilePath& directory) const {
|
|
|
|
auto it = value_store_map_.find(directory);
|
|
|
|
DCHECK(it != value_store_map_.end());
|
|
|
|
return it->second;
|
2017-09-11 20:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefValueStoreFactory::Reset() {
|
|
|
|
last_created_store_ = nullptr;
|
2021-09-20 11:06:23 +02:00
|
|
|
value_store_map_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ValueStore> CefValueStoreFactory::CreateStore() {
|
|
|
|
std::unique_ptr<ValueStore> store;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (db_path_.empty()) {
|
2021-09-20 11:06:23 +02:00
|
|
|
store = std::make_unique<CefValueStore>();
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2021-09-20 11:06:23 +02:00
|
|
|
store = std::make_unique<LeveldbValueStore>(kUMAClientName, db_path_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-09-20 11:06:23 +02:00
|
|
|
last_created_store_ = store.get();
|
|
|
|
return store;
|
2017-09-11 20:42:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
} // namespace value_store
|