|
Lines 1-135
a/Source/WebKit2/Shared/gtk/KeyedEncoder.cpp_sec1
|
| 1 |
/* |
|
|
| 2 |
* Copyright (C) 2015 Igalia S.L. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "KeyedEncoder.h" |
| 28 |
|
| 29 |
#include <WebCore/SharedBuffer.h> |
| 30 |
#include <wtf/text/CString.h> |
| 31 |
|
| 32 |
using namespace WebCore; |
| 33 |
|
| 34 |
namespace WebKit { |
| 35 |
|
| 36 |
KeyedEncoder::KeyedEncoder() |
| 37 |
{ |
| 38 |
g_variant_builder_init(&m_variantBuilder, G_VARIANT_TYPE("a{sv}")); |
| 39 |
m_variantBuilderStack.append(&m_variantBuilder); |
| 40 |
} |
| 41 |
|
| 42 |
KeyedEncoder::~KeyedEncoder() |
| 43 |
{ |
| 44 |
ASSERT(m_variantBuilderStack.size() == 1); |
| 45 |
ASSERT(m_variantBuilderStack.last() == &m_variantBuilder); |
| 46 |
ASSERT(m_arrayStack.isEmpty()); |
| 47 |
ASSERT(m_objectStack.isEmpty()); |
| 48 |
} |
| 49 |
|
| 50 |
void KeyedEncoder::encodeBytes(const String& key, const uint8_t* bytes, size_t size) |
| 51 |
{ |
| 52 |
GRefPtr<GBytes> gBytes = adoptGRef(g_bytes_new_static(bytes, size)); |
| 53 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_from_bytes(G_VARIANT_TYPE("ay"), gBytes.get(), TRUE)); |
| 54 |
} |
| 55 |
|
| 56 |
void KeyedEncoder::encodeBool(const String& key, bool value) |
| 57 |
{ |
| 58 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_boolean(value)); |
| 59 |
} |
| 60 |
|
| 61 |
void KeyedEncoder::encodeUInt32(const String& key, uint32_t value) |
| 62 |
{ |
| 63 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_uint32(value)); |
| 64 |
} |
| 65 |
|
| 66 |
void KeyedEncoder::encodeInt32(const String& key, int32_t value) |
| 67 |
{ |
| 68 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_int32(value)); |
| 69 |
} |
| 70 |
|
| 71 |
void KeyedEncoder::encodeInt64(const String& key, int64_t value) |
| 72 |
{ |
| 73 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_int64(value)); |
| 74 |
} |
| 75 |
|
| 76 |
void KeyedEncoder::encodeFloat(const String& key, float value) |
| 77 |
{ |
| 78 |
encodeDouble(key, value); |
| 79 |
} |
| 80 |
|
| 81 |
void KeyedEncoder::encodeDouble(const String& key, double value) |
| 82 |
{ |
| 83 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_double(value)); |
| 84 |
} |
| 85 |
|
| 86 |
void KeyedEncoder::encodeString(const String& key, const String& value) |
| 87 |
{ |
| 88 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", key.utf8().data(), g_variant_new_string(value.utf8().data())); |
| 89 |
} |
| 90 |
|
| 91 |
void KeyedEncoder::beginObject(const String& key) |
| 92 |
{ |
| 93 |
GRefPtr<GVariantBuilder> builder = adoptGRef(g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"))); |
| 94 |
m_objectStack.append(std::make_pair(key, builder)); |
| 95 |
m_variantBuilderStack.append(builder.get()); |
| 96 |
} |
| 97 |
|
| 98 |
void KeyedEncoder::endObject() |
| 99 |
{ |
| 100 |
GVariantBuilder* builder = m_variantBuilderStack.takeLast(); |
| 101 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", m_objectStack.last().first.utf8().data(), g_variant_builder_end(builder)); |
| 102 |
m_objectStack.removeLast(); |
| 103 |
} |
| 104 |
|
| 105 |
void KeyedEncoder::beginArray(const String& key) |
| 106 |
{ |
| 107 |
m_arrayStack.append(std::make_pair(key, adoptGRef(g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"))))); |
| 108 |
} |
| 109 |
|
| 110 |
void KeyedEncoder::beginArrayElement() |
| 111 |
{ |
| 112 |
m_variantBuilderStack.append(g_variant_builder_new(G_VARIANT_TYPE("a{sv}"))); |
| 113 |
} |
| 114 |
|
| 115 |
void KeyedEncoder::endArrayElement() |
| 116 |
{ |
| 117 |
GRefPtr<GVariantBuilder> variantBuilder = adoptGRef(m_variantBuilderStack.takeLast()); |
| 118 |
g_variant_builder_add_value(m_arrayStack.last().second.get(), g_variant_builder_end(variantBuilder.get())); |
| 119 |
} |
| 120 |
|
| 121 |
void KeyedEncoder::endArray() |
| 122 |
{ |
| 123 |
g_variant_builder_add(m_variantBuilderStack.last(), "{sv}", m_arrayStack.last().first.utf8().data(), g_variant_builder_end(m_arrayStack.last().second.get())); |
| 124 |
m_arrayStack.removeLast(); |
| 125 |
} |
| 126 |
|
| 127 |
PassRefPtr<SharedBuffer> KeyedEncoder::finishEncoding() |
| 128 |
{ |
| 129 |
g_assert(m_variantBuilderStack.last() == &m_variantBuilder); |
| 130 |
GRefPtr<GVariant> variant = g_variant_builder_end(&m_variantBuilder); |
| 131 |
GRefPtr<GBytes> data = g_variant_get_data_as_bytes(variant.get()); |
| 132 |
return SharedBuffer::create(static_cast<const unsigned char*>(g_bytes_get_data(data.get(), nullptr)), static_cast<unsigned>(g_bytes_get_size(data.get()))); |
| 133 |
} |
| 134 |
|
| 135 |
} // namespace WebKit |