Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 7714028: Switch to using .pak files for locale data on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more deps Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_dummy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ui/base/resource/resource_bundle.h" 5 #include "ui/base/resource/resource_bundle.h"
6 6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
7 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/path_service.h"
8 #include "base/stl_util.h" 11 #include "base/stl_util.h"
9 #include "base/string_piece.h" 12 #include "base/string_piece.h"
10 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
11 #include "build/build_config.h" 14 #include "build/build_config.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/resource/data_pack.h" 17 #include "ui/base/resource/data_pack.h"
18 #include "ui/base/ui_base_paths.h"
19 #include "ui/base/ui_base_switches.h"
14 #include "ui/gfx/codec/png_codec.h" 20 #include "ui/gfx/codec/png_codec.h"
15 #include "ui/gfx/font.h" 21 #include "ui/gfx/font.h"
16 #include "ui/gfx/image/image.h" 22 #include "ui/gfx/image/image.h"
17 23
18 namespace ui { 24 namespace ui {
19 25
20 namespace { 26 namespace {
21 27
22 // Font sizes relative to base font. 28 // Font sizes relative to base font.
23 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) 29 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 72
67 /* static */ 73 /* static */
68 void ResourceBundle::InitSharedInstanceForTest(const FilePath& path) { 74 void ResourceBundle::InitSharedInstanceForTest(const FilePath& path) {
69 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; 75 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
70 g_shared_instance_ = new ResourceBundle(); 76 g_shared_instance_ = new ResourceBundle();
71 77
72 g_shared_instance_->LoadTestResources(path); 78 g_shared_instance_->LoadTestResources(path);
73 } 79 }
74 80
75 /* static */ 81 /* static */
82 DataPack* ResourceBundle::LoadResourcesDataPak(const FilePath& path) {
83 DataPack* datapack = new DataPack;
84 bool success = datapack->Load(path);
85 if (!success) {
86 delete datapack;
87 datapack = NULL;
88 }
89 return datapack;
90 }
91
92 /* static */
76 std::string ResourceBundle::ReloadSharedInstance( 93 std::string ResourceBundle::ReloadSharedInstance(
77 const std::string& pref_locale) { 94 const std::string& pref_locale) {
78 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; 95 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized";
79 96
80 g_shared_instance_->UnloadLocaleResources(); 97 g_shared_instance_->UnloadLocaleResources();
81 return g_shared_instance_->LoadLocaleResources(pref_locale); 98 return g_shared_instance_->LoadLocaleResources(pref_locale);
82 } 99 }
83 100
84 /* static */ 101 /* static */
85 void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) { 102 void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) {
86 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; 103 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized";
87 g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path)); 104 g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path));
88 } 105 }
89 106
90 /* static */ 107 /* static */
91 void ResourceBundle::CleanupSharedInstance() { 108 void ResourceBundle::CleanupSharedInstance() {
92 if (g_shared_instance_) { 109 if (g_shared_instance_) {
93 delete g_shared_instance_; 110 delete g_shared_instance_;
94 g_shared_instance_ = NULL; 111 g_shared_instance_ = NULL;
95 } 112 }
96 } 113 }
97 114
98 /* static */ 115 /* static */
99 ResourceBundle& ResourceBundle::GetSharedInstance() { 116 ResourceBundle& ResourceBundle::GetSharedInstance() {
100 // Must call InitSharedInstance before this function. 117 // Must call InitSharedInstance before this function.
101 CHECK(g_shared_instance_ != NULL); 118 CHECK(g_shared_instance_ != NULL);
102 return *g_shared_instance_; 119 return *g_shared_instance_;
103 } 120 }
104 121
122 #if !defined(OS_MACOSX)
123 /* static */
124 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
125 FilePath locale_file_path;
126 PathService::Get(ui::DIR_LOCALES, &locale_file_path);
127 if (locale_file_path.empty())
128 return locale_file_path;
129 if (app_locale.empty())
130 return FilePath();
131 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak");
132 if (!file_util::PathExists(locale_file_path))
133 return FilePath();
134 return locale_file_path;
135 }
136 #endif
137
138 std::string ResourceBundle::LoadLocaleResources(
139 const std::string& pref_locale) {
140 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded";
141 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
142 FilePath locale_file_path;
143 CommandLine *command_line = CommandLine::ForCurrentProcess();
144 if (command_line->HasSwitch(switches::kLocalePak)) {
145 locale_file_path =
146 command_line->GetSwitchValuePath(switches::kLocalePak);
147 } else {
148 locale_file_path = GetLocaleFilePath(app_locale);
149 }
150 if (locale_file_path.empty()) {
151 // It's possible that there is no locale.pak.
152 NOTREACHED();
153 return std::string();
154 }
155 locale_resources_data_.reset(LoadResourcesDataPak(locale_file_path));
156 CHECK(locale_resources_data_.get()) << "failed to load locale.pak";
157 return app_locale;
158 }
159
160 void ResourceBundle::UnloadLocaleResources() {
161 locale_resources_data_.reset();
162 }
163
164 string16 ResourceBundle::GetLocalizedString(int message_id) {
165 // If for some reason we were unable to load a resource pak, return an empty
166 // string (better than crashing).
167 if (!locale_resources_data_.get()) {
168 LOG(WARNING) << "locale resources are not loaded";
169 return string16();
170 }
171
172 base::StringPiece data;
173 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
174 // Fall back on the main data pack (shouldn't be any strings here except in
175 // unittests).
176 data = GetRawDataResource(message_id);
177 if (data.empty()) {
178 NOTREACHED() << "unable to find resource: " << message_id;
179 return string16();
180 }
181 }
182
183 // Data pack encodes strings as UTF16.
184 DCHECK_EQ(data.length() % 2, 0U);
185 string16 msg(reinterpret_cast<const char16*>(data.data()),
186 data.length() / 2);
187 return msg;
188 }
189
105 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { 190 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) {
106 const SkBitmap* bitmap = 191 const SkBitmap* bitmap =
107 static_cast<const SkBitmap*>(GetImageNamed(resource_id)); 192 static_cast<const SkBitmap*>(GetImageNamed(resource_id));
108 return const_cast<SkBitmap*>(bitmap); 193 return const_cast<SkBitmap*>(bitmap);
109 } 194 }
110 195
111 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { 196 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) {
112 // Check to see if the image is already in the cache. 197 // Check to see if the image is already in the cache.
113 { 198 {
114 base::AutoLock lock_scope(*lock_); 199 base::AutoLock lock_scope(*lock_);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 272
188 void ResourceBundle::ReloadFonts() { 273 void ResourceBundle::ReloadFonts() {
189 base::AutoLock lock_scope(*lock_); 274 base::AutoLock lock_scope(*lock_);
190 base_font_.reset(); 275 base_font_.reset();
191 LoadFontsIfNecessary(); 276 LoadFontsIfNecessary();
192 } 277 }
193 278
194 ResourceBundle::ResourceBundle() 279 ResourceBundle::ResourceBundle()
195 : lock_(new base::Lock), 280 : lock_(new base::Lock),
196 resources_data_(NULL), 281 resources_data_(NULL),
197 large_icon_resources_data_(NULL), 282 large_icon_resources_data_(NULL) {
198 locale_resources_data_(NULL) {
199 } 283 }
200 284
201 void ResourceBundle::FreeImages() { 285 void ResourceBundle::FreeImages() {
202 STLDeleteContainerPairSecondPointers(images_.begin(), 286 STLDeleteContainerPairSecondPointers(images_.begin(),
203 images_.end()); 287 images_.end());
204 images_.clear(); 288 images_.clear();
205 } 289 }
206 290
207 void ResourceBundle::LoadFontsIfNecessary() { 291 void ResourceBundle::LoadFontsIfNecessary() {
208 lock_->AssertAcquired(); 292 lock_->AssertAcquired();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 379 }
296 380
297 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( 381 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory(
298 int resource_id) const { 382 int resource_id) const {
299 if (!data_pack_.get()) 383 if (!data_pack_.get())
300 return NULL; 384 return NULL;
301 return data_pack_->GetStaticMemory(resource_id); 385 return data_pack_->GetStaticMemory(resource_id);
302 } 386 }
303 387
304 } // namespace ui 388 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_dummy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698