[WIN] Make WebHistory compile without USE(CF)
https://bugs.webkit.org/show_bug.cgi?id=122010

Reviewed by Brent Fulgham.

Add an alternative code path for !USE(CF) to be able to
compile WebHistory without the CoreFoundation library.

* COMVariantSetter.h: Add support for setting a Vector.
* WebHistory.cpp:
(createUserInfoFromArray):
(createUserInfoFromHistoryItem):
(WebHistory::removeAllItems):
(WebHistory::removeItem):
(WebHistory::addItem):
(WebHistory::visitedURL):
* WebHistory.h:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@158370 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/win/COMVariantSetter.h b/Source/WebKit/win/COMVariantSetter.h
index 8ce6542..0f1ab1d 100644
--- a/Source/WebKit/win/COMVariantSetter.h
+++ b/Source/WebKit/win/COMVariantSetter.h
@@ -30,6 +30,7 @@
 #include <WebCore/COMPtr.h>
 #include <wtf/Assertions.h>
 #include <wtf/Forward.h>
+#include <wtf/Vector.h>
 
 template<typename T> struct COMVariantSetter {};
 
@@ -120,6 +121,24 @@
     }
 };
 
+template<typename T> struct COMVariantSetter<Vector<T>> : COMVariantSetterBase<Vector<T>> {
+    static const VARTYPE VariantType = VT_ARRAY | COMVariantSetter<T>::VariantType;
+
+    static void setVariant(VARIANT* variant, const Vector<T>& value)
+    {
+        ASSERT(V_VT(variant) == VT_EMPTY);
+
+        SAFEARRAY* safeArray = ::SafeArrayCreateVector(COMVariantSetter<T>::VariantType, 0, value.size());
+        for (LONG i = 0; i < value.size(); ++i) {
+            COMVariant item(value[i]);
+            ::SafeArrayPutElement(safeArray, &i, &item);
+        }
+
+        V_VT(variant) = VariantType;
+        V_ARRAY(variant) = safeArray;
+    }
+};
+
 template<typename COMType, typename UnderlyingType>
 struct COMIUnknownVariantSetter : COMVariantSetterBase<UnderlyingType>
 {
diff --git a/Source/WebKit/win/ChangeLog b/Source/WebKit/win/ChangeLog
index f0677e8..dc26435 100644
--- a/Source/WebKit/win/ChangeLog
+++ b/Source/WebKit/win/ChangeLog
@@ -1,3 +1,23 @@
+2013-10-31  Patrick Gansterer  <paroga@webkit.org>
+
+        [WIN] Make WebHistory compile without USE(CF)
+        https://bugs.webkit.org/show_bug.cgi?id=122010
+
+        Reviewed by Brent Fulgham.
+
+        Add an alternative code path for !USE(CF) to be able to
+        compile WebHistory without the CoreFoundation library.
+
+        * COMVariantSetter.h: Add support for setting a Vector.
+        * WebHistory.cpp:
+        (createUserInfoFromArray):
+        (createUserInfoFromHistoryItem):
+        (WebHistory::removeAllItems):
+        (WebHistory::removeItem):
+        (WebHistory::addItem):
+        (WebHistory::visitedURL):
+        * WebHistory.h:
+
 2013-10-30  Myles C. Maxfield  <mmaxfield@apple.com>
 
         WebKit/win/WebKitGraphics.h:void WebDrawText(WebTextRenderInfo*); is never called
diff --git a/Source/WebKit/win/WebHistory.cpp b/Source/WebKit/win/WebHistory.cpp
index 7e63707..bbddfa9 100644
--- a/Source/WebKit/win/WebHistory.cpp
+++ b/Source/WebKit/win/WebHistory.cpp
@@ -27,7 +27,6 @@
 #include "WebKitDLL.h"
 #include "WebHistory.h"
 
-#include "CFDictionaryPropertyBag.h"
 #include "MemoryStream.h"
 #include "WebKit.h"
 #include "MarshallingHelpers.h"
@@ -35,7 +34,6 @@
 #include "WebKit.h"
 #include "WebNotificationCenter.h"
 #include "WebPreferences.h"
-#include <CoreFoundation/CoreFoundation.h>
 #include <WebCore/BString.h>
 #include <WebCore/HistoryItem.h>
 #include <WebCore/URL.h>
@@ -46,6 +44,13 @@
 #include <wtf/StdLibExtras.h>
 #include <wtf/Vector.h>
 
+#if USE(CF)
+#include "CFDictionaryPropertyBag.h"
+#include <CoreFoundation/CoreFoundation.h>
+#else
+#include "COMPropertyBag.h"
+#endif
+
 using namespace WebCore;
 using namespace std;
 
@@ -55,25 +60,36 @@
     return (diff < .000001 && diff > -.000001);
 }
 
-static COMPtr<CFDictionaryPropertyBag> createUserInfoFromArray(BSTR notificationStr, CFArrayRef arrayItem)
+static COMPtr<IPropertyBag> createUserInfoFromArray(BSTR notificationStr, IWebHistoryItem** data, size_t size)
 {
+#if USE(CF)
+    RetainPtr<CFArrayRef> arrayItem = adoptCF(CFArrayCreate(kCFAllocatorDefault, (const void**)data, size, &MarshallingHelpers::kIUnknownArrayCallBacks));
+
     RetainPtr<CFMutableDictionaryRef> dictionary = adoptCF(
         CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
 
     RetainPtr<CFStringRef> key = adoptCF(MarshallingHelpers::BSTRToCFStringRef(notificationStr));
-    CFDictionaryAddValue(dictionary.get(), key.get(), arrayItem);
+    CFDictionaryAddValue(dictionary.get(), key.get(), arrayItem.get());
 
     COMPtr<CFDictionaryPropertyBag> result = CFDictionaryPropertyBag::createInstance();
     result->setDictionary(dictionary.get());
-    return result;
+    return COMPtr<IPropertyBag>(AdoptCOM, result.leakRef());
+#else
+    Vector<COMPtr<IWebHistoryItem>, 1> arrayItem;
+    arrayItem.reserveInitialCapacity(size);
+    for (size_t i = 0; i < size; ++i)
+        arrayItem[i] = data[i];
+
+    HashMap<String, Vector<COMPtr<IWebHistoryItem>>> dictionary;
+    String key(notificationStr, ::SysStringLen(notificationStr));
+    dictionary.set(key, std::move(arrayItem));
+    return COMPtr<IPropertyBag>(AdoptCOM, COMPropertyBag<Vector<COMPtr<IWebHistoryItem>>>::adopt(dictionary));
+#endif
 }
 
-static COMPtr<CFDictionaryPropertyBag> createUserInfoFromHistoryItem(BSTR notificationStr, IWebHistoryItem* item)
+static inline COMPtr<IPropertyBag> createUserInfoFromHistoryItem(BSTR notificationStr, IWebHistoryItem* item)
 {
-    // reference counting of item added to the array is managed by the CFArray value callbacks
-    RetainPtr<CFArrayRef> itemList = adoptCF(CFArrayCreate(0, (const void**) &item, 1, &MarshallingHelpers::kIUnknownArrayCallBacks));
-    COMPtr<CFDictionaryPropertyBag> info = createUserInfoFromArray(notificationStr, itemList.get());
-    return info;
+    return createUserInfoFromArray(notificationStr, &item, 1);
 }
 
 static inline void addDayToSystemTime(SYSTEMTIME& systemTime)
@@ -306,13 +322,12 @@
     itemsVector.reserveInitialCapacity(m_entriesByURL.size());
     for (auto it = m_entriesByURL.begin(); it != m_entriesByURL.end(); ++it)
         itemsVector.append(it->value.get());
-    RetainPtr<CFArrayRef> allItems = adoptCF(CFArrayCreate(kCFAllocatorDefault, (const void**)itemsVector.data(), itemsVector.size(), &MarshallingHelpers::kIUnknownArrayCallBacks));
+    COMPtr<IPropertyBag> userInfo = createUserInfoFromArray(getNotificationString(kWebHistoryAllItemsRemovedNotification), itemsVector.data(), itemsVector.size());
 
     m_entriesByURL.clear();
 
     PageGroup::removeAllVisitedLinks();
 
-    COMPtr<CFDictionaryPropertyBag> userInfo = createUserInfoFromArray(getNotificationString(kWebHistoryAllItemsRemovedNotification), allItems.get());
     return postNotification(kWebHistoryAllItemsRemovedNotification, userInfo.get());
 }
 
@@ -472,7 +487,7 @@
     if (FAILED(hr))
         return hr;
 
-    COMPtr<CFDictionaryPropertyBag> userInfo = createUserInfoFromHistoryItem(
+    COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
         getNotificationString(kWebHistoryItemsRemovedNotification), entry);
     hr = postNotification(kWebHistoryItemsRemovedNotification, userInfo.get());
 
@@ -520,7 +535,7 @@
 
     m_entriesByURL.set(urlString, entry);
 
-    COMPtr<CFDictionaryPropertyBag> userInfo = createUserInfoFromHistoryItem(
+    COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
         getNotificationString(kWebHistoryItemsAddedNotification), entry);
     hr = postNotification(kWebHistoryItemsAddedNotification, userInfo.get());
 
@@ -576,7 +591,7 @@
     COMPtr<WebHistoryItem> item(Query, entry);
     item->historyItem()->setRedirectURLs(nullptr);
 
-    COMPtr<CFDictionaryPropertyBag> userInfo = createUserInfoFromHistoryItem(
+    COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
         getNotificationString(kWebHistoryItemsAddedNotification), entry);
     postNotification(kWebHistoryItemsAddedNotification, userInfo.get());
 }
diff --git a/Source/WebKit/win/WebHistory.h b/Source/WebKit/win/WebHistory.h
index ff777cf..208f73b 100644
--- a/Source/WebKit/win/WebHistory.h
+++ b/Source/WebKit/win/WebHistory.h
@@ -27,11 +27,9 @@
 #define WebHistory_H
 
 #include "WebKit.h"
-#include <CoreFoundation/CoreFoundation.h>
 #include <WebCore/COMPtr.h>
 #include <memory>
 #include <wtf/Forward.h>
-#include <wtf/RetainPtr.h>
 
 namespace WebCore {
     class URL;