| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "app/resource_bundle.h" |
| 6 |
| 7 #include "app/gfx/font.h" |
| 8 #include "base/data_pack.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/string16.h" |
| 11 #include "base/string_piece.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 base::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { |
| 16 base::DataPack* resources_pak = new base::DataPack; |
| 17 bool success = resources_pak->Load(resources_pak_path); |
| 18 if (!success) { |
| 19 delete resources_pak; |
| 20 resources_pak = NULL; |
| 21 } |
| 22 return resources_pak; |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 ResourceBundle::~ResourceBundle() { |
| 28 FreeImages(); |
| 29 #if defined(OS_LINUX) |
| 30 FreeGdkPixBufs(); |
| 31 #endif |
| 32 delete locale_resources_data_; |
| 33 locale_resources_data_ = NULL; |
| 34 delete resources_data_; |
| 35 resources_data_ = NULL; |
| 36 } |
| 37 |
| 38 // static |
| 39 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
| 40 DataHandle module, int resource_id) { |
| 41 DCHECK(module); |
| 42 return module->GetStaticMemory(resource_id); |
| 43 } |
| 44 |
| 45 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { |
| 46 DCHECK(resources_data_); |
| 47 base::StringPiece data; |
| 48 if (!resources_data_->GetStringPiece(resource_id, &data)) { |
| 49 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { |
| 50 return base::StringPiece(); |
| 51 } |
| 52 } |
| 53 return data; |
| 54 } |
| 55 |
| 56 string16 ResourceBundle::GetLocalizedString(int message_id) { |
| 57 // If for some reason we were unable to load a resource pak, return an empty |
| 58 // string (better than crashing). |
| 59 if (!locale_resources_data_) { |
| 60 LOG(WARNING) << "locale resources are not loaded"; |
| 61 return string16(); |
| 62 } |
| 63 |
| 64 base::StringPiece data; |
| 65 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { |
| 66 // Fall back on the main data pack (shouldn't be any strings here except in |
| 67 // unittests). |
| 68 data = GetRawDataResource(message_id); |
| 69 if (data.empty()) { |
| 70 NOTREACHED() << "unable to find resource: " << message_id; |
| 71 return string16(); |
| 72 } |
| 73 } |
| 74 |
| 75 // Data pack encodes strings as UTF16. |
| 76 DCHECK_EQ(data.length() % 2, 0U); |
| 77 string16 msg(reinterpret_cast<const char16*>(data.data()), |
| 78 data.length() / 2); |
| 79 return msg; |
| 80 } |
| 81 |
| 82 void ResourceBundle::LoadResources(const std::wstring& pref_locale) { |
| 83 DCHECK(!resources_data_) << "chrome.pak already loaded"; |
| 84 FilePath resources_file_path = GetResourcesFilePath(); |
| 85 DCHECK(!resources_file_path.empty()) << "chrome.pak not found"; |
| 86 resources_data_ = LoadResourcesDataPak(resources_file_path); |
| 87 DCHECK(resources_data_) << "failed to load chrome.pak"; |
| 88 |
| 89 DCHECK(!locale_resources_data_) << "locale.pak already loaded"; |
| 90 FilePath locale_file_path = GetLocaleFilePath(pref_locale); |
| 91 if (locale_file_path.empty()) { |
| 92 // It's possible that there is no locale.pak. |
| 93 NOTREACHED(); |
| 94 return; |
| 95 } |
| 96 locale_resources_data_ = LoadResourcesDataPak(locale_file_path); |
| 97 DCHECK(locale_resources_data_) << "failed to load locale.pak"; |
| 98 } |
| OLD | NEW |