| OLD | NEW |
| 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 LOG(ERROR) << "Failed to generate JSON"; | 142 LOG(ERROR) << "Failed to generate JSON"; |
| 143 return; | 143 return; |
| 144 } | 144 } |
| 145 if (contents.size() > options_.max_available_list_cached_size) { | 145 if (contents.size() > options_.max_available_list_cached_size) { |
| 146 LOG(ERROR) << "Serialized available printers list too large (size is " | 146 LOG(ERROR) << "Serialized available printers list too large (size is " |
| 147 << contents.size() << " bytes)"; | 147 << contents.size() << " bytes)"; |
| 148 return; | 148 return; |
| 149 } | 149 } |
| 150 if (base::WriteFile(available_printers_file_, contents.data(), | 150 if (base::WriteFile(available_printers_file_, contents.data(), |
| 151 contents.size()) != static_cast<int>(contents.size())) { | 151 contents.size()) != static_cast<int>(contents.size())) { |
| 152 LOG(ERROR) << "Failed to write available printers cache"; | 152 LOG(ERROR) << "Failed to write available printers cache to " |
| 153 << available_printers_file_.MaybeAsASCII(); |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 private: | 157 private: |
| 157 // Get the file path at which we expect to find a PPD if it's cached. | 158 // Get the file path at which we expect to find a PPD if it's cached. |
| 158 // | 159 // |
| 159 // This is, ultimately, just a hash function. It's extremely infrequently | 160 // This is, ultimately, just a hash function. It's extremely infrequently |
| 160 // used (called once when trying to look up information on a printer or store | 161 // used (called once when trying to look up information on a printer or store |
| 161 // a PPD), and should be stable, as changing the function will make previously | 162 // a PPD), and should be stable, as changing the function will make previously |
| 162 // cached entries unfindable, causing resolve logic to be reinvoked | 163 // cached entries unfindable, causing resolve logic to be reinvoked |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 base::File cache_file(available_printers_file_, | 213 base::File cache_file(available_printers_file_, |
| 213 base::File::FLAG_OPEN | base::File::FLAG_READ); | 214 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 214 base::File::Info info; | 215 base::File::Info info; |
| 215 if (cache_file.IsValid() && cache_file.GetInfo(&info) && | 216 if (cache_file.IsValid() && cache_file.GetInfo(&info) && |
| 216 (base::Time::Now() - info.last_modified <= | 217 (base::Time::Now() - info.last_modified <= |
| 217 options_.max_available_list_staleness)) { | 218 options_.max_available_list_staleness)) { |
| 218 // We have a file that's recent enough to use. | 219 // We have a file that's recent enough to use. |
| 219 if (!base::ReadFileToStringWithMaxSize( | 220 if (!base::ReadFileToStringWithMaxSize( |
| 220 available_printers_file_, buf, | 221 available_printers_file_, buf, |
| 221 options_.max_available_list_cached_size)) { | 222 options_.max_available_list_cached_size)) { |
| 222 LOG(ERROR) << "Failed to read printer cache"; | 223 LOG(ERROR) << "Failed to read printer cache from " |
| 224 << available_printers_file_.MaybeAsASCII(); |
| 223 buf->clear(); | 225 buf->clear(); |
| 224 return false; | 226 return false; |
| 225 } | 227 } |
| 226 return true; | 228 return true; |
| 227 } | 229 } |
| 228 // Either we don't have an openable file, or it's too old. | 230 // Either we don't have an openable file, or it's too old. |
| 229 // | 231 // |
| 230 // If we have an invalid file and it's not valid for reasons other than | 232 // 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 | 233 // NOT_FOUND, that's unexpected and worth logging. Otherwise this is |
| 232 // a normal cache miss. | 234 // a normal cache miss. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 248 } // namespace | 250 } // namespace |
| 249 | 251 |
| 250 // static | 252 // static |
| 251 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir, | 253 std::unique_ptr<PpdCache> PpdCache::Create(const base::FilePath& cache_base_dir, |
| 252 const PpdCache::Options& options) { | 254 const PpdCache::Options& options) { |
| 253 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options); | 255 return base::MakeUnique<PpdCacheImpl>(cache_base_dir, options); |
| 254 } | 256 } |
| 255 | 257 |
| 256 } // namespace printing | 258 } // namespace printing |
| 257 } // namespace chromeos | 259 } // namespace chromeos |
| OLD | NEW |