forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple resource loader to Chrome Frame that is capable of findi…
…ng, loading and extracting resources from the Chrome locale DLLs. Add the Chrome Frame resource strings to the Chrome .grds so they get built directly into the Chrome locale dlls. There is one remaining todo here, which is to load the dialog template into a grd + rc somewhere (probably in generated_resources.grd) and then get CF to load dialog templates from a different module. Will do that in another patch. BUG=24305 TEST=Chrome Frame when loaded on machines whose locales are not US English will display strings appropriate to those locales. Review URL: http://codereview.chromium.org/1240001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42423 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
robertshield@chromium.org
committed
Mar 24, 2010
1 parent
6502d03
commit 36a26ed
Showing
8 changed files
with
246 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright (c) 2010 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. | ||
|
||
#include "chrome_frame/simple_resource_loader.h" | ||
|
||
#include <atlbase.h> | ||
#include <string> | ||
|
||
#include "base/base_paths.h" | ||
#include "base/file_path.h" | ||
#include "base/file_util.h" | ||
#include "base/path_service.h" | ||
#include "base/i18n/rtl.h" | ||
#include "base/string_util.h" | ||
#include "base/win_util.h" | ||
|
||
const wchar_t kLocalesDirName[] = L"Locales"; | ||
|
||
HINSTANCE SimpleResourceLoader::locale_dll_handle_; | ||
|
||
SimpleResourceLoader::SimpleResourceLoader() { | ||
// Find and load the resource DLL. | ||
std::wstring locale = GetSystemLocale(); | ||
std::wstring locale_dll_path; | ||
|
||
if (GetLocaleFilePath(locale, &locale_dll_path)) { | ||
DCHECK(locale_dll_handle_ == NULL) << "Locale DLL is already loaded!"; | ||
locale_dll_handle_ = LoadLocaleDll(locale_dll_path); | ||
DCHECK(locale_dll_handle_ != NULL) << "Failed to load locale dll!"; | ||
} | ||
} | ||
|
||
|
||
std::wstring SimpleResourceLoader::GetSystemLocale() { | ||
std::string language, region; | ||
base::i18n::GetLanguageAndRegionFromOS(&language, ®ion); | ||
std::string ret; | ||
if (!language.empty()) | ||
ret.append(language); | ||
if (!region.empty()) { | ||
ret.append("-"); | ||
ret.append(region); | ||
} | ||
return ASCIIToWide(ret); | ||
} | ||
|
||
bool SimpleResourceLoader::GetLocaleFilePath(const std::wstring& locale, | ||
std::wstring* file_path) { | ||
DCHECK(file_path); | ||
|
||
FilePath module_path; | ||
PathService::Get(base::DIR_MODULE, &module_path); | ||
FilePath locales_path = module_path.Append(kLocalesDirName); | ||
|
||
// We may be residing in the "locales" directory's parent, or we might be | ||
// in a sibling directory. Move up one and look for Locales again in the | ||
// latter case. | ||
if (!file_util::DirectoryExists(locales_path)) { | ||
locales_path = module_path.DirName(); | ||
locales_path = locales_path.Append(kLocalesDirName); | ||
} | ||
|
||
bool found_dll = false; | ||
if (file_util::DirectoryExists(locales_path)) { | ||
std::wstring dll_name(locale); | ||
dll_name += L".dll"; | ||
|
||
// First look for the named locale DLL. | ||
FilePath look_path(locales_path.Append(dll_name)); | ||
if (file_util::PathExists(look_path)) { | ||
*file_path = look_path.value(); | ||
found_dll = true; | ||
} else { | ||
|
||
// If we didn't find it, try defaulting to en-US.dll. | ||
dll_name = L"en-US.dll"; | ||
look_path = locales_path.Append(dll_name); | ||
if (file_util::PathExists(look_path)) { | ||
*file_path = look_path.value(); | ||
found_dll = true; | ||
} | ||
} | ||
} else { | ||
NOTREACHED() << "Could not locate locales DLL directory."; | ||
} | ||
|
||
return found_dll; | ||
} | ||
|
||
HINSTANCE SimpleResourceLoader::LoadLocaleDll(const std::wstring& dll_path) { | ||
DWORD load_flags = 0; | ||
if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { | ||
load_flags = LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | | ||
LOAD_LIBRARY_AS_IMAGE_RESOURCE; | ||
} else { | ||
load_flags = DONT_RESOLVE_DLL_REFERENCES; | ||
} | ||
|
||
// The dll should only have resources, not executable code. | ||
HINSTANCE locale_dll_handle = LoadLibraryEx(dll_path.c_str(), NULL, | ||
load_flags); | ||
DCHECK(locale_dll_handle != NULL) << "unable to load generated resources: " | ||
<< GetLastError(); | ||
|
||
return locale_dll_handle; | ||
} | ||
|
||
std::wstring SimpleResourceLoader::GetLocalizedResource(int message_id) { | ||
if (!locale_dll_handle_) { | ||
LOG(WARNING) << "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(); | ||
} | ||
} | ||
return std::wstring(image->achString, image->nLength); | ||
} | ||
|
||
// static | ||
std::wstring SimpleResourceLoader::Get(int message_id) { | ||
SimpleResourceLoader* loader = SimpleResourceLoader::instance(); | ||
return loader->GetLocalizedResource(message_id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2010 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. | ||
// | ||
// This class implements a simplified and much stripped down version of | ||
// Chrome's app/resource_bundle machinery. It notably avoids unwanted | ||
// dependencies on things like Skia. | ||
// | ||
|
||
#ifndef CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_ | ||
#define CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_ | ||
|
||
#include <windows.h> | ||
#include <string> | ||
|
||
#include "base/singleton.h" | ||
|
||
class SimpleResourceLoader { | ||
public: | ||
|
||
static SimpleResourceLoader* instance() { | ||
return Singleton<SimpleResourceLoader>::get(); | ||
} | ||
|
||
// Helper method to return the string resource identified by message_id | ||
// from the currently loaded locale dll. | ||
static std::wstring Get(int message_id); | ||
|
||
private: | ||
SimpleResourceLoader(); | ||
|
||
// Retrieves the system locale in the <language>-<region> format using ICU. | ||
std::wstring GetSystemLocale(); | ||
|
||
// Uses |locale| to build the resource DLL name and then looks for the named | ||
// DLL in known locales paths. If it doesn't find it, it falls back to | ||
// looking for an en-US.dll. | ||
// | ||
// Returns true if a locale DLL can be found, false otherwise. | ||
bool GetLocaleFilePath(const std::wstring& locale, std::wstring* file_path); | ||
|
||
// Loads the locale dll at the given path. Returns a handle to the DLL or | ||
// NULL on failure. | ||
HINSTANCE LoadLocaleDll(const std::wstring& dll_path); | ||
|
||
// Returns the string resource identified by message_id from the currently | ||
// loaded locale dll. | ||
std::wstring GetLocalizedResource(int message_id); | ||
|
||
friend struct DefaultSingletonTraits<SimpleResourceLoader>; | ||
|
||
static HINSTANCE locale_dll_handle_; | ||
}; | ||
|
||
#endif // CHROME_FRAME_SIMPLE_RESOURCE_LOADER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters