<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[170599] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/170599">170599</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2014-06-30 14:53:58 -0700 (Mon, 30 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add code for encoding legacy session history entries
https://bugs.webkit.org/show_bug.cgi?id=134452

Reviewed by Andreas Kling.

Source/WebKit2:
* UIProcess/mac/LegacySessionStateCoding.cpp:
(WebKit::HistoryEntryDataEncoder::HistoryEntryDataEncoder):
(WebKit::HistoryEntryDataEncoder::operator&lt;&lt;):
(WebKit::HistoryEntryDataEncoder::finishEncoding):
(WebKit::HistoryEntryDataEncoder::encodeArithmeticType):
(WebKit::HistoryEntryDataEncoder::encodeFixedLengthData):
(WebKit::HistoryEntryDataEncoder::grow):
(WebKit::HistoryEntryDataEncoder::growCapacity):
(WebKit::encodeFrameStateNode):
(WebKit::encodeLegacySessionHistoryEntryData):
(WebKit::decodeSessionHistoryEntryData):
(WebKit::decodeLegacySessionHistoryEntryData):
* UIProcess/mac/LegacySessionStateCoding.h:

Source/WTF:
* wtf/MallocPtr.h:
(WTF::MallocPtr::malloc):
(WTF::MallocPtr::realloc):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfMallocPtrh">trunk/Source/WTF/wtf/MallocPtr.h</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacLegacySessionStateCodingcpp">trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacLegacySessionStateCodingh">trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (170598 => 170599)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2014-06-30 21:25:33 UTC (rev 170598)
+++ trunk/Source/WTF/ChangeLog        2014-06-30 21:53:58 UTC (rev 170599)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2014-06-30  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        Add code for encoding legacy session history entries
+        https://bugs.webkit.org/show_bug.cgi?id=134452
+
+        Reviewed by Andreas Kling.
+
+        * wtf/MallocPtr.h:
+        (WTF::MallocPtr::malloc):
+        (WTF::MallocPtr::realloc):
+
</ins><span class="cx"> 2014-06-27  Andreas Kling  &lt;akling@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [ARMv7] Skip one particularly expensive fastFree() alignment check.
</span></span></pre></div>
<a id="trunkSourceWTFwtfMallocPtrh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/MallocPtr.h (170598 => 170599)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MallocPtr.h        2014-06-30 21:25:33 UTC (rev 170598)
+++ trunk/Source/WTF/wtf/MallocPtr.h        2014-06-30 21:53:58 UTC (rev 170599)
</span><span class="lines">@@ -86,6 +86,19 @@
</span><span class="cx"> 
</span><span class="cx">     template&lt;typename U&gt; friend MallocPtr&lt;U&gt; adoptMallocPtr(U*);
</span><span class="cx"> 
</span><ins>+    static MallocPtr malloc(size_t size)
+    {
+        MallocPtr mallocPtr;
+        mallocPtr.m_ptr = static_cast&lt;T*&gt;(fastMalloc(size));
+
+        return mallocPtr;
+    }
+
+    void realloc(size_t newSize)
+    {
+        m_ptr = static_cast&lt;T*&gt;(fastRealloc(m_ptr, newSize));
+    }
+
</ins><span class="cx"> private:
</span><span class="cx">     explicit MallocPtr(T* ptr)
</span><span class="cx">         : m_ptr(ptr)
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (170598 => 170599)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2014-06-30 21:25:33 UTC (rev 170598)
+++ trunk/Source/WebKit2/ChangeLog        2014-06-30 21:53:58 UTC (rev 170599)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2014-06-30  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        Add code for encoding legacy session history entries
+        https://bugs.webkit.org/show_bug.cgi?id=134452
+
+        Reviewed by Andreas Kling.
+
+        * UIProcess/mac/LegacySessionStateCoding.cpp:
+        (WebKit::HistoryEntryDataEncoder::HistoryEntryDataEncoder):
+        (WebKit::HistoryEntryDataEncoder::operator&lt;&lt;):
+        (WebKit::HistoryEntryDataEncoder::finishEncoding):
+        (WebKit::HistoryEntryDataEncoder::encodeArithmeticType):
+        (WebKit::HistoryEntryDataEncoder::encodeFixedLengthData):
+        (WebKit::HistoryEntryDataEncoder::grow):
+        (WebKit::HistoryEntryDataEncoder::growCapacity):
+        (WebKit::encodeFrameStateNode):
+        (WebKit::encodeLegacySessionHistoryEntryData):
+        (WebKit::decodeSessionHistoryEntryData):
+        (WebKit::decodeLegacySessionHistoryEntryData):
+        * UIProcess/mac/LegacySessionStateCoding.h:
+
</ins><span class="cx"> 2014-06-29  Yoav Weiss  &lt;yoav@yoav.ws&gt;
</span><span class="cx"> 
</span><span class="cx">         Add support for HTMLImageElement's sizes attribute
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacLegacySessionStateCodingcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.cpp (170598 => 170599)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.cpp        2014-06-30 21:25:33 UTC (rev 170598)
+++ trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.cpp        2014-06-30 21:53:58 UTC (rev 170599)
</span><span class="lines">@@ -28,7 +28,9 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;APIData.h&quot;
</span><span class="cx"> #include &quot;SessionState.h&quot;
</span><ins>+#include &lt;wtf/MallocPtr.h&gt;
</ins><span class="cx"> #include &lt;wtf/cf/TypeCasts.h&gt;
</span><ins>+#include &lt;wtf/text/StringView.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="lines">@@ -55,6 +57,233 @@
</span><span class="cx"> 
</span><span class="cx"> template&lt;typename T&gt; void isValidEnum(T);
</span><span class="cx"> 
</span><ins>+class HistoryEntryDataEncoder {
+public:
+    HistoryEntryDataEncoder()
+        : m_bufferSize(0)
+        , m_bufferCapacity(512)
+        , m_buffer(MallocPtr&lt;uint8_t&gt;::malloc(m_bufferCapacity))
+        , m_bufferPointer(m_buffer.get())
+    {
+        // Keep format compatibility by encoding an unused uint64_t here.
+        *this &lt;&lt; static_cast&lt;uint64_t&gt;(0);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(uint32_t value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(int32_t value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(uint64_t value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(int64_t value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(float value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(double value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(bool value)
+    {
+        return encodeArithmeticType(value);
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(const String&amp; value)
+    {
+        // Special case the null string.
+        if (value.isNull())
+            return *this &lt;&lt; std::numeric_limits&lt;uint32_t&gt;::max();
+
+        uint32_t length = value.length();
+        *this &lt;&lt; length;
+
+        *this &lt;&lt; static_cast&lt;uint64_t&gt;(length * sizeof(UChar));
+        encodeFixedLengthData(reinterpret_cast&lt;const uint8_t*&gt;(StringView(value).upconvertedCharacters().get()), length * sizeof(UChar), alignof(UChar));
+
+        return *this;
+    }
+
+#if PLATFORM(IOS)
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(WebCore::FloatRect value)
+    {
+        *this &lt;&lt; value.x();
+        *this &lt;&lt; value.y();
+        *this &lt;&lt; value.width();
+        *this &lt;&lt; value.height();
+
+        return *this;
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(WebCore::IntRect value)
+    {
+        *this &lt;&lt; value.x();
+        *this &lt;&lt; value.y();
+        *this &lt;&lt; value.width();
+        *this &lt;&lt; value.height();
+
+        return *this;
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(WebCore::FloatSize value)
+    {
+        *this &lt;&lt; value.width();
+        *this &lt;&lt; value.height();
+
+        return *this;
+    }
+
+    HistoryEntryDataEncoder&amp; operator&lt;&lt;(WebCore::IntSize value)
+    {
+        *this &lt;&lt; value.width();
+        *this &lt;&lt; value.height();
+
+        return *this;
+    }
+#endif
+
+    MallocPtr&lt;uint8_t&gt; finishEncoding(size_t&amp; size)
+    {
+        size = m_bufferSize;
+        return std::move(m_buffer);
+    }
+
+private:
+    template&lt;typename Type&gt;
+    HistoryEntryDataEncoder&amp; encodeArithmeticType(Type value)
+    {
+        static_assert(std::is_arithmetic&lt;Type&gt;::value, &quot;&quot;);
+
+        encodeFixedLengthData(reinterpret_cast&lt;uint8_t*&gt;(&amp;value), sizeof(value), sizeof(value));
+        return *this;
+    }
+
+    void encodeFixedLengthData(const uint8_t* data, size_t size, unsigned alignment)
+    {
+        ASSERT(!(reinterpret_cast&lt;uintptr_t&gt;(data) % alignment));
+
+        uint8_t* buffer = grow(alignment, size);
+        memcpy(buffer, data, size);
+    }
+
+    uint8_t* grow(unsigned alignment, size_t size)
+    {
+        size_t alignedSize = ((m_bufferSize + alignment - 1) / alignment) * alignment;
+
+        growCapacity(alignedSize + size);
+
+        m_bufferSize = alignedSize + size;
+        m_bufferPointer = m_buffer.get() + m_bufferSize;
+
+        return m_buffer.get() + alignedSize;
+    }
+
+    void growCapacity(size_t newSize)
+    {
+        if (newSize &lt;= m_bufferCapacity)
+            return;
+
+        size_t newCapacity = m_bufferCapacity * 2;
+        while (newCapacity &lt; newSize)
+            newCapacity *= 2;
+
+        m_buffer.realloc(newCapacity);
+        m_bufferCapacity = newCapacity;
+    }
+
+    size_t m_bufferSize;
+    size_t m_bufferCapacity;
+    MallocPtr&lt;uint8_t&gt; m_buffer;
+    uint8_t* m_bufferPointer;
+};
+
+static void encodeFrameStateNode(HistoryEntryDataEncoder&amp; encoder, const FrameState&amp; frameState)
+{
+    encoder &lt;&lt; static_cast&lt;uint64_t&gt;(frameState.children.size());
+
+    for (const auto&amp; childFrameState : frameState.children) {
+        // FIXME: Implement.
+        (void)childFrameState;
+        ASSERT_NOT_REACHED();
+    }
+
+    encoder &lt;&lt; frameState.documentSequenceNumber;
+
+    encoder &lt;&lt; static_cast&lt;uint64_t&gt;(frameState.documentState.size());
+    for (const auto&amp; documentState : frameState.documentState) {
+        // FIXME: Implement.
+        (void)documentState;
+        ASSERT_NOT_REACHED();
+    }
+
+    encoder &lt;&lt; !!frameState.httpBody;
+    if (frameState.httpBody) {
+        // FIXME: Implement.
+        ASSERT_NOT_REACHED();
+    }
+
+    encoder &lt;&lt; frameState.itemSequenceNumber;
+
+    encoder &lt;&lt; frameState.referrer;
+
+    encoder &lt;&lt; frameState.scrollPoint.x();
+    encoder &lt;&lt; frameState.scrollPoint.y();
+
+    encoder &lt;&lt; frameState.pageScaleFactor;
+
+    encoder &lt;&lt; !!frameState.stateObjectData;
+    if (frameState.stateObjectData) {
+        // FIXME: Implement.
+        ASSERT_NOT_REACHED();
+    }
+
+    encoder &lt;&lt; frameState.target;
+
+#if PLATFORM(IOS)
+    // FIXME: iOS should not use the legacy session state encoder.
+    encoder &lt;&lt; frameState.exposedContentRect;
+    encoder &lt;&lt; frameState.unobscuredContentRect;
+    encoder &lt;&lt; frameState.minimumLayoutSizeInScrollViewCoordinates;
+    encoder &lt;&lt; frameState.contentSize;
+    encoder &lt;&lt; frameState.scaleIsInitial;
+#endif
+}
+
+static MallocPtr&lt;uint8_t&gt; encodeLegacySessionHistoryEntryData(const FrameState&amp; frameState, size_t&amp; bufferSize)
+{
+    HistoryEntryDataEncoder encoder;
+
+    encoder &lt;&lt; sessionHistoryEntryDataVersion;
+    encodeFrameStateNode(encoder, frameState);
+
+    return encoder.finishEncoding(bufferSize);
+}
+
+RefPtr&lt;API::Data&gt; encodeLegacySessionHistoryEntryData(const FrameState&amp; frameState)
+{
+    size_t bufferSize;
+    auto buffer = encodeLegacySessionHistoryEntryData(frameState, bufferSize);
+
+    return API::Data::createWithoutCopying(buffer.leakPtr(), bufferSize, [] (unsigned char* buffer, const void* context) {
+        fastFree(buffer);
+    }, nullptr);
+}
+
</ins><span class="cx"> class HistoryEntryDataDecoder {
</span><span class="cx"> public:
</span><span class="cx">     HistoryEntryDataDecoder(const uint8_t* buffer, size_t bufferSize)
</span><span class="lines">@@ -496,9 +725,9 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static bool decodeSessionHistoryEntryData(CFDataRef historyEntryData, FrameState&amp; mainFrameState)
</del><ins>+static bool decodeSessionHistoryEntryData(const uint8_t* buffer, size_t bufferSize, FrameState&amp; mainFrameState)
</ins><span class="cx"> {
</span><del>-    HistoryEntryDataDecoder decoder { CFDataGetBytePtr(historyEntryData), static_cast&lt;size_t&gt;(CFDataGetLength(historyEntryData)) };
</del><ins>+    HistoryEntryDataDecoder decoder { buffer, bufferSize };
</ins><span class="cx"> 
</span><span class="cx">     uint32_t version;
</span><span class="cx">     decoder &gt;&gt; version;
</span><span class="lines">@@ -511,6 +740,11 @@
</span><span class="cx">     return decoder.finishDecoding();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool decodeSessionHistoryEntryData(CFDataRef historyEntryData, FrameState&amp; mainFrameState)
+{
+    return decodeSessionHistoryEntryData(CFDataGetBytePtr(historyEntryData), static_cast&lt;size_t&gt;(CFDataGetLength(historyEntryData)), mainFrameState);
+}
+
</ins><span class="cx"> static bool decodeSessionHistoryEntry(CFDictionaryRef entryDictionary, PageState&amp; pageState)
</span><span class="cx"> {
</span><span class="cx">     auto title = dynamic_cf_cast&lt;CFStringRef&gt;(CFDictionaryGetValue(entryDictionary, sessionHistoryEntryTitleKey));
</span><span class="lines">@@ -672,4 +906,9 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool decodeLegacySessionHistoryEntryData(const API::Data&amp; historyEntryData, FrameState&amp; mainFrameState)
+{
+    return decodeSessionHistoryEntryData(historyEntryData.bytes(), historyEntryData.size(), mainFrameState);
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacLegacySessionStateCodingh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.h (170598 => 170599)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.h        2014-06-30 21:25:33 UTC (rev 170598)
+++ trunk/Source/WebKit2/UIProcess/mac/LegacySessionStateCoding.h        2014-06-30 21:53:58 UTC (rev 170599)
</span><span class="lines">@@ -34,9 +34,13 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><ins>+struct FrameState;
</ins><span class="cx"> struct SessionState;
</span><span class="cx"> 
</span><ins>+RefPtr&lt;API::Data&gt; encodeLegacySessionHistoryEntryData(const FrameState&amp;);
+
</ins><span class="cx"> bool decodeLegacySessionState(const API::Data&amp;, SessionState&amp;);
</span><ins>+bool decodeLegacySessionHistoryEntryData(const API::Data&amp;, FrameState&amp;);
</ins><span class="cx"> 
</span><span class="cx"> } // namespace WebKit
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>