| 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 #ifndef CHROMEOS_PRINTING_PPD_CACHE_H_ | 5 #ifndef CHROMEOS_PRINTING_PPD_CACHE_H_ |
| 6 #define CHROMEOS_PRINTING_PPD_CACHE_H_ | 6 #define CHROMEOS_PRINTING_PPD_CACHE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/optional.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/sequenced_task_runner.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "chromeos/chromeos_export.h" | 16 #include "chromeos/chromeos_export.h" |
| 15 #include "chromeos/printing/ppd_provider.h" | |
| 16 #include "chromeos/printing/printer_configuration.h" | |
| 17 | 17 |
| 18 namespace chromeos { | 18 namespace chromeos { |
| 19 namespace printing { | 19 namespace printing { |
| 20 | 20 |
| 21 // PpdCache manages a cache of locally-stored PPD files. At its core, it | 21 // PpdCache manages a cache of locally-stored PPD files. At its core, it |
| 22 // operates like a persistent hash from PpdReference to files. If you give the | 22 // operates like a persistent hash from PpdReference to files. If you give the |
| 23 // same PpdReference to Find() that was previously given to store, you should | 23 // same PpdReference to Find() that was previously given to store, you should |
| 24 // get the same FilePath back out (unless the previous entry has timed out of | 24 // get the same FilePath back out (unless the previous entry has timed out of |
| 25 // the cache). However, changing *any* field in PpdReference will make the | 25 // the cache). However, changing *any* field in PpdReference will make the |
| 26 // previous cache entry invalid. This is the intentional behavior -- we want to | 26 // previous cache entry invalid. This is the intentional behavior -- we want to |
| 27 // re-run the resolution logic if we have new meta-information about a printer. | 27 // re-run the resolution logic if we have new meta-information about a printer. |
| 28 // | 28 // |
| 29 // All cache functions must be called on a thread which is permitted to do I/O. | 29 // All cache functions must be called on a thread which is permitted to do I/O. |
| 30 class CHROMEOS_EXPORT PpdCache { | 30 class CHROMEOS_EXPORT PpdCache : public base::RefCounted<PpdCache> { |
| 31 public: | 31 public: |
| 32 // Options that can be tweaked. These should all have sane defaults. This | 32 struct FindResult { |
| 33 // structure is copyable. | 33 // Did we find something? If this is false, none of the other fields are |
| 34 struct Options { | 34 // valid. |
| 35 Options() {} | 35 bool success = false; |
| 36 | 36 |
| 37 // If the cached available printer data is older than this, we will consider | 37 // How old is this entry? |
| 38 // it stale and won't return it. A non-positive value here means we will | 38 base::TimeDelta age; |
| 39 // always consider data stale (which is useful for tests). | |
| 40 // Default is 14 days. | |
| 41 base::TimeDelta max_available_list_staleness = | |
| 42 base::TimeDelta::FromDays(14); | |
| 43 | 39 |
| 44 // Size limit on the serialized cached available printer list, in bytes. | 40 // Contents of the entry. |
| 45 // This is just a check to make sure we don't consume ridiculous amounts of | 41 std::string contents; |
| 46 // disk if we get bad data. | |
| 47 // Default is 10 MB | |
| 48 size_t max_available_list_cached_size = 10 * 1024 * 1024; | |
| 49 }; | 42 }; |
| 50 | 43 |
| 44 using FindCallback = base::Callback<void(const FindResult& result)>; |
| 45 |
| 51 // Create and return a Ppdcache that uses cache_dir to store state. If | 46 // Create and return a Ppdcache that uses cache_dir to store state. If |
| 52 // cache_base_dir does not exist, it will be lazily created the first time the | 47 // cache_base_dir does not exist, it will be lazily created the first time the |
| 53 // cache needs to store state. | 48 // cache needs to store state. |
| 54 static std::unique_ptr<PpdCache> Create(const base::FilePath& cache_base_dir, | 49 static scoped_refptr<PpdCache> Create( |
| 55 const Options& options = Options()); | 50 const base::FilePath& cache_base_dir, |
| 51 scoped_refptr<base::SequencedTaskRunner> disk_task_runner); |
| 52 |
| 53 // Start a Find, looking, for an entry with the given key that is at most |
| 54 // |max_age| old. |cb| will be invoked on the calling thread. |
| 55 virtual void Find(const std::string& key, const FindCallback& cb) = 0; |
| 56 |
| 57 // Store the given contents at the given key. If cb is non-null, it will |
| 58 // be invoked on completion. |
| 59 virtual void Store(const std::string& key, |
| 60 const std::string& contents, |
| 61 const base::Callback<void()>& cb) = 0; |
| 62 |
| 63 // Hook for testing. Returns true if all outstanding cache operations |
| 64 // are complete. |
| 65 virtual bool Idle() const = 0; |
| 66 |
| 67 protected: |
| 68 friend class base::RefCounted<PpdCache>; |
| 56 virtual ~PpdCache() {} | 69 virtual ~PpdCache() {} |
| 57 | |
| 58 // Find a PPD that was previously cached with the given reference. Note that | |
| 59 // all fields of the reference must be the same, otherwise we'll miss in the | |
| 60 // cache and re-run resolution for the PPD. | |
| 61 // | |
| 62 // If a FilePath is returned, it is guaranteed to be non-empty and | |
| 63 // remain valid until the next Store() call. | |
| 64 virtual base::Optional<base::FilePath> Find( | |
| 65 const Printer::PpdReference& reference) const = 0; | |
| 66 | |
| 67 // Take the contents of a PPD file, store it to the cache, and return the | |
| 68 // path to the stored file keyed on reference. | |
| 69 // | |
| 70 // If a different PPD was previously Stored for the given reference, it | |
| 71 // will be replaced. | |
| 72 // | |
| 73 // If a FilePath is returned, it is guaranteed to be non-empty and | |
| 74 // remain valid until the next Store() call. | |
| 75 virtual base::Optional<base::FilePath> Store( | |
| 76 const Printer::PpdReference& reference, | |
| 77 const std::string& ppd_contents) = 0; | |
| 78 | |
| 79 // Return a map of available printers, if we have one available and it's | |
| 80 // not too stale. Returns nullopt if no map is available. Note that, if the | |
| 81 // number of printers gets extremely large this map copy could get expensive | |
| 82 // and need to be reworked. | |
| 83 virtual base::Optional<PpdProvider::AvailablePrintersMap> | |
| 84 FindAvailablePrinters() = 0; | |
| 85 | |
| 86 // Store |available_printers|, replacing any existing entry. | |
| 87 virtual void StoreAvailablePrinters( | |
| 88 std::unique_ptr<PpdProvider::AvailablePrintersMap> | |
| 89 available_printers) = 0; | |
| 90 }; | 70 }; |
| 91 | 71 |
| 92 } // namespace printing | 72 } // namespace printing |
| 93 } // namespace chromeos | 73 } // namespace chromeos |
| 94 | 74 |
| 95 #endif // CHROMEOS_PRINTING_PPD_CACHE_H_ | 75 #endif // CHROMEOS_PRINTING_PPD_CACHE_H_ |
| OLD | NEW |