Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: components/variations/service/ui_string_overrider.cc

Issue 1363243004: Allow embedders to share code to override UI strings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment to test and class and fix typo Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW
« no previous file with comments | « components/variations/service/ui_string_overrider.h ('k') | components/variations/service/variations_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698