OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 <gtk/gtk.h> | |
8 | |
9 #include "app/app_paths.h" | |
10 #include "base/base_paths.h" | |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/i18n/rtl.h" | |
14 #include "base/lock.h" | |
15 #include "base/logging.h" | |
16 #include "base/path_service.h" | |
17 #include "base/string_piece.h" | |
18 #include "base/string_util.h" | |
19 #include "gfx/font.h" | |
20 #include "gfx/gtk_util.h" | |
21 #include "third_party/skia/include/core/SkBitmap.h" | |
22 | |
23 namespace { | |
24 | |
25 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned | |
26 // has a ref count of 1 so the caller must call g_object_unref to free the | |
27 // memory. | |
28 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { | |
29 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); | |
30 bool ok = data && gdk_pixbuf_loader_write(loader.get(), | |
31 reinterpret_cast<const guint8*>(data->front()), data->size(), NULL); | |
32 if (!ok) | |
33 return NULL; | |
34 // Calling gdk_pixbuf_loader_close forces the data to be parsed by the | |
35 // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf. | |
36 ok = gdk_pixbuf_loader_close(loader.get(), NULL); | |
37 if (!ok) | |
38 return NULL; | |
39 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader.get()); | |
40 if (!pixbuf) | |
41 return NULL; | |
42 | |
43 if (base::i18n::IsRTL() && rtl_enabled) { | |
44 // |pixbuf| will get unreffed and destroyed (see below). The returned value | |
45 // has ref count 1. | |
46 return gdk_pixbuf_flip(pixbuf, TRUE); | |
47 } else { | |
48 // The pixbuf is owned by the loader, so add a ref so when we delete the | |
49 // loader (when the ScopedGObject goes out of scope), the pixbuf still | |
50 // exists. | |
51 g_object_ref(pixbuf); | |
52 return pixbuf; | |
53 } | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 void ResourceBundle::FreeGdkPixBufs() { | |
59 for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); | |
60 i != gdk_pixbufs_.end(); i++) { | |
61 g_object_unref(i->second); | |
62 } | |
63 gdk_pixbufs_.clear(); | |
64 } | |
65 | |
66 // static | |
67 FilePath ResourceBundle::GetResourcesFilePath() { | |
68 FilePath resources_file_path; | |
69 PathService::Get(app::FILE_RESOURCES_PAK, &resources_file_path); | |
70 return resources_file_path; | |
71 } | |
72 | |
73 // static | |
74 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | |
75 FilePath locale_file_path; | |
76 PathService::Get(app::DIR_LOCALES, &locale_file_path); | |
77 if (locale_file_path.empty()) | |
78 return locale_file_path; | |
79 if (app_locale.empty()) | |
80 return FilePath(); | |
81 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | |
82 if (!file_util::PathExists(locale_file_path)) | |
83 return FilePath(); | |
84 return locale_file_path; | |
85 } | |
86 | |
87 GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { | |
88 // Use the negative |resource_id| for the key for BIDI-aware images. | |
89 int key = rtl_enabled ? -resource_id : resource_id; | |
90 | |
91 // Check to see if we already have the pixbuf in the cache. | |
92 { | |
93 AutoLock lock_scope(*lock_); | |
94 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); | |
95 if (found != gdk_pixbufs_.end()) | |
96 return found->second; | |
97 } | |
98 | |
99 scoped_refptr<RefCountedStaticMemory> data( | |
100 LoadDataResourceBytes(resource_id)); | |
101 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); | |
102 | |
103 // We loaded successfully. Cache the pixbuf. | |
104 if (pixbuf) { | |
105 AutoLock lock_scope(*lock_); | |
106 | |
107 // Another thread raced us, and has already cached the pixbuf. | |
108 if (gdk_pixbufs_.count(key)) { | |
109 g_object_unref(pixbuf); | |
110 return gdk_pixbufs_[key]; | |
111 } | |
112 | |
113 gdk_pixbufs_[key] = pixbuf; | |
114 return pixbuf; | |
115 } | |
116 | |
117 // We failed to retrieve the bitmap, show a debugging red square. | |
118 { | |
119 LOG(WARNING) << "Unable to load GdkPixbuf with id " << resource_id; | |
120 NOTREACHED(); // Want to assert in debug mode. | |
121 | |
122 AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. | |
123 | |
124 static GdkPixbuf* empty_bitmap = NULL; | |
125 if (!empty_bitmap) { | |
126 // The placeholder bitmap is bright red so people notice the problem. | |
127 // This bitmap will be leaked, but this code should never be hit. | |
128 scoped_ptr<SkBitmap> skia_bitmap(new SkBitmap()); | |
129 skia_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | |
130 skia_bitmap->allocPixels(); | |
131 skia_bitmap->eraseARGB(255, 255, 0, 0); | |
132 empty_bitmap = gfx::GdkPixbufFromSkBitmap(skia_bitmap.get()); | |
133 } | |
134 return empty_bitmap; | |
135 } | |
136 } | |
137 | |
138 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { | |
139 return GetPixbufImpl(resource_id, false); | |
140 } | |
141 | |
142 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { | |
143 return GetPixbufImpl(resource_id, true); | |
144 } | |
OLD | NEW |