diff options
author | ananta@chromium.org <ananta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-26 22:26:13 +0000 |
---|---|---|
committer | ananta@chromium.org <ananta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-26 22:26:13 +0000 |
commit | 3a78fbdb6ccf439fc5d991c5b3c7ca8adc80a780 (patch) | |
tree | e3b388538d7997f07e2d409d9da1d854ded67656 /chrome_frame | |
parent | aec1d834ed08f78123cc81668fcd3b7f0d91600c (diff) | |
download | chromium_src-3a78fbdb6ccf439fc5d991c5b3c7ca8adc80a780.zip chromium_src-3a78fbdb6ccf439fc5d991c5b3c7ca8adc80a780.tar.gz chromium_src-3a78fbdb6ccf439fc5d991c5b3c7ca8adc80a780.tar.bz2 |
Load both language packs and the resource dlls in ChromeFrame and look for localized strings
in the language pack and the other resources like dialogs in the dll. The latter part is done
automatically by ATL.
BUG=94362
TEST=No change in behavior. The resources should continue to load as before.
Review URL: http://codereview.chromium.org/7763006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98502 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome_frame')
-rw-r--r-- | chrome_frame/simple_resource_loader.cc | 90 | ||||
-rw-r--r-- | chrome_frame/simple_resource_loader.h | 21 | ||||
-rw-r--r-- | chrome_frame/test/simple_resource_loader_test.cc | 44 |
3 files changed, 99 insertions, 56 deletions
diff --git a/chrome_frame/simple_resource_loader.cc b/chrome_frame/simple_resource_loader.cc index 6f3f6e2..2d1e80a 100644 --- a/chrome_frame/simple_resource_loader.cc +++ b/chrome_frame/simple_resource_loader.cc @@ -18,8 +18,9 @@ #include "base/utf_string_conversions.h" #include "base/win/i18n.h" #include "base/win/windows_version.h" - #include "chrome_frame/policy_settings.h" +#include "ui/base/resource/data_pack.h" +#include "ui/base/resource/resource_bundle.h" namespace { @@ -83,7 +84,8 @@ bool PushBackWithFallbackIfAbsent( } // namespace SimpleResourceLoader::SimpleResourceLoader() - : locale_dll_handle_(NULL) { + : data_pack_(NULL), + locale_dll_handle_(NULL) { // Find and load the resource DLL. std::vector<std::wstring> language_tags; @@ -100,19 +102,16 @@ SimpleResourceLoader::SimpleResourceLoader() language_tags.push_back(L"en-US"); FilePath locales_path; - FilePath locale_dll_path; DetermineLocalesDirectory(&locales_path); - if (LoadLocaleDll(language_tags, locales_path, &locale_dll_handle_, - &locale_dll_path)) { - language_ = locale_dll_path.BaseName().RemoveExtension().value(); - } else { + if (!LoadLocalePack(language_tags, locales_path, &locale_dll_handle_, + &data_pack_, &language_)) { NOTREACHED() << "Failed loading any resource dll (even \"en-US\")."; } } SimpleResourceLoader::~SimpleResourceLoader() { - locale_dll_handle_ = NULL; + delete data_pack_; } // static @@ -132,7 +131,6 @@ void SimpleResourceLoader::GetPreferredLanguages( PushBackIfAbsent(*scan, language_tags); } } - // Use the base i18n routines (i.e., ICU) as a last, best hope for something // meaningful for the user. PushBackWithFallbackIfAbsent(ASCIIToWide(base::i18n::GetConfiguredLocale()), @@ -172,20 +170,24 @@ bool SimpleResourceLoader::IsValidLanguageTag( } // static -bool SimpleResourceLoader::LoadLocaleDll( +bool SimpleResourceLoader::LoadLocalePack( const std::vector<std::wstring>& language_tags, const FilePath& locales_path, HMODULE* dll_handle, - FilePath* file_path) { - DCHECK(file_path); + ui::DataPack** data_pack, + std::wstring* language) { + DCHECK(language); // The dll should only have resources, not executable code. const DWORD load_flags = (base::win::GetVersion() >= base::win::VERSION_VISTA ? LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE : DONT_RESOLVE_DLL_REFERENCES); + const std::wstring dll_suffix(L".dll"); - bool found_dll = false; + const std::wstring pack_suffix(L".pak"); + + bool found_pack = false; for (std::vector<std::wstring>::const_iterator scan = language_tags.begin(), end = language_tags.end(); @@ -196,46 +198,54 @@ bool SimpleResourceLoader::LoadLocaleDll( " \"" << *scan << "\""; continue; } - FilePath look_path = locales_path.Append(*scan + dll_suffix); - HMODULE locale_dll_handle = LoadLibraryEx(look_path.value().c_str(), NULL, - load_flags); - if (NULL != locale_dll_handle) { - *dll_handle = locale_dll_handle; - *file_path = look_path; - found_dll = true; - break; + + // Attempt to load both the resource pack and the dll. We return success + // only we load both. + FilePath resource_pack_path = locales_path.Append(*scan + pack_suffix); + FilePath dll_path = locales_path.Append(*scan + dll_suffix); + + if (file_util::PathExists(resource_pack_path) && + file_util::PathExists(dll_path)) { + *data_pack = ui::ResourceBundle::LoadResourcesDataPak(resource_pack_path); + if (!*data_pack) { + continue; + } + HMODULE locale_dll_handle = LoadLibraryEx(dll_path.value().c_str(), NULL, + load_flags); + if (locale_dll_handle) { + *dll_handle = locale_dll_handle; + *language = dll_path.BaseName().RemoveExtension().value(); + found_pack = true; + break; + } else { + *data_pack = NULL; + } } - DPCHECK(ERROR_FILE_NOT_FOUND == GetLastError()) - << "Unable to load generated resources from " << look_path.value(); } - - DCHECK(found_dll || file_util::DirectoryExists(locales_path)) + DCHECK(found_pack || file_util::DirectoryExists(locales_path)) << "Could not locate locales DLL directory."; - - return found_dll; + return found_pack; } std::wstring SimpleResourceLoader::GetLocalizedResource(int message_id) { - if (!locale_dll_handle_) { + if (!data_pack_) { DLOG(ERROR) << "locale resources are not loaded"; return std::wstring(); } DCHECK(IS_INTRESOURCE(message_id)); - const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( - locale_dll_handle_, message_id); - if (!image) { - // Fall back on the current module (shouldn't be any strings here except - // in unittests). - image = AtlGetStringResourceImage(_AtlBaseModule.GetModuleInstance(), - message_id); - if (!image) { - NOTREACHED() << "unable to find resource: " << message_id; - return std::wstring(); - } + base::StringPiece data; + if (!data_pack_->GetStringPiece(message_id, &data)) { + DLOG(ERROR) << "Unable to find string for resource id:" << message_id; + return std::wstring(); } - return std::wstring(image->achString, image->nLength); + + // Data pack encodes strings as UTF16. + DCHECK_EQ(data.length() % 2, 0U); + string16 msg(reinterpret_cast<const char16*>(data.data()), + data.length() / 2); + return msg; } // static diff --git a/chrome_frame/simple_resource_loader.h b/chrome_frame/simple_resource_loader.h index 176e9a7..ceaf7f2 100644 --- a/chrome_frame/simple_resource_loader.h +++ b/chrome_frame/simple_resource_loader.h @@ -18,6 +18,10 @@ #include "base/gtest_prod_util.h" #include "base/memory/singleton.h" +namespace ui { +class DataPack; +} // namespace ui + class SimpleResourceLoader { public: @@ -48,15 +52,18 @@ class SimpleResourceLoader { SimpleResourceLoader(); ~SimpleResourceLoader(); - // Finds the most-preferred resource DLL for the laguages in |language_tags| + // Finds the most-preferred resource dll and language pack for the laguages + // in |language_tags| // in |locales_path|. // // Returns true on success with a handle to the DLL that was loaded in - // |dll_handle| and its path in |file_path|. - static bool LoadLocaleDll(const std::vector<std::wstring>& language_tags, - const FilePath& locales_path, - HMODULE* dll_handle, - FilePath* file_path); + // |dll_handle|, the data pack in |data_pack| and the locale language in + // the |language| parameter. + static bool LoadLocalePack(const std::vector<std::wstring>& language_tags, + const FilePath& locales_path, + HMODULE* dll_handle, + ui::DataPack** data_pack, + std::wstring* language); // Returns the string resource identified by message_id from the currently // loaded locale dll. @@ -67,6 +74,8 @@ class SimpleResourceLoader { FRIEND_TEST_ALL_PREFIXES(SimpleResourceLoaderTest, LoadLocaleDll); std::wstring language_; + ui::DataPack* data_pack_; + HINSTANCE locale_dll_handle_; }; diff --git a/chrome_frame/test/simple_resource_loader_test.cc b/chrome_frame/test/simple_resource_loader_test.cc index 98729f3..bf881c0 100644 --- a/chrome_frame/test/simple_resource_loader_test.cc +++ b/chrome_frame/test/simple_resource_loader_test.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,6 +7,7 @@ #include "base/file_path.h" #include "base/win/windows_version.h" #include "testing/gtest/include/gtest/gtest.h" +#include "ui/base/resource/data_pack.h" TEST(SimpleResourceLoaderTest, LoadLocaleDll) { std::vector<std::wstring> language_tags; @@ -20,40 +21,63 @@ TEST(SimpleResourceLoaderTest, LoadLocaleDll) { language_tags.clear(); language_tags.push_back(L"en-GB"); language_tags.push_back(L"en"); + ui::DataPack* data_pack = NULL; + + std::wstring language; + EXPECT_TRUE( - SimpleResourceLoader::LoadLocaleDll(language_tags, locales_path, - &dll_handle, &file_path)); + SimpleResourceLoader::LoadLocalePack(language_tags, locales_path, + &dll_handle, &data_pack, + &language)); if (NULL != dll_handle) { FreeLibrary(dll_handle); dll_handle = NULL; } - EXPECT_TRUE(file_path.BaseName() == FilePath(L"en-GB.dll")); + EXPECT_TRUE(data_pack != NULL); + delete data_pack; + data_pack = NULL; + + EXPECT_EQ(language, L"en-GB"); + language.clear(); // Test valid language-region string for which we only have a language dll: language_tags.clear(); language_tags.push_back(L"fr-FR"); language_tags.push_back(L"fr"); EXPECT_TRUE( - SimpleResourceLoader::LoadLocaleDll(language_tags, locales_path, - &dll_handle, &file_path)); + SimpleResourceLoader::LoadLocalePack(language_tags, locales_path, + &dll_handle, &data_pack, + &language)); if (NULL != dll_handle) { FreeLibrary(dll_handle); dll_handle = NULL; } - EXPECT_TRUE(file_path.BaseName() == FilePath(L"fr.dll")); + EXPECT_TRUE(data_pack != NULL); + delete data_pack; + data_pack = NULL; + + EXPECT_EQ(language, L"fr"); + language.clear(); // Test invalid language-region string, make sure fallback works: language_tags.clear(); language_tags.push_back(L"xx-XX"); language_tags.push_back(L"en-US"); EXPECT_TRUE( - SimpleResourceLoader::LoadLocaleDll(language_tags, locales_path, - &dll_handle, &file_path)); + SimpleResourceLoader::LoadLocalePack(language_tags, locales_path, + &dll_handle, &data_pack, + &language)); if (NULL != dll_handle) { FreeLibrary(dll_handle); dll_handle = NULL; } - EXPECT_TRUE(file_path.BaseName() == FilePath(L"en-US.dll")); + + EXPECT_TRUE(data_pack != NULL); + delete data_pack; + data_pack = NULL; + + EXPECT_EQ(language, L"en-US"); + language.clear(); } TEST(SimpleResourceLoaderTest, InstanceTest) { |