Commit 9136bef4 authored by jwd@chromium.org's avatar jwd@chromium.org

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
parent fb0d52fa
......@@ -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()) {
......
......@@ -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);
};
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment