KeyedEncoder should be able to encoder objects
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 9 Nov 2013 00:51:54 +0000 (00:51 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 9 Nov 2013 00:51:54 +0000 (00:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=124085

Reviewed by Sam Weinig.

Source/WebCore:

* history/HistoryItem.cpp:
(WebCore::HistoryItem::encodeBackForwardTree):
Encode the root object.

(WebCore::HistoryItem::encodeBackForwardTreeNode):
Encode the target.

* history/HistoryItem.h:
Add new members.

* platform/KeyedCoding.h:
(WebCore::KeyedEncoder::encodeObject):
Call beginObject, call the functor and then call endObject().

Source/WebKit2:

Add a dictionary stack to KeyedEncoder that's pushed and popped by
beginObject/endObject.

* Shared/cf/KeyedEncoder.cpp:
(WebKit::KeyedEncoder::KeyedEncoder):
(WebKit::KeyedEncoder::~KeyedEncoder):
(WebKit::KeyedEncoder::encodeUInt32):
(WebKit::KeyedEncoder::encodeString):
(WebKit::KeyedEncoder::beginObject):
(WebKit::KeyedEncoder::endObject):
* Shared/cf/KeyedEncoder.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158978 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/history/HistoryItem.cpp
Source/WebCore/history/HistoryItem.h
Source/WebCore/platform/KeyedCoding.h
Source/WebKit2/ChangeLog
Source/WebKit2/Shared/cf/KeyedEncoder.cpp
Source/WebKit2/Shared/cf/KeyedEncoder.h

index fa816e2..6a888f4 100644 (file)
@@ -1,3 +1,24 @@
+2013-11-08  Anders Carlsson  <andersca@apple.com>
+
+        KeyedEncoder should be able to encoder objects
+        https://bugs.webkit.org/show_bug.cgi?id=124085
+
+        Reviewed by Sam Weinig.
+
+        * history/HistoryItem.cpp:
+        (WebCore::HistoryItem::encodeBackForwardTree):
+        Encode the root object.
+
+        (WebCore::HistoryItem::encodeBackForwardTreeNode):
+        Encode the target.
+
+        * history/HistoryItem.h:
+        Add new members.
+
+        * platform/KeyedCoding.h:
+        (WebCore::KeyedEncoder::encodeObject):
+        Call beginObject, call the functor and then call endObject().
+
 2013-11-08  Sam Weinig  <sam@webkit.org>
 
         Teach CanvasObserver about references
index f1ea342..4b6a347 100644 (file)
@@ -687,7 +687,9 @@ void HistoryItem::encodeBackForwardTree(KeyedEncoder& encoder) const
 {
     encoder.encodeUInt32("version", backForwardTreeEncodingVersion);
 
-    // FIXME: Encode the tree.
+    encoder.encodeObject("root", *this, [](KeyedEncoder& encoder, const HistoryItem& item) {
+        item.encodeBackForwardTreeNode(encoder);
+    });
 }
 
 void HistoryItem::encodeBackForwardTreeNode(Encoder& encoder) const
@@ -733,6 +735,13 @@ void HistoryItem::encodeBackForwardTreeNode(Encoder& encoder) const
     encoder.encodeString(m_target);
 }
 
+void HistoryItem::encodeBackForwardTreeNode(KeyedEncoder& encoder) const
+{
+    // FIXME: Implement.
+
+    encoder.encodeString("target", m_target);
+}
+
 struct DecodeRecursionStackElement {
     RefPtr<HistoryItem> node;
     size_t i;
index 67ef41a..4511ece 100644 (file)
@@ -223,10 +223,7 @@ private:
     HistoryItem* findTargetItem();
 
     void encodeBackForwardTreeNode(Encoder&) const;
-
-    /* When adding new member variables to this class, please notify the Qt team.
-     * qt/HistoryItemQt.cpp contains code to serialize history items.
-     */
+    void encodeBackForwardTreeNode(KeyedEncoder&) const;
 
     String m_urlString;
     String m_originalURLString;
index c7da9cc..a2f3867 100644 (file)
@@ -36,6 +36,20 @@ protected:
 
 public:
     virtual void encodeUInt32(const String& key, uint32_t) = 0;
+
+    virtual void encodeString(const String& key, const String&) = 0;
+
+    template<typename T, typename F>
+    void encodeObject(const String& key, const T& object, F function)
+    {
+        this->beginObject(key);
+        function(*this, object);
+        this->endObject();
+    }
+
+private:
+    virtual void beginObject(const String& key) = 0;
+    virtual void endObject() = 0;
 };
 
 } // namespace WebCore
index b2c16e4..4ac1986 100644 (file)
@@ -1,3 +1,22 @@
+2013-11-08  Anders Carlsson  <andersca@apple.com>
+
+        KeyedEncoder should be able to encoder objects
+        https://bugs.webkit.org/show_bug.cgi?id=124085
+
+        Reviewed by Sam Weinig.
+
+        Add a dictionary stack to KeyedEncoder that's pushed and popped by
+        beginObject/endObject.
+
+        * Shared/cf/KeyedEncoder.cpp:
+        (WebKit::KeyedEncoder::KeyedEncoder):
+        (WebKit::KeyedEncoder::~KeyedEncoder):
+        (WebKit::KeyedEncoder::encodeUInt32):
+        (WebKit::KeyedEncoder::encodeString):
+        (WebKit::KeyedEncoder::beginObject):
+        (WebKit::KeyedEncoder::endObject):
+        * Shared/cf/KeyedEncoder.h:
+
 2013-11-08  Alexandru Chiculita  <achicu@adobe.com>
 
         Web Inspector: It should be possible to debug the Inspector code
index 73aa44c..438ff0f 100644 (file)
@@ -39,16 +39,37 @@ static RetainPtr<CFMutableDictionaryRef> createDictionary()
 KeyedEncoder::KeyedEncoder()
     : m_rootDictionary(createDictionary())
 {
+    m_dictionaryStack.append(m_rootDictionary.get());
 }
     
 KeyedEncoder::~KeyedEncoder()
 {
+    ASSERT(m_dictionaryStack.size() == 1);
+    ASSERT(m_dictionaryStack.last() == m_rootDictionary);
 }
 
 void KeyedEncoder::encodeUInt32(const String& key, uint32_t value)
 {
     RetainPtr<CFNumberRef> number = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value));
-    CFDictionarySetValue(m_rootDictionary.get(), key.createCFString().get(), number.get());
+    CFDictionarySetValue(m_dictionaryStack.last(), key.createCFString().get(), number.get());
+}
+
+void KeyedEncoder::encodeString(const String& key, const String& value)
+{
+    CFDictionarySetValue(m_dictionaryStack.last(), key.createCFString().get(), value.createCFString().get());
+}
+
+void KeyedEncoder::beginObject(const String& key)
+{
+    auto dictionary = createDictionary();
+    CFDictionarySetValue(m_dictionaryStack.last(), key.createCFString().get(), dictionary.get());
+
+    m_dictionaryStack.append(dictionary.get());
+}
+
+void KeyedEncoder::endObject()
+{
+    m_dictionaryStack.removeLast();
 }
 
 } // namespace WebKit
index 5e433ce..d399da2 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <WebCore/KeyedCoding.h>
 #include <wtf/RetainPtr.h>
+#include <wtf/Vector.h>
 
 namespace WebKit {
 
@@ -38,8 +39,13 @@ public:
 
 private:
     virtual void encodeUInt32(const String& key, uint32_t) OVERRIDE;
+    virtual void encodeString(const String& key, const String&) OVERRIDE;
+
+    virtual void beginObject(const String& key) OVERRIDE;
+    virtual void endObject() OVERRIDE;
 
     RetainPtr<CFMutableDictionaryRef> m_rootDictionary;
+    Vector<CFMutableDictionaryRef, 16> m_dictionaryStack;
 };
 
 } // namespace WebKit