OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/variations/service/ui_string_overrider.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/base/resource/resource_bundle.h" |
| 11 |
| 12 namespace variations { |
| 13 |
| 14 UIStringOverrider::UIStringOverrider() |
| 15 : resource_hashes_(nullptr), |
| 16 resource_indices_(nullptr), |
| 17 num_resources_(0) {} |
| 18 |
| 19 UIStringOverrider::UIStringOverrider(const uint32_t* resource_hashes, |
| 20 const int* resource_indices, |
| 21 size_t num_resources) |
| 22 : resource_hashes_(resource_hashes), |
| 23 resource_indices_(resource_indices), |
| 24 num_resources_(num_resources) { |
| 25 DCHECK(!num_resources || resource_hashes_); |
| 26 DCHECK(!num_resources || resource_indices_); |
| 27 } |
| 28 |
| 29 UIStringOverrider::~UIStringOverrider() {} |
| 30 |
| 31 int UIStringOverrider::GetResourceIndex(uint32_t hash) { |
| 32 if (!num_resources_) |
| 33 return -1; |
| 34 const uint32_t* end = resource_hashes_ + num_resources_; |
| 35 const uint32_t* element = std::lower_bound(resource_hashes_, end, hash); |
| 36 if (element == end || *element != hash) |
| 37 return -1; |
| 38 return resource_indices_[element - resource_hashes_]; |
| 39 } |
| 40 |
| 41 void UIStringOverrider::OverrideUIString(uint32_t hash, |
| 42 const base::string16& string) { |
| 43 int resource_id = GetResourceIndex(hash); |
| 44 if (resource_id == -1) |
| 45 return; |
| 46 |
| 47 ui::ResourceBundle::GetSharedInstance().OverrideLocaleStringResource( |
| 48 resource_id, string); |
| 49 } |
| 50 |
| 51 } // namespace variations |
OLD | NEW |