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..99999f27d9271aa4245f6f61f33963ca857a4e1d 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 = 3 * sizeof(uint32); |
| #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. |
| - // First uint32: version; second: resource count. |
| + // First uint32: version; second: resource count; third: text encoding. |
| const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); |
| uint32 version = ptr[0]; |
| if (version != kFileFormatVersion) { |
| @@ -88,6 +88,14 @@ bool DataPack::Load(const FilePath& path) { |
| return false; |
| } |
| resource_count_ = ptr[1]; |
| + text_encoding_type_ = ptr[2]; |
| + 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. |
| @@ -156,7 +164,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, |
| + const uint32 textEncodingType) { |
| FILE* file = file_util::OpenFile(path, "wb"); |
| if (!file) |
| return false; |
| @@ -176,6 +185,20 @@ bool DataPack::WritePack(const FilePath& path, |
| return false; |
| } |
| + if (textEncodingType != UTF8 && textEncodingType != UTF16 && |
| + textEncodingType != BINARY) { |
| + LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType |
| + << ", expected betwee " << BINARY << " and " << UTF16; |
|
tony
2011/08/29 21:24:39
typo: between
marcvs
2011/08/30 07:58:55
Done.
|
| + file_util::CloseFile(file); |
| + return false; |
| + } |
| + |
| + if (fwrite(&textEncodingType, sizeof(textEncodingType), 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); |