Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / base / resource / resource_bundle.cc
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
3 // found in the LICENSE file.
4
5 #include "ui/base/resource/resource_bundle.h"
6
7 #include <vector>
8
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "build/build_config.h"
20 #include "grit/app_locale_settings.h"
21 #include "net/base/big_endian.h"
22 #include "skia/ext/image_operations.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/layout.h"
26 #include "ui/base/resource/data_pack.h"
27 #include "ui/base/ui_base_paths.h"
28 #include "ui/base/ui_base_switches.h"
29 #include "ui/gfx/codec/jpeg_codec.h"
30 #include "ui/gfx/codec/png_codec.h"
31 #include "ui/gfx/image/image_skia.h"
32 #include "ui/gfx/image/image_skia_source.h"
33 #include "ui/gfx/safe_integer_conversions.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/size_conversions.h"
36 #include "ui/gfx/skbitmap_operations.h"
37
38 #if defined(OS_CHROMEOS)
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/gfx/platform_font_pango.h"
41 #endif
42
43 #if defined(OS_WIN)
44 #include "ui/base/win/dpi_setup.h"
45 #include "ui/gfx/win/dpi.h"
46 #endif
47
48 #if defined(OS_MACOSX) && !defined(OS_IOS)
49 #include "base/mac/mac_util.h"
50 #endif
51
52 namespace ui {
53
54 namespace {
55
56 // Font sizes relative to base font.
57 const int kSmallFontSizeDelta = -1;
58 const int kMediumFontSizeDelta = 3;
59 const int kLargeFontSizeDelta = 8;
60
61 // PNG-related constants.
62 const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
63 const size_t kPngChunkMetadataSize = 12;  // length, type, crc32
64 const unsigned char kPngScaleChunkType[4] = { 'c', 's', 'C', 'l' };
65 const unsigned char kPngDataChunkType[4] = { 'I', 'D', 'A', 'T' };
66
67 ResourceBundle* g_shared_instance_ = NULL;
68
69 void InitDefaultFontList() {
70 #if defined(OS_CHROMEOS)
71   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
72   std::string font_family = base::UTF16ToUTF8(
73       rb.GetLocalizedString(IDS_UI_FONT_FAMILY_CROS));
74   gfx::FontList::SetDefaultFontDescription(font_family);
75
76   // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to
77   // the font list is done.  We will no longer need SetDefaultFontDescription()
78   // after every client gets started using a FontList instead of a Font.
79   gfx::PlatformFontPango::SetDefaultFontDescription(font_family);
80 #else
81   // Use a single default font as the default font list.
82   gfx::FontList::SetDefaultFontDescription(std::string());
83 #endif
84 }
85
86 }  // namespace
87
88 // An ImageSkiaSource that loads bitmaps for the requested scale factor from
89 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the
90 // requested scale factor does not exist, it will return the 1x bitmap scaled
91 // by the scale factor. This may lead to broken UI if the correct size of the
92 // scaled image is not exactly |scale_factor| * the size of the 1x resource.
93 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images
94 // are higlighted by blending them with red.
95 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource {
96  public:
97   ResourceBundleImageSource(ResourceBundle* rb, int resource_id)
98       : rb_(rb), resource_id_(resource_id) {}
99   virtual ~ResourceBundleImageSource() {}
100
101   // gfx::ImageSkiaSource overrides:
102   virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
103     SkBitmap image;
104     bool fell_back_to_1x = false;
105     ScaleFactor scale_factor = GetSupportedScaleFactor(scale);
106     bool found = rb_->LoadBitmap(resource_id_, &scale_factor,
107                                  &image, &fell_back_to_1x);
108     // Force to a supported scale.
109     scale = ui::GetImageScale(scale_factor);
110     if (!found)
111       return gfx::ImageSkiaRep();
112
113     if (fell_back_to_1x) {
114       // GRIT fell back to the 100% image, so rescale it to the correct size.
115       image = skia::ImageOperations::Resize(
116           image,
117           skia::ImageOperations::RESIZE_LANCZOS3,
118           gfx::ToCeiledInt(image.width() * scale),
119           gfx::ToCeiledInt(image.height() * scale));
120       // If --highlight-missing-scaled-resources is specified, log the resource
121       // id and blend the created resource with red.
122       if (ShouldHighlightMissingScaledResources()) {
123         LOG(ERROR) << "Missing " << scale << "x scaled resource. id="
124                    << resource_id_;
125
126         SkBitmap mask;
127         mask.setConfig(SkBitmap::kARGB_8888_Config,
128                        image.width(), image.height());
129         mask.allocPixels();
130         mask.eraseColor(SK_ColorRED);
131         image = SkBitmapOperations::CreateBlendedBitmap(image, mask, 0.2);
132       }
133     }
134
135     return gfx::ImageSkiaRep(image, scale);
136   }
137
138  private:
139   ResourceBundle* rb_;
140   const int resource_id_;
141
142   DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource);
143 };
144
145 // static
146 std::string ResourceBundle::InitSharedInstanceWithLocale(
147     const std::string& pref_locale, Delegate* delegate) {
148   InitSharedInstance(delegate);
149   g_shared_instance_->LoadCommonResources();
150   std::string result = g_shared_instance_->LoadLocaleResources(pref_locale);
151   InitDefaultFontList();
152   return result;
153 }
154
155 // static
156 std::string ResourceBundle::InitSharedInstanceLocaleOnly(
157     const std::string& pref_locale, Delegate* delegate) {
158   InitSharedInstance(delegate);
159   std::string result = g_shared_instance_->LoadLocaleResources(pref_locale);
160   InitDefaultFontList();
161   return result;
162 }
163
164 // static
165 void ResourceBundle::InitSharedInstanceWithPakFile(
166     base::File pak_file, bool should_load_common_resources) {
167   InitSharedInstance(NULL);
168   if (should_load_common_resources)
169     g_shared_instance_->LoadCommonResources();
170
171   scoped_ptr<DataPack> data_pack(
172       new DataPack(SCALE_FACTOR_100P));
173   if (!data_pack->LoadFromFile(pak_file.Pass())) {
174     NOTREACHED() << "failed to load pak file";
175     return;
176   }
177   g_shared_instance_->locale_resources_data_.reset(data_pack.release());
178   InitDefaultFontList();
179 }
180
181 // static
182 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) {
183   InitSharedInstance(NULL);
184   g_shared_instance_->LoadTestResources(path, path);
185
186   InitDefaultFontList();
187 }
188
189 // static
190 void ResourceBundle::CleanupSharedInstance() {
191   if (g_shared_instance_) {
192     delete g_shared_instance_;
193     g_shared_instance_ = NULL;
194   }
195 }
196
197 // static
198 bool ResourceBundle::HasSharedInstance() {
199   return g_shared_instance_ != NULL;
200 }
201
202 // static
203 ResourceBundle& ResourceBundle::GetSharedInstance() {
204   // Must call InitSharedInstance before this function.
205   CHECK(g_shared_instance_ != NULL);
206   return *g_shared_instance_;
207 }
208
209 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) {
210   return !GetLocaleFilePath(locale, true).empty();
211 }
212
213 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path,
214                                          ScaleFactor scale_factor) {
215   AddDataPackFromPathInternal(path, scale_factor, false);
216 }
217
218 void ResourceBundle::AddOptionalDataPackFromPath(const base::FilePath& path,
219                                          ScaleFactor scale_factor) {
220   AddDataPackFromPathInternal(path, scale_factor, true);
221 }
222
223 void ResourceBundle::AddDataPackFromFile(base::File file,
224                                          ScaleFactor scale_factor) {
225   scoped_ptr<DataPack> data_pack(
226       new DataPack(scale_factor));
227   if (data_pack->LoadFromFile(file.Pass())) {
228     AddDataPack(data_pack.release());
229   } else {
230     LOG(ERROR) << "Failed to load data pack from file."
231                << "\nSome features may not be available.";
232   }
233 }
234
235 #if !defined(OS_MACOSX)
236 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale,
237                                                  bool test_file_exists) {
238   if (app_locale.empty())
239     return base::FilePath();
240
241   base::FilePath locale_file_path;
242
243   PathService::Get(ui::DIR_LOCALES, &locale_file_path);
244
245   if (!locale_file_path.empty())
246     locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak");
247
248   if (delegate_) {
249     locale_file_path =
250         delegate_->GetPathForLocalePack(locale_file_path, app_locale);
251   }
252
253   // Don't try to load empty values or values that are not absolute paths.
254   if (locale_file_path.empty() || !locale_file_path.IsAbsolute())
255     return base::FilePath();
256
257   if (test_file_exists && !base::PathExists(locale_file_path))
258     return base::FilePath();
259
260   return locale_file_path;
261 }
262 #endif
263
264 std::string ResourceBundle::LoadLocaleResources(
265     const std::string& pref_locale) {
266   DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded";
267   std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
268   base::FilePath locale_file_path = GetOverriddenPakPath();
269   if (locale_file_path.empty()) {
270     CommandLine* command_line = CommandLine::ForCurrentProcess();
271     if (command_line->HasSwitch(switches::kLocalePak)) {
272       locale_file_path =
273           command_line->GetSwitchValuePath(switches::kLocalePak);
274     } else {
275       locale_file_path = GetLocaleFilePath(app_locale, true);
276     }
277   }
278
279   if (locale_file_path.empty()) {
280     // It's possible that there is no locale.pak.
281     LOG(WARNING) << "locale_file_path.empty()";
282     return std::string();
283   }
284
285   scoped_ptr<DataPack> data_pack(
286       new DataPack(SCALE_FACTOR_100P));
287   if (!data_pack->LoadFromPath(locale_file_path)) {
288     UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError",
289                               logging::GetLastSystemErrorCode(), 16000);
290     LOG(ERROR) << "failed to load locale.pak";
291     NOTREACHED();
292     return std::string();
293   }
294
295   locale_resources_data_.reset(data_pack.release());
296   return app_locale;
297 }
298
299 void ResourceBundle::LoadTestResources(const base::FilePath& path,
300                                        const base::FilePath& locale_path) {
301   // Use the given resource pak for both common and localized resources.
302   scoped_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P));
303   if (!path.empty() && data_pack->LoadFromPath(path))
304     AddDataPack(data_pack.release());
305
306   data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE));
307   if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) {
308     locale_resources_data_.reset(data_pack.release());
309   } else {
310     locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE));
311   }
312 }
313
314 void ResourceBundle::UnloadLocaleResources() {
315   locale_resources_data_.reset();
316 }
317
318 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) {
319   overridden_pak_path_ = pak_path;
320 }
321
322 const base::FilePath& ResourceBundle::GetOverriddenPakPath() {
323   return overridden_pak_path_;
324 }
325
326 std::string ResourceBundle::ReloadLocaleResources(
327     const std::string& pref_locale) {
328   base::AutoLock lock_scope(*locale_resources_data_lock_);
329   UnloadLocaleResources();
330   return LoadLocaleResources(pref_locale);
331 }
332
333 gfx::ImageSkia* ResourceBundle::GetImageSkiaNamed(int resource_id) {
334   const gfx::ImageSkia* image = GetImageNamed(resource_id).ToImageSkia();
335   return const_cast<gfx::ImageSkia*>(image);
336 }
337
338 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) {
339   // Check to see if the image is already in the cache.
340   {
341     base::AutoLock lock_scope(*images_and_fonts_lock_);
342     if (images_.count(resource_id))
343       return images_[resource_id];
344   }
345
346   gfx::Image image;
347   if (delegate_)
348     image = delegate_->GetImageNamed(resource_id);
349
350   if (image.IsEmpty()) {
351     DCHECK(!data_packs_.empty()) <<
352         "Missing call to SetResourcesDataDLL?";
353
354 #if defined(OS_CHROMEOS)
355     ui::ScaleFactor scale_factor_to_load = GetMaxScaleFactor();
356 #else
357     ui::ScaleFactor scale_factor_to_load = ui::SCALE_FACTOR_100P;
358 #endif
359
360     float scale = GetImageScale(scale_factor_to_load);
361     // TODO(oshima): Consider reading the image size from png IHDR chunk and
362     // skip decoding here and remove #ifdef below.
363     // ResourceBundle::GetSharedInstance() is destroyed after the
364     // BrowserMainLoop has finished running. |image_skia| is guaranteed to be
365     // destroyed before the resource bundle is destroyed.
366     gfx::ImageSkia image_skia(new ResourceBundleImageSource(this, resource_id),
367                               scale);
368     if (image_skia.isNull()) {
369       LOG(WARNING) << "Unable to load image with id " << resource_id;
370       NOTREACHED();  // Want to assert in debug mode.
371       // The load failed to retrieve the image; show a debugging red square.
372       return GetEmptyImage();
373     }
374     image_skia.SetReadOnly();
375     image = gfx::Image(image_skia);
376   }
377
378   // The load was successful, so cache the image.
379   base::AutoLock lock_scope(*images_and_fonts_lock_);
380
381   // Another thread raced the load and has already cached the image.
382   if (images_.count(resource_id))
383     return images_[resource_id];
384
385   images_[resource_id] = image;
386   return images_[resource_id];
387 }
388
389 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
390   return GetNativeImageNamed(resource_id, RTL_DISABLED);
391 }
392
393 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes(
394     int resource_id) const {
395   return LoadDataResourceBytesForScale(resource_id, ui::SCALE_FACTOR_NONE);
396 }
397
398 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytesForScale(
399     int resource_id,
400     ScaleFactor scale_factor) const {
401   base::RefCountedStaticMemory* bytes = NULL;
402   if (delegate_)
403     bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
404
405   if (!bytes) {
406     base::StringPiece data =
407         GetRawDataResourceForScale(resource_id, scale_factor);
408     if (!data.empty()) {
409       bytes = new base::RefCountedStaticMemory(data.data(), data.length());
410     }
411   }
412
413   return bytes;
414 }
415
416 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
417   return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE);
418 }
419
420 base::StringPiece ResourceBundle::GetRawDataResourceForScale(
421     int resource_id,
422     ScaleFactor scale_factor) const {
423   base::StringPiece data;
424   if (delegate_ &&
425       delegate_->GetRawDataResource(resource_id, scale_factor, &data))
426     return data;
427
428   if (scale_factor != ui::SCALE_FACTOR_100P) {
429     for (size_t i = 0; i < data_packs_.size(); i++) {
430       if (data_packs_[i]->GetScaleFactor() == scale_factor &&
431           data_packs_[i]->GetStringPiece(resource_id, &data))
432         return data;
433     }
434   }
435   for (size_t i = 0; i < data_packs_.size(); i++) {
436     if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
437          data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
438         data_packs_[i]->GetStringPiece(resource_id, &data))
439       return data;
440   }
441
442   return base::StringPiece();
443 }
444
445 base::string16 ResourceBundle::GetLocalizedString(int message_id) {
446   base::string16 string;
447   if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
448     return string;
449
450   // Ensure that ReloadLocaleResources() doesn't drop the resources while
451   // we're using them.
452   base::AutoLock lock_scope(*locale_resources_data_lock_);
453
454   // If for some reason we were unable to load the resources , return an empty
455   // string (better than crashing).
456   if (!locale_resources_data_.get()) {
457     LOG(WARNING) << "locale resources are not loaded";
458     return base::string16();
459   }
460
461   base::StringPiece data;
462   if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
463     // Fall back on the main data pack (shouldn't be any strings here except in
464     // unittests).
465     data = GetRawDataResource(message_id);
466     if (data.empty()) {
467       NOTREACHED() << "unable to find resource: " << message_id;
468       return base::string16();
469     }
470   }
471
472   // Strings should not be loaded from a data pack that contains binary data.
473   ResourceHandle::TextEncodingType encoding =
474       locale_resources_data_->GetTextEncodingType();
475   DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
476       << "requested localized string from binary pack file";
477
478   // Data pack encodes strings as either UTF8 or UTF16.
479   base::string16 msg;
480   if (encoding == ResourceHandle::UTF16) {
481     msg = base::string16(reinterpret_cast<const base::char16*>(data.data()),
482                          data.length() / 2);
483   } else if (encoding == ResourceHandle::UTF8) {
484     msg = base::UTF8ToUTF16(data);
485   }
486   return msg;
487 }
488
489 const gfx::FontList& ResourceBundle::GetFontList(FontStyle style) {
490   {
491     base::AutoLock lock_scope(*images_and_fonts_lock_);
492     LoadFontsIfNecessary();
493   }
494   switch (style) {
495     case BoldFont:
496       return *bold_font_list_;
497     case SmallFont:
498       return *small_font_list_;
499     case MediumFont:
500       return *medium_font_list_;
501     case SmallBoldFont:
502       return *small_bold_font_list_;
503     case MediumBoldFont:
504       return *medium_bold_font_list_;
505     case LargeFont:
506       return *large_font_list_;
507     case LargeBoldFont:
508       return *large_bold_font_list_;
509     default:
510       return *base_font_list_;
511   }
512 }
513
514 const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
515   return GetFontList(style).GetPrimaryFont();
516 }
517
518 void ResourceBundle::ReloadFonts() {
519   base::AutoLock lock_scope(*images_and_fonts_lock_);
520   base_font_list_.reset();
521   LoadFontsIfNecessary();
522 }
523
524 ScaleFactor ResourceBundle::GetMaxScaleFactor() const {
525 #if defined(OS_CHROMEOS)
526   return max_scale_factor_;
527 #else
528   return GetSupportedScaleFactors().back();
529 #endif
530 }
531
532 ResourceBundle::ResourceBundle(Delegate* delegate)
533     : delegate_(delegate),
534       images_and_fonts_lock_(new base::Lock),
535       locale_resources_data_lock_(new base::Lock),
536       max_scale_factor_(SCALE_FACTOR_100P) {
537 }
538
539 ResourceBundle::~ResourceBundle() {
540   FreeImages();
541   UnloadLocaleResources();
542 }
543
544 // static
545 void ResourceBundle::InitSharedInstance(Delegate* delegate) {
546   DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
547   g_shared_instance_ = new ResourceBundle(delegate);
548   static std::vector<ScaleFactor> supported_scale_factors;
549 #if !defined(OS_IOS)
550   // On platforms other than iOS, 100P is always a supported scale factor.
551   supported_scale_factors.push_back(SCALE_FACTOR_100P);
552 #endif
553 #if defined(OS_ANDROID)
554   const gfx::Display display =
555       gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
556   const float display_density = display.device_scale_factor();
557   const ScaleFactor closest = FindClosestScaleFactorUnsafe(display_density);
558   if (closest != SCALE_FACTOR_100P)
559     supported_scale_factors.push_back(closest);
560 #elif defined(OS_IOS)
561     gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
562   if (display.device_scale_factor() > 1.0) {
563     DCHECK_EQ(2.0, display.device_scale_factor());
564     supported_scale_factors.push_back(SCALE_FACTOR_200P);
565   } else {
566     supported_scale_factors.push_back(SCALE_FACTOR_100P);
567   }
568 #elif defined(OS_MACOSX)
569   if (base::mac::IsOSLionOrLater())
570     supported_scale_factors.push_back(SCALE_FACTOR_200P);
571 #elif defined(OS_CHROMEOS)
572   // TODO(oshima): Include 200P only if the device support 200P
573   supported_scale_factors.push_back(SCALE_FACTOR_200P);
574 #elif defined(OS_LINUX) && defined(ENABLE_HIDPI)
575   supported_scale_factors.push_back(SCALE_FACTOR_200P);
576 #endif
577   ui::SetSupportedScaleFactors(supported_scale_factors);
578 #if defined(OS_WIN)
579   // Must be called _after_ supported scale factors are set since it
580   // uses them.
581   ui::win::InitDeviceScaleFactor();
582 #endif
583 }
584
585 void ResourceBundle::FreeImages() {
586   images_.clear();
587 }
588
589 void ResourceBundle::AddDataPackFromPathInternal(const base::FilePath& path,
590                                                  ScaleFactor scale_factor,
591                                                  bool optional) {
592   // Do not pass an empty |path| value to this method. If the absolute path is
593   // unknown pass just the pack file name.
594   DCHECK(!path.empty());
595
596   base::FilePath pack_path = path;
597   if (delegate_)
598     pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor);
599
600   // Don't try to load empty values or values that are not absolute paths.
601   if (pack_path.empty() || !pack_path.IsAbsolute())
602     return;
603
604   scoped_ptr<DataPack> data_pack(
605       new DataPack(scale_factor));
606   if (data_pack->LoadFromPath(pack_path)) {
607     AddDataPack(data_pack.release());
608   } else if (!optional) {
609     LOG(ERROR) << "Failed to load " << pack_path.value()
610                << "\nSome features may not be available.";
611   }
612 }
613
614 void ResourceBundle::AddDataPack(DataPack* data_pack) {
615   data_packs_.push_back(data_pack);
616
617   if (GetImageScale(data_pack->GetScaleFactor()) >
618       GetImageScale(max_scale_factor_))
619     max_scale_factor_ = data_pack->GetScaleFactor();
620 }
621
622 void ResourceBundle::LoadFontsIfNecessary() {
623   images_and_fonts_lock_->AssertAcquired();
624   if (!base_font_list_.get()) {
625     if (delegate_) {
626       base_font_list_ = GetFontListFromDelegate(BaseFont);
627       bold_font_list_ = GetFontListFromDelegate(BoldFont);
628       small_font_list_ = GetFontListFromDelegate(SmallFont);
629       small_bold_font_list_ = GetFontListFromDelegate(SmallBoldFont);
630       medium_font_list_ = GetFontListFromDelegate(MediumFont);
631       medium_bold_font_list_ = GetFontListFromDelegate(MediumBoldFont);
632       large_font_list_ = GetFontListFromDelegate(LargeFont);
633       large_bold_font_list_ = GetFontListFromDelegate(LargeBoldFont);
634     }
635
636     if (!base_font_list_.get())
637       base_font_list_.reset(new gfx::FontList());
638
639     if (!bold_font_list_.get()) {
640       bold_font_list_.reset(new gfx::FontList());
641       *bold_font_list_ = base_font_list_->DeriveWithStyle(
642           base_font_list_->GetFontStyle() | gfx::Font::BOLD);
643     }
644
645     if (!small_font_list_.get()) {
646       small_font_list_.reset(new gfx::FontList());
647       *small_font_list_ =
648           base_font_list_->DeriveWithSizeDelta(kSmallFontSizeDelta);
649     }
650
651     if (!small_bold_font_list_.get()) {
652       small_bold_font_list_.reset(new gfx::FontList());
653       *small_bold_font_list_ = small_font_list_->DeriveWithStyle(
654           small_font_list_->GetFontStyle() | gfx::Font::BOLD);
655     }
656
657     if (!medium_font_list_.get()) {
658       medium_font_list_.reset(new gfx::FontList());
659       *medium_font_list_ =
660           base_font_list_->DeriveWithSizeDelta(kMediumFontSizeDelta);
661     }
662
663     if (!medium_bold_font_list_.get()) {
664       medium_bold_font_list_.reset(new gfx::FontList());
665       *medium_bold_font_list_ = medium_font_list_->DeriveWithStyle(
666           medium_font_list_->GetFontStyle() | gfx::Font::BOLD);
667     }
668
669     if (!large_font_list_.get()) {
670       large_font_list_.reset(new gfx::FontList());
671       *large_font_list_ =
672           base_font_list_->DeriveWithSizeDelta(kLargeFontSizeDelta);
673     }
674
675     if (!large_bold_font_list_.get()) {
676       large_bold_font_list_.reset(new gfx::FontList());
677       *large_bold_font_list_ = large_font_list_->DeriveWithStyle(
678           large_font_list_->GetFontStyle() | gfx::Font::BOLD);
679     }
680   }
681 }
682
683 scoped_ptr<gfx::FontList> ResourceBundle::GetFontListFromDelegate(
684     FontStyle style) {
685   DCHECK(delegate_);
686   scoped_ptr<gfx::Font> font = delegate_->GetFont(style);
687   if (font.get())
688     return scoped_ptr<gfx::FontList>(new gfx::FontList(*font));
689   return scoped_ptr<gfx::FontList>();
690 }
691
692 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle,
693                                 int resource_id,
694                                 SkBitmap* bitmap,
695                                 bool* fell_back_to_1x) const {
696   DCHECK(fell_back_to_1x);
697   scoped_refptr<base::RefCountedMemory> memory(
698       data_handle.GetStaticMemory(resource_id));
699   if (!memory.get())
700     return false;
701
702   if (DecodePNG(memory->front(), memory->size(), bitmap, fell_back_to_1x))
703     return true;
704
705 #if !defined(OS_IOS)
706   // iOS does not compile or use the JPEG codec.  On other platforms,
707   // 99% of our assets are PNGs, however fallback to JPEG.
708   scoped_ptr<SkBitmap> jpeg_bitmap(
709       gfx::JPEGCodec::Decode(memory->front(), memory->size()));
710   if (jpeg_bitmap.get()) {
711     bitmap->swap(*jpeg_bitmap.get());
712     *fell_back_to_1x = false;
713     return true;
714   }
715 #endif
716
717   NOTREACHED() << "Unable to decode theme image resource " << resource_id;
718   return false;
719 }
720
721 bool ResourceBundle::LoadBitmap(int resource_id,
722                                 ScaleFactor* scale_factor,
723                                 SkBitmap* bitmap,
724                                 bool* fell_back_to_1x) const {
725   DCHECK(fell_back_to_1x);
726   for (size_t i = 0; i < data_packs_.size(); ++i) {
727     // If the resource is in the package with SCALE_FACTOR_NONE, it
728     // can be used in any scale factor, but set 100P in ImageSkia so
729     // that it will be scaled property.
730     if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE &&
731         LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) {
732       *scale_factor = ui::SCALE_FACTOR_100P;
733       DCHECK(!*fell_back_to_1x);
734       return true;
735     }
736     if (data_packs_[i]->GetScaleFactor() == *scale_factor &&
737         LoadBitmap(*data_packs_[i], resource_id, bitmap, fell_back_to_1x)) {
738       return true;
739     }
740   }
741   return false;
742 }
743
744 gfx::Image& ResourceBundle::GetEmptyImage() {
745   base::AutoLock lock(*images_and_fonts_lock_);
746
747   if (empty_image_.IsEmpty()) {
748     // The placeholder bitmap is bright red so people notice the problem.
749     SkBitmap bitmap;
750     bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
751     bitmap.allocPixels();
752     bitmap.eraseARGB(255, 255, 0, 0);
753     empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap);
754   }
755   return empty_image_;
756 }
757
758 // static
759 bool ResourceBundle::ShouldHighlightMissingScaledResources() {
760   return CommandLine::ForCurrentProcess()->HasSwitch(
761       switches::kHighlightMissingScaledResources);
762 }
763
764 // static
765 bool ResourceBundle::PNGContainsFallbackMarker(const unsigned char* buf,
766                                size_t size) {
767   if (size < arraysize(kPngMagic) ||
768       memcmp(buf, kPngMagic, arraysize(kPngMagic)) != 0) {
769     // Data invalid or a JPEG.
770     return false;
771   }
772   size_t pos = arraysize(kPngMagic);
773
774   // Scan for custom chunks until we find one, find the IDAT chunk, or run out
775   // of chunks.
776   for (;;) {
777     if (size - pos < kPngChunkMetadataSize)
778       break;
779     uint32 length = 0;
780     net::ReadBigEndian(reinterpret_cast<const char*>(buf + pos), &length);
781     if (size - pos - kPngChunkMetadataSize < length)
782       break;
783     if (length == 0 && memcmp(buf + pos + sizeof(uint32), kPngScaleChunkType,
784                               arraysize(kPngScaleChunkType)) == 0) {
785       return true;
786     }
787     if (memcmp(buf + pos + sizeof(uint32), kPngDataChunkType,
788                arraysize(kPngDataChunkType)) == 0) {
789       // Stop looking for custom chunks, any custom chunks should be before an
790       // IDAT chunk.
791       break;
792     }
793     pos += length + kPngChunkMetadataSize;
794   }
795   return false;
796 }
797
798 // static
799 bool ResourceBundle::DecodePNG(const unsigned char* buf,
800                                size_t size,
801                                SkBitmap* bitmap,
802                                bool* fell_back_to_1x) {
803   *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
804   return gfx::PNGCodec::Decode(buf, size, bitmap);
805 }
806
807 }  // namespace ui