Commit d8fef52b authored by erg@google.com's avatar erg@google.com

Modify DataPacks so they can return RefCountedStaticMemory objects.

(This is in preparation for a very large reworking of BrowserThemeProvider).

BUG=http://crbug.com/21121

Review URL: http://codereview.chromium.org/338027

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30241 0039d316-1c4b-4281-b951-d872f2087c98
parent 19e0ff7b
......@@ -121,24 +121,17 @@ void ResourceBundle::LoadThemeResources() {
RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
DataHandle module, int resource_id) {
DCHECK(module);
base::StringPiece bytes;
if (!module->Get(resource_id, &bytes))
return NULL;
return new RefCountedStaticMemory(
reinterpret_cast<const unsigned char*>(bytes.data()), bytes.length());
return module->GetStaticMemory(resource_id);
}
base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
DCHECK(resources_data_);
base::StringPiece data;
if (!resources_data_->Get(resource_id, &data)) {
if (!locale_resources_data_->Get(resource_id, &data)) {
if (!resources_data_->GetStringPiece(resource_id, &data)) {
if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
return base::StringPiece();
}
}
return data;
}
......@@ -151,7 +144,7 @@ string16 ResourceBundle::GetLocalizedString(int message_id) {
}
base::StringPiece data;
if (!locale_resources_data_->Get(message_id, &data)) {
if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
// Fall back on the main data pack (shouldn't be any strings here except in
// unittests).
data = GetRawDataResource(message_id);
......
......@@ -75,24 +75,17 @@ void ResourceBundle::LoadThemeResources() {
RefCountedStaticMemory* ResourceBundle::LoadResourceBytes(
DataHandle module, int resource_id) {
DCHECK(module);
base::StringPiece bytes;
if (!module->Get(resource_id, &bytes))
return NULL;
return new RefCountedStaticMemory(
reinterpret_cast<const unsigned char*>(bytes.data()), bytes.length());
return module->GetStaticMemory(resource_id);
}
base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) {
DCHECK(resources_data_);
base::StringPiece data;
if (!resources_data_->Get(resource_id, &data)) {
if (!locale_resources_data_->Get(resource_id, &data)) {
if (!resources_data_->GetStringPiece(resource_id, &data)) {
if (!locale_resources_data_->GetStringPiece(resource_id, &data)) {
return base::StringPiece();
}
}
return data;
}
......@@ -105,7 +98,7 @@ string16 ResourceBundle::GetLocalizedString(int message_id) {
}
base::StringPiece data;
if (!locale_resources_data_->Get(message_id, &data)) {
if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
// Fall back on the main data pack (shouldn't be any strings here except in
// unittests).
data = GetRawDataResource(message_id);
......
......@@ -89,7 +89,7 @@ bool DataPack::Load(const FilePath& path) {
return true;
}
bool DataPack::Get(uint32_t resource_id, StringPiece* data) {
bool DataPack::GetStringPiece(uint32_t resource_id, StringPiece* data) {
// It won't be hard to make this endian-agnostic, but it's not worth
// bothering to do right now.
#if defined(__BYTE_ORDER)
......@@ -113,4 +113,13 @@ bool DataPack::Get(uint32_t resource_id, StringPiece* data) {
return true;
}
RefCountedStaticMemory* DataPack::GetStaticMemory(uint32_t resource_id) {
base::StringPiece piece;
if (!GetStringPiece(resource_id, &piece))
return NULL;
return new RefCountedStaticMemory(
reinterpret_cast<const unsigned char*>(piece.data()), piece.length());
}
} // namespace base
......@@ -10,6 +10,7 @@
#define BASE_DATA_PACK_H_
#include "base/basictypes.h"
#include "base/ref_counted_memory.h"
#include "base/scoped_ptr.h"
namespace file_util {
......@@ -32,7 +33,12 @@ class DataPack {
// Get resource by id |resource_id|, filling in |data|.
// The data is owned by the DataPack object and should not be modified.
// Returns false if the resource id isn't found.
bool Get(uint32_t resource_id, StringPiece* data);
bool GetStringPiece(uint32_t resource_id, StringPiece* data);
// Like GetStringPiece(), but returns a reference to memory. This interface
// is used for image data, while the StringPiece interface is usually used
// for localization strings.
RefCountedStaticMemory* GetStaticMemory(uint32_t resource_id);
private:
// The memory-mapped data.
......
......@@ -25,17 +25,17 @@ TEST_F(DataPackTest, Load) {
ASSERT_TRUE(pack.Load(data_path_));
base::StringPiece data;
ASSERT_TRUE(pack.Get(4, &data));
ASSERT_TRUE(pack.GetStringPiece(4, &data));
EXPECT_EQ("this is id 4", data);
ASSERT_TRUE(pack.Get(6, &data));
ASSERT_TRUE(pack.GetStringPiece(6, &data));
EXPECT_EQ("this is id 6", data);
// Try reading zero-length data blobs, just in case.
ASSERT_TRUE(pack.Get(1, &data));
ASSERT_TRUE(pack.GetStringPiece(1, &data));
EXPECT_EQ(0U, data.length());
ASSERT_TRUE(pack.Get(10, &data));
ASSERT_TRUE(pack.GetStringPiece(10, &data));
EXPECT_EQ(0U, data.length());
// Try looking up an invalid key.
ASSERT_FALSE(pack.Get(140, &data));
ASSERT_FALSE(pack.GetStringPiece(140, &data));
}
......@@ -77,7 +77,7 @@ NSString* LoadStringFromDataPack(base::DataPack* data_pack,
const char* resource_id_str) {
NSString* result = nil;
base::StringPiece data;
if (data_pack->Get(resource_id, &data)) {
if (data_pack->GetStringPiece(resource_id, &data)) {
// Data pack encodes strings as UTF16.
result =
[[[NSString alloc] initWithBytes:data.data()
......
......@@ -185,7 +185,7 @@ void TestShell::InitializeTestShell(bool layout_test_mode) {
// fontconfig only knows how to load font config configs from a file name, so
// we write to a temp file.
base::StringPiece font_config_xml;
g_resource_data_pack->Get(IDR_LINUX_FONT_CONFIG, &font_config_xml);
g_resource_data_pack->GetStringPiece(IDR_LINUX_FONT_CONFIG, &font_config_xml);
FilePath fontconfig_path;
if (!file_util::CreateTemporaryFile(&fontconfig_path)) {
LOG(FATAL) << "failed to create temp font config file";
......@@ -274,7 +274,7 @@ void TestShell::InitializeTestShell(bool layout_test_mode) {
// Also load the layout-test-specific "Ahem" font.
base::StringPiece ahem_font;
g_resource_data_pack->Get(IDR_AHEM_FONT, &ahem_font);
g_resource_data_pack->GetStringPiece(IDR_AHEM_FONT, &ahem_font);
g_ahem_path = new FilePath;
if (!file_util::CreateTemporaryFile(g_ahem_path)) {
LOG(FATAL) << "failed to create temp ahem font";
......@@ -637,7 +637,7 @@ void TestShell::ShowStartupDebuggingDialog() {
// static
base::StringPiece TestShell::NetResourceProvider(int key) {
base::StringPiece res;
g_resource_data_pack->Get(key, &res);
g_resource_data_pack->GetStringPiece(key, &res);
return res;
}
......@@ -647,7 +647,7 @@ namespace webkit_glue {
string16 GetLocalizedString(int message_id) {
base::StringPiece res;
if (!g_resource_data_pack->Get(message_id, &res)) {
if (!g_resource_data_pack->GetStringPiece(message_id, &res)) {
LOG(FATAL) << "failed to load webkit string with id " << message_id;
}
......
......@@ -635,7 +635,7 @@ void TestShell::ShowStartupDebuggingDialog() {
base::StringPiece TestShell::NetResourceProvider(int key) {
base::StringPiece res;
g_resource_data_pack->Get(key, &res);
g_resource_data_pack->GetStringPiece(key, &res);
return res;
}
......@@ -645,7 +645,7 @@ namespace webkit_glue {
string16 GetLocalizedString(int message_id) {
base::StringPiece res;
if (!g_resource_data_pack->Get(message_id, &res)) {
if (!g_resource_data_pack->GetStringPiece(message_id, &res)) {
LOG(FATAL) << "failed to load webkit string with id " << message_id;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment