diff options
author | hayato@chromium.org <hayato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-03 05:25:11 +0000 |
---|---|---|
committer | hayato@chromium.org <hayato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-03 05:25:11 +0000 |
commit | 32caba271be320eaba3ef4cee0a5806de0b2cbca (patch) | |
tree | 0ecbe2fdd97e243a20fe009144e3ee1746744a91 /app/resource_bundle_posix.cc | |
parent | 717fcd89505d0e318b052c24a60fb1b64a6de480 (diff) | |
download | chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.zip chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.tar.gz chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.tar.bz2 |
Share code between Mac and Linux in ResourceBundle.
BUG=25891
TEST=None
Review URL: http://codereview.chromium.org/442002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33668 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/resource_bundle_posix.cc')
-rw-r--r-- | app/resource_bundle_posix.cc | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/app/resource_bundle_posix.cc b/app/resource_bundle_posix.cc new file mode 100644 index 0000000..ec6893a --- /dev/null +++ b/app/resource_bundle_posix.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2009 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 "app/resource_bundle.h" + +#include "app/gfx/font.h" +#include "base/data_pack.h" +#include "base/logging.h" +#include "base/string16.h" +#include "base/string_piece.h" + +namespace { + +base::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { + base::DataPack* resources_pak = new base::DataPack; + bool success = resources_pak->Load(resources_pak_path); + if (!success) { + delete resources_pak; + resources_pak = NULL; + } + return resources_pak; +} + +} // namespace + +ResourceBundle::~ResourceBundle() { + FreeImages(); +#if defined(OS_LINUX) + FreeGdkPixBufs(); +#endif + delete locale_resources_data_; + locale_resources_data_ = NULL; + delete resources_data_; + resources_data_ = NULL; +} + +// static +RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( + DataHandle module, int resource_id) { + DCHECK(module); + return module->GetStaticMemory(resource_id); +} + +base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { + DCHECK(resources_data_); + base::StringPiece data; + if (!resources_data_->GetStringPiece(resource_id, &data)) { + if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { + return base::StringPiece(); + } + } + return data; +} + +string16 ResourceBundle::GetLocalizedString(int message_id) { + // If for some reason we were unable to load a resource pak, return an empty + // string (better than crashing). + if (!locale_resources_data_) { + LOG(WARNING) << "locale resources are not loaded"; + return string16(); + } + + base::StringPiece data; + if (!locale_resources_data_->GetStringPiece(message_id, &data)) { + // Fall back on the main data pack (shouldn't be any strings here except in + // unittests). + data = GetRawDataResource(message_id); + if (data.empty()) { + NOTREACHED() << "unable to find resource: " << message_id; + return string16(); + } + } + + // 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; +} + +void ResourceBundle::LoadResources(const std::wstring& pref_locale) { + DCHECK(!resources_data_) << "chrome.pak already loaded"; + FilePath resources_file_path = GetResourcesFilePath(); + DCHECK(!resources_file_path.empty()) << "chrome.pak not found"; + resources_data_ = LoadResourcesDataPak(resources_file_path); + DCHECK(resources_data_) << "failed to load chrome.pak"; + + DCHECK(!locale_resources_data_) << "locale.pak already loaded"; + FilePath locale_file_path = GetLocaleFilePath(pref_locale); + if (locale_file_path.empty()) { + // It's possible that there is no locale.pak. + NOTREACHED(); + return; + } + locale_resources_data_ = LoadResourcesDataPak(locale_file_path); + DCHECK(locale_resources_data_) << "failed to load locale.pak"; +} |