| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "app/resource_bundle.h" | 5 #include "app/resource_bundle.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "app/app_paths.h" | 9 #include "app/app_paths.h" |
| 10 #include "app/gfx/font.h" | 10 #include "app/gfx/font.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // The pixbuf is owned by the loader, so add a ref so when we delete the | 49 // The pixbuf is owned by the loader, so add a ref so when we delete the |
| 50 // loader (when the ScopedGObject goes out of scope), the pixbuf still | 50 // loader (when the ScopedGObject goes out of scope), the pixbuf still |
| 51 // exists. | 51 // exists. |
| 52 g_object_ref(pixbuf); | 52 g_object_ref(pixbuf); |
| 53 return pixbuf; | 53 return pixbuf; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 ResourceBundle::~ResourceBundle() { | 59 void ResourceBundle::FreeGdkPixBufs() { |
| 60 FreeImages(); | |
| 61 // Free GdkPixbufs. | |
| 62 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); | 60 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); |
| 63 i != gdk_pixbufs_.end(); i++) { | 61 i != gdk_pixbufs_.end(); i++) { |
| 64 g_object_unref(i->second); | 62 g_object_unref(i->second); |
| 65 } | 63 } |
| 66 gdk_pixbufs_.clear(); | 64 gdk_pixbufs_.clear(); |
| 67 | |
| 68 delete locale_resources_data_; | |
| 69 locale_resources_data_ = NULL; | |
| 70 delete resources_data_; | |
| 71 resources_data_ = NULL; | |
| 72 } | 65 } |
| 73 | 66 |
| 74 void ResourceBundle::LoadResources(const std::wstring& pref_locale) { | 67 // static |
| 75 FilePath resources_data_path; | 68 FilePath ResourceBundle::GetResourcesFilePath() { |
| 76 PathService::Get(base::DIR_EXE, &resources_data_path); | 69 FilePath resources_file_path; |
| 77 resources_data_path = resources_data_path.Append( | 70 PathService::Get(base::DIR_EXE, &resources_file_path); |
| 78 FILE_PATH_LITERAL("chrome.pak")); | 71 if (resources_file_path.empty()) |
| 79 DCHECK(resources_data_ == NULL) << "resource data already loaded!"; | 72 return resources_file_path; |
| 80 resources_data_ = new base::DataPack; | 73 return resources_file_path.Append(FILE_PATH_LITERAL("chrome.pak")); |
| 81 bool success = resources_data_->Load(resources_data_path); | |
| 82 DCHECK(success) << "failed to load chrome.pak"; | |
| 83 | |
| 84 DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!"; | |
| 85 const FilePath& locale_path = GetLocaleFilePath(pref_locale); | |
| 86 if (locale_path.value().empty()) { | |
| 87 // It's possible that there are no locale dlls found, in which case we just | |
| 88 // return. | |
| 89 NOTREACHED(); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 locale_resources_data_ = new base::DataPack; | |
| 94 success = locale_resources_data_->Load(locale_path); | |
| 95 DCHECK(success) << "failed to load locale pak file"; | |
| 96 } | 74 } |
| 97 | 75 |
| 76 // static |
| 98 FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { | 77 FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { |
| 99 FilePath locale_path; | 78 FilePath locale_file_path; |
| 100 PathService::Get(app::DIR_LOCALES, &locale_path); | 79 PathService::Get(app::DIR_LOCALES, &locale_file_path); |
| 101 | 80 if (locale_file_path.empty()) |
| 81 return locale_file_path; |
| 102 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 82 const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| 103 if (app_locale.empty()) | 83 if (app_locale.empty()) |
| 104 return FilePath(); | 84 return FilePath(); |
| 105 | 85 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
| 106 return locale_path.AppendASCII(app_locale + ".pak"); | 86 if (!file_util::PathExists(locale_file_path)) |
| 107 } | 87 return FilePath(); |
| 108 | 88 return locale_file_path; |
| 109 // static | |
| 110 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( | |
| 111 DataHandle module, int resource_id) { | |
| 112 DCHECK(module); | |
| 113 return module->GetStaticMemory(resource_id); | |
| 114 } | |
| 115 | |
| 116 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { | |
| 117 DCHECK(resources_data_); | |
| 118 base::StringPiece data; | |
| 119 if (!resources_data_->GetStringPiece(resource_id, &data)) { | |
| 120 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { | |
| 121 return base::StringPiece(); | |
| 122 } | |
| 123 } | |
| 124 return data; | |
| 125 } | |
| 126 | |
| 127 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 128 // If for some reason we were unable to load a resource dll, return an empty | |
| 129 // string (better than crashing). | |
| 130 if (!locale_resources_data_) { | |
| 131 LOG(WARNING) << "locale resources are not loaded"; | |
| 132 return string16(); | |
| 133 } | |
| 134 | |
| 135 base::StringPiece data; | |
| 136 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 137 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 138 // unittests). | |
| 139 data = GetRawDataResource(message_id); | |
| 140 if (data.empty()) { | |
| 141 NOTREACHED() << "unable to find resource: " << message_id; | |
| 142 return string16(); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Data pack encodes strings as UTF16. | |
| 147 string16 msg(reinterpret_cast<const char16*>(data.data()), | |
| 148 data.length() / 2); | |
| 149 return msg; | |
| 150 } | 89 } |
| 151 | 90 |
| 152 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { | 91 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { |
| 153 // Use the negative |resource_id| for the key for BIDI-aware images. | 92 // Use the negative |resource_id| for the key for BIDI-aware images. |
| 154 int key = rtl_enabled ? -resource_id : resource_id; | 93 int key = rtl_enabled ? -resource_id : resource_id; |
| 155 | 94 |
| 156 // Check to see if we already have the pixbuf in the cache. | 95 // Check to see if we already have the pixbuf in the cache. |
| 157 { | 96 { |
| 158 AutoLock lock_scope(lock_); | 97 AutoLock lock_scope(lock_); |
| 159 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); | 98 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 139 } |
| 201 } | 140 } |
| 202 | 141 |
| 203 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { | 142 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { |
| 204 return GetPixbufImpl(resource_id, false); | 143 return GetPixbufImpl(resource_id, false); |
| 205 } | 144 } |
| 206 | 145 |
| 207 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { | 146 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { |
| 208 return GetPixbufImpl(resource_id, true); | 147 return GetPixbufImpl(resource_id, true); |
| 209 } | 148 } |
| OLD | NEW |