| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/search_provider_logos/logo_cache.h" | 5 #include "components/search_provider_logos/logo_cache.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // The cached logo metadata is persisted as JSON using these keys. | 19 // The cached logo metadata is persisted as JSON using these keys. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return metadata_.get(); | 78 return metadata_.get(); |
| 78 } | 79 } |
| 79 | 80 |
| 80 void LogoCache::SetCachedLogo(const EncodedLogo* logo) { | 81 void LogoCache::SetCachedLogo(const EncodedLogo* logo) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 scoped_ptr<LogoMetadata> metadata; | 83 scoped_ptr<LogoMetadata> metadata; |
| 83 if (logo) { | 84 if (logo) { |
| 84 metadata.reset(new LogoMetadata(logo->metadata)); | 85 metadata.reset(new LogoMetadata(logo->metadata)); |
| 85 logo_num_bytes_ = static_cast<int>(logo->encoded_image->size()); | 86 logo_num_bytes_ = static_cast<int>(logo->encoded_image->size()); |
| 86 } | 87 } |
| 87 UpdateMetadata(metadata.Pass()); | 88 UpdateMetadata(std::move(metadata)); |
| 88 WriteLogo(logo ? logo->encoded_image : NULL); | 89 WriteLogo(logo ? logo->encoded_image : NULL); |
| 89 } | 90 } |
| 90 | 91 |
| 91 scoped_ptr<EncodedLogo> LogoCache::GetCachedLogo() { | 92 scoped_ptr<EncodedLogo> LogoCache::GetCachedLogo() { |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 | 94 |
| 94 ReadMetadataIfNeeded(); | 95 ReadMetadataIfNeeded(); |
| 95 if (!metadata_) | 96 if (!metadata_) |
| 96 return scoped_ptr<EncodedLogo>(); | 97 return scoped_ptr<EncodedLogo>(); |
| 97 | 98 |
| 98 scoped_refptr<base::RefCountedString> encoded_image = | 99 scoped_refptr<base::RefCountedString> encoded_image = |
| 99 new base::RefCountedString(); | 100 new base::RefCountedString(); |
| 100 if (!base::ReadFileToString(GetLogoPath(), &encoded_image->data())) { | 101 if (!base::ReadFileToString(GetLogoPath(), &encoded_image->data())) { |
| 101 UpdateMetadata(scoped_ptr<LogoMetadata>()); | 102 UpdateMetadata(scoped_ptr<LogoMetadata>()); |
| 102 return scoped_ptr<EncodedLogo>(); | 103 return scoped_ptr<EncodedLogo>(); |
| 103 } | 104 } |
| 104 | 105 |
| 105 if (encoded_image->size() != static_cast<size_t>(logo_num_bytes_)) { | 106 if (encoded_image->size() != static_cast<size_t>(logo_num_bytes_)) { |
| 106 // Delete corrupt metadata and logo. | 107 // Delete corrupt metadata and logo. |
| 107 DeleteLogoAndMetadata(); | 108 DeleteLogoAndMetadata(); |
| 108 UpdateMetadata(scoped_ptr<LogoMetadata>()); | 109 UpdateMetadata(scoped_ptr<LogoMetadata>()); |
| 109 return scoped_ptr<EncodedLogo>(); | 110 return scoped_ptr<EncodedLogo>(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 scoped_ptr<EncodedLogo> logo(new EncodedLogo()); | 113 scoped_ptr<EncodedLogo> logo(new EncodedLogo()); |
| 113 logo->encoded_image = encoded_image; | 114 logo->encoded_image = encoded_image; |
| 114 logo->metadata = *metadata_; | 115 logo->metadata = *metadata_; |
| 115 return logo.Pass(); | 116 return logo; |
| 116 } | 117 } |
| 117 | 118 |
| 118 // static | 119 // static |
| 119 scoped_ptr<LogoMetadata> LogoCache::LogoMetadataFromString( | 120 scoped_ptr<LogoMetadata> LogoCache::LogoMetadataFromString( |
| 120 const std::string& str, int* logo_num_bytes) { | 121 const std::string& str, int* logo_num_bytes) { |
| 121 scoped_ptr<base::Value> value = base::JSONReader::Read(str); | 122 scoped_ptr<base::Value> value = base::JSONReader::Read(str); |
| 122 base::DictionaryValue* dict; | 123 base::DictionaryValue* dict; |
| 123 if (!value || !value->GetAsDictionary(&dict)) | 124 if (!value || !value->GetAsDictionary(&dict)) |
| 124 return scoped_ptr<LogoMetadata>(); | 125 return scoped_ptr<LogoMetadata>(); |
| 125 | 126 |
| 126 scoped_ptr<LogoMetadata> metadata(new LogoMetadata()); | 127 scoped_ptr<LogoMetadata> metadata(new LogoMetadata()); |
| 127 if (!dict->GetString(kSourceUrlKey, &metadata->source_url) || | 128 if (!dict->GetString(kSourceUrlKey, &metadata->source_url) || |
| 128 !dict->GetString(kFingerprintKey, &metadata->fingerprint) || | 129 !dict->GetString(kFingerprintKey, &metadata->fingerprint) || |
| 129 !dict->GetString(kOnClickURLKey, &metadata->on_click_url) || | 130 !dict->GetString(kOnClickURLKey, &metadata->on_click_url) || |
| 130 !dict->GetString(kAltTextKey, &metadata->alt_text) || | 131 !dict->GetString(kAltTextKey, &metadata->alt_text) || |
| 131 !dict->GetString(kAnimatedUrlKey, &metadata->animated_url) || | 132 !dict->GetString(kAnimatedUrlKey, &metadata->animated_url) || |
| 132 !dict->GetString(kMimeTypeKey, &metadata->mime_type) || | 133 !dict->GetString(kMimeTypeKey, &metadata->mime_type) || |
| 133 !dict->GetBoolean(kCanShowAfterExpirationKey, | 134 !dict->GetBoolean(kCanShowAfterExpirationKey, |
| 134 &metadata->can_show_after_expiration) || | 135 &metadata->can_show_after_expiration) || |
| 135 !dict->GetInteger(kNumBytesKey, logo_num_bytes) || | 136 !dict->GetInteger(kNumBytesKey, logo_num_bytes) || |
| 136 !GetTimeValue(*dict, kExpirationTimeKey, &metadata->expiration_time)) { | 137 !GetTimeValue(*dict, kExpirationTimeKey, &metadata->expiration_time)) { |
| 137 return scoped_ptr<LogoMetadata>(); | 138 return scoped_ptr<LogoMetadata>(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 return metadata.Pass(); | 141 return metadata; |
| 141 } | 142 } |
| 142 | 143 |
| 143 // static | 144 // static |
| 144 void LogoCache::LogoMetadataToString(const LogoMetadata& metadata, | 145 void LogoCache::LogoMetadataToString(const LogoMetadata& metadata, |
| 145 int num_bytes, | 146 int num_bytes, |
| 146 std::string* str) { | 147 std::string* str) { |
| 147 base::DictionaryValue dict; | 148 base::DictionaryValue dict; |
| 148 dict.SetString(kSourceUrlKey, metadata.source_url); | 149 dict.SetString(kSourceUrlKey, metadata.source_url); |
| 149 dict.SetString(kFingerprintKey, metadata.fingerprint); | 150 dict.SetString(kFingerprintKey, metadata.fingerprint); |
| 150 dict.SetString(kOnClickURLKey, metadata.on_click_url); | 151 dict.SetString(kOnClickURLKey, metadata.on_click_url); |
| 151 dict.SetString(kAltTextKey, metadata.alt_text); | 152 dict.SetString(kAltTextKey, metadata.alt_text); |
| 152 dict.SetString(kAnimatedUrlKey, metadata.animated_url); | 153 dict.SetString(kAnimatedUrlKey, metadata.animated_url); |
| 153 dict.SetString(kMimeTypeKey, metadata.mime_type); | 154 dict.SetString(kMimeTypeKey, metadata.mime_type); |
| 154 dict.SetBoolean(kCanShowAfterExpirationKey, | 155 dict.SetBoolean(kCanShowAfterExpirationKey, |
| 155 metadata.can_show_after_expiration); | 156 metadata.can_show_after_expiration); |
| 156 dict.SetInteger(kNumBytesKey, num_bytes); | 157 dict.SetInteger(kNumBytesKey, num_bytes); |
| 157 SetTimeValue(dict, kExpirationTimeKey, metadata.expiration_time); | 158 SetTimeValue(dict, kExpirationTimeKey, metadata.expiration_time); |
| 158 base::JSONWriter::Write(dict, str); | 159 base::JSONWriter::Write(dict, str); |
| 159 } | 160 } |
| 160 | 161 |
| 161 base::FilePath LogoCache::GetLogoPath() { | 162 base::FilePath LogoCache::GetLogoPath() { |
| 162 return cache_directory_.Append(FILE_PATH_LITERAL("logo")); | 163 return cache_directory_.Append(FILE_PATH_LITERAL("logo")); |
| 163 } | 164 } |
| 164 | 165 |
| 165 base::FilePath LogoCache::GetMetadataPath() { | 166 base::FilePath LogoCache::GetMetadataPath() { |
| 166 return cache_directory_.Append(FILE_PATH_LITERAL("metadata")); | 167 return cache_directory_.Append(FILE_PATH_LITERAL("metadata")); |
| 167 } | 168 } |
| 168 | 169 |
| 169 void LogoCache::UpdateMetadata(scoped_ptr<LogoMetadata> metadata) { | 170 void LogoCache::UpdateMetadata(scoped_ptr<LogoMetadata> metadata) { |
| 170 metadata_ = metadata.Pass(); | 171 metadata_ = std::move(metadata); |
| 171 metadata_is_valid_ = true; | 172 metadata_is_valid_ = true; |
| 172 } | 173 } |
| 173 | 174 |
| 174 void LogoCache::ReadMetadataIfNeeded() { | 175 void LogoCache::ReadMetadataIfNeeded() { |
| 175 if (metadata_is_valid_) | 176 if (metadata_is_valid_) |
| 176 return; | 177 return; |
| 177 | 178 |
| 178 scoped_ptr<LogoMetadata> metadata; | 179 scoped_ptr<LogoMetadata> metadata; |
| 179 base::FilePath metadata_path = GetMetadataPath(); | 180 base::FilePath metadata_path = GetMetadataPath(); |
| 180 std::string str; | 181 std::string str; |
| 181 if (base::ReadFileToString(metadata_path, &str)) { | 182 if (base::ReadFileToString(metadata_path, &str)) { |
| 182 metadata = LogoMetadataFromString(str, &logo_num_bytes_); | 183 metadata = LogoMetadataFromString(str, &logo_num_bytes_); |
| 183 if (!metadata) { | 184 if (!metadata) { |
| 184 // Delete corrupt metadata and logo. | 185 // Delete corrupt metadata and logo. |
| 185 DeleteLogoAndMetadata(); | 186 DeleteLogoAndMetadata(); |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 | 189 |
| 189 UpdateMetadata(metadata.Pass()); | 190 UpdateMetadata(std::move(metadata)); |
| 190 } | 191 } |
| 191 | 192 |
| 192 void LogoCache::WriteMetadata() { | 193 void LogoCache::WriteMetadata() { |
| 193 if (!EnsureCacheDirectoryExists()) | 194 if (!EnsureCacheDirectoryExists()) |
| 194 return; | 195 return; |
| 195 | 196 |
| 196 std::string str; | 197 std::string str; |
| 197 LogoMetadataToString(*metadata_, logo_num_bytes_, &str); | 198 LogoMetadataToString(*metadata_, logo_num_bytes_, &str); |
| 198 base::WriteFile(GetMetadataPath(), str.data(), static_cast<int>(str.size())); | 199 base::WriteFile(GetMetadataPath(), str.data(), static_cast<int>(str.size())); |
| 199 } | 200 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 base::DeleteFile(GetMetadataPath(), false); | 233 base::DeleteFile(GetMetadataPath(), false); |
| 233 } | 234 } |
| 234 | 235 |
| 235 bool LogoCache::EnsureCacheDirectoryExists() { | 236 bool LogoCache::EnsureCacheDirectoryExists() { |
| 236 if (base::DirectoryExists(cache_directory_)) | 237 if (base::DirectoryExists(cache_directory_)) |
| 237 return true; | 238 return true; |
| 238 return base::CreateDirectory(cache_directory_); | 239 return base::CreateDirectory(cache_directory_); |
| 239 } | 240 } |
| 240 | 241 |
| 241 } // namespace search_provider_logos | 242 } // namespace search_provider_logos |
| OLD | NEW |