OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef CompressibleString_h |
| 6 #define CompressibleString_h |
| 7 |
| 8 #include "platform/PlatformExport.h" |
| 9 #include "wtf/text/Unicode.h" |
| 10 #include "wtf/text/WTFString.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 // TODO(hajimehoshi): Now these classes are in platform/text to use UMA. Move |
| 15 // them to wtf/text. |
| 16 |
| 17 // This method must be called only by Platform::initialize. |
| 18 void initializeCompressibleStringTable(); |
| 19 |
| 20 class PLATFORM_EXPORT CompressibleStringImpl final : public RefCounted<Compressi
bleStringImpl> { |
| 21 WTF_MAKE_NONCOPYABLE(CompressibleStringImpl); |
| 22 public: |
| 23 static void compressAll(); |
| 24 static void setPageBackground(bool); |
| 25 |
| 26 CompressibleStringImpl() |
| 27 : m_string() |
| 28 , m_isCompressed(false) |
| 29 { |
| 30 } |
| 31 |
| 32 explicit CompressibleStringImpl(PassRefPtr<StringImpl>); |
| 33 ~CompressibleStringImpl(); |
| 34 |
| 35 bool isEmpty() const { return originalLength() == 0; } |
| 36 |
| 37 bool isCompressed() const { return m_isCompressed; } |
| 38 unsigned originalLength() const { return m_string.length(); } |
| 39 bool is8Bit() const { return m_string.is8Bit(); } |
| 40 |
| 41 unsigned originalContentSizeInBytes() const |
| 42 { |
| 43 if (is8Bit()) |
| 44 return originalLength() * sizeof(LChar); |
| 45 return originalLength() * sizeof(UChar); |
| 46 } |
| 47 |
| 48 // TODO(hajimehoshi): Update this once we implement compression. |
| 49 unsigned currentSizeInBytes() const |
| 50 { |
| 51 return originalContentSizeInBytes(); |
| 52 } |
| 53 |
| 54 const String& toString() |
| 55 { |
| 56 if (UNLIKELY(isCompressed())) |
| 57 decompressString(); |
| 58 return m_string; |
| 59 } |
| 60 |
| 61 const LChar* characters8() |
| 62 { |
| 63 return toString().characters8(); |
| 64 } |
| 65 |
| 66 const UChar* characters16() |
| 67 { |
| 68 return toString().characters16(); |
| 69 } |
| 70 |
| 71 void compressString(); |
| 72 void decompressString(); |
| 73 |
| 74 private: |
| 75 static bool s_isPageBackground; |
| 76 |
| 77 String m_string; |
| 78 bool m_isCompressed; |
| 79 }; |
| 80 |
| 81 class PLATFORM_EXPORT CompressibleString final { |
| 82 public: |
| 83 CompressibleString() |
| 84 : m_impl(nullptr) |
| 85 { |
| 86 } |
| 87 |
| 88 CompressibleString(const CompressibleString& rhs) |
| 89 : m_impl(rhs.m_impl) |
| 90 { |
| 91 } |
| 92 |
| 93 explicit CompressibleString(PassRefPtr<StringImpl> impl) |
| 94 : m_impl(impl ? adoptRef(new CompressibleStringImpl(impl)) : nullptr) |
| 95 { |
| 96 } |
| 97 |
| 98 bool isNull() const { return !m_impl; } |
| 99 bool isEmpty() const { return isNull() || m_impl->isEmpty(); } |
| 100 unsigned length() const { return m_impl ? m_impl->originalLength() : 0; } |
| 101 |
| 102 unsigned currentSizeInBytes() const { return m_impl ? m_impl->currentSizeInB
ytes() : 0; } |
| 103 |
| 104 bool isCompressed() const { return m_impl ? m_impl->isCompressed() : false;
} |
| 105 bool is8Bit() const { return m_impl ? m_impl->is8Bit() : false; } |
| 106 |
| 107 const String& toString() const { return m_impl ? m_impl->toString() : emptyS
tring(); } |
| 108 const LChar* characters8() const { return m_impl ? m_impl->characters8() : n
ullptr; } |
| 109 const UChar* characters16() const { return m_impl ? m_impl->characters16() :
nullptr; } |
| 110 |
| 111 CompressibleStringImpl* impl() const { return m_impl.get(); } |
| 112 |
| 113 private: |
| 114 void compressString() const { m_impl->compressString(); } |
| 115 void decompressString() const { m_impl->decompressString(); } |
| 116 |
| 117 mutable RefPtr<CompressibleStringImpl> m_impl; |
| 118 }; |
| 119 |
| 120 } // namespace blink |
| 121 |
| 122 using blink::CompressibleString; |
| 123 using blink::CompressibleStringImpl; |
| 124 |
| 125 #endif |
OLD | NEW |