summaryrefslogtreecommitdiffstats
path: root/ui/base/resource
diff options
context:
space:
mode:
authorjwd@chromium.org <jwd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-12 00:47:29 +0000
committerjwd@chromium.org <jwd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-12 00:47:29 +0000
commit9136bef494dc560b4ddf764dc402b0628aec25da (patch)
tree9820152f3aa6c841906900876ebb2ca26cbaeaa8 /ui/base/resource
parentfb0d52fa9455c6eb25c14750760397741ab87835 (diff)
downloadchromium_src-9136bef494dc560b4ddf764dc402b0628aec25da.zip
chromium_src-9136bef494dc560b4ddf764dc402b0628aec25da.tar.gz
chromium_src-9136bef494dc560b4ddf764dc402b0628aec25da.tar.bz2
Adding OverrideStringResource API to ResourceBundle.
This is to facilitate overriding UI strings from the variations service. BUG=370033 Review URL: https://codereview.chromium.org/322523002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282758 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/resource')
-rw-r--r--ui/base/resource/resource_bundle.cc15
-rw-r--r--ui/base/resource/resource_bundle.h14
-rw-r--r--ui/base/resource/resource_bundle_unittest.cc34
3 files changed, 61 insertions, 2 deletions
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc
index 1afd46a..b2be5e4 100644
--- a/ui/base/resource/resource_bundle.cc
+++ b/ui/base/resource/resource_bundle.cc
@@ -324,6 +324,12 @@ void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) {
overridden_pak_path_ = pak_path;
}
+void ResourceBundle::OverrideLocaleStringResource(
+ int message_id,
+ const base::string16& string) {
+ overridden_locale_strings_[message_id] = string;
+}
+
const base::FilePath& ResourceBundle::GetOverriddenPakPath() {
return overridden_pak_path_;
}
@@ -331,6 +337,10 @@ const base::FilePath& ResourceBundle::GetOverriddenPakPath() {
std::string ResourceBundle::ReloadLocaleResources(
const std::string& pref_locale) {
base::AutoLock lock_scope(*locale_resources_data_lock_);
+
+ // Remove all overriden strings, as they will not be valid for the new locale.
+ overridden_locale_strings_.clear();
+
UnloadLocaleResources();
return LoadLocaleResources(pref_locale);
}
@@ -456,6 +466,11 @@ base::string16 ResourceBundle::GetLocalizedString(int message_id) {
// we're using them.
base::AutoLock lock_scope(*locale_resources_data_lock_);
+ IdToStringMap::const_iterator it =
+ overridden_locale_strings_.find(message_id);
+ if (it != overridden_locale_strings_.end())
+ return it->second;
+
// If for some reason we were unable to load the resources , return an empty
// string (better than crashing).
if (!locale_resources_data_.get()) {
diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h
index 060ea6c..1f40728 100644
--- a/ui/base/resource/resource_bundle.h
+++ b/ui/base/resource/resource_bundle.h
@@ -9,6 +9,7 @@
#include <string>
#include "base/basictypes.h"
+#include "base/containers/hash_tables.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
@@ -245,6 +246,15 @@ class UI_BASE_EXPORT ResourceBundle {
// loaded. Pass an empty path to undo.
void OverrideLocalePakForTest(const base::FilePath& pak_path);
+ // Overrides a localized string resource with the given string. If no delegate
+ // is present, the |string| will be returned when getting the localized string
+ // |message_id|. If |ReloadLocaleResources| is called, all overrides are
+ // cleared. This is intended to be used in conjunction with field trials and
+ // the variations service to experiment with different UI strings. This method
+ // is not thread safe!
+ void OverrideLocaleStringResource(int message_id,
+ const base::string16& string);
+
// Returns the full pathname of the locale file to load. May return an empty
// string if no locale data files are found and |test_file_exists| is true.
// Used on Android to load the local file in the browser process and pass it
@@ -271,6 +281,8 @@ class UI_BASE_EXPORT ResourceBundle {
class ResourceBundleImageSource;
friend class ResourceBundleImageSource;
+ typedef base::hash_map<int, base::string16> IdToStringMap;
+
// Ctor/dtor are private, since we're a singleton.
explicit ResourceBundle(Delegate* delegate);
~ResourceBundle();
@@ -395,6 +407,8 @@ class UI_BASE_EXPORT ResourceBundle {
base::FilePath overridden_pak_path_;
+ IdToStringMap overridden_locale_strings_;
+
DISALLOW_COPY_AND_ASSIGN(ResourceBundle);
};
diff --git a/ui/base/resource/resource_bundle_unittest.cc b/ui/base/resource/resource_bundle_unittest.cc
index 73efed5..9fdfd36 100644
--- a/ui/base/resource/resource_bundle_unittest.cc
+++ b/ui/base/resource/resource_bundle_unittest.cc
@@ -310,6 +310,37 @@ TEST_F(ResourceBundleTest, DelegateGetLocalizedString) {
EXPECT_EQ(data, result);
}
+TEST_F(ResourceBundleTest, OverrideStringResource) {
+ ResourceBundle* resource_bundle = CreateResourceBundle(NULL);
+
+ base::string16 data = base::ASCIIToUTF16("My test data");
+ int resource_id = 5;
+
+ base::string16 result = resource_bundle->GetLocalizedString(resource_id);
+ EXPECT_EQ(base::string16(), result);
+
+ resource_bundle->OverrideLocaleStringResource(resource_id, data);
+
+ result = resource_bundle->GetLocalizedString(resource_id);
+ EXPECT_EQ(data, result);
+}
+
+TEST_F(ResourceBundleTest, DelegateGetLocalizedStringWithOverride) {
+ MockResourceBundleDelegate delegate;
+ ResourceBundle* resource_bundle = CreateResourceBundle(&delegate);
+
+ base::string16 delegate_data = base::ASCIIToUTF16("My delegate data");
+ int resource_id = 5;
+
+ EXPECT_CALL(delegate, GetLocalizedStringMock(resource_id)).Times(1).WillOnce(
+ Return(delegate_data));
+
+ base::string16 override_data = base::ASCIIToUTF16("My override data");
+
+ base::string16 result = resource_bundle->GetLocalizedString(resource_id);
+ EXPECT_EQ(delegate_data, result);
+}
+
#if defined(USE_OZONE) && !defined(USE_PANGO)
#define MAYBE_DelegateGetFontList DISABLED_DelegateGetFontList
#else
@@ -361,8 +392,7 @@ class ResourceBundleImageTest : public ResourceBundleTest {
// Write an empty data pak for locale data.
const base::FilePath& locale_path = dir_path().Append(
FILE_PATH_LITERAL("locale.pak"));
- EXPECT_EQ(base::WriteFile(locale_path, kEmptyPakContents,
- kEmptyPakSize),
+ EXPECT_EQ(base::WriteFile(locale_path, kEmptyPakContents, kEmptyPakSize),
static_cast<int>(kEmptyPakSize));
ui::ResourceBundle* resource_bundle = CreateResourceBundle(NULL);