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

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

Issue 2476073003: Update PpdProvider threading model. (Closed)
Patch Set: Fix some comment typos. 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
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"
11 #include "base/json/json_parser.h" 11 #include "base/json/json_parser.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/threading/thread_restrictions.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "base/values.h" 18 #include "base/values.h"
18 #include "crypto/sha2.h" 19 #include "crypto/sha2.h"
19 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
20 #include "net/filter/gzip_header.h" 21 #include "net/filter/gzip_header.h"
21 22
22 namespace chromeos { 23 namespace chromeos {
23 namespace printing { 24 namespace printing {
24 namespace { 25 namespace {
25 26
(...skipping 15 matching lines...) Expand all
41 const PpdCache::Options& options) 42 const PpdCache::Options& options)
42 : cache_base_dir_(cache_base_dir), 43 : cache_base_dir_(cache_base_dir),
43 available_printers_file_( 44 available_printers_file_(
44 cache_base_dir.Append(kAvailablePrintersFilename)), 45 cache_base_dir.Append(kAvailablePrintersFilename)),
45 options_(options) {} 46 options_(options) {}
46 ~PpdCacheImpl() override {} 47 ~PpdCacheImpl() override {}
47 48
48 // Public API functions. 49 // Public API functions.
49 base::Optional<base::FilePath> Find( 50 base::Optional<base::FilePath> Find(
50 const Printer::PpdReference& reference) const override { 51 const Printer::PpdReference& reference) const override {
52 base::ThreadRestrictions::AssertIOAllowed();
51 base::Optional<base::FilePath> ret; 53 base::Optional<base::FilePath> ret;
52 54
53 // We can't know here if we have a gzipped or un-gzipped version, so just 55 // We can't know here if we have a gzipped or un-gzipped version, so just
54 // look for both. 56 // look for both.
55 base::FilePath contents_path_base = GetCachePathBase(reference); 57 base::FilePath contents_path_base = GetCachePathBase(reference);
56 for (const std::string& extension : {".ppd", ".ppd.gz"}) { 58 for (const std::string& extension : {".ppd", ".ppd.gz"}) {
57 base::FilePath contents_path = contents_path_base.AddExtension(extension); 59 base::FilePath contents_path = contents_path_base.AddExtension(extension);
58 if (base::PathExists(contents_path)) { 60 if (base::PathExists(contents_path)) {
59 ret = contents_path; 61 ret = contents_path;
60 break; 62 break;
(...skipping 23 matching lines...) Expand all
84 if (!base::DeleteFile(contents_path, false)) { 86 if (!base::DeleteFile(contents_path, false)) {
85 LOG(ERROR) << "Failed to cleanup partially-written file " 87 LOG(ERROR) << "Failed to cleanup partially-written file "
86 << contents_path.LossyDisplayName(); 88 << contents_path.LossyDisplayName();
87 return ret; 89 return ret;
88 } 90 }
89 } 91 }
90 return ret; 92 return ret;
91 } 93 }
92 94
93 const PpdProvider::AvailablePrintersMap* FindAvailablePrinters() override { 95 const PpdProvider::AvailablePrintersMap* FindAvailablePrinters() override {
96 base::ThreadRestrictions::AssertIOAllowed();
94 if (available_printers_ != nullptr && 97 if (available_printers_ != nullptr &&
95 base::Time::Now() - available_printers_timestamp_ < 98 base::Time::Now() - available_printers_timestamp_ <
96 options_.max_available_list_staleness) { 99 options_.max_available_list_staleness) {
97 // Satisfy from memory cache. 100 // Satisfy from memory cache.
98 return available_printers_.get(); 101 return available_printers_.get();
99 } 102 }
100 std::string buf; 103 std::string buf;
101 if (!MaybeReadAvailablePrintersCache(&buf)) { 104 if (!MaybeReadAvailablePrintersCache(&buf)) {
102 // Disk cache miss. 105 // Disk cache miss.
103 return nullptr; 106 return nullptr;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 LOG(ERROR) << "Failed to generate JSON"; 155 LOG(ERROR) << "Failed to generate JSON";
153 return; 156 return;
154 } 157 }
155 if (contents.size() > options_.max_available_list_cached_size) { 158 if (contents.size() > options_.max_available_list_cached_size) {
156 LOG(ERROR) << "Serialized available printers list too large (size is " 159 LOG(ERROR) << "Serialized available printers list too large (size is "
157 << contents.size() << " bytes)"; 160 << contents.size() << " bytes)";
158 return; 161 return;
159 } 162 }
160 if (base::WriteFile(available_printers_file_, contents.data(), 163 if (base::WriteFile(available_printers_file_, contents.data(),
161 contents.size()) != static_cast<int>(contents.size())) { 164 contents.size()) != static_cast<int>(contents.size())) {
162 LOG(ERROR) << "Failed to write available printers cache"; 165 LOG(ERROR) << "Failed to write available printers cache to "
166 << available_printers_file_.MaybeAsASCII();
163 } 167 }
164 } 168 }
165 169
166 private: 170 private:
167 // Get the file path at which we expect to find a PPD if it's cached. 171 // Get the file path at which we expect to find a PPD if it's cached.
168 // 172 //
169 // This is, ultimately, just a hash function. It's extremely infrequently 173 // This is, ultimately, just a hash function. It's extremely infrequently
170 // used (called once when trying to look up information on a printer or store 174 // used (called once when trying to look up information on a printer or store
171 // a PPD), and should be stable, as changing the function will make previously 175 // a PPD), and should be stable, as changing the function will make previously
172 // cached entries unfindable, causing resolve logic to be reinvoked 176 // cached entries unfindable, causing resolve logic to be reinvoked
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 base::File cache_file(available_printers_file_, 226 base::File cache_file(available_printers_file_,
223 base::File::FLAG_OPEN | base::File::FLAG_READ); 227 base::File::FLAG_OPEN | base::File::FLAG_READ);
224 base::File::Info info; 228 base::File::Info info;
225 if (cache_file.IsValid() && cache_file.GetInfo(&info) && 229 if (cache_file.IsValid() && cache_file.GetInfo(&info) &&
226 (base::Time::Now() - info.last_modified <= 230 (base::Time::Now() - info.last_modified <=
227 options_.max_available_list_staleness)) { 231 options_.max_available_list_staleness)) {
228 // We have a file that's recent enough to use. 232 // We have a file that's recent enough to use.
229 if (!base::ReadFileToStringWithMaxSize( 233 if (!base::ReadFileToStringWithMaxSize(
230 available_printers_file_, buf, 234 available_printers_file_, buf,
231 options_.max_available_list_cached_size)) { 235 options_.max_available_list_cached_size)) {
232 LOG(ERROR) << "Failed to read printer cache"; 236 LOG(ERROR) << "Failed to read printer cache from "
237 << available_printers_file_.MaybeAsASCII();
233 buf->clear(); 238 buf->clear();
234 return false; 239 return false;
235 } 240 }
236 available_printers_timestamp_ = info.last_modified; 241 available_printers_timestamp_ = info.last_modified;
237 return true; 242 return true;
238 } 243 }
239 // Either we don't have an openable file, or it's too old. 244 // Either we don't have an openable file, or it's too old.
240 // 245 //
241 // If we have an invalid file and it's not valid for reasons other than 246 // If we have an invalid file and it's not valid for reasons other than
242 // NOT_FOUND, that's unexpected and worth logging. Otherwise this is 247 // NOT_FOUND, that's unexpected and worth logging. Otherwise this is
(...skipping 24 matching lines...) Expand all
267 } // namespace 272 } // namespace
268 273
269 // static 274 // static
270 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir, 275 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir,
271 const PpdCache::Options& options) { 276 const PpdCache::Options& options) {
272 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options); 277 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options);
273 } 278 }
274 279
275 } // namespace printing 280 } // namespace printing
276 } // namespace chromeos 281 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698