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

Side by Side Diff: chromeos/printing/ppd_cache.cc

Issue 2487343006: Add in-memory caching to PpdCache available printers. (Closed)
Patch Set: Address skau comments 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/printing/ppd_cache.h" 5 #include "chromeos/printing/ppd_cache.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // something is exceptionally hosed. 83 // something is exceptionally hosed.
84 if (!base::DeleteFile(contents_path, false)) { 84 if (!base::DeleteFile(contents_path, false)) {
85 LOG(ERROR) << "Failed to cleanup partially-written file " 85 LOG(ERROR) << "Failed to cleanup partially-written file "
86 << contents_path.LossyDisplayName(); 86 << contents_path.LossyDisplayName();
87 return ret; 87 return ret;
88 } 88 }
89 } 89 }
90 return ret; 90 return ret;
91 } 91 }
92 92
93 base::Optional<PpdProvider::AvailablePrintersMap> FindAvailablePrinters() 93 const PpdProvider::AvailablePrintersMap* FindAvailablePrinters() override {
94 override { 94 if (available_printers_ != nullptr &&
95 base::Time::Now() - available_printers_timestamp_ <
96 options_.max_available_list_staleness) {
97 // Satisfy from memory cache.
98 return available_printers_.get();
99 }
95 std::string buf; 100 std::string buf;
96 if (!MaybeReadAvailablePrintersCache(&buf)) { 101 if (!MaybeReadAvailablePrintersCache(&buf)) {
97 return base::nullopt; 102 // Disk cache miss.
103 return nullptr;
98 } 104 }
99 auto dict = base::DictionaryValue::From(base::JSONReader::Read(buf)); 105 auto dict = base::DictionaryValue::From(base::JSONReader::Read(buf));
100 if (dict == nullptr) { 106 if (dict == nullptr) {
101 LOG(ERROR) << "Failed to deserialize available printers cache"; 107 LOG(ERROR) << "Failed to deserialize available printers cache";
102 return base::nullopt; 108 return nullptr;
103 } 109 }
104 PpdProvider::AvailablePrintersMap ret; 110 // Note if we got here, we've already set available_printers_timestamp_ to
111 // the mtime of the file we read from.
112 available_printers_.reset(new PpdProvider::AvailablePrintersMap);
Lei Zhang 2016/11/11 20:01:50 nit: Can you use MakeUnique instead? available_pr
Carlson 2016/11/11 20:26:42 Done.
105 const base::ListValue* models; 113 const base::ListValue* models;
106 std::string model; 114 std::string model;
107 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); 115 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
108 it.Advance()) { 116 it.Advance()) {
109 auto& out = ret[it.key()]; 117 auto& out = (*available_printers_)[it.key()];
110 if (!it.value().GetAsList(&models)) { 118 if (!it.value().GetAsList(&models)) {
111 LOG(ERROR) << "Skipping malformed printer make: " << it.key(); 119 LOG(ERROR) << "Skipping malformed printer make: " << it.key();
112 continue; 120 continue;
113 } 121 }
114 for (const auto& model_value : *models) { 122 for (const auto& model_value : *models) {
115 if (model_value->GetAsString(&model)) { 123 if (model_value->GetAsString(&model)) {
116 out.push_back(model); 124 out.push_back(model);
117 } else { 125 } else {
118 LOG(ERROR) << "Skipping malformed printer model in: " << it.key() 126 LOG(ERROR) << "Skipping malformed printer model in: " << it.key()
119 << ". Expected a string, found a " 127 << ". Expected a string, found a "
120 << base::Value::GetTypeName(model_value->GetType()); 128 << base::Value::GetTypeName(model_value->GetType());
121 } 129 }
122 } 130 }
123 } 131 }
124 return ret; 132 return available_printers_.get();
125 } 133 }
126 134
127 // Note we throw up our hands and fail (gracefully) to store if we encounter 135 // Note we throw up our hands and fail (gracefully) to store if we encounter
128 // non-unicode things in the strings of |available_printers|. Since these 136 // non-unicode things in the strings of |available_printers|. Since these
129 // strings come from a source we control, being less paranoid about these 137 // strings come from a source we control, being less paranoid about these
130 // values seems reasonable. 138 // values seems reasonable.
131 void StoreAvailablePrinters( 139 void StoreAvailablePrinters(std::unique_ptr<PpdProvider::AvailablePrintersMap>
132 const PpdProvider::AvailablePrintersMap& available_printers) override { 140 available_printers) override {
141 available_printers_ = std::move(available_printers);
142 available_printers_timestamp_ = base::Time::Now();
133 // Convert the map to Values, in preparation for jsonification. 143 // Convert the map to Values, in preparation for jsonification.
134 base::DictionaryValue top_level; 144 base::DictionaryValue top_level;
135 for (const auto& entry : available_printers) { 145 for (const auto& entry : *available_printers_) {
136 auto printers = base::MakeUnique<base::ListValue>(); 146 auto printers = base::MakeUnique<base::ListValue>();
137 printers->AppendStrings(entry.second); 147 printers->AppendStrings(entry.second);
138 top_level.Set(entry.first, std::move(printers)); 148 top_level.Set(entry.first, std::move(printers));
139 } 149 }
140 std::string contents; 150 std::string contents;
141 if (!base::JSONWriter::Write(top_level, &contents)) { 151 if (!base::JSONWriter::Write(top_level, &contents)) {
142 LOG(ERROR) << "Failed to generate JSON"; 152 LOG(ERROR) << "Failed to generate JSON";
143 return; 153 return;
144 } 154 }
145 if (contents.size() > options_.max_available_list_cached_size) { 155 if (contents.size() > options_.max_available_list_cached_size) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 (base::Time::Now() - info.last_modified <= 226 (base::Time::Now() - info.last_modified <=
217 options_.max_available_list_staleness)) { 227 options_.max_available_list_staleness)) {
218 // We have a file that's recent enough to use. 228 // We have a file that's recent enough to use.
219 if (!base::ReadFileToStringWithMaxSize( 229 if (!base::ReadFileToStringWithMaxSize(
220 available_printers_file_, buf, 230 available_printers_file_, buf,
221 options_.max_available_list_cached_size)) { 231 options_.max_available_list_cached_size)) {
222 LOG(ERROR) << "Failed to read printer cache"; 232 LOG(ERROR) << "Failed to read printer cache";
223 buf->clear(); 233 buf->clear();
224 return false; 234 return false;
225 } 235 }
236 available_printers_timestamp_ = info.last_modified;
226 return true; 237 return true;
227 } 238 }
228 // Either we don't have an openable file, or it's too old. 239 // Either we don't have an openable file, or it's too old.
229 // 240 //
230 // If we have an invalid file and it's not valid for reasons other than 241 // If we have an invalid file and it's not valid for reasons other than
231 // NOT_FOUND, that's unexpected and worth logging. Otherwise this is 242 // NOT_FOUND, that's unexpected and worth logging. Otherwise this is
232 // a normal cache miss. 243 // a normal cache miss.
233 if (!cache_file.IsValid() && 244 if (!cache_file.IsValid() &&
234 cache_file.error_details() != base::File::FILE_ERROR_NOT_FOUND) { 245 cache_file.error_details() != base::File::FILE_ERROR_NOT_FOUND) {
235 LOG(ERROR) << "Unexpected result when attempting to open printer cache: " 246 LOG(ERROR) << "Unexpected result when attempting to open printer cache: "
236 << base::File::ErrorToString(cache_file.error_details()); 247 << base::File::ErrorToString(cache_file.error_details());
237 } 248 }
238 return false; 249 return false;
239 } 250 }
240 251
252 // In-memory copy of the available printers map, null if we don't have an
253 // in-memory copy yet. Filled in the first time the map is fetched from
254 // disk or stored.
255 std::unique_ptr<PpdProvider::AvailablePrintersMap> available_printers_;
256 // Timestamp for the in-memory copy of the cache. (The on-disk version uses
257 // the file mtime).
258 base::Time available_printers_timestamp_;
259
241 const base::FilePath cache_base_dir_; 260 const base::FilePath cache_base_dir_;
242 const base::FilePath available_printers_file_; 261 const base::FilePath available_printers_file_;
243 const PpdCache::Options options_; 262 const PpdCache::Options options_;
244 263
245 DISALLOW_COPY_AND_ASSIGN(PpdCacheImpl); 264 DISALLOW_COPY_AND_ASSIGN(PpdCacheImpl);
246 }; 265 };
247 266
248 } // namespace 267 } // namespace
249 268
250 // static 269 // static
251 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir, 270 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir,
252 const PpdCache::Options& options) { 271 const PpdCache::Options& options) {
253 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options); 272 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options);
254 } 273 }
255 274
256 } // namespace printing 275 } // namespace printing
257 } // namespace chromeos 276 } // namespace chromeos
OLDNEW
« 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