logo_cache.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2014 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 COMPONENTS_SEARCH_PROVIDER_LOGOS_LOGO_CACHE_H_
  5. #define COMPONENTS_SEARCH_PROVIDER_LOGOS_LOGO_CACHE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/ref_counted_memory.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "components/search_provider_logos/logo_common.h"
  15. namespace search_provider_logos {
  16. // A file-based cache for the search provider's logo. This allows clients to
  17. // store and retrieve a logo (of type EncodedLogo) and its associated metadata
  18. // (of type LogoMetadata). Metadata can be updated independently from the logo
  19. // to handle cases where, e.g. the expiration date changes, but the logo stays
  20. // the same. If corruption is detected in the metadata or logo, the cache will
  21. // be cleared.
  22. //
  23. // Note: this class must only be used on a single thread. All methods are
  24. // are blocking, so this should not be used on the UI thread.
  25. //
  26. // The logo and its metadata are stored in files, so they persist even when
  27. // Chrome is closed. Once loaded from disk, the metadata is kept in memory to
  28. // enable quick retrieval. The logo is not kept around in memory because of its
  29. // size.
  30. class LogoCache {
  31. public:
  32. // Constructs a logo cache that stores data in |cache_directory|.
  33. // |cache_directory| will be created if it does not already exist.
  34. explicit LogoCache(const base::FilePath& cache_directory);
  35. LogoCache(const LogoCache&) = delete;
  36. LogoCache& operator=(const LogoCache&) = delete;
  37. virtual ~LogoCache();
  38. // Updates the metadata for the cached logo.
  39. virtual void UpdateCachedLogoMetadata(const LogoMetadata& metadata);
  40. // Returns metadata for the cached logo, or null if logo is cached.
  41. virtual const LogoMetadata* GetCachedLogoMetadata();
  42. // Sets the cached logo and metadata. |logo| may be null, in which case the
  43. // cached logo and metadata will be cleared.
  44. virtual void SetCachedLogo(const EncodedLogo* logo);
  45. // Returns the cached logo, or null if no logo is cached or the cached logo is
  46. // corrupt.
  47. virtual std::unique_ptr<EncodedLogo> GetCachedLogo();
  48. private:
  49. FRIEND_TEST_ALL_PREFIXES(LogoCacheSerializationTest, SerializeMetadata);
  50. FRIEND_TEST_ALL_PREFIXES(LogoCacheSerializationTest,
  51. DeserializeCorruptMetadata);
  52. FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, StoreAndRetrieveMetadata);
  53. FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptMetadata);
  54. FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptLogo);
  55. // Converts string |str| to a LogoMetadata object and returns it. Returns null
  56. // if |str| cannot be converted.
  57. static std::unique_ptr<LogoMetadata> LogoMetadataFromString(
  58. const std::string& str,
  59. int* logo_num_bytes,
  60. int* dark_logo_num_bytes);
  61. // Converts |metadata| to a string and stores it in |str|.
  62. static void LogoMetadataToString(const LogoMetadata& metadata,
  63. int logo_num_bytes,
  64. int dark_logo_num_bytes,
  65. std::string* str);
  66. // Returns the path where the cached logo will be saved.
  67. base::FilePath GetLogoPath();
  68. base::FilePath GetDarkLogoPath();
  69. // Returns the path where the metadata for the cached logo will be saved.
  70. base::FilePath GetMetadataPath();
  71. // Updates the in-memory metadata.
  72. void UpdateMetadata(std::unique_ptr<LogoMetadata> metadata);
  73. // If the cached logo's metadata isn't available in memory (i.e.
  74. // |metadata_is_valid_| is false), reads it from disk and stores it in
  75. // |metadata_|. If no logo is cached, |metadata_| will be updated to null.
  76. void ReadMetadataIfNeeded();
  77. // Writes the metadata for the cached logo to disk.
  78. void WriteMetadata();
  79. // Writes |metadata_| to the cached metadata file and |encoded_image| to the
  80. // cached logo file.
  81. void WriteLogo(scoped_refptr<base::RefCountedMemory> encoded_image,
  82. scoped_refptr<base::RefCountedMemory> dark_encoded_image);
  83. // Deletes all the cache files.
  84. void DeleteLogoAndMetadata();
  85. // Tries to create the cache directory if it does not already exist. Returns
  86. // whether the cache directory exists.
  87. bool EnsureCacheDirectoryExists();
  88. // The directory in which the cached logo and metadata will be saved.
  89. base::FilePath cache_directory_;
  90. // The metadata describing the cached logo, or null if no logo is cached. This
  91. // value is meaningful iff |metadata_is_valid_| is true; otherwise, the
  92. // metadata must be read from file and |metadata_| will be null.
  93. // Note: Once read from file, metadata will be stored in memory indefinitely.
  94. std::unique_ptr<LogoMetadata> metadata_;
  95. bool metadata_is_valid_;
  96. // The number of bytes in the logo file, as recorded in the metadata file.
  97. // Valid iff |metadata_is_valid_|. This is used to verify that the logo file
  98. // is complete and corresponds to the current metadata file.
  99. int logo_num_bytes_ = 0;
  100. int dark_logo_num_bytes_ = 0;
  101. // Ensure LogoCache is only used sequentially.
  102. SEQUENCE_CHECKER(sequence_checker_);
  103. };
  104. } // namespace search_provider_logos
  105. #endif // COMPONENTS_SEARCH_PROVIDER_LOGOS_LOGO_CACHE_H_