OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 const std::string& pref_locale, Delegate* delegate) { | 101 const std::string& pref_locale, Delegate* delegate) { |
102 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 102 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
103 g_shared_instance_ = new ResourceBundle(delegate); | 103 g_shared_instance_ = new ResourceBundle(delegate); |
104 | 104 |
105 g_shared_instance_->LoadCommonResources(); | 105 g_shared_instance_->LoadCommonResources(); |
106 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); | 106 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
107 return result; | 107 return result; |
108 } | 108 } |
109 | 109 |
110 // static | 110 // static |
111 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { | 111 void ResourceBundle::InitSharedInstanceWithPakFile( |
| 112 base::PlatformFile pak_file, bool should_load_common_resources) { |
112 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 113 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
113 g_shared_instance_ = new ResourceBundle(NULL); | 114 g_shared_instance_ = new ResourceBundle(NULL); |
114 | 115 |
| 116 if (should_load_common_resources) |
| 117 g_shared_instance_->LoadCommonResources(); |
| 118 |
| 119 scoped_ptr<DataPack> data_pack( |
| 120 new DataPack(SCALE_FACTOR_100P)); |
| 121 if (!data_pack->LoadFromFile(pak_file)) { |
| 122 NOTREACHED() << "failed to load pak file"; |
| 123 return; |
| 124 } |
| 125 g_shared_instance_->locale_resources_data_.reset(data_pack.release()); |
| 126 } |
| 127 |
| 128 // static |
| 129 void ResourceBundle::InitSharedInstanceWithPakPath(const FilePath& path) { |
| 130 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| 131 g_shared_instance_ = new ResourceBundle(NULL); |
| 132 |
115 g_shared_instance_->LoadTestResources(path, path); | 133 g_shared_instance_->LoadTestResources(path, path); |
116 } | 134 } |
117 | 135 |
118 // static | 136 // static |
119 void ResourceBundle::CleanupSharedInstance() { | 137 void ResourceBundle::CleanupSharedInstance() { |
120 if (g_shared_instance_) { | 138 if (g_shared_instance_) { |
121 delete g_shared_instance_; | 139 delete g_shared_instance_; |
122 g_shared_instance_ = NULL; | 140 g_shared_instance_ = NULL; |
123 } | 141 } |
124 } | 142 } |
125 | 143 |
126 // static | 144 // static |
127 bool ResourceBundle::HasSharedInstance() { | 145 bool ResourceBundle::HasSharedInstance() { |
128 return g_shared_instance_ != NULL; | 146 return g_shared_instance_ != NULL; |
129 } | 147 } |
130 | 148 |
131 // static | 149 // static |
132 ResourceBundle& ResourceBundle::GetSharedInstance() { | 150 ResourceBundle& ResourceBundle::GetSharedInstance() { |
133 // Must call InitSharedInstance before this function. | 151 // Must call InitSharedInstance before this function. |
134 CHECK(g_shared_instance_ != NULL); | 152 CHECK(g_shared_instance_ != NULL); |
135 return *g_shared_instance_; | 153 return *g_shared_instance_; |
136 } | 154 } |
137 | 155 |
138 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 156 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
139 return !GetLocaleFilePath(locale).empty(); | 157 return !GetLocaleFilePath(locale, true).empty(); |
140 } | 158 } |
141 | 159 |
142 void ResourceBundle::AddDataPack(const FilePath& path, | 160 void ResourceBundle::AddDataPackFromPath(const FilePath& path, |
143 ScaleFactor scale_factor) { | 161 ScaleFactor scale_factor) { |
144 // Do not pass an empty |path| value to this method. If the absolute path is | 162 // Do not pass an empty |path| value to this method. If the absolute path is |
145 // unknown pass just the pack file name. | 163 // unknown pass just the pack file name. |
146 DCHECK(!path.empty()); | 164 DCHECK(!path.empty()); |
147 | 165 |
148 FilePath pack_path = path; | 166 FilePath pack_path = path; |
149 if (delegate_) | 167 if (delegate_) |
150 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | 168 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); |
151 | 169 |
152 // Don't try to load empty values or values that are not absolute paths. | 170 // Don't try to load empty values or values that are not absolute paths. |
153 if (pack_path.empty() || !pack_path.IsAbsolute()) | 171 if (pack_path.empty() || !pack_path.IsAbsolute()) |
154 return; | 172 return; |
155 | 173 |
156 scoped_ptr<DataPack> data_pack( | 174 scoped_ptr<DataPack> data_pack( |
157 new DataPack(scale_factor)); | 175 new DataPack(scale_factor)); |
158 if (data_pack->Load(pack_path)) { | 176 if (data_pack->LoadFromPath(pack_path)) { |
159 data_packs_.push_back(data_pack.release()); | 177 data_packs_.push_back(data_pack.release()); |
160 } else { | 178 } else { |
161 LOG(ERROR) << "Failed to load " << pack_path.value() | 179 LOG(ERROR) << "Failed to load " << pack_path.value() |
162 << "\nSome features may not be available."; | 180 << "\nSome features may not be available."; |
163 } | 181 } |
164 } | 182 } |
165 | 183 |
| 184 void ResourceBundle::AddDataPackFromFile(base::PlatformFile file, |
| 185 ScaleFactor scale_factor) { |
| 186 scoped_ptr<DataPack> data_pack( |
| 187 new DataPack(scale_factor)); |
| 188 if (data_pack->LoadFromFile(file)) { |
| 189 data_packs_.push_back(data_pack.release()); |
| 190 } else { |
| 191 LOG(ERROR) << "Failed to load data pack from file." |
| 192 << "\nSome features may not be available."; |
| 193 } |
| 194 } |
| 195 |
166 #if !defined(OS_MACOSX) | 196 #if !defined(OS_MACOSX) |
167 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 197 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, |
| 198 bool test_file_exists) { |
168 if (app_locale.empty()) | 199 if (app_locale.empty()) |
169 return FilePath(); | 200 return FilePath(); |
170 | 201 |
171 FilePath locale_file_path; | 202 FilePath locale_file_path; |
172 | 203 |
173 #if defined(OS_ANDROID) | 204 #if defined(OS_ANDROID) |
174 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); | 205 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); |
175 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); | 206 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); |
176 #else | 207 #else |
177 PathService::Get(ui::DIR_LOCALES, &locale_file_path); | 208 PathService::Get(ui::DIR_LOCALES, &locale_file_path); |
178 #endif | 209 #endif |
179 | 210 |
180 if (!locale_file_path.empty()) | 211 if (!locale_file_path.empty()) |
181 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | 212 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
182 | 213 |
183 if (delegate_) { | 214 if (delegate_) { |
184 locale_file_path = | 215 locale_file_path = |
185 delegate_->GetPathForLocalePack(locale_file_path, app_locale); | 216 delegate_->GetPathForLocalePack(locale_file_path, app_locale); |
186 } | 217 } |
187 | 218 |
188 // Don't try to load empty values or values that are not absolute paths. | 219 // Don't try to load empty values or values that are not absolute paths. |
189 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) | 220 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) |
190 return FilePath(); | 221 return FilePath(); |
191 | 222 |
192 if (!file_util::PathExists(locale_file_path)) | 223 if (test_file_exists && !file_util::PathExists(locale_file_path)) |
193 return FilePath(); | 224 return FilePath(); |
194 | 225 |
195 return locale_file_path; | 226 return locale_file_path; |
196 } | 227 } |
197 #endif | 228 #endif |
198 | 229 |
199 std::string ResourceBundle::LoadLocaleResources( | 230 std::string ResourceBundle::LoadLocaleResources( |
200 const std::string& pref_locale) { | 231 const std::string& pref_locale) { |
201 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; | 232 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; |
202 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 233 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
203 FilePath locale_file_path = GetOverriddenPakPath(); | 234 FilePath locale_file_path = GetOverriddenPakPath(); |
204 if (locale_file_path.empty()) { | 235 if (locale_file_path.empty()) { |
205 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 236 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
206 if (command_line->HasSwitch(switches::kLocalePak)) { | 237 if (command_line->HasSwitch(switches::kLocalePak)) { |
207 locale_file_path = | 238 locale_file_path = |
208 command_line->GetSwitchValuePath(switches::kLocalePak); | 239 command_line->GetSwitchValuePath(switches::kLocalePak); |
209 } else { | 240 } else { |
210 locale_file_path = GetLocaleFilePath(app_locale); | 241 locale_file_path = GetLocaleFilePath(app_locale, true); |
211 } | 242 } |
212 } | 243 } |
213 | 244 |
214 if (locale_file_path.empty()) { | 245 if (locale_file_path.empty()) { |
215 // It's possible that there is no locale.pak. | 246 // It's possible that there is no locale.pak. |
216 LOG(WARNING) << "locale_file_path.empty()"; | 247 LOG(WARNING) << "locale_file_path.empty()"; |
217 return std::string(); | 248 return std::string(); |
218 } | 249 } |
219 | 250 |
220 scoped_ptr<DataPack> data_pack( | 251 scoped_ptr<DataPack> data_pack( |
221 new DataPack(SCALE_FACTOR_100P)); | 252 new DataPack(SCALE_FACTOR_100P)); |
222 if (!data_pack->Load(locale_file_path)) { | 253 if (!data_pack->LoadFromPath(locale_file_path)) { |
223 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 254 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
224 logging::GetLastSystemErrorCode(), 16000); | 255 logging::GetLastSystemErrorCode(), 16000); |
225 LOG(ERROR) << "failed to load locale.pak"; | 256 LOG(ERROR) << "failed to load locale.pak"; |
226 NOTREACHED(); | 257 NOTREACHED(); |
227 return std::string(); | 258 return std::string(); |
228 } | 259 } |
229 | 260 |
230 locale_resources_data_.reset(data_pack.release()); | 261 locale_resources_data_.reset(data_pack.release()); |
231 return app_locale; | 262 return app_locale; |
232 } | 263 } |
233 | 264 |
234 void ResourceBundle::LoadTestResources(const FilePath& path, | 265 void ResourceBundle::LoadTestResources(const FilePath& path, |
235 const FilePath& locale_path) { | 266 const FilePath& locale_path) { |
236 // Use the given resource pak for both common and localized resources. | 267 // Use the given resource pak for both common and localized resources. |
237 scoped_ptr<DataPack> data_pack( | 268 scoped_ptr<DataPack> data_pack( |
238 new DataPack(SCALE_FACTOR_100P)); | 269 new DataPack(SCALE_FACTOR_100P)); |
239 if (!path.empty() && data_pack->Load(path)) | 270 if (!path.empty() && data_pack->LoadFromPath(path)) |
240 data_packs_.push_back(data_pack.release()); | 271 data_packs_.push_back(data_pack.release()); |
241 | 272 |
242 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); | 273 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
243 if (!locale_path.empty() && data_pack->Load(locale_path)) { | 274 if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) { |
244 locale_resources_data_.reset(data_pack.release()); | 275 locale_resources_data_.reset(data_pack.release()); |
245 } else { | 276 } else { |
246 locale_resources_data_.reset( | 277 locale_resources_data_.reset( |
247 new DataPack(ui::SCALE_FACTOR_NONE)); | 278 new DataPack(ui::SCALE_FACTOR_NONE)); |
248 } | 279 } |
249 } | 280 } |
250 | 281 |
251 void ResourceBundle::UnloadLocaleResources() { | 282 void ResourceBundle::UnloadLocaleResources() { |
252 locale_resources_data_.reset(); | 283 locale_resources_data_.reset(); |
253 } | 284 } |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 SkBitmap bitmap; | 577 SkBitmap bitmap; |
547 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 578 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
548 bitmap.allocPixels(); | 579 bitmap.allocPixels(); |
549 bitmap.eraseARGB(255, 255, 0, 0); | 580 bitmap.eraseARGB(255, 255, 0, 0); |
550 empty_image_ = gfx::Image(bitmap); | 581 empty_image_ = gfx::Image(bitmap); |
551 } | 582 } |
552 return empty_image_; | 583 return empty_image_; |
553 } | 584 } |
554 | 585 |
555 } // namespace ui | 586 } // namespace ui |
OLD | NEW |