blob: ff60e6b39939dcbb3f5ef0493759e0e9c5595de4 [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.
//
// The PrinterConfigCache class accepts requests to fetch things from
// the Chrome OS Printing serving root. It only stores things in memory.
//
// In practice, the present class fetches either PPDs or PPD metadata.
#ifndef CHROMEOS_PRINTING_PRINTER_CONFIG_CACHE_H_
#define CHROMEOS_PRINTING_PRINTER_CONFIG_CACHE_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_piece.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "services/network/public/cpp/simple_url_loader.h"
namespace chromeos {
// A PrinterConfigCache maps keys to values. By convention, keys are
// relative paths to files in the Chrome OS Printing serving root
// (hardcoded into this class). In practice, that means keys will either
// * start with "metadata_v3/" or
// * start with "ppds_for_metadata_v3/."
//
// This class must always be constructed on, used on, and destroyed from
// a sequenced context.
class COMPONENT_EXPORT(CHROMEOS_PRINTING) PrinterConfigCache {
public:
// |loader_factory_dispenser| is a functor that can create fresh
// URLLoaderFactory instances. We use this indirection to avoid
// caching raw pointers to URLLoaderFactory instances, which are
// invalidated by network service restarts.
//
// Caller must guarantee that |loader_factory_dispenser| is always
// safe to Run() for the lifetime of |this|.
static std::unique_ptr<PrinterConfigCache> Create(
const base::Clock* clock,
base::RepeatingCallback<network::mojom::URLLoaderFactory*()>);
virtual ~PrinterConfigCache() = default;
// Result of calling Fetch(). The |key| identifies how Fetch() was
// originally invoked. The |contents| and |time_of_fetch| are well-
// defined iff |succeeded| is true.
struct FetchResult {
static FetchResult Failure(const std::string& key);
static FetchResult Success(const std::string& key,
const std::string& contents,
base::Time time_of_fetch);
bool succeeded;
std::string key;
std::string contents;
base::Time time_of_fetch;
};
// Caller is responsible for providing sequencing of this type.
using FetchCallback = base::OnceCallback<void(const FetchResult&)>;
// Queries the Chrome OS Printing serving root for |key|. Calls |cb|
// with the contents. If an entry newer than |expiration| is resident,
// calls |cb| immediately with those contents. Caller should not pass
// keys with leading slashes.
//
// Using TimeDelta implies the caller is asking for "some entry not
// older than |expiration|," e.g. "metadata_v3/index-00.json that
// was fetched within the last 30 minutes."
//
// Naturally,
// * passing the Max() TimeDelta means "perform this Fetch() with no
// limit on staleness" and
// * passing a zero TimeDelta should practically force a networked
// fetch (less esoteric timing quirks etc.).
virtual void Fetch(const std::string& key,
base::TimeDelta expiration,
FetchCallback cb) = 0;
// Drops Entry corresponding to |key|.
virtual void Drop(const std::string& key) = 0;
};
} // namespace chromeos
#endif // CHROMEOS_PRINTING_PRINTER_CONFIG_CACHE_H_