Chromium Code Reviews| Index: ui/base/resource/data_pack.cc |
| diff --git a/ui/base/resource/data_pack.cc b/ui/base/resource/data_pack.cc |
| index 1fcdfe1dff58a8629bf7d0858b2f512680562872..34c8892a11d1c7913e836b7f8abcfc41ee8bf88d 100644 |
| --- a/ui/base/resource/data_pack.cc |
| +++ b/ui/base/resource/data_pack.cc |
| @@ -17,9 +17,9 @@ |
| namespace { |
| -static const uint32 kFileFormatVersion = 3; |
| -// Length of file header: version and entry count. |
| -static const size_t kHeaderLength = 2 * sizeof(uint32); |
| +static const uint32 kFileFormatVersion = 4; |
| +// Length of file header: version, entry count and text encoding type. |
| +static const size_t kHeaderLength = 2 * sizeof(uint32) + sizeof(uint8); |
| #pragma pack(push,2) |
| struct DataPackEntry { |
| @@ -60,7 +60,7 @@ enum LoadErrors { |
| namespace ui { |
| // In .cc for MemoryMappedFile dtor. |
| -DataPack::DataPack() : resource_count_(0) { |
| +DataPack::DataPack() : resource_count_(0), text_encoding_type_(BINARY) { |
| } |
| DataPack::~DataPack() { |
| } |
| @@ -76,7 +76,7 @@ bool DataPack::Load(const FilePath& path) { |
| } |
| // Parse the header of the file. |
|
tony
2011/08/30 21:19:58
We should probably make sure that kHeaderLength <=
marcvs
2011/08/30 22:17:22
Done.
|
| - // First uint32: version; second: resource count. |
| + // First uint32: version; second: resource count; |
| const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); |
| uint32 version = ptr[0]; |
| if (version != kFileFormatVersion) { |
| @@ -89,6 +89,17 @@ bool DataPack::Load(const FilePath& path) { |
| } |
| resource_count_ = ptr[1]; |
| + // third: text encoding. |
| + const uint8* ptr_encoding = reinterpret_cast<const uint8*>(ptr + 2); |
| + text_encoding_type_ = (TextEncodingType)(*ptr_encoding); |
|
tony
2011/08/30 21:19:58
Nit: static_cast<TextEncodingType>
marcvs
2011/08/30 22:17:22
Done.
|
| + if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && |
| + text_encoding_type_ != BINARY) { |
| + LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ |
| + << ", expected between " << BINARY << " and " << UTF16; |
| + mmap_.reset(); |
| + return false; |
| + } |
| + |
| // Sanity check the file. |
| // 1) Check we have enough entries. |
| if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) > |
| @@ -156,7 +167,8 @@ RefCountedStaticMemory* DataPack::GetStaticMemory(uint16 resource_id) const { |
| // static |
| bool DataPack::WritePack(const FilePath& path, |
| - const std::map<uint16, base::StringPiece>& resources) { |
| + const std::map<uint16, base::StringPiece>& resources, |
| + TextEncodingType textEncodingType) { |
| FILE* file = file_util::OpenFile(path, "wb"); |
| if (!file) |
| return false; |
| @@ -176,6 +188,21 @@ bool DataPack::WritePack(const FilePath& path, |
| return false; |
| } |
| + if (textEncodingType != UTF8 && textEncodingType != UTF16 && |
| + textEncodingType != BINARY) { |
| + LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType |
| + << ", expected between " << BINARY << " and " << UTF16; |
| + file_util::CloseFile(file); |
| + return false; |
| + } |
| + |
| + uint8 write_buffer = textEncodingType; |
| + if (fwrite(&write_buffer, sizeof(uint8), 1, file) != 1) { |
| + LOG(ERROR) << "Failed to write file text resources encoding"; |
| + file_util::CloseFile(file); |
| + return false; |
| + } |
| + |
| // Each entry is a uint16 + a uint32. We have an extra entry after the last |
| // item so we can compute the size of the list item. |
| uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); |