diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-23 05:56:05 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-23 05:56:05 +0000 |
commit | 426d0393942b3886c94d4fe27e22b1a132d8b41b (patch) | |
tree | 91c907cb212c2e43533f7b4e9ad85c6aa41a0864 /app | |
parent | be8fd116e295fd7ef6c75e4dd717017d68c7fd57 (diff) | |
download | chromium_src-426d0393942b3886c94d4fe27e22b1a132d8b41b.zip chromium_src-426d0393942b3886c94d4fe27e22b1a132d8b41b.tar.gz chromium_src-426d0393942b3886c94d4fe27e22b1a132d8b41b.tar.bz2 |
Move data pack from base to app (it's just part of the resource bundle system).
TEST=it compiles
BUG=none
Review URL: http://codereview.chromium.org/5992006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70038 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/app.gyp | 1 | ||||
-rw-r--r-- | app/app_base.gypi | 2 | ||||
-rw-r--r-- | app/data_pack.cc | 219 | ||||
-rw-r--r-- | app/data_pack.h | 66 | ||||
-rw-r--r-- | app/data_pack_unittest.cc | 77 | ||||
-rw-r--r-- | app/resource_bundle.cc | 4 | ||||
-rw-r--r-- | app/resource_bundle.h | 6 | ||||
-rw-r--r-- | app/resource_bundle_posix.cc | 6 | ||||
-rw-r--r-- | app/resource_bundle_win.cc | 2 | ||||
-rw-r--r-- | app/test/data/data_pack_unittest/sample.pak | bin | 0 -> 80 bytes |
10 files changed, 374 insertions, 9 deletions
diff --git a/app/app.gyp b/app/app.gyp index 1527c55..e34f161 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -41,6 +41,7 @@ 'animation_container_unittest.cc', 'animation_unittest.cc', 'clipboard/clipboard_unittest.cc', + 'data_pack_unittest.cc', 'l10n_util_mac_unittest.mm', 'l10n_util_unittest.cc', 'multi_animation_unittest.cc', diff --git a/app/app_base.gypi b/app/app_base.gypi index cc2c0e6..60159ac 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -115,6 +115,8 @@ 'clipboard/scoped_clipboard_writer.cc', 'clipboard/scoped_clipboard_writer.h', 'combobox_model.h', + 'data_pack.cc', + 'data_pack.h', 'drag_drop_types_gtk.cc', 'drag_drop_types_win.cc', 'drag_drop_types.h', diff --git a/app/data_pack.cc b/app/data_pack.cc new file mode 100644 index 0000000..93f7e82 --- /dev/null +++ b/app/data_pack.cc @@ -0,0 +1,219 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/data_pack.h" + +#include <errno.h> + +#include "base/file_util.h" +#include "base/logging.h" +#include "base/metrics/histogram.h" +#include "base/ref_counted_memory.h" +#include "base/string_piece.h" + +// For details of the file layout, see +// http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalizedstrings + +namespace { + +// A word is four bytes. +static const size_t kWord = 4; + +static const uint32 kFileFormatVersion = 1; +// Length of file header: version and entry count. +static const size_t kHeaderLength = 2 * sizeof(uint32); + +struct DataPackEntry { + uint32 resource_id; + uint32 file_offset; + uint32 length; + + static int CompareById(const void* void_key, const void* void_entry) { + uint32 key = *reinterpret_cast<const uint32*>(void_key); + const DataPackEntry* entry = + reinterpret_cast<const DataPackEntry*>(void_entry); + if (key < entry->resource_id) { + return -1; + } else if (key > entry->resource_id) { + return 1; + } else { + return 0; + } + } +}; + +COMPILE_ASSERT(sizeof(DataPackEntry) == 12, size_of_header_must_be_twelve); + +// We're crashing when trying to load a pak file on Windows. Add some error +// codes for logging. +// http://crbug.com/58056 +enum LoadErrors { + INIT_FAILED = 1, + BAD_VERSION, + INDEX_TRUNCATED, + ENTRY_NOT_FOUND, + + LOAD_ERRORS_COUNT, +}; + +} // anonymous namespace + +namespace app { + +// In .cc for MemoryMappedFile dtor. +DataPack::DataPack() : resource_count_(0) { +} +DataPack::~DataPack() { +} + +bool DataPack::Load(const FilePath& path) { + mmap_.reset(new file_util::MemoryMappedFile); + if (!mmap_->Initialize(path)) { + DLOG(ERROR) << "Failed to mmap datapack"; + UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, + LOAD_ERRORS_COUNT); + return false; + } + + // Parse the header of the file. + // First uint32: version; second: resource count. + const uint32* ptr = reinterpret_cast<const uint32*>(mmap_->data()); + uint32 version = ptr[0]; + if (version != kFileFormatVersion) { + LOG(ERROR) << "Bad data pack version: got " << version << ", expected " + << kFileFormatVersion; + UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, + LOAD_ERRORS_COUNT); + mmap_.reset(); + return false; + } + resource_count_ = ptr[1]; + + // Sanity check the file. + // 1) Check we have enough entries. + if (kHeaderLength + resource_count_ * sizeof(DataPackEntry) > + mmap_->length()) { + LOG(ERROR) << "Data pack file corruption: too short for number of " + "entries specified."; + UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, + LOAD_ERRORS_COUNT); + mmap_.reset(); + return false; + } + // 2) Verify the entries are within the appropriate bounds. + for (size_t i = 0; i < resource_count_; ++i) { + const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( + mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); + if (entry->file_offset + entry->length > mmap_->length()) { + LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " + << "Was the file corrupted?"; + UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, + LOAD_ERRORS_COUNT); + mmap_.reset(); + return false; + } + } + + return true; +} + +bool DataPack::GetStringPiece(uint32 resource_id, + base::StringPiece* data) const { + // It won't be hard to make this endian-agnostic, but it's not worth + // bothering to do right now. +#if defined(__BYTE_ORDER) + // Linux check + COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, + datapack_assumes_little_endian); +#elif defined(__BIG_ENDIAN__) + // Mac check + #error DataPack assumes little endian +#endif + + DataPackEntry* target = reinterpret_cast<DataPackEntry*>( + bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, + sizeof(DataPackEntry), DataPackEntry::CompareById)); + if (!target) { + return false; + } + + data->set(mmap_->data() + target->file_offset, target->length); + return true; +} + +RefCountedStaticMemory* DataPack::GetStaticMemory(uint32 resource_id) const { + base::StringPiece piece; + if (!GetStringPiece(resource_id, &piece)) + return NULL; + + return new RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); +} + +// static +bool DataPack::WritePack(const FilePath& path, + const std::map<uint32, base::StringPiece>& resources) { + FILE* file = file_util::OpenFile(path, "wb"); + if (!file) + return false; + + if (fwrite(&kFileFormatVersion, 1, kWord, file) != kWord) { + LOG(ERROR) << "Failed to write file version"; + file_util::CloseFile(file); + return false; + } + + // Note: the python version of this function explicitly sorted keys, but + // std::map is a sorted associative container, we shouldn't have to do that. + uint32 entry_count = resources.size(); + if (fwrite(&entry_count, 1, kWord, file) != kWord) { + LOG(ERROR) << "Failed to write entry count"; + file_util::CloseFile(file); + return false; + } + + // Each entry is 3 uint32s. + uint32 index_length = entry_count * 3 * kWord; + uint32 data_offset = kHeaderLength + index_length; + for (std::map<uint32, base::StringPiece>::const_iterator it = + resources.begin(); + it != resources.end(); ++it) { + if (fwrite(&it->first, 1, kWord, file) != kWord) { + LOG(ERROR) << "Failed to write id for " << it->first; + file_util::CloseFile(file); + return false; + } + + if (fwrite(&data_offset, 1, kWord, file) != kWord) { + LOG(ERROR) << "Failed to write offset for " << it->first; + file_util::CloseFile(file); + return false; + } + + uint32 len = it->second.length(); + if (fwrite(&len, 1, kWord, file) != kWord) { + LOG(ERROR) << "Failed to write length for " << it->first; + file_util::CloseFile(file); + return false; + } + + data_offset += len; + } + + for (std::map<uint32, base::StringPiece>::const_iterator it = + resources.begin(); + it != resources.end(); ++it) { + if (fwrite(it->second.data(), it->second.length(), 1, file) != 1) { + LOG(ERROR) << "Failed to write data for " << it->first; + file_util::CloseFile(file); + return false; + } + } + + file_util::CloseFile(file); + + return true; +} + +} // namespace app diff --git a/app/data_pack.h b/app/data_pack.h new file mode 100644 index 0000000..aeeee98 --- /dev/null +++ b/app/data_pack.h @@ -0,0 +1,66 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// DataPack represents a read-only view onto an on-disk file that contains +// (key, value) pairs of data. It's used to store static resources like +// translation strings and images. + +#ifndef APP_DATA_PACK_H_ +#define APP_DATA_PACK_H_ +#pragma once + +#include <map> + +#include "base/basictypes.h" +#include "base/scoped_ptr.h" + +namespace base { +class StringPiece; +} + +namespace file_util { +class MemoryMappedFile; +} + +class FilePath; +class RefCountedStaticMemory; + + +namespace app { + +class DataPack { + public: + DataPack(); + ~DataPack(); + + // Load a pack file from |path|, returning false on error. + bool Load(const FilePath& path); + + // 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 GetStringPiece(uint32 resource_id, base::StringPiece* data) const; + + // 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 resource_id) const; + + // Writes a pack file containing |resources| to |path|. + static bool WritePack(const FilePath& path, + const std::map<uint32, base::StringPiece>& resources); + + private: + // The memory-mapped data. + scoped_ptr<file_util::MemoryMappedFile> mmap_; + + // Number of resources in the data. + size_t resource_count_; + + DISALLOW_COPY_AND_ASSIGN(DataPack); +}; + +} // namespace app + +#endif // APP_DATA_PACK_H_ diff --git a/app/data_pack_unittest.cc b/app/data_pack_unittest.cc new file mode 100644 index 0000000..83521ff --- /dev/null +++ b/app/data_pack_unittest.cc @@ -0,0 +1,77 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/data_pack.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "base/scoped_temp_dir.h" +#include "base/string_piece.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace app { + +TEST(DataPackTest, Load) { + FilePath data_path; + PathService::Get(base::DIR_SOURCE_ROOT, &data_path); + data_path = data_path.Append( + FILE_PATH_LITERAL("app/test/data/data_pack_unittest/sample.pak")); + + DataPack pack; + ASSERT_TRUE(pack.Load(data_path)); + + base::StringPiece data; + ASSERT_TRUE(pack.GetStringPiece(4, &data)); + EXPECT_EQ("this is id 4", 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.GetStringPiece(1, &data)); + EXPECT_EQ(0U, data.length()); + ASSERT_TRUE(pack.GetStringPiece(10, &data)); + EXPECT_EQ(0U, data.length()); + + // Try looking up an invalid key. + ASSERT_FALSE(pack.GetStringPiece(140, &data)); +} + +TEST(DataPackTest, Write) { + ScopedTempDir dir; + ASSERT_TRUE(dir.CreateUniqueTempDir()); + FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); + + std::string one("one"); + std::string two("two"); + std::string three("three"); + std::string four("four"); + std::string fifteen("fifteen"); + + std::map<uint32, base::StringPiece> resources; + resources.insert(std::make_pair(1, base::StringPiece(one))); + resources.insert(std::make_pair(2, base::StringPiece(two))); + resources.insert(std::make_pair(15, base::StringPiece(fifteen))); + resources.insert(std::make_pair(3, base::StringPiece(three))); + resources.insert(std::make_pair(4, base::StringPiece(four))); + ASSERT_TRUE(DataPack::WritePack(file, resources)); + + // Now try to read the data back in. + DataPack pack; + ASSERT_TRUE(pack.Load(file)); + + base::StringPiece data; + ASSERT_TRUE(pack.GetStringPiece(1, &data)); + EXPECT_EQ(one, data); + ASSERT_TRUE(pack.GetStringPiece(2, &data)); + EXPECT_EQ(two, data); + ASSERT_TRUE(pack.GetStringPiece(3, &data)); + EXPECT_EQ(three, data); + ASSERT_TRUE(pack.GetStringPiece(4, &data)); + EXPECT_EQ(four, data); + ASSERT_TRUE(pack.GetStringPiece(15, &data)); + EXPECT_EQ(fifteen, data); +} + +} // namespace app diff --git a/app/resource_bundle.cc b/app/resource_bundle.cc index ab34609..b0e79ed 100644 --- a/app/resource_bundle.cc +++ b/app/resource_bundle.cc @@ -4,7 +4,7 @@ #include "app/resource_bundle.h" -#include "base/data_pack.h" +#include "app/data_pack.h" #include "base/lock.h" #include "base/logging.h" #include "base/string_piece.h" @@ -246,7 +246,7 @@ ResourceBundle::LoadedDataPack::~LoadedDataPack() { void ResourceBundle::LoadedDataPack::Load() { DCHECK(!data_pack_.get()); - data_pack_.reset(new base::DataPack); + data_pack_.reset(new app::DataPack); bool success = data_pack_->Load(path_); CHECK(success) << "Failed to load " << path_.value(); } diff --git a/app/resource_bundle.h b/app/resource_bundle.h index 19905c2..d2c2d59 100644 --- a/app/resource_bundle.h +++ b/app/resource_bundle.h @@ -23,7 +23,7 @@ #include "base/string16.h" #include "gfx/native_widget_types.h" -namespace base { +namespace app { class DataPack; } #if defined(USE_X11) @@ -178,7 +178,7 @@ class ResourceBundle { private: void Load(); - scoped_ptr<base::DataPack> data_pack_; + scoped_ptr<app::DataPack> data_pack_; FilePath path_; DISALLOW_COPY_AND_ASSIGN(LoadedDataPack); @@ -191,7 +191,7 @@ class ResourceBundle { typedef HINSTANCE DataHandle; #elif defined(USE_BASE_DATA_PACK) // Linux uses base::DataPack. - typedef base::DataPack* DataHandle; + typedef app::DataPack* DataHandle; #endif // Ctor/dtor are private, since we're a singleton. diff --git a/app/resource_bundle_posix.cc b/app/resource_bundle_posix.cc index 9944280..f3c868b 100644 --- a/app/resource_bundle_posix.cc +++ b/app/resource_bundle_posix.cc @@ -4,8 +4,8 @@ #include "app/resource_bundle.h" +#include "app/data_pack.h" #include "app/l10n_util.h" -#include "base/data_pack.h" #include "base/lock.h" #include "base/logging.h" #include "base/stl_util-inl.h" @@ -15,8 +15,8 @@ namespace { -base::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { - base::DataPack* resources_pak = new base::DataPack; +app::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { + app::DataPack* resources_pak = new app::DataPack; bool success = resources_pak->Load(resources_pak_path); if (!success) { delete resources_pak; diff --git a/app/resource_bundle_win.cc b/app/resource_bundle_win.cc index fbc40b4..5527160 100644 --- a/app/resource_bundle_win.cc +++ b/app/resource_bundle_win.cc @@ -7,8 +7,8 @@ #include <atlbase.h> #include "app/app_paths.h" +#include "app/data_pack.h" #include "app/l10n_util.h" -#include "base/data_pack.h" #include "base/debug_util.h" #include "base/debug/stack_trace.h" #include "base/file_util.h" diff --git a/app/test/data/data_pack_unittest/sample.pak b/app/test/data/data_pack_unittest/sample.pak Binary files differnew file mode 100644 index 0000000..fdbe2b5 --- /dev/null +++ b/app/test/data/data_pack_unittest/sample.pak |