This source file includes following definitions.
- LoadPixbuf
- GetResourcesPakFilePath
- LoadCommonResources
- GetNativeImageNamed
#include "ui/base/resource/resource_bundle.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/path_service.h"
#include "base/synchronization/lock.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_handle.h"
#include "ui/base/ui_base_paths.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/scoped_gobject.h"
#include <gtk/gtk.h>
namespace ui {
namespace {
GdkPixbuf* LoadPixbuf(base::RefCountedStaticMemory* data, bool rtl_enabled) {
ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new());
bool ok = data && gdk_pixbuf_loader_write(loader.get(),
data->front_as<guint8>(), data->size(), NULL);
if (!ok)
return NULL;
ok = gdk_pixbuf_loader_close(loader.get(), NULL);
if (!ok)
return NULL;
GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader.get());
if (!pixbuf)
return NULL;
if (base::i18n::IsRTL() && rtl_enabled) {
return gdk_pixbuf_flip(pixbuf, TRUE);
} else {
g_object_ref(pixbuf);
return pixbuf;
}
}
base::FilePath GetResourcesPakFilePath(const std::string& pak_name) {
base::FilePath path;
if (PathService::Get(base::DIR_MODULE, &path))
return path.AppendASCII(pak_name.c_str());
return base::FilePath(pak_name.c_str());
}
}
void ResourceBundle::LoadCommonResources() {
AddDataPackFromPath(GetResourcesPakFilePath(
"chrome_100_percent.pak"),
SCALE_FACTOR_100P);
}
gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
int key = rtl == RTL_ENABLED ? -resource_id : resource_id;
{
base::AutoLock lock_scope(*images_and_fonts_lock_);
if (images_.count(key))
return images_[key];
}
gfx::Image image;
if (delegate_)
image = delegate_->GetNativeImageNamed(resource_id, rtl);
if (image.IsEmpty()) {
scoped_refptr<base::RefCountedStaticMemory> data(
LoadDataResourceBytesForScale(resource_id, SCALE_FACTOR_100P));
GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED);
if (!pixbuf) {
LOG(WARNING) << "Unable to load pixbuf with id " << resource_id;
NOTREACHED();
return GetEmptyImage();
}
image = gfx::Image(pixbuf);
}
base::AutoLock lock_scope(*images_and_fonts_lock_);
if (images_.count(key))
return images_[key];
images_[key] = image;
return images_[key];
}
}