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

Unified Diff: chromeos/printing/ppd_cache.cc

Issue 2487343006: Add in-memory caching to PpdCache available printers. (Closed)
Patch Set: 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
Index: chromeos/printing/ppd_cache.cc
diff --git a/chromeos/printing/ppd_cache.cc b/chromeos/printing/ppd_cache.cc
index 4d89e62f6483599e5694f2f8ce0b81aa962c0c99..eff5185ef47fda1f150ee9aaa562c4827d9332a5 100644
--- a/chromeos/printing/ppd_cache.cc
+++ b/chromeos/printing/ppd_cache.cc
@@ -91,23 +91,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_.reset(new 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;
@@ -122,18 +130,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));
@@ -224,8 +234,10 @@ 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.
//
// If we have an invalid file and it's not valid for reasons other than
@@ -239,6 +251,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 (whether
+ // from disk or server), and remains for the life of the PpdCache.
skau 2016/11/10 20:46:37 This isn't strictly true. It's reset when the cac
Carlson 2016/11/11 01:39:27 You're right, I changed this after I wrote the com
+ 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_;

Powered by Google App Engine
This is Rietveld 408576698