OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 209 } |
210 | 210 |
211 ui::ScaleFactor DataPack::GetScaleFactor() const { | 211 ui::ScaleFactor DataPack::GetScaleFactor() const { |
212 return scale_factor_; | 212 return scale_factor_; |
213 } | 213 } |
214 | 214 |
215 // static | 215 // static |
216 bool DataPack::WritePack(const base::FilePath& path, | 216 bool DataPack::WritePack(const base::FilePath& path, |
217 const std::map<uint16, base::StringPiece>& resources, | 217 const std::map<uint16, base::StringPiece>& resources, |
218 TextEncodingType textEncodingType) { | 218 TextEncodingType textEncodingType) { |
219 FILE* file = file_util::OpenFile(path, "wb"); | 219 FILE* file = base::OpenFile(path, "wb"); |
220 if (!file) | 220 if (!file) |
221 return false; | 221 return false; |
222 | 222 |
223 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { | 223 if (fwrite(&kFileFormatVersion, sizeof(kFileFormatVersion), 1, file) != 1) { |
224 LOG(ERROR) << "Failed to write file version"; | 224 LOG(ERROR) << "Failed to write file version"; |
225 file_util::CloseFile(file); | 225 base::CloseFile(file); |
226 return false; | 226 return false; |
227 } | 227 } |
228 | 228 |
229 // Note: the python version of this function explicitly sorted keys, but | 229 // Note: the python version of this function explicitly sorted keys, but |
230 // std::map is a sorted associative container, we shouldn't have to do that. | 230 // std::map is a sorted associative container, we shouldn't have to do that. |
231 uint32 entry_count = resources.size(); | 231 uint32 entry_count = resources.size(); |
232 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { | 232 if (fwrite(&entry_count, sizeof(entry_count), 1, file) != 1) { |
233 LOG(ERROR) << "Failed to write entry count"; | 233 LOG(ERROR) << "Failed to write entry count"; |
234 file_util::CloseFile(file); | 234 base::CloseFile(file); |
235 return false; | 235 return false; |
236 } | 236 } |
237 | 237 |
238 if (textEncodingType != UTF8 && textEncodingType != UTF16 && | 238 if (textEncodingType != UTF8 && textEncodingType != UTF16 && |
239 textEncodingType != BINARY) { | 239 textEncodingType != BINARY) { |
240 LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType | 240 LOG(ERROR) << "Invalid text encoding type, got " << textEncodingType |
241 << ", expected between " << BINARY << " and " << UTF16; | 241 << ", expected between " << BINARY << " and " << UTF16; |
242 file_util::CloseFile(file); | 242 base::CloseFile(file); |
243 return false; | 243 return false; |
244 } | 244 } |
245 | 245 |
246 uint8 write_buffer = textEncodingType; | 246 uint8 write_buffer = textEncodingType; |
247 if (fwrite(&write_buffer, sizeof(uint8), 1, file) != 1) { | 247 if (fwrite(&write_buffer, sizeof(uint8), 1, file) != 1) { |
248 LOG(ERROR) << "Failed to write file text resources encoding"; | 248 LOG(ERROR) << "Failed to write file text resources encoding"; |
249 file_util::CloseFile(file); | 249 base::CloseFile(file); |
250 return false; | 250 return false; |
251 } | 251 } |
252 | 252 |
253 // Each entry is a uint16 + a uint32. We have an extra entry after the last | 253 // Each entry is a uint16 + a uint32. We have an extra entry after the last |
254 // item so we can compute the size of the list item. | 254 // item so we can compute the size of the list item. |
255 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); | 255 uint32 index_length = (entry_count + 1) * sizeof(DataPackEntry); |
256 uint32 data_offset = kHeaderLength + index_length; | 256 uint32 data_offset = kHeaderLength + index_length; |
257 for (std::map<uint16, base::StringPiece>::const_iterator it = | 257 for (std::map<uint16, base::StringPiece>::const_iterator it = |
258 resources.begin(); | 258 resources.begin(); |
259 it != resources.end(); ++it) { | 259 it != resources.end(); ++it) { |
260 uint16 resource_id = it->first; | 260 uint16 resource_id = it->first; |
261 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { | 261 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { |
262 LOG(ERROR) << "Failed to write id for " << resource_id; | 262 LOG(ERROR) << "Failed to write id for " << resource_id; |
263 file_util::CloseFile(file); | 263 base::CloseFile(file); |
264 return false; | 264 return false; |
265 } | 265 } |
266 | 266 |
267 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { | 267 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { |
268 LOG(ERROR) << "Failed to write offset for " << resource_id; | 268 LOG(ERROR) << "Failed to write offset for " << resource_id; |
269 file_util::CloseFile(file); | 269 base::CloseFile(file); |
270 return false; | 270 return false; |
271 } | 271 } |
272 | 272 |
273 data_offset += it->second.length(); | 273 data_offset += it->second.length(); |
274 } | 274 } |
275 | 275 |
276 // We place an extra entry after the last item that allows us to read the | 276 // We place an extra entry after the last item that allows us to read the |
277 // size of the last item. | 277 // size of the last item. |
278 uint16 resource_id = 0; | 278 uint16 resource_id = 0; |
279 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { | 279 if (fwrite(&resource_id, sizeof(resource_id), 1, file) != 1) { |
280 LOG(ERROR) << "Failed to write extra resource id."; | 280 LOG(ERROR) << "Failed to write extra resource id."; |
281 file_util::CloseFile(file); | 281 base::CloseFile(file); |
282 return false; | 282 return false; |
283 } | 283 } |
284 | 284 |
285 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { | 285 if (fwrite(&data_offset, sizeof(data_offset), 1, file) != 1) { |
286 LOG(ERROR) << "Failed to write extra offset."; | 286 LOG(ERROR) << "Failed to write extra offset."; |
287 file_util::CloseFile(file); | 287 base::CloseFile(file); |
288 return false; | 288 return false; |
289 } | 289 } |
290 | 290 |
291 for (std::map<uint16, base::StringPiece>::const_iterator it = | 291 for (std::map<uint16, base::StringPiece>::const_iterator it = |
292 resources.begin(); | 292 resources.begin(); |
293 it != resources.end(); ++it) { | 293 it != resources.end(); ++it) { |
294 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { | 294 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { |
295 LOG(ERROR) << "Failed to write data for " << it->first; | 295 LOG(ERROR) << "Failed to write data for " << it->first; |
296 file_util::CloseFile(file); | 296 base::CloseFile(file); |
297 return false; | 297 return false; |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 file_util::CloseFile(file); | 301 base::CloseFile(file); |
302 | 302 |
303 return true; | 303 return true; |
304 } | 304 } |
305 | 305 |
306 } // namespace ui | 306 } // namespace ui |
OLD | NEW |