HistoryPropertyList.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  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. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "HistoryPropertyList.h"
  26. #include <WebCore/HistoryItem.h>
  27. #include <wtf/StringExtras.h>
  28. using namespace WebCore;
  29. static const int currentFileVersion = 1;
  30. HistoryPropertyListWriter::HistoryPropertyListWriter()
  31. : m_dailyVisitCountsKey("D")
  32. , m_displayTitleKey("displayTitle")
  33. , m_lastVisitWasFailureKey("lastVisitWasFailure")
  34. , m_lastVisitWasHTTPNonGetKey("lastVisitWasHTTPNonGet")
  35. , m_lastVisitedDateKey("lastVisitedDate")
  36. , m_redirectURLsKey("redirectURLs")
  37. , m_titleKey("title")
  38. , m_urlKey("")
  39. , m_visitCountKey("visitCount")
  40. , m_weeklyVisitCountsKey("W")
  41. , m_buffer(0)
  42. {
  43. }
  44. UInt8* HistoryPropertyListWriter::buffer(size_t size)
  45. {
  46. ASSERT(!m_buffer);
  47. m_buffer = static_cast<UInt8*>(CFAllocatorAllocate(0, size, 0));
  48. m_bufferSize = size;
  49. return m_buffer;
  50. }
  51. RetainPtr<CFDataRef> HistoryPropertyListWriter::releaseData()
  52. {
  53. UInt8* buffer = m_buffer;
  54. if (!buffer)
  55. return 0;
  56. m_buffer = 0;
  57. RetainPtr<CFDataRef> data = adoptCF(CFDataCreateWithBytesNoCopy(0, buffer, m_bufferSize, 0));
  58. if (!data) {
  59. CFAllocatorDeallocate(0, buffer);
  60. return 0;
  61. }
  62. return data;
  63. }
  64. void HistoryPropertyListWriter::writeObjects(BinaryPropertyListObjectStream& stream)
  65. {
  66. size_t outerDictionaryStart = stream.writeDictionaryStart();
  67. stream.writeString("WebHistoryFileVersion");
  68. stream.writeString("WebHistoryDates");
  69. stream.writeInteger(currentFileVersion);
  70. size_t outerDateArrayStart = stream.writeArrayStart();
  71. writeHistoryItems(stream);
  72. stream.writeArrayEnd(outerDateArrayStart);
  73. stream.writeDictionaryEnd(outerDictionaryStart);
  74. }
  75. void HistoryPropertyListWriter::writeHistoryItem(BinaryPropertyListObjectStream& stream, HistoryItem* item)
  76. {
  77. size_t itemDictionaryStart = stream.writeDictionaryStart();
  78. const String& title = item->title();
  79. const String& displayTitle = item->alternateTitle();
  80. double lastVisitedDate = item->lastVisitedTime();
  81. int visitCount = item->visitCount();
  82. Vector<String>* redirectURLs = item->redirectURLs();
  83. const Vector<int>& dailyVisitCounts = item->dailyVisitCounts();
  84. const Vector<int>& weeklyVisitCounts = item->weeklyVisitCounts();
  85. // keys
  86. stream.writeString(m_urlKey);
  87. if (!title.isEmpty())
  88. stream.writeString(m_titleKey);
  89. if (!displayTitle.isEmpty())
  90. stream.writeString(m_displayTitleKey);
  91. if (lastVisitedDate)
  92. stream.writeString(m_lastVisitedDateKey);
  93. if (visitCount)
  94. stream.writeString(m_visitCountKey);
  95. if (item->lastVisitWasFailure())
  96. stream.writeString(m_lastVisitWasFailureKey);
  97. if (item->lastVisitWasHTTPNonGet())
  98. stream.writeString(m_lastVisitWasHTTPNonGetKey);
  99. if (redirectURLs)
  100. stream.writeString(m_redirectURLsKey);
  101. if (!dailyVisitCounts.isEmpty())
  102. stream.writeString(m_dailyVisitCountsKey);
  103. if (!weeklyVisitCounts.isEmpty())
  104. stream.writeString(m_weeklyVisitCountsKey);
  105. // values
  106. stream.writeUniqueString(item->urlString());
  107. if (!title.isEmpty())
  108. stream.writeString(title);
  109. if (!displayTitle.isEmpty())
  110. stream.writeString(displayTitle);
  111. if (lastVisitedDate) {
  112. char buffer[32];
  113. snprintf(buffer, sizeof(buffer), "%.1lf", lastVisitedDate);
  114. stream.writeUniqueString(buffer);
  115. }
  116. if (visitCount)
  117. stream.writeInteger(visitCount);
  118. if (item->lastVisitWasFailure())
  119. stream.writeBooleanTrue();
  120. if (item->lastVisitWasHTTPNonGet()) {
  121. ASSERT(item->urlString().startsWith("http:", false) || item->urlString().startsWith("https:", false));
  122. stream.writeBooleanTrue();
  123. }
  124. if (redirectURLs) {
  125. size_t redirectArrayStart = stream.writeArrayStart();
  126. size_t size = redirectURLs->size();
  127. ASSERT(size);
  128. for (size_t i = 0; i < size; ++i)
  129. stream.writeUniqueString(redirectURLs->at(i));
  130. stream.writeArrayEnd(redirectArrayStart);
  131. }
  132. if (size_t size = dailyVisitCounts.size())
  133. stream.writeIntegerArray(dailyVisitCounts.data(), size);
  134. if (size_t size = weeklyVisitCounts.size())
  135. stream.writeIntegerArray(weeklyVisitCounts.data(), size);
  136. stream.writeDictionaryEnd(itemDictionaryStart);
  137. }