Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "platform/text/CompressableString.h" | |
| 7 | |
| 8 // TODO: This seems illegal: Fix checkdeps? | |
|
haraken
2015/11/24 11:15:42
We should add a public API to talk with component/
| |
| 9 #include "components/compression/compression_utils.h" | |
| 10 #include "wtf/Assertions.h" | |
| 11 #include "wtf/Partitions.h" | |
| 12 #include "wtf/text/WTFString.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 void CompressableStringImpl::purgeMemory() | |
|
haraken
2015/11/24 11:15:42
What are you intending to do with the purgeMemory(
| |
| 17 { | |
| 18 } | |
| 19 | |
| 20 CompressableStringImpl::CompressableStringImpl(PassRefPtr<StringImpl> impl) | |
| 21 : m_impl(impl) | |
| 22 , m_compressedData(nullptr) | |
| 23 , m_originalLength(m_impl->length()) | |
| 24 , m_is8Bit(m_impl->is8Bit()) | |
| 25 { | |
| 26 } | |
| 27 | |
| 28 unsigned CompressableStringImpl::sizeInBytes() const | |
| 29 { | |
| 30 if (is8Bit()) | |
| 31 return m_originalLength * 2; | |
| 32 return m_originalLength; | |
| 33 } | |
| 34 | |
| 35 String CompressableStringImpl::toString() | |
| 36 { | |
| 37 if (UNLIKELY(isCompressed())) | |
| 38 uncompress(); | |
| 39 return m_impl.get(); | |
| 40 } | |
| 41 | |
| 42 const LChar* CompressableStringImpl::characters8() | |
| 43 { | |
| 44 return toString().characters8(); | |
| 45 } | |
| 46 | |
| 47 const UChar* CompressableStringImpl::characters16() | |
| 48 { | |
| 49 return toString().characters16(); | |
| 50 } | |
| 51 | |
| 52 void CompressableStringImpl::compress() | |
| 53 { | |
| 54 ASSERT(m_impl); | |
| 55 ASSERT(!isCompressed()); | |
| 56 | |
| 57 // TODO(hajimehoshi): Now components offers funcitons accepting only | |
| 58 // std::strings. This is not efficient. We should offer char* version. | |
| 59 std::string in, out; | |
| 60 if (m_is8Bit) | |
| 61 in = std::string(reinterpret_cast<const char*>(m_impl->characters8()), m _impl->sizeInBytes()); | |
| 62 else | |
| 63 in = std::string(reinterpret_cast<const char*>(m_impl->characters16()), m_impl->sizeInBytes()); | |
| 64 compression::GzipCompress(in, &out); | |
| 65 | |
| 66 m_impl = nullptr; | |
| 67 m_compressedData = SharedBuffer::create(out.c_str(), out.size()); | |
| 68 } | |
| 69 | |
| 70 void CompressableStringImpl::uncompress() | |
| 71 { | |
| 72 ASSERT(m_compressedData); | |
| 73 ASSERT(!m_impl); | |
| 74 | |
| 75 std::string in(m_compressedData->data(), m_compressedData->size()); | |
| 76 std::string out; | |
| 77 compression::GzipUncompress(in, &out); | |
| 78 | |
| 79 if (m_is8Bit) { | |
| 80 LChar* data = nullptr; | |
| 81 m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), dat a); | |
| 82 memcpy(data, out.c_str(), out.size()); | |
| 83 } else { | |
| 84 UChar* data = nullptr; | |
| 85 m_impl = StringImpl::createUninitialized(out.size() / sizeof(UChar), dat a); | |
| 86 memcpy(data, out.c_str(), out.size()); | |
| 87 } | |
| 88 | |
| 89 m_compressedData = nullptr; | |
| 90 ASSERT(m_originalLength == m_impl->length()); | |
| 91 } | |
| 92 | |
| 93 CompressableString::CompressableString() | |
| 94 : m_impl(nullptr) | |
| 95 { | |
| 96 } | |
| 97 | |
| 98 CompressableString::CompressableString(const CompressableString& rhs) | |
| 99 : m_impl(rhs.m_impl) | |
| 100 { | |
| 101 } | |
| 102 | |
| 103 CompressableString::CompressableString(PassRefPtr<StringImpl> impl) | |
| 104 : m_impl(impl ? adoptRef(new CompressableStringImpl(impl)) : nullptr) | |
| 105 { | |
| 106 } | |
| 107 | |
| 108 void CompressableString::compress() const | |
| 109 { | |
| 110 m_impl->compress(); | |
| 111 } | |
| 112 | |
| 113 void CompressableString::uncompress() const | |
| 114 { | |
| 115 m_impl->uncompress(); | |
| 116 } | |
| 117 | |
| 118 } // namespace blink | |
| OLD | NEW |