blob: ce0c20a9327cae51f57fe4d592342cde7c8e6899 [file] [log] [blame]
// Copyright 2020 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 <string>
#include <utility>
#include "chromeos/printing/fake_printer_config_cache.h"
#include "base/containers/flat_map.h"
#include "base/strings/string_piece.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chromeos/printing/printer_config_cache.h"
namespace chromeos {
FakePrinterConfigCache::FakePrinterConfigCache() = default;
FakePrinterConfigCache::~FakePrinterConfigCache() = default;
void FakePrinterConfigCache::SetFetchResponseForTesting(
base::StringPiece key,
base::StringPiece value) {
contents_.insert_or_assign(std::string(key), std::string(value));
// If Fetch(|key|) was previously being consumed by prior call to
// DiscardFetchRequestFor(), we unblock it now.
fetch_requests_to_ignore_.erase(key);
}
void FakePrinterConfigCache::DiscardFetchRequestFor(base::StringPiece key) {
fetch_requests_to_ignore_.insert(std::string(key));
contents_.erase(key);
}
void FakePrinterConfigCache::Fetch(const std::string& key,
base::TimeDelta unused_expiration,
PrinterConfigCache::FetchCallback cb) {
if (contents_.contains(key)) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(cb),
PrinterConfigCache::FetchResult::Success(
key, contents_.at(key), base::Time())));
return;
} else if (fetch_requests_to_ignore_.contains(key)) {
// Caller has directed us, by way of DiscardFetchRequestFor(), to
// _not_ respond to this Fetch().
return;
}
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(cb),
PrinterConfigCache::FetchResult::Failure(key)));
}
void FakePrinterConfigCache::Drop(const std::string& key) {
contents_.erase(key);
// If Fetch(|key|) was previously being consumed by prior call to
// DiscardFetchRequestFor(), we unblock it now.
fetch_requests_to_ignore_.erase(key);
}
} // namespace chromeos