| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/data_pack.h" | 5 #include "app/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/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/ref_counted_memory.h" | 12 #include "base/ref_counted_memory.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 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 INIT_FAILED = 1, | 52 INIT_FAILED = 1, |
| 53 BAD_VERSION, | 53 BAD_VERSION, |
| 54 INDEX_TRUNCATED, | 54 INDEX_TRUNCATED, |
| 55 ENTRY_NOT_FOUND, | 55 ENTRY_NOT_FOUND, |
| 56 | 56 |
| 57 LOAD_ERRORS_COUNT, | 57 LOAD_ERRORS_COUNT, |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 } // anonymous namespace | 60 } // anonymous namespace |
| 61 | 61 |
| 62 namespace base { | 62 namespace app { |
| 63 | 63 |
| 64 // In .cc for MemoryMappedFile dtor. | 64 // In .cc for MemoryMappedFile dtor. |
| 65 DataPack::DataPack() : resource_count_(0) { | 65 DataPack::DataPack() : resource_count_(0) { |
| 66 } | 66 } |
| 67 DataPack::~DataPack() { | 67 DataPack::~DataPack() { |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool DataPack::Load(const FilePath& path) { | 70 bool DataPack::Load(const FilePath& path) { |
| 71 mmap_.reset(new file_util::MemoryMappedFile); | 71 mmap_.reset(new file_util::MemoryMappedFile); |
| 72 if (!mmap_->Initialize(path)) { | 72 if (!mmap_->Initialize(path)) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 111 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
| 112 LOAD_ERRORS_COUNT); | 112 LOAD_ERRORS_COUNT); |
| 113 mmap_.reset(); | 113 mmap_.reset(); |
| 114 return false; | 114 return false; |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 return true; | 118 return true; |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool DataPack::GetStringPiece(uint32 resource_id, StringPiece* data) const { | 121 bool DataPack::GetStringPiece(uint32 resource_id, |
| 122 base::StringPiece* data) const { |
| 122 // It won't be hard to make this endian-agnostic, but it's not worth | 123 // It won't be hard to make this endian-agnostic, but it's not worth |
| 123 // bothering to do right now. | 124 // bothering to do right now. |
| 124 #if defined(__BYTE_ORDER) | 125 #if defined(__BYTE_ORDER) |
| 125 // Linux check | 126 // Linux check |
| 126 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, | 127 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, |
| 127 datapack_assumes_little_endian); | 128 datapack_assumes_little_endian); |
| 128 #elif defined(__BIG_ENDIAN__) | 129 #elif defined(__BIG_ENDIAN__) |
| 129 // Mac check | 130 // Mac check |
| 130 #error DataPack assumes little endian | 131 #error DataPack assumes little endian |
| 131 #endif | 132 #endif |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 base::StringPiece piece; | 146 base::StringPiece piece; |
| 146 if (!GetStringPiece(resource_id, &piece)) | 147 if (!GetStringPiece(resource_id, &piece)) |
| 147 return NULL; | 148 return NULL; |
| 148 | 149 |
| 149 return new RefCountedStaticMemory( | 150 return new RefCountedStaticMemory( |
| 150 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); | 151 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); |
| 151 } | 152 } |
| 152 | 153 |
| 153 // static | 154 // static |
| 154 bool DataPack::WritePack(const FilePath& path, | 155 bool DataPack::WritePack(const FilePath& path, |
| 155 const std::map<uint32, StringPiece>& resources) { | 156 const std::map<uint32, base::StringPiece>& resources) { |
| 156 FILE* file = file_util::OpenFile(path, "wb"); | 157 FILE* file = file_util::OpenFile(path, "wb"); |
| 157 if (!file) | 158 if (!file) |
| 158 return false; | 159 return false; |
| 159 | 160 |
| 160 if (fwrite(&kFileFormatVersion, 1, kWord, file) != kWord) { | 161 if (fwrite(&kFileFormatVersion, 1, kWord, file) != kWord) { |
| 161 LOG(ERROR) << "Failed to write file version"; | 162 LOG(ERROR) << "Failed to write file version"; |
| 162 file_util::CloseFile(file); | 163 file_util::CloseFile(file); |
| 163 return false; | 164 return false; |
| 164 } | 165 } |
| 165 | 166 |
| 166 // Note: the python version of this function explicitly sorted keys, but | 167 // Note: the python version of this function explicitly sorted keys, but |
| 167 // std::map is a sorted associative container, we shouldn't have to do that. | 168 // std::map is a sorted associative container, we shouldn't have to do that. |
| 168 uint32 entry_count = resources.size(); | 169 uint32 entry_count = resources.size(); |
| 169 if (fwrite(&entry_count, 1, kWord, file) != kWord) { | 170 if (fwrite(&entry_count, 1, kWord, file) != kWord) { |
| 170 LOG(ERROR) << "Failed to write entry count"; | 171 LOG(ERROR) << "Failed to write entry count"; |
| 171 file_util::CloseFile(file); | 172 file_util::CloseFile(file); |
| 172 return false; | 173 return false; |
| 173 } | 174 } |
| 174 | 175 |
| 175 // Each entry is 3 uint32s. | 176 // Each entry is 3 uint32s. |
| 176 uint32 index_length = entry_count * 3 * kWord; | 177 uint32 index_length = entry_count * 3 * kWord; |
| 177 uint32 data_offset = kHeaderLength + index_length; | 178 uint32 data_offset = kHeaderLength + index_length; |
| 178 for (std::map<uint32, StringPiece>::const_iterator it = resources.begin(); | 179 for (std::map<uint32, base::StringPiece>::const_iterator it = |
| 180 resources.begin(); |
| 179 it != resources.end(); ++it) { | 181 it != resources.end(); ++it) { |
| 180 if (fwrite(&it->first, 1, kWord, file) != kWord) { | 182 if (fwrite(&it->first, 1, kWord, file) != kWord) { |
| 181 LOG(ERROR) << "Failed to write id for " << it->first; | 183 LOG(ERROR) << "Failed to write id for " << it->first; |
| 182 file_util::CloseFile(file); | 184 file_util::CloseFile(file); |
| 183 return false; | 185 return false; |
| 184 } | 186 } |
| 185 | 187 |
| 186 if (fwrite(&data_offset, 1, kWord, file) != kWord) { | 188 if (fwrite(&data_offset, 1, kWord, file) != kWord) { |
| 187 LOG(ERROR) << "Failed to write offset for " << it->first; | 189 LOG(ERROR) << "Failed to write offset for " << it->first; |
| 188 file_util::CloseFile(file); | 190 file_util::CloseFile(file); |
| 189 return false; | 191 return false; |
| 190 } | 192 } |
| 191 | 193 |
| 192 uint32 len = it->second.length(); | 194 uint32 len = it->second.length(); |
| 193 if (fwrite(&len, 1, kWord, file) != kWord) { | 195 if (fwrite(&len, 1, kWord, file) != kWord) { |
| 194 LOG(ERROR) << "Failed to write length for " << it->first; | 196 LOG(ERROR) << "Failed to write length for " << it->first; |
| 195 file_util::CloseFile(file); | 197 file_util::CloseFile(file); |
| 196 return false; | 198 return false; |
| 197 } | 199 } |
| 198 | 200 |
| 199 data_offset += len; | 201 data_offset += len; |
| 200 } | 202 } |
| 201 | 203 |
| 202 for (std::map<uint32, StringPiece>::const_iterator it = resources.begin(); | 204 for (std::map<uint32, base::StringPiece>::const_iterator it = |
| 205 resources.begin(); |
| 203 it != resources.end(); ++it) { | 206 it != resources.end(); ++it) { |
| 204 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { | 207 if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { |
| 205 LOG(ERROR) << "Failed to write data for " << it->first; | 208 LOG(ERROR) << "Failed to write data for " << it->first; |
| 206 file_util::CloseFile(file); | 209 file_util::CloseFile(file); |
| 207 return false; | 210 return false; |
| 208 } | 211 } |
| 209 } | 212 } |
| 210 | 213 |
| 211 file_util::CloseFile(file); | 214 file_util::CloseFile(file); |
| 212 | 215 |
| 213 return true; | 216 return true; |
| 214 } | 217 } |
| 215 | 218 |
| 216 } // namespace base | 219 } // namespace app |
| OLD | NEW |