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/logging.h" | 10 #include "base/logging.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 HEADER_TRUNCATED, | 54 HEADER_TRUNCATED, |
55 WRONG_ENCODING, | 55 WRONG_ENCODING, |
56 | 56 |
57 LOAD_ERRORS_COUNT, | 57 LOAD_ERRORS_COUNT, |
58 }; | 58 }; |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 namespace ui { | 62 namespace ui { |
63 | 63 |
64 DataPack::DataPack(float scale_factor) | 64 DataPack::DataPack(ui::ScaleFactor scale_factor) |
65 : resource_count_(0), | 65 : resource_count_(0), |
66 text_encoding_type_(BINARY), | 66 text_encoding_type_(BINARY), |
67 scale_factor_(scale_factor) { | 67 scale_factor_(scale_factor) { |
68 } | 68 } |
69 | 69 |
70 DataPack::~DataPack() { | 70 DataPack::~DataPack() { |
71 } | 71 } |
72 | 72 |
73 bool DataPack::Load(const FilePath& path) { | 73 bool DataPack::Load(const FilePath& path) { |
74 mmap_.reset(new file_util::MemoryMappedFile); | 74 mmap_.reset(new file_util::MemoryMappedFile); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 138 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
139 LOAD_ERRORS_COUNT); | 139 LOAD_ERRORS_COUNT); |
140 mmap_.reset(); | 140 mmap_.reset(); |
141 return false; | 141 return false; |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 return true; | 145 return true; |
146 } | 146 } |
147 | 147 |
| 148 bool DataPack::HasResource(uint16 resource_id) const { |
| 149 return !!bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, |
| 150 sizeof(DataPackEntry), DataPackEntry::CompareById); |
| 151 } |
| 152 |
148 bool DataPack::GetStringPiece(uint16 resource_id, | 153 bool DataPack::GetStringPiece(uint16 resource_id, |
149 base::StringPiece* data) const { | 154 base::StringPiece* data) const { |
150 // It won't be hard to make this endian-agnostic, but it's not worth | 155 // It won't be hard to make this endian-agnostic, but it's not worth |
151 // bothering to do right now. | 156 // bothering to do right now. |
152 #if defined(__BYTE_ORDER) | 157 #if defined(__BYTE_ORDER) |
153 // Linux check | 158 // Linux check |
154 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, | 159 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, |
155 datapack_assumes_little_endian); | 160 datapack_assumes_little_endian); |
156 #elif defined(__BIG_ENDIAN__) | 161 #elif defined(__BIG_ENDIAN__) |
157 // Mac check | 162 // Mac check |
(...skipping 21 matching lines...) Expand all Loading... |
179 return NULL; | 184 return NULL; |
180 | 185 |
181 return new base::RefCountedStaticMemory( | 186 return new base::RefCountedStaticMemory( |
182 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); | 187 reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); |
183 } | 188 } |
184 | 189 |
185 ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { | 190 ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { |
186 return text_encoding_type_; | 191 return text_encoding_type_; |
187 } | 192 } |
188 | 193 |
189 float DataPack::GetScaleFactor() const { | 194 ui::ScaleFactor DataPack::GetScaleFactor() const { |
190 return scale_factor_; | 195 return scale_factor_; |
191 } | 196 } |
192 | 197 |
193 // static | 198 // static |
194 bool DataPack::WritePack(const FilePath& path, | 199 bool DataPack::WritePack(const FilePath& path, |
195 const std::map<uint16, base::StringPiece>& resources, | 200 const std::map<uint16, base::StringPiece>& resources, |
196 TextEncodingType textEncodingType) { | 201 TextEncodingType textEncodingType) { |
197 FILE* file = file_util::OpenFile(path, "wb"); | 202 FILE* file = file_util::OpenFile(path, "wb"); |
198 if (!file) | 203 if (!file) |
199 return false; | 204 return false; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 return false; | 280 return false; |
276 } | 281 } |
277 } | 282 } |
278 | 283 |
279 file_util::CloseFile(file); | 284 file_util::CloseFile(file); |
280 | 285 |
281 return true; | 286 return true; |
282 } | 287 } |
283 | 288 |
284 } // namespace ui | 289 } // namespace ui |
OLD | NEW |