Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Unified Diff: chromeos/printing/ppd_cache.cc

Issue 2487343006: Add in-memory caching to PpdCache available printers. (Closed)
Patch Set: Address thestig@ comment Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/printing/ppd_cache.h ('k') | chromeos/printing/ppd_cache_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/printing/ppd_cache.cc
diff --git a/chromeos/printing/ppd_cache.cc b/chromeos/printing/ppd_cache.cc
index 2b4abd1288111de8b4a9a3db9ea639f4b874e870..437d626a5b3fecc1baab73a02398581731b289b9 100644
--- a/chromeos/printing/ppd_cache.cc
+++ b/chromeos/printing/ppd_cache.cc
@@ -90,23 +90,31 @@ class PpdCacheImpl : public PpdCache {
return ret;
}
- base::Optional<PpdProvider::AvailablePrintersMap> FindAvailablePrinters()
- override {
+ const PpdProvider::AvailablePrintersMap* FindAvailablePrinters() override {
+ if (available_printers_ != nullptr &&
+ base::Time::Now() - available_printers_timestamp_ <
+ options_.max_available_list_staleness) {
+ // Satisfy from memory cache.
+ return available_printers_.get();
+ }
std::string buf;
if (!MaybeReadAvailablePrintersCache(&buf)) {
- return base::nullopt;
+ // Disk cache miss.
+ return nullptr;
}
auto dict = base::DictionaryValue::From(base::JSONReader::Read(buf));
if (dict == nullptr) {
LOG(ERROR) << "Failed to deserialize available printers cache";
- return base::nullopt;
+ return nullptr;
}
- PpdProvider::AvailablePrintersMap ret;
+ // Note if we got here, we've already set available_printers_timestamp_ to
+ // the mtime of the file we read from.
+ available_printers_ = base::MakeUnique<PpdProvider::AvailablePrintersMap>();
const base::ListValue* models;
std::string model;
for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
it.Advance()) {
- auto& out = ret[it.key()];
+ auto& out = (*available_printers_)[it.key()];
if (!it.value().GetAsList(&models)) {
LOG(ERROR) << "Skipping malformed printer make: " << it.key();
continue;
@@ -121,18 +129,20 @@ class PpdCacheImpl : public PpdCache {
}
}
}
- return ret;
+ return available_printers_.get();
}
// Note we throw up our hands and fail (gracefully) to store if we encounter
// non-unicode things in the strings of |available_printers|. Since these
// strings come from a source we control, being less paranoid about these
// values seems reasonable.
- void StoreAvailablePrinters(
- const PpdProvider::AvailablePrintersMap& available_printers) override {
+ void StoreAvailablePrinters(std::unique_ptr<PpdProvider::AvailablePrintersMap>
+ available_printers) override {
+ available_printers_ = std::move(available_printers);
+ available_printers_timestamp_ = base::Time::Now();
// Convert the map to Values, in preparation for jsonification.
base::DictionaryValue top_level;
- for (const auto& entry : available_printers) {
+ for (const auto& entry : *available_printers_) {
auto printers = base::MakeUnique<base::ListValue>();
printers->AppendStrings(entry.second);
top_level.Set(entry.first, std::move(printers));
@@ -223,6 +233,7 @@ class PpdCacheImpl : public PpdCache {
buf->clear();
return false;
}
+ available_printers_timestamp_ = info.last_modified;
return true;
}
// Either we don't have an openable file, or it's too old.
@@ -238,6 +249,14 @@ class PpdCacheImpl : public PpdCache {
return false;
}
+ // In-memory copy of the available printers map, null if we don't have an
+ // in-memory copy yet. Filled in the first time the map is fetched from
+ // disk or stored.
+ std::unique_ptr<PpdProvider::AvailablePrintersMap> available_printers_;
+ // Timestamp for the in-memory copy of the cache. (The on-disk version uses
+ // the file mtime).
+ base::Time available_printers_timestamp_;
+
const base::FilePath cache_base_dir_;
const base::FilePath available_printers_file_;
const PpdCache::Options options_;
« no previous file with comments | « chromeos/printing/ppd_cache.h ('k') | chromeos/printing/ppd_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698