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

Side by Side Diff: chromeos/printing/ppd_provider.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 unified diff | Download patch
« no previous file with comments | « chromeos/printing/ppd_cache_unittest.cc ('k') | no next file » | 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_provider.h" 5 #include "chromeos/printing/ppd_provider.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/json/json_parser.h" 10 #include "base/json/json_parser.h"
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 120 }
121 121
122 void QueryAvailable(const QueryAvailableCallback& cb) override { 122 void QueryAvailable(const QueryAvailableCallback& cb) override {
123 CHECK(!cb.is_null()); 123 CHECK(!cb.is_null());
124 CHECK(query_sequence_checker_.CalledOnValidSequence()); 124 CHECK(query_sequence_checker_.CalledOnValidSequence());
125 CHECK(base::SequencedTaskRunnerHandle::IsSet()) 125 CHECK(base::SequencedTaskRunnerHandle::IsSet())
126 << "QueryAvailable() must be called from a SequencedTaskRunner context"; 126 << "QueryAvailable() must be called from a SequencedTaskRunner context";
127 CHECK(query_fetcher_ == nullptr) 127 CHECK(query_fetcher_ == nullptr)
128 << "Can't have concurrent PpdProvider QueryAvailable() calls"; 128 << "Can't have concurrent PpdProvider QueryAvailable() calls";
129 129
130 base::Optional<PpdProvider::AvailablePrintersMap> result = 130 const PpdProvider::AvailablePrintersMap* result =
131 cache_->FindAvailablePrinters(); 131 cache_->FindAvailablePrinters();
132 if (result) { 132 if (result != nullptr) {
133 // Satisfy from cache. 133 // Satisfy from cache.
134 base::SequencedTaskRunnerHandle::Get()->PostTask( 134 base::SequencedTaskRunnerHandle::Get()->PostTask(
135 FROM_HERE, base::Bind(cb, PpdProvider::SUCCESS, result.value())); 135 FROM_HERE, base::Bind(cb, PpdProvider::SUCCESS, *result));
136 return; 136 return;
137 } 137 }
138 // Not in the cache, ask QuirksServer. 138 // Not in the cache, ask QuirksServer.
139 query_done_callback_ = cb; 139 query_done_callback_ = cb;
140 140
141 query_fetcher_ = 141 query_fetcher_ =
142 net::URLFetcher::Create(GetQuirksServerPpdListURL(), 142 net::URLFetcher::Create(GetQuirksServerPpdListURL(),
143 net::URLFetcher::GET, &forwarding_delegate_); 143 net::URLFetcher::GET, &forwarding_delegate_);
144 query_fetcher_->SetRequestContext(url_context_getter_.get()); 144 query_fetcher_->SetRequestContext(url_context_getter_.get());
145 query_fetcher_->SetLoadFlags( 145 query_fetcher_->SetLoadFlags(
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 auto top_dict = 251 auto top_dict =
252 base::DictionaryValue::From(base::JSONReader::Read(contents)); 252 base::DictionaryValue::From(base::JSONReader::Read(contents));
253 const base::ListValue* top_list; 253 const base::ListValue* top_list;
254 if (top_dict == nullptr || !top_dict->GetList(kJSONTopListKey, &top_list)) { 254 if (top_dict == nullptr || !top_dict->GetList(kJSONTopListKey, &top_list)) {
255 LOG(ERROR) << "Malformed response from quirks server"; 255 LOG(ERROR) << "Malformed response from quirks server";
256 query_done_callback_.Run(PpdProvider::SERVER_ERROR, 256 query_done_callback_.Run(PpdProvider::SERVER_ERROR,
257 PpdProvider::AvailablePrintersMap()); 257 PpdProvider::AvailablePrintersMap());
258 return; 258 return;
259 } 259 }
260 260
261 PpdProvider::AvailablePrintersMap result; 261 auto result = base::MakeUnique<PpdProvider::AvailablePrintersMap>();
262 for (const std::unique_ptr<base::Value>& entry : *top_list) { 262 for (const std::unique_ptr<base::Value>& entry : *top_list) {
263 base::DictionaryValue* dict; 263 base::DictionaryValue* dict;
264 std::string manufacturer; 264 std::string manufacturer;
265 std::string model; 265 std::string model;
266 base::ListValue* model_list; 266 base::ListValue* model_list;
267 if (!entry->GetAsDictionary(&dict) || 267 if (!entry->GetAsDictionary(&dict) ||
268 !dict->GetString(kJSONManufacturer, &manufacturer) || 268 !dict->GetString(kJSONManufacturer, &manufacturer) ||
269 !dict->GetList(kJSONModelList, &model_list)) { 269 !dict->GetList(kJSONModelList, &model_list)) {
270 LOG(ERROR) << "Unexpected contents in quirks server printer list."; 270 LOG(ERROR) << "Unexpected contents in quirks server printer list.";
271 // Just skip this entry instead of aborting the whole thing. 271 // Just skip this entry instead of aborting the whole thing.
272 continue; 272 continue;
273 } 273 }
274 274
275 std::vector<std::string>& dest = result[manufacturer]; 275 std::vector<std::string>& dest = (*result)[manufacturer];
276 for (const std::unique_ptr<base::Value>& model_value : *model_list) { 276 for (const std::unique_ptr<base::Value>& model_value : *model_list) {
277 if (model_value->GetAsString(&model)) { 277 if (model_value->GetAsString(&model)) {
278 dest.push_back(model); 278 dest.push_back(model);
279 } else { 279 } else {
280 LOG(ERROR) << "Skipping unknown model for manufacturer " 280 LOG(ERROR) << "Skipping unknown model for manufacturer "
281 << manufacturer; 281 << manufacturer;
282 } 282 }
283 } 283 }
284 } 284 }
285 if (!result.empty()) { 285 query_done_callback_.Run(PpdProvider::SUCCESS, *result);
286 cache_->StoreAvailablePrinters(result); 286 if (!result->empty()) {
287 cache_->StoreAvailablePrinters(std::move(result));
287 } else { 288 } else {
288 // An empty map means something is probably wrong; if we cache this map, 289 // An empty map means something is probably wrong; if we cache this map,
289 // we'll have an empty map until the cache expires. So complain and 290 // we'll have an empty map until the cache expires. So complain and
290 // refuse to cache. 291 // refuse to cache.
291 LOG(ERROR) << "Available printers map is unexpectedly empty. Refusing " 292 LOG(ERROR) << "Available printers map is unexpectedly empty. Refusing "
292 "to cache this."; 293 "to cache this.";
293 } 294 }
294 query_done_callback_.Run(PpdProvider::SUCCESS, result);
295 } 295 }
296 296
297 // Generate a url to look up a manufacturer/model from the quirks server 297 // Generate a url to look up a manufacturer/model from the quirks server
298 GURL GetQuirksServerPpdLookupURL( 298 GURL GetQuirksServerPpdLookupURL(
299 const Printer::PpdReference& ppd_reference) const { 299 const Printer::PpdReference& ppd_reference) const {
300 return GURL(base::StringPrintf( 300 return GURL(base::StringPrintf(
301 "https://%s/v2/printer/manufacturers/%s/models/%s?key=%s", 301 "https://%s/v2/printer/manufacturers/%s/models/%s?key=%s",
302 options_.quirks_server.c_str(), 302 options_.quirks_server.c_str(),
303 ppd_reference.effective_manufacturer.c_str(), 303 ppd_reference.effective_manufacturer.c_str(),
304 ppd_reference.effective_model.c_str(), api_key_.c_str())); 304 ppd_reference.effective_model.c_str(), api_key_.c_str()));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 const std::string& api_key, 371 const std::string& api_key,
372 scoped_refptr<net::URLRequestContextGetter> url_context_getter, 372 scoped_refptr<net::URLRequestContextGetter> url_context_getter,
373 std::unique_ptr<PpdCache> cache, 373 std::unique_ptr<PpdCache> cache,
374 const PpdProvider::Options& options) { 374 const PpdProvider::Options& options) {
375 return base::MakeUnique<PpdProviderImpl>(api_key, url_context_getter, 375 return base::MakeUnique<PpdProviderImpl>(api_key, url_context_getter,
376 std::move(cache), options); 376 std::move(cache), options);
377 } 377 }
378 378
379 } // namespace printing 379 } // namespace printing
380 } // namespace chromeos 380 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/printing/ppd_cache_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698