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" |
11 #include "app/gfx/gtk_util.h" | 11 #include "app/gfx/gtk_util.h" |
12 #include "app/l10n_util.h" | 12 #include "app/l10n_util.h" |
13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
14 #include "base/data_pack.h" | 14 #include "base/data_pack.h" |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/string_piece.h" | 19 #include "base/string_piece.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Convert the raw image data into a GdkPixbuf. The GdkPixbuf that is returned | 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 | 26 // has a ref count of 1 so the caller must call g_object_unref to free the |
27 // memory. | 27 // memory. |
28 GdkPixbuf* LoadPixbuf(std::vector<unsigned char>& data, bool rtl_enabled) { | 28 GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { |
29 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); | 29 ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); |
30 bool ok = gdk_pixbuf_loader_write(loader.get(), | 30 bool ok = gdk_pixbuf_loader_write(loader.get(), |
31 static_cast<guint8*>(data.data()), data.size(), NULL); | 31 reinterpret_cast<const guint8*>(data->front()), data->size(), NULL); |
32 if (!ok) | 32 if (!ok) |
33 return NULL; | 33 return NULL; |
34 // Calling gdk_pixbuf_loader_close forces the data to be parsed by the | 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. | 35 // loader. We must do this before calling gdk_pixbuf_loader_get_pixbuf. |
36 ok = gdk_pixbuf_loader_close(loader.get(), NULL); | 36 ok = gdk_pixbuf_loader_close(loader.get(), NULL); |
37 if (!ok) | 37 if (!ok) |
38 return NULL; | 38 return NULL; |
39 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader.get()); | 39 GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader.get()); |
40 if (!pixbuf) | 40 if (!pixbuf) |
41 return NULL; | 41 return NULL; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 void ResourceBundle::LoadThemeResources() { | 111 void ResourceBundle::LoadThemeResources() { |
112 FilePath theme_data_path; | 112 FilePath theme_data_path; |
113 PathService::Get(app::DIR_THEMES, &theme_data_path); | 113 PathService::Get(app::DIR_THEMES, &theme_data_path); |
114 theme_data_path = theme_data_path.Append(FILE_PATH_LITERAL("default.pak")); | 114 theme_data_path = theme_data_path.Append(FILE_PATH_LITERAL("default.pak")); |
115 theme_data_ = new base::DataPack; | 115 theme_data_ = new base::DataPack; |
116 bool success = theme_data_->Load(theme_data_path); | 116 bool success = theme_data_->Load(theme_data_path); |
117 DCHECK(success) << "failed to load theme data"; | 117 DCHECK(success) << "failed to load theme data"; |
118 } | 118 } |
119 | 119 |
120 /* static */ | 120 // static |
121 bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id, | 121 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
122 std::vector<unsigned char>* bytes) { | 122 DataHandle module, int resource_id) { |
123 DCHECK(module); | 123 DCHECK(module); |
124 base::StringPiece data; | 124 base::StringPiece bytes; |
125 if (!module->Get(resource_id, &data)) | 125 if (!module->Get(resource_id, &bytes)) |
126 return false; | 126 return NULL; |
127 | 127 |
128 bytes->resize(data.length()); | 128 return new RefCountedStaticMemory( |
129 memcpy(&(bytes->front()), data.data(), data.length()); | 129 reinterpret_cast<const unsigned char*>(bytes.data()), bytes.length()); |
130 | |
131 return true; | |
132 } | 130 } |
133 | 131 |
134 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { | 132 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { |
135 DCHECK(resources_data_); | 133 DCHECK(resources_data_); |
136 base::StringPiece data; | 134 base::StringPiece data; |
137 if (!resources_data_->Get(resource_id, &data)) | 135 if (!resources_data_->Get(resource_id, &data)) |
138 return base::StringPiece(); | 136 return base::StringPiece(); |
139 return data; | 137 return data; |
140 } | 138 } |
141 | 139 |
(...skipping 27 matching lines...) Expand all Loading... |
169 int key = rtl_enabled ? -resource_id : resource_id; | 167 int key = rtl_enabled ? -resource_id : resource_id; |
170 | 168 |
171 // Check to see if we already have the pixbuf in the cache. | 169 // Check to see if we already have the pixbuf in the cache. |
172 { | 170 { |
173 AutoLock lock_scope(lock_); | 171 AutoLock lock_scope(lock_); |
174 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); | 172 GdkPixbufMap::const_iterator found = gdk_pixbufs_.find(key); |
175 if (found != gdk_pixbufs_.end()) | 173 if (found != gdk_pixbufs_.end()) |
176 return found->second; | 174 return found->second; |
177 } | 175 } |
178 | 176 |
179 std::vector<unsigned char> data; | 177 scoped_refptr<RefCountedStaticMemory> data( |
180 LoadImageResourceBytes(resource_id, &data); | 178 LoadImageResourceBytes(resource_id)); |
181 GdkPixbuf* pixbuf = LoadPixbuf(data, rtl_enabled); | 179 GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); |
182 | 180 |
183 // We loaded successfully. Cache the pixbuf. | 181 // We loaded successfully. Cache the pixbuf. |
184 if (pixbuf) { | 182 if (pixbuf) { |
185 AutoLock lock_scope(lock_); | 183 AutoLock lock_scope(lock_); |
186 | 184 |
187 // Another thread raced us, and has already cached the pixbuf. | 185 // Another thread raced us, and has already cached the pixbuf. |
188 if (gdk_pixbufs_.count(key)) { | 186 if (gdk_pixbufs_.count(key)) { |
189 g_object_unref(pixbuf); | 187 g_object_unref(pixbuf); |
190 return gdk_pixbufs_[key]; | 188 return gdk_pixbufs_[key]; |
191 } | 189 } |
(...skipping 23 matching lines...) Expand all Loading... |
215 } | 213 } |
216 } | 214 } |
217 | 215 |
218 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { | 216 GdkPixbuf* ResourceBundle::GetPixbufNamed(int resource_id) { |
219 return GetPixbufImpl(resource_id, false); | 217 return GetPixbufImpl(resource_id, false); |
220 } | 218 } |
221 | 219 |
222 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { | 220 GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { |
223 return GetPixbufImpl(resource_id, true); | 221 return GetPixbufImpl(resource_id, true); |
224 } | 222 } |
OLD | NEW |