summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-27 20:45:08 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-27 20:45:08 +0000
commitd8fef52b071be812fd8a0f7ebea6c577bdb38592 (patch)
tree3ad1d267a3d978e4e68c0f3c49c3781e155e1a3f
parent19e0ff7bf0b74a121d52a75b1d883222d266b789 (diff)
downloadchromium_src-d8fef52b071be812fd8a0f7ebea6c577bdb38592.zip
chromium_src-d8fef52b071be812fd8a0f7ebea6c577bdb38592.tar.gz
chromium_src-d8fef52b071be812fd8a0f7ebea6c577bdb38592.tar.bz2
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
-rw-r--r--app/resource_bundle_linux.cc15
-rw-r--r--app/resource_bundle_mac.mm15
-rw-r--r--base/data_pack.cc11
-rw-r--r--base/data_pack.h8
-rw-r--r--base/data_pack_unittest.cc10
-rw-r--r--chrome/tools/mac_helpers/infoplist_strings_util.mm2
-rw-r--r--webkit/tools/test_shell/test_shell_gtk.cc8
-rw-r--r--webkit/tools/test_shell/test_shell_mac.mm4
8 files changed, 37 insertions, 36 deletions
diff --git a/app/resource_bundle_linux.cc b/app/resource_bundle_linux.cc
index 5c5cb50..10d5caa 100644
--- a/app/resource_bundle_linux.cc
+++ b/app/resource_bundle_linux.cc
@@ -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);
diff --git a/app/resource_bundle_mac.mm b/app/resource_bundle_mac.mm
index 92b9120..0bde757 100644
--- a/app/resource_bundle_mac.mm
+++ b/app/resource_bundle_mac.mm
@@ -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);
diff --git a/base/data_pack.cc b/base/data_pack.cc
index 46f3cb4..27054d3 100644
--- a/base/data_pack.cc
+++ b/base/data_pack.cc
@@ -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
diff --git a/base/data_pack.h b/base/data_pack.h
index 02a3849..7c9abde 100644
--- a/base/data_pack.h
+++ b/base/data_pack.h
@@ -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.
diff --git a/base/data_pack_unittest.cc b/base/data_pack_unittest.cc
index 7c2cfeb..a62acf0 100644
--- a/base/data_pack_unittest.cc
+++ b/base/data_pack_unittest.cc
@@ -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));
}
diff --git a/chrome/tools/mac_helpers/infoplist_strings_util.mm b/chrome/tools/mac_helpers/infoplist_strings_util.mm
index fbdc924..d5249bf 100644
--- a/chrome/tools/mac_helpers/infoplist_strings_util.mm
+++ b/chrome/tools/mac_helpers/infoplist_strings_util.mm
@@ -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()
diff --git a/webkit/tools/test_shell/test_shell_gtk.cc b/webkit/tools/test_shell/test_shell_gtk.cc
index 433f191..e968b08 100644
--- a/webkit/tools/test_shell/test_shell_gtk.cc
+++ b/webkit/tools/test_shell/test_shell_gtk.cc
@@ -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;
}
diff --git a/webkit/tools/test_shell/test_shell_mac.mm b/webkit/tools/test_shell/test_shell_mac.mm
index 2214ef3..fb30636 100644
--- a/webkit/tools/test_shell/test_shell_mac.mm
+++ b/webkit/tools/test_shell/test_shell_mac.mm
@@ -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;
}