Chromium Code Reviews| Index: third_party/WebKit/Source/platform/text/CompressableString.cpp |
| diff --git a/third_party/WebKit/Source/platform/text/CompressableString.cpp b/third_party/WebKit/Source/platform/text/CompressableString.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7548a070f27814d7ebffd0df310ca40301586173 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/text/CompressableString.cpp |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2015 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 "config.h" |
| +#include "platform/text/CompressableString.h" |
| + |
| +// TODO: This seems illegal: Fix checkdeps? |
|
haraken
2015/11/24 11:15:42
We should add a public API to talk with component/
|
| +#include "components/compression/compression_utils.h" |
| +#include "wtf/Assertions.h" |
| +#include "wtf/Partitions.h" |
| +#include "wtf/text/WTFString.h" |
| + |
| +namespace blink { |
| + |
| +void CompressableStringImpl::purgeMemory() |
|
haraken
2015/11/24 11:15:42
What are you intending to do with the purgeMemory(
|
| +{ |
| +} |
| + |
| +CompressableStringImpl::CompressableStringImpl(PassRefPtr<StringImpl> impl) |
| + : m_impl(impl) |
| + , m_compressedData(nullptr) |
| + , m_originalLength(m_impl->length()) |
| + , m_is8Bit(m_impl->is8Bit()) |
| +{ |
| +} |
| + |
| +unsigned CompressableStringImpl::sizeInBytes() const |
| +{ |
| + if (is8Bit()) |
| + return m_originalLength * 2; |
| + return m_originalLength; |
| +} |
| + |
| +String CompressableStringImpl::toString() |
| +{ |
| + if (UNLIKELY(isCompressed())) |
| + uncompress(); |
| + return m_impl.get(); |
| +} |
| + |
| +const LChar* CompressableStringImpl::characters8() |
| +{ |
| + return toString().characters8(); |
| +} |
| + |
| +const UChar* CompressableStringImpl::characters16() |
| +{ |
| + return toString().characters16(); |
| +} |
| + |
| +void CompressableStringImpl::compress() |
| +{ |
| + ASSERT(m_impl); |
| + ASSERT(!isCompressed()); |
| + |
| + // TODO(hajimehoshi): Now components offers funcitons accepting only |
| + // std::strings. This is not efficient. We should offer char* version. |
| + std::string in, out; |
| + if (m_is8Bit) |
| + in = std::string(reinterpret_cast<const char*>(m_impl->characters8()), m_impl->sizeInBytes()); |
| + else |
| + in = std::string(reinterpret_cast<const char*>(m_impl->characters16()), m_impl->sizeInBytes()); |
| + compression::GzipCompress(in, &out); |
| + |
| + m_impl = nullptr; |
| + m_compressedData = SharedBuffer::create(out.c_str(), out.size()); |
| +} |
| + |
| +void CompressableStringImpl::uncompress() |
| +{ |
| + ASSERT(m_compressedData); |
| + ASSERT(!m_impl); |
| + |
| + std::string in(m_compressedData->data(), m_compressedData->size()); |
| + std::string out; |
| + compression::GzipUncompress(in, &out); |
| + |
| + if (m_is8Bit) { |
| + LChar* data = nullptr; |
| + m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), data); |
| + memcpy(data, out.c_str(), out.size()); |
| + } else { |
| + UChar* data = nullptr; |
| + m_impl = StringImpl::createUninitialized(out.size() / sizeof(UChar), data); |
| + memcpy(data, out.c_str(), out.size()); |
| + } |
| + |
| + m_compressedData = nullptr; |
| + ASSERT(m_originalLength == m_impl->length()); |
| +} |
| + |
| +CompressableString::CompressableString() |
| + : m_impl(nullptr) |
| +{ |
| +} |
| + |
| +CompressableString::CompressableString(const CompressableString& rhs) |
| + : m_impl(rhs.m_impl) |
| +{ |
| +} |
| + |
| +CompressableString::CompressableString(PassRefPtr<StringImpl> impl) |
| + : m_impl(impl ? adoptRef(new CompressableStringImpl(impl)) : nullptr) |
| +{ |
| +} |
| + |
| +void CompressableString::compress() const |
| +{ |
| + m_impl->compress(); |
| +} |
| + |
| +void CompressableString::uncompress() const |
| +{ |
| + m_impl->uncompress(); |
| +} |
| + |
| +} // namespace blink |