Modernize some aspects of text codecs, eliminate WebKit use of strcasecmp
[WebKit-https.git] / Source / WebKitLegacy / mac / History / HistoryPropertyList.mm
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
26 #import "HistoryPropertyList.h"
27
28 #import "WebHistoryItemInternal.h"
29 #import <WebCore/HistoryItem.h>
30
31 using namespace WebCore;
32
33 static const int currentFileVersion = 1;
34
35 HistoryPropertyListWriter::HistoryPropertyListWriter()
36     : m_displayTitleKey("displayTitle")
37     , m_lastVisitWasFailureKey("lastVisitWasFailure")
38     , m_lastVisitedDateKey("lastVisitedDate")
39     , m_redirectURLsKey("redirectURLs")
40     , m_titleKey("title")
41     , m_urlKey("")
42     , m_buffer(0)
43 {
44 }
45
46 UInt8* HistoryPropertyListWriter::buffer(size_t size)
47 {
48     ASSERT(!m_buffer);
49     m_buffer = static_cast<UInt8*>(CFAllocatorAllocate(0, size, 0));
50     m_bufferSize = size;
51     return m_buffer;
52 }
53
54 RetainPtr<CFDataRef> HistoryPropertyListWriter::releaseData()
55 {
56     UInt8* buffer = m_buffer;
57     if (!buffer)
58         return 0;
59     m_buffer = 0;
60     RetainPtr<CFDataRef> data = adoptCF(CFDataCreateWithBytesNoCopy(0, buffer, m_bufferSize, 0));
61     if (!data) {
62         CFAllocatorDeallocate(0, buffer);
63         return 0;
64     }
65     return data;
66 }
67
68 void HistoryPropertyListWriter::writeObjects(BinaryPropertyListObjectStream& stream)
69 {
70     size_t outerDictionaryStart = stream.writeDictionaryStart();
71
72     stream.writeString("WebHistoryFileVersion");
73     stream.writeString("WebHistoryDates");
74
75     stream.writeInteger(currentFileVersion);
76     size_t outerDateArrayStart = stream.writeArrayStart();
77     writeHistoryItems(stream);
78     stream.writeArrayEnd(outerDateArrayStart);
79
80     stream.writeDictionaryEnd(outerDictionaryStart);
81 }
82
83 void HistoryPropertyListWriter::writeHistoryItem(BinaryPropertyListObjectStream& stream, WebHistoryItem* webHistoryItem)
84 {
85     HistoryItem* item = core(webHistoryItem);
86
87     size_t itemDictionaryStart = stream.writeDictionaryStart();
88
89     const String& title = item->title();
90     const String& displayTitle = item->alternateTitle();
91     double lastVisitedDate = webHistoryItem->_private->_lastVisitedTime;
92     Vector<String>* redirectURLs = webHistoryItem->_private->_redirectURLs.get();
93
94     // keys
95     stream.writeString(m_urlKey);
96     if (!title.isEmpty())
97         stream.writeString(m_titleKey);
98     if (!displayTitle.isEmpty())
99         stream.writeString(m_displayTitleKey);
100     if (lastVisitedDate)
101         stream.writeString(m_lastVisitedDateKey);
102     if (item->lastVisitWasFailure())
103         stream.writeString(m_lastVisitWasFailureKey);
104     if (redirectURLs)
105         stream.writeString(m_redirectURLsKey);
106
107     // values
108     stream.writeUniqueString(item->urlString());
109     if (!title.isEmpty())
110         stream.writeString(title);
111     if (!displayTitle.isEmpty())
112         stream.writeString(displayTitle);
113     if (lastVisitedDate) {
114         char buffer[32];
115         snprintf(buffer, sizeof(buffer), "%.1lf", lastVisitedDate);
116         stream.writeUniqueString(buffer);
117     }
118     if (item->lastVisitWasFailure())
119         stream.writeBooleanTrue();
120     if (redirectURLs) {
121         size_t redirectArrayStart = stream.writeArrayStart();
122         size_t size = redirectURLs->size();
123         ASSERT(size);
124         for (size_t i = 0; i < size; ++i)
125             stream.writeUniqueString(redirectURLs->at(i));
126         stream.writeArrayEnd(redirectArrayStart);
127     }
128
129     stream.writeDictionaryEnd(itemDictionaryStart);
130 }
131