1 /*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebHistoryItem.h"
33
34 #include "WebSerializedScriptValue.h"
35 #include "bindings/v8/SerializedScriptValue.h"
36 #include "core/history/HistoryItem.h"
37 #include "core/html/forms/FormController.h"
38 #include "platform/network/FormData.h"
39 #include "platform/weborigin/KURL.h"
40 #include "public/platform/WebHTTPBody.h"
41 #include "public/platform/WebPoint.h"
42 #include "public/platform/WebString.h"
43 #include "public/platform/WebVector.h"
44 #include "wtf/text/StringHash.h"
45
46 using namespace WebCore;
47
48 namespace blink {
49 namespace {
50
addReferencedFilePaths(HistoryItem * item,HashSet<String> & results)51 void addReferencedFilePaths(HistoryItem* item, HashSet<String>& results)
52 {
53 const FormData* formData = item->formData();
54 if (formData) {
55 for (size_t i = 0; i < formData->elements().size(); ++i) {
56 const FormDataElement& element = formData->elements()[i];
57 if (element.m_type == FormDataElement::encodedFile)
58 results.add(element.m_filename);
59 }
60 }
61
62 const Vector<String>& filePaths = FormController::getReferencedFilePaths(item->documentState());
63 for (size_t i = 0; i < filePaths.size(); ++i)
64 results.add(filePaths[i]);
65
66 const HistoryItemVector& children = item->children();
67 for (size_t i = 0; i < children.size(); ++i)
68 addReferencedFilePaths(children[i].get(), results);
69 }
70
71 } // namespace
72
initialize()73 void WebHistoryItem::initialize()
74 {
75 m_private = HistoryItem::create();
76 }
77
reset()78 void WebHistoryItem::reset()
79 {
80 m_private.reset();
81 }
82
assign(const WebHistoryItem & other)83 void WebHistoryItem::assign(const WebHistoryItem& other)
84 {
85 m_private = other.m_private;
86 }
87
urlString() const88 WebString WebHistoryItem::urlString() const
89 {
90 return m_private->urlString();
91 }
92
setURLString(const WebString & url)93 void WebHistoryItem::setURLString(const WebString& url)
94 {
95 ensureMutable();
96 m_private->setURLString(KURL(ParsedURLString, url).string());
97 }
98
originalURLString() const99 WebString WebHistoryItem::originalURLString() const
100 {
101 return m_private->originalURLString();
102 }
103
setOriginalURLString(const WebString & originalURLString)104 void WebHistoryItem::setOriginalURLString(const WebString& originalURLString)
105 {
106 ensureMutable();
107 m_private->setOriginalURLString(originalURLString);
108 }
109
referrer() const110 WebString WebHistoryItem::referrer() const
111 {
112 return m_private->referrer();
113 }
114
setReferrer(const WebString & referrer)115 void WebHistoryItem::setReferrer(const WebString& referrer)
116 {
117 ensureMutable();
118 m_private->setReferrer(referrer);
119 }
120
target() const121 WebString WebHistoryItem::target() const
122 {
123 return m_private->target();
124 }
125
setTarget(const WebString & target)126 void WebHistoryItem::setTarget(const WebString& target)
127 {
128 ensureMutable();
129 m_private->setTarget(target);
130 }
131
scrollOffset() const132 WebPoint WebHistoryItem::scrollOffset() const
133 {
134 return m_private->scrollPoint();
135 }
136
setScrollOffset(const WebPoint & scrollOffset)137 void WebHistoryItem::setScrollOffset(const WebPoint& scrollOffset)
138 {
139 ensureMutable();
140 m_private->setScrollPoint(scrollOffset);
141 }
142
pageScaleFactor() const143 float WebHistoryItem::pageScaleFactor() const
144 {
145 return m_private->pageScaleFactor();
146 }
147
setPageScaleFactor(float scale)148 void WebHistoryItem::setPageScaleFactor(float scale)
149 {
150 ensureMutable();
151 m_private->setPageScaleFactor(scale);
152 }
153
documentState() const154 WebVector<WebString> WebHistoryItem::documentState() const
155 {
156 return m_private->documentState();
157 }
158
setDocumentState(const WebVector<WebString> & state)159 void WebHistoryItem::setDocumentState(const WebVector<WebString>& state)
160 {
161 ensureMutable();
162 // FIXME: would be nice to avoid the intermediate copy
163 Vector<String> ds;
164 for (size_t i = 0; i < state.size(); ++i)
165 ds.append(state[i]);
166 m_private->setDocumentState(ds);
167 }
168
itemSequenceNumber() const169 long long WebHistoryItem::itemSequenceNumber() const
170 {
171 return m_private->itemSequenceNumber();
172 }
173
setItemSequenceNumber(long long itemSequenceNumber)174 void WebHistoryItem::setItemSequenceNumber(long long itemSequenceNumber)
175 {
176 ensureMutable();
177 m_private->setItemSequenceNumber(itemSequenceNumber);
178 }
179
documentSequenceNumber() const180 long long WebHistoryItem::documentSequenceNumber() const
181 {
182 return m_private->documentSequenceNumber();
183 }
184
setDocumentSequenceNumber(long long documentSequenceNumber)185 void WebHistoryItem::setDocumentSequenceNumber(long long documentSequenceNumber)
186 {
187 ensureMutable();
188 m_private->setDocumentSequenceNumber(documentSequenceNumber);
189 }
190
targetFrameID() const191 long long WebHistoryItem::targetFrameID() const
192 {
193 return m_private->targetFrameID();
194 }
195
setTargetFrameID(long long targetFrameID)196 void WebHistoryItem::setTargetFrameID(long long targetFrameID)
197 {
198 ensureMutable();
199 m_private->setTargetFrameID(targetFrameID);
200 }
201
stateObject() const202 WebSerializedScriptValue WebHistoryItem::stateObject() const
203 {
204 return WebSerializedScriptValue(m_private->stateObject());
205 }
206
setStateObject(const WebSerializedScriptValue & object)207 void WebHistoryItem::setStateObject(const WebSerializedScriptValue& object)
208 {
209 ensureMutable();
210 m_private->setStateObject(object);
211 }
212
httpContentType() const213 WebString WebHistoryItem::httpContentType() const
214 {
215 return m_private->formContentType();
216 }
217
setHTTPContentType(const WebString & httpContentType)218 void WebHistoryItem::setHTTPContentType(const WebString& httpContentType)
219 {
220 ensureMutable();
221 m_private->setFormContentType(httpContentType);
222 }
223
httpBody() const224 WebHTTPBody WebHistoryItem::httpBody() const
225 {
226 return WebHTTPBody(m_private->formData());
227 }
228
setHTTPBody(const WebHTTPBody & httpBody)229 void WebHistoryItem::setHTTPBody(const WebHTTPBody& httpBody)
230 {
231 ensureMutable();
232 m_private->setFormData(httpBody);
233 }
234
children() const235 WebVector<WebHistoryItem> WebHistoryItem::children() const
236 {
237 return m_private->children();
238 }
239
setChildren(const WebVector<WebHistoryItem> & items)240 void WebHistoryItem::setChildren(const WebVector<WebHistoryItem>& items)
241 {
242 ensureMutable();
243 m_private->clearChildren();
244 for (size_t i = 0; i < items.size(); ++i)
245 m_private->addChildItem(items[i]);
246 }
247
appendToChildren(const WebHistoryItem & item)248 void WebHistoryItem::appendToChildren(const WebHistoryItem& item)
249 {
250 ensureMutable();
251 m_private->addChildItem(item);
252 }
253
getReferencedFilePaths() const254 WebVector<WebString> WebHistoryItem::getReferencedFilePaths() const
255 {
256 HashSet<String> filePaths;
257 addReferencedFilePaths(m_private.get(), filePaths);
258
259 Vector<String> results;
260 copyToVector(filePaths, results);
261 return results;
262 }
263
WebHistoryItem(const PassRefPtr<HistoryItem> & item)264 WebHistoryItem::WebHistoryItem(const PassRefPtr<HistoryItem>& item)
265 : m_private(item)
266 {
267 }
268
operator =(const PassRefPtr<HistoryItem> & item)269 WebHistoryItem& WebHistoryItem::operator=(const PassRefPtr<HistoryItem>& item)
270 {
271 m_private = item;
272 return *this;
273 }
274
operator PassRefPtr<HistoryItem>() const275 WebHistoryItem::operator PassRefPtr<HistoryItem>() const
276 {
277 return m_private.get();
278 }
279
ensureMutable()280 void WebHistoryItem::ensureMutable()
281 {
282 if (!m_private->hasOneRef())
283 m_private = m_private->copy();
284 }
285
286 } // namespace blink
287