diff options
author | mstrydom@google.com <mstrydom@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-02 22:21:31 +0000 |
---|---|---|
committer | mstrydom@google.com <mstrydom@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-02 22:21:31 +0000 |
commit | 8607f6dae7f80f494d55cb6701234ca7be18738b (patch) | |
tree | 2121ef79ee8402d5ada98b5c634c76c30263af1a /o3d/import | |
parent | 74d7f63a55085f2d7badff916b08ebe459184b9b (diff) | |
download | chromium_src-8607f6dae7f80f494d55cb6701234ca7be18738b.zip chromium_src-8607f6dae7f80f494d55cb6701234ca7be18738b.tar.gz chromium_src-8607f6dae7f80f494d55cb6701234ca7be18738b.tar.bz2 |
This CL adds the createRawDataFromDataURL function to o3d.Pack. The function
takes as input a data URL, decodes the data, and returns the raw data in a
RawData object. If the data URL has an invalid format, an error message is
displayed. Currently only data URL's encoded in base64 are supported. The mime
type is ignored.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25252 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/import')
-rw-r--r-- | o3d/import/cross/raw_data.cc | 26 | ||||
-rw-r--r-- | o3d/import/cross/raw_data.h | 9 | ||||
-rw-r--r-- | o3d/import/cross/raw_data_test.cc | 18 |
3 files changed, 51 insertions, 2 deletions
diff --git a/o3d/import/cross/raw_data.cc b/o3d/import/cross/raw_data.cc index 66e0109..91e2bc6 100644 --- a/o3d/import/cross/raw_data.cc +++ b/o3d/import/cross/raw_data.cc @@ -38,6 +38,7 @@ #include "utils/cross/file_path_utils.h" #include "base/file_path.h" #include "base/file_util.h" +#include "utils/cross/dataurl.h" #ifdef OS_MACOSX #include <CoreFoundation/CoreFoundation.h> @@ -92,7 +93,16 @@ RawData::Ref RawData::CreateFromFile(ServiceLocator* service_locator, return data; } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +RawData::Ref RawData::CreateFromDataURL(ServiceLocator* service_locator, + const String& data_url) { + RawData::Ref raw_data(Create(service_locator, "", NULL, 0)); + if (!raw_data->SetFromDataURL(data_url)) { + raw_data.Reset(); + } + return raw_data; +} // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RawData::~RawData() { Discard(); @@ -137,7 +147,21 @@ bool RawData::SetFromFile(const String& filename) { return result; } - +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bool RawData::SetFromDataURL(const String& data_url) { + String error_string; + size_t data_length = 0; + bool no_errors = dataurl::FromDataURL(data_url, + &data_, + &data_length, + &error_string); + length_ = data_length; + if (!no_errors) { + O3D_ERROR(service_locator()) << error_string; + return false; + } + return true; +} // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ const uint8 *RawData::GetData() const { // Return data immediately if we have it diff --git a/o3d/import/cross/raw_data.h b/o3d/import/cross/raw_data.h index 092c800..96ea3f4 100644 --- a/o3d/import/cross/raw_data.h +++ b/o3d/import/cross/raw_data.h @@ -62,6 +62,11 @@ class RawData : public ParamObject { const String &uri, const String& filename); + // Creates a RawData object, taking as input a string containing a + // data URL. + static RawData::Ref CreateFromDataURL(ServiceLocator* service_locator, + const String& data_url); + virtual ~RawData(); const uint8 *GetData() const; @@ -110,6 +115,10 @@ class RawData : public ParamObject { bool SetFromFile(const String& filename); + // Decodes data from a data URL and stores that data in this + // RawData object. Returns false on error, true otherwise + bool SetFromDataURL(const String& data_url); + friend class IClassManager; friend class Pack; diff --git a/o3d/import/cross/raw_data_test.cc b/o3d/import/cross/raw_data_test.cc index b4c5f86..fe0f212 100644 --- a/o3d/import/cross/raw_data_test.cc +++ b/o3d/import/cross/raw_data_test.cc @@ -157,7 +157,7 @@ struct TestData { size_t offset; }; -} // anonymous namespace +} // anonymous namespace TEST_F(RawDataTest, StringValue) { // A BOM in the front (valid) @@ -266,4 +266,20 @@ TEST_F(RawDataTest, StringValue) { } } +TEST_F(RawDataTest, CreateFromDataURL) { + RawData::Ref ref = RawData::CreateFromDataURL(g_service_locator, + "data:;base64,YWJj"); + ASSERT_FALSE(ref.IsNull()); + EXPECT_EQ(3u, ref->GetLength()); + EXPECT_FALSE(CheckErrorExists(error_status())); + EXPECT_EQ(0, memcmp(ref->GetData(), "abc", 3)); +} + +TEST_F(RawDataTest, CreateFromDataURLFail) { + RawData::Ref ref = RawData::CreateFromDataURL(g_service_locator, + "data:;base64,Y"); + EXPECT_TRUE(ref.IsNull()); + EXPECT_TRUE(CheckErrorExists(error_status())); +} + } // namespace o3d |