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 "chrome/browser/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 int prs_id = kPersistingImages[i].persistent_id; | 181 int prs_id = kPersistingImages[i].persistent_id; |
182 (*lookup_table)[idr] = prs_id; | 182 (*lookup_table)[idr] = prs_id; |
183 } | 183 } |
184 } | 184 } |
185 std::map<int,int>::iterator it = lookup_table->find(idr); | 185 std::map<int,int>::iterator it = lookup_table->find(idr); |
186 return (it == lookup_table->end()) ? -1 : it->second; | 186 return (it == lookup_table->end()) ? -1 : it->second; |
187 } | 187 } |
188 | 188 |
189 // Returns true if the scales in |input| match those in |expected|. | 189 // Returns true if the scales in |input| match those in |expected|. |
190 // The order must match as the index is used in determining the raw id. | 190 // The order must match as the index is used in determining the raw id. |
191 bool InputScalesValid(const char* input, | 191 bool InputScalesValid(const base::StringPiece& input, |
192 const std::vector<ui::ScaleFactor>& expected) { | 192 const std::vector<ui::ScaleFactor>& expected) { |
193 const float* scales = reinterpret_cast<const float*>(input); | 193 size_t scales_size = static_cast<size_t>(input.size() / sizeof(float)); |
194 size_t index = 0; | 194 if (scales_size != expected.size()) |
195 for (const float* end = scales; *end != -1.0f; ++end) { | 195 return false; |
196 if (index >= expected.size()) | 196 scoped_array<float> scales(new float[scales_size]); |
| 197 // Do a memcpy to avoid misaligned memory access. |
| 198 memcpy(scales.get(), input.data(), input.size()); |
| 199 for (size_t index = 0; index < scales_size; ++index) { |
| 200 if (scales[index] != ui::GetScaleFactorScale(expected[index])) |
197 return false; | 201 return false; |
198 if (*end != ui::GetScaleFactorScale(expected[index])) | |
199 return false; | |
200 index++; | |
201 } | 202 } |
202 return (index == expected.size()); | 203 return true; |
203 } | 204 } |
204 | 205 |
205 // Returns |scale_factors| as a string to be written to disk. | 206 // Returns |scale_factors| as a string to be written to disk. |
206 std::string GetScaleFactorsAsString( | 207 std::string GetScaleFactorsAsString( |
207 const std::vector<ui::ScaleFactor>& scale_factors) { | 208 const std::vector<ui::ScaleFactor>& scale_factors) { |
208 size_t scales_size = scale_factors.size() + 1; | 209 scoped_array<float> scales(new float[scale_factors.size()]); |
209 float* scales = new float[scales_size]; | |
210 for (size_t i = 0; i < scale_factors.size(); ++i) | 210 for (size_t i = 0; i < scale_factors.size(); ++i) |
211 scales[i] = ui::GetScaleFactorScale(scale_factors[i]); | 211 scales[i] = ui::GetScaleFactorScale(scale_factors[i]); |
212 scales[scales_size - 1] = -1.0f; | |
213 std::string out_string = std::string( | 212 std::string out_string = std::string( |
214 reinterpret_cast<const char*>(scales), | 213 reinterpret_cast<const char*>(scales.get()), |
215 scales_size * sizeof(float)); | 214 scale_factors.size() * sizeof(float)); |
216 delete[] scales; | |
217 return out_string; | 215 return out_string; |
218 } | 216 } |
219 | 217 |
220 struct StringToIntTable { | 218 struct StringToIntTable { |
221 const char* key; | 219 const char* key; |
222 int id; | 220 int id; |
223 }; | 221 }; |
224 | 222 |
225 // Strings used by themes to identify tints in the JSON. | 223 // Strings used by themes to identify tints in the JSON. |
226 StringToIntTable kTintTable[] = { | 224 StringToIntTable kTintTable[] = { |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 const_cast<char*>(pointer.data())); | 499 const_cast<char*>(pointer.data())); |
502 | 500 |
503 if (!pack->data_pack_->GetStringPiece(kSourceImagesID, &pointer)) | 501 if (!pack->data_pack_->GetStringPiece(kSourceImagesID, &pointer)) |
504 return NULL; | 502 return NULL; |
505 pack->source_images_ = reinterpret_cast<int*>( | 503 pack->source_images_ = reinterpret_cast<int*>( |
506 const_cast<char*>(pointer.data())); | 504 const_cast<char*>(pointer.data())); |
507 | 505 |
508 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) | 506 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) |
509 return NULL; | 507 return NULL; |
510 | 508 |
511 if (!InputScalesValid(const_cast<char*>(pointer.data()), | 509 if (!InputScalesValid(pointer, pack->scale_factors_)) { |
512 pack->scale_factors_)) { | |
513 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " | 510 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " |
514 << "from those supported by platform."; | 511 << "from those supported by platform."; |
515 } | 512 } |
516 | |
517 return pack; | 513 return pack; |
518 } | 514 } |
519 | 515 |
520 bool BrowserThemePack::WriteToDisk(const FilePath& path) const { | 516 bool BrowserThemePack::WriteToDisk(const FilePath& path) const { |
521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 517 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
522 // Add resources for each of the property arrays. | 518 // Add resources for each of the property arrays. |
523 RawDataForWriting resources; | 519 RawDataForWriting resources; |
524 resources[kHeaderID] = base::StringPiece( | 520 resources[kHeaderID] = base::StringPiece( |
525 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); | 521 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); |
526 resources[kTintsID] = base::StringPiece( | 522 resources[kTintsID] = base::StringPiece( |
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 ui::ScaleFactor scale_factor) const { | 1228 ui::ScaleFactor scale_factor) const { |
1233 if (prs_id < 0) | 1229 if (prs_id < 0) |
1234 return -1; | 1230 return -1; |
1235 | 1231 |
1236 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1232 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1237 if (scale_factors_[i] == scale_factor) | 1233 if (scale_factors_[i] == scale_factor) |
1238 return static_cast<int>(kPersistingImagesLength * i) + prs_id; | 1234 return static_cast<int>(kPersistingImagesLength * i) + prs_id; |
1239 } | 1235 } |
1240 return -1; | 1236 return -1; |
1241 } | 1237 } |
OLD | NEW |