Chromium Code Reviews| Index: ui/gfx/icon_util.cc | 
| diff --git a/ui/gfx/icon_util.cc b/ui/gfx/icon_util.cc | 
| index 37ab0d59f53b16e2a702038c541697817ce1ff00..8f7f1ea0c05b3d9dcf244606725d93af167d2e92 100644 | 
| --- a/ui/gfx/icon_util.cc | 
| +++ b/ui/gfx/icon_util.cc | 
| @@ -6,6 +6,7 @@ | 
| #include "base/file_util.h" | 
| #include "base/logging.h" | 
| +#include "base/md5.h" | 
| #include "base/memory/scoped_ptr.h" | 
| #include "base/win/resource_util.h" | 
| #include "base/win/scoped_gdi_object.h" | 
| @@ -20,6 +21,9 @@ | 
| namespace { | 
| +const base::FilePath::CharType kIconChecksumFileExt[] = | 
| + FILE_PATH_LITERAL(".ico.md5"); | 
| + | 
| struct ScopedICONINFO : ICONINFO { | 
| ScopedICONINFO() { | 
| hbmColor = NULL; | 
| @@ -707,3 +711,60 @@ void IconUtil::ComputeBitmapSizeComponents(const SkBitmap& bitmap, | 
| size_t masks_size = *xor_mask_size + and_mask_size; | 
| *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); | 
| } | 
| + | 
| +void IconUtil::GetImageCheckSum(const gfx::ImageFamily& image, | 
| + base::MD5Digest* digest) { | 
| + DCHECK(digest); | 
| + base::MD5Context md5_context; | 
| + base::MD5Init(&md5_context); | 
| + | 
| + for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); | 
| + ++it) { | 
| + SkBitmap bitmap = it->AsBitmap(); | 
| + | 
| + SkAutoLockPixels image_lock(bitmap); | 
| + base::StringPiece image_data( | 
| + reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize()); | 
| + base::MD5Update(&md5_context, image_data); | 
| + } | 
| + | 
| + base::MD5Final(digest, &md5_context); | 
| +} | 
| + | 
| +bool IconUtil::SaveIconWithCheckSum(const base::FilePath& icon_file, | 
| + const gfx::ImageFamily& image) { | 
| + if (!CreateIconFileFromImageFamily(image, icon_file)) | 
| + return false; | 
| + | 
| + base::MD5Digest digest; | 
| + GetImageCheckSum(image, &digest); | 
| + | 
| + base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); | 
| 
 
Alexei Svitkine (slow)
2013/05/09 15:34:57
Nit: cheksum -> checksum
 
 | 
| + return file_util::WriteFile(cheksum_file, | 
| + reinterpret_cast<const char*>(&digest), | 
| + sizeof(digest)) == sizeof(digest); | 
| +} | 
| + | 
| +bool IconUtil::ShouldUpdateIcon(const base::FilePath& icon_file, | 
| + const gfx::ImageFamily& image) { | 
| 
 
Alexei Svitkine (slow)
2013/05/09 15:34:57
Nit: Align.
 
 | 
| + base::FilePath checksum_file( | 
| + icon_file.ReplaceExtension(kIconChecksumFileExt)); | 
| + | 
| + // Returns true if icon_file or checksum file is missing. | 
| + if (!file_util::PathExists(icon_file) || | 
| + !file_util::PathExists(checksum_file)) | 
| + return true; | 
| + | 
| + base::MD5Digest persisted_image_checksum; | 
| + if (sizeof(persisted_image_checksum) != file_util::ReadFile(checksum_file, | 
| + reinterpret_cast<char*>(&persisted_image_checksum), | 
| + sizeof(persisted_image_checksum))) | 
| + return true; | 
| + | 
| + base::MD5Digest downloaded_image_checksum; | 
| + GetImageCheckSum(image, &downloaded_image_checksum); | 
| + | 
| + // Update icon if checksums are not equal. | 
| + return memcmp(&persisted_image_checksum, &downloaded_image_checksum, | 
| + sizeof(base::MD5Digest)) != 0; | 
| +} |