Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: ui/base/resource/data_pack.cc

Issue 7890060: This broke lots of layout tests on OSX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/resource/data_pack.h ('k') | ui/base/resource/data_pack_literal.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ui/base/resource/data_pack.h" 5 #include "ui/base/resource/data_pack.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted_memory.h" 11 #include "base/memory/ref_counted_memory.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/string_piece.h" 13 #include "base/string_piece.h"
14 14
15 // For details of the file layout, see 15 // For details of the file layout, see
16 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings 16 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings
17 17
18 namespace { 18 namespace {
19 19
20 static const uint32 kFileFormatVersion = 4; 20 static const uint32 kFileFormatVersion = 3;
21 // Length of file header: version, entry count and text encoding type. 21 // Length of file header: version and entry count.
22 static const size_t kHeaderLength = 2 * sizeof(uint32) + sizeof(uint8); 22 static const size_t kHeaderLength = 2 * sizeof(uint32);
23 23
24 #pragma pack(push,2) 24 #pragma pack(push,2)
25 struct DataPackEntry { 25 struct DataPackEntry {
26 uint16 resource_id; 26 uint16 resource_id;
27 uint32 file_offset; 27 uint32 file_offset;
28 28
29 static int CompareById(const void* void_key, const void* void_entry) { 29 static int CompareById(const void* void_key, const void* void_entry) {
30 uint16 key = *reinterpret_cast<const uint16*>(void_key); 30 uint16 key = *reinterpret_cast<const uint16*>(void_key);
31 const DataPackEntry* entry = 31 const DataPackEntry* entry =
32 reinterpret_cast<const DataPackEntry*>(void_entry); 32 reinterpret_cast<const DataPackEntry*>(void_entry);
(...skipping 20 matching lines...) Expand all
53 ENTRY_NOT_FOUND, 53 ENTRY_NOT_FOUND,
54 54
55 LOAD_ERRORS_COUNT, 55 LOAD_ERRORS_COUNT,
56 }; 56 };
57 57
58 } // namespace 58 } // namespace
59 59
60 namespace ui { 60 namespace ui {
61 61
62 // In .cc for MemoryMappedFile dtor. 62 // In .cc for MemoryMappedFile dtor.
63 DataPack::DataPack() : resource_count_(0), text_encoding_type_(BINARY) { 63 DataPack::DataPack() : resource_count_(0) {
64 } 64 }
65 DataPack::~DataPack() { 65 DataPack::~DataPack() {
66 } 66 }
67 67
68 bool DataPack::Load(const FilePath& path) { 68 bool DataPack::Load(const FilePath& path) {
69 mmap_.reset(new file_util::MemoryMappedFile); 69 mmap_.reset(new file_util::MemoryMappedFile);
70 if (!mmap_->Initialize(path)) { 70 if (!mmap_->Initialize(path)) {
71 DLOG(ERROR) << "Failed to mmap datapack"; 71 DLOG(ERROR) << "Failed to mmap datapack";
72 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, 72 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED,
73 LOAD_ERRORS_COUNT); 73 LOAD_ERRORS_COUNT);
74 mmap_.reset(); 74 mmap_.reset();
75 return false; 75 return false;
76 } 76 }
77 77
78 // Sanity check the header of the file. 78 // Sanity check the header of the file.
79 if (kHeaderLength > mmap_->length()) { 79 if (kHeaderLength > mmap_->length()) {
80 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; 80 DLOG(ERROR) << "Data pack file corruption: incomplete file header.";
81 mmap_.reset(); 81 mmap_.reset();
82 return false; 82 return false;
83 } 83 }
84 84
85 // Parse the header of the file. 85 // Parse the header of the file.
86 // First uint32: version; second: resource count; 86 // First uint32: version; second: resource count.
87 const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); 87 const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data());
88 uint32 version = ptr[0]; 88 uint32 version = ptr[0];
89 if (version != kFileFormatVersion) { 89 if (version != kFileFormatVersion) {
90 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " 90 LOG(ERROR) << "Bad data pack version: got " << version << ", expected "
91 << kFileFormatVersion; 91 << kFileFormatVersion;
92 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, 92 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION,
93 LOAD_ERRORS_COUNT); 93 LOAD_ERRORS_COUNT);
94 mmap_.reset(); 94 mmap_.reset();
95 return false; 95 return false;
96 } 96 }
97 resource_count_ = ptr[1]; 97 resource_count_ = ptr[1];
98 98
99 // third: text encoding.
100 const uint8* ptr_encoding = reinterpret_cast<const uint8*>(ptr + 2);
101 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding);
102 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 &&
103 text_encoding_type_ != BINARY) {
104 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_
105 << ", expected between " << BINARY << " and " << UTF16;
106 mmap_.reset();
107 return false;
108 }
109
110 // Sanity check the file. 99 // Sanity check the file.
111 // 1) Check we have enough entries. 100 // 1) Check we have enough entries.
112 if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) > 101 if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) >
113 mmap_->length()) { 102 mmap_->length()) {
114 LOG(ERROR) << "Data pack file corruption: too short for number of " 103 LOG(ERROR) << "Data pack file corruption: too short for number of "
115 "entries specified."; 104 "entries specified.";
116 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, 105 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED,
117 LOAD_ERRORS_COUNT); 106 LOAD_ERRORS_COUNT);
118 mmap_.reset(); 107 mmap_.reset();
119 return false; 108 return false;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 base::StringPiece piece; 156 base::StringPiece piece;
168 if (!GetStringPiece(resource_id, &piece)) 157 if (!GetStringPiece(resource_id, &piece))
169 return NULL; 158 return NULL;
170 159
171 return new RefCountedStaticMemory( 160 return new RefCountedStaticMemory(
172 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); 161 reinterpret_cast<const unsigned char*>(piece.data()), piece.length());
173 } 162 }
174 163
175 // static 164 // static
176 bool DataPack::WritePack(const FilePath& path, 165 bool DataPack::WritePack(const FilePath& path,
177 const std::map<uint16, base::StringPiece>& resources, 166 const std::map<uint16, base::StringPiece>& resources) {
178 TextEncodingType textEncodingType) {
179 FILE* file = file_util::OpenFile(path, "wb"); 167 FILE* file = file_util::OpenFile(path, "wb");
180 if (!file) 168 if (!file)
181 return false; 169 return false;
182 170
183 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { 171 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) {
184 LOG(ERROR) << "Failed to write file version"; 172 LOG(ERROR) << "Failed to write file version";
185 file_util::CloseFile(file); 173 file_util::CloseFile(file);
186 return false; 174 return false;
187 } 175 }
188 176
189 // Note: the python version of this function explicitly sorted keys, but 177 // Note: the python version of this function explicitly sorted keys, but
190 // std::map is a sorted associative container, we shouldn't have to do that. 178 // std::map is a sorted associative container, we shouldn't have to do that.
191 uint32 entry_count = resources.size(); 179 uint32 entry_count = resources.size();
192 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { 180 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) {
193 LOG(ERROR) << "Failed to write entry count"; 181 LOG(ERROR) << "Failed to write entry count";
194 file_util::CloseFile(file); 182 file_util::CloseFile(file);
195 return false; 183 return false;
196 } 184 }
197 185
198 if (textEncodingType != UTF8 && textEncodingType != UTF16 &&
199 textEncodingType != BINARY) {
200 LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType
201 << ", expected between " << BINARY << " and " << UTF16;
202 file_util::CloseFile(file);
203 return false;
204 }
205
206 uint8 write_buffer = textEncodingType;
207 if (fwrite(&write_buffer, sizeof(uint8), 1, file) != 1) {
208 LOG(ERROR) << "Failed to write file text resources encoding";
209 file_util::CloseFile(file);
210 return false;
211 }
212
213 // Each entry is a uint16 + a uint32. We have an extra entry after the last 186 // Each entry is a uint16 + a uint32. We have an extra entry after the last
214 // item so we can compute the size of the list item. 187 // item so we can compute the size of the list item.
215 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); 188 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry);
216 uint32 data_offset = kHeaderLength + index_length; 189 uint32 data_offset = kHeaderLength + index_length;
217 for (std::map<uint16, base::StringPiece>::const_iterator it = 190 for (std::map<uint16, base::StringPiece>::const_iterator it =
218 resources.begin(); 191 resources.begin();
219 it != resources.end(); ++it) { 192 it != resources.end(); ++it) {
220 uint16 resource_id = it->first; 193 uint16 resource_id = it->first;
221 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { 194 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) {
222 LOG(ERROR) << "Failed to write id for " << resource_id; 195 LOG(ERROR) << "Failed to write id for " << resource_id;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 return false; 230 return false;
258 } 231 }
259 } 232 }
260 233
261 file_util::CloseFile(file); 234 file_util::CloseFile(file);
262 235
263 return true; 236 return true;
264 } 237 }
265 238
266 } // namespace ui 239 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/data_pack.h ('k') | ui/base/resource/data_pack_literal.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698