ppd_cache.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  3. // found in the LICENSE file.
  4. #ifndef CHROMEOS_PRINTING_PPD_CACHE_H_
  5. #define CHROMEOS_PRINTING_PPD_CACHE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/component_export.h"
  10. #include "base/files/file_path.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/task/sequenced_task_runner.h"
  13. #include "base/time/time.h"
  14. namespace chromeos {
  15. // PpdCache manages a cache of locally-stored PPD files. At its core, it
  16. // operates like a persistent hash from PpdReference to files. If you give the
  17. // same PpdReference to Find() that was previously given to store, you should
  18. // get the same FilePath back out (unless the previous entry has timed out of
  19. // the cache). However, changing *any* field in PpdReference will make the
  20. // previous cache entry invalid. This is the intentional behavior -- we want to
  21. // re-run the resolution logic if we have new meta-information about a printer.
  22. class COMPONENT_EXPORT(CHROMEOS_PRINTING) PpdCache
  23. : public base::RefCounted<PpdCache> {
  24. public:
  25. struct FindResult {
  26. // Did we find something? If this is false, none of the other fields are
  27. // valid.
  28. bool success = false;
  29. // How old is this entry? Zero on failure.
  30. base::TimeDelta age;
  31. // Contents of the entry. Empty on failure.
  32. std::string contents;
  33. };
  34. using FindCallback = base::OnceCallback<void(const FindResult& result)>;
  35. // Create and return a Ppdcache that uses cache_dir to store state. If
  36. // cache_base_dir does not exist, it will be lazily created the first time the
  37. // cache needs to store state.
  38. static scoped_refptr<PpdCache> Create(const base::FilePath& cache_base_dir);
  39. // Create a PpdCache that uses the given task runner for background
  40. // processing.
  41. static scoped_refptr<PpdCache> CreateForTesting(
  42. const base::FilePath& cache_base_dir,
  43. scoped_refptr<base::SequencedTaskRunner> io_task_runner);
  44. // Start a Find, looking, for an entry with the given key that is at most
  45. // |max_age| old. |cb| will be invoked on the calling thread.
  46. virtual void Find(const std::string& key, FindCallback cb) = 0;
  47. // Store |contents| at the the location indicated by |key|. The
  48. // file operation will complete asynchronously.
  49. virtual void Store(const std::string& key,
  50. const std::string& contents) = 0;
  51. // Store the given contents at the given key, and change the resulting
  52. // cache file's last modified date to be |age| before now.
  53. virtual void StoreForTesting(const std::string& key,
  54. const std::string& contents,
  55. base::TimeDelta age) = 0;
  56. protected:
  57. friend class base::RefCounted<PpdCache>;
  58. virtual ~PpdCache() {}
  59. };
  60. } // namespace chromeos
  61. #endif // CHROMEOS_PRINTING_PPD_CACHE_H_