Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / HistoryItem.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2008, 2011 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 COMPUTER, 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 COMPUTER, 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 #include "config.h"
27 #include "core/loader/HistoryItem.h"
28
29 #include "core/dom/Document.h"
30 #include "platform/network/ResourceRequest.h"
31 #include "wtf/CurrentTime.h"
32 #include "wtf/text/CString.h"
33
34 namespace WebCore {
35
36 static long long generateSequenceNumber()
37 {
38     // Initialize to the current time to reduce the likelihood of generating
39     // identifiers that overlap with those from past/future browser sessions.
40     static long long next = static_cast<long long>(currentTime() * 1000000.0);
41     return ++next;
42 }
43
44 HistoryItem::HistoryItem()
45     : m_pageScaleFactor(0)
46     , m_itemSequenceNumber(generateSequenceNumber())
47     , m_documentSequenceNumber(generateSequenceNumber())
48     , m_targetFrameID(0)
49 {
50 }
51
52 HistoryItem::~HistoryItem()
53 {
54 }
55
56 inline HistoryItem::HistoryItem(const HistoryItem& item)
57     : RefCounted<HistoryItem>()
58     , m_urlString(item.m_urlString)
59     , m_originalURLString(item.m_originalURLString)
60     , m_referrer(item.m_referrer)
61     , m_target(item.m_target)
62     , m_scrollPoint(item.m_scrollPoint)
63     , m_pageScaleFactor(item.m_pageScaleFactor)
64     , m_documentState(item.m_documentState)
65     , m_itemSequenceNumber(item.m_itemSequenceNumber)
66     , m_documentSequenceNumber(item.m_documentSequenceNumber)
67     , m_targetFrameID(item.m_targetFrameID)
68     , m_stateObject(item.m_stateObject)
69     , m_formContentType(item.m_formContentType)
70 {
71     if (item.m_formData)
72         m_formData = item.m_formData->copy();
73
74     unsigned size = item.m_children.size();
75     m_children.reserveInitialCapacity(size);
76     for (unsigned i = 0; i < size; ++i)
77         m_children.uncheckedAppend(item.m_children[i]->copy());
78 }
79
80 PassRefPtr<HistoryItem> HistoryItem::copy() const
81 {
82     return adoptRef(new HistoryItem(*this));
83 }
84
85 void HistoryItem::generateNewSequenceNumbers()
86 {
87     m_itemSequenceNumber = generateSequenceNumber();
88     m_documentSequenceNumber = generateSequenceNumber();
89 }
90
91 const String& HistoryItem::urlString() const
92 {
93     return m_urlString;
94 }
95
96 // The first URL we loaded to get to where this history item points. Includes both client
97 // and server redirects.
98 const String& HistoryItem::originalURLString() const
99 {
100     return m_originalURLString;
101 }
102
103 KURL HistoryItem::url() const
104 {
105     return KURL(ParsedURLString, m_urlString);
106 }
107
108 KURL HistoryItem::originalURL() const
109 {
110     return KURL(ParsedURLString, m_originalURLString);
111 }
112
113 const Referrer& HistoryItem::referrer() const
114 {
115     return m_referrer;
116 }
117
118 const String& HistoryItem::target() const
119 {
120     return m_target;
121 }
122
123 void HistoryItem::setURLString(const String& urlString)
124 {
125     if (m_urlString != urlString)
126         m_urlString = urlString;
127 }
128
129 void HistoryItem::setURL(const KURL& url)
130 {
131     setURLString(url.string());
132     clearDocumentState();
133 }
134
135 void HistoryItem::setOriginalURLString(const String& urlString)
136 {
137     m_originalURLString = urlString;
138 }
139
140 void HistoryItem::setReferrer(const Referrer& referrer)
141 {
142     m_referrer = referrer;
143 }
144
145 void HistoryItem::setTarget(const String& target)
146 {
147     m_target = target;
148 }
149
150 const IntPoint& HistoryItem::scrollPoint() const
151 {
152     return m_scrollPoint;
153 }
154
155 void HistoryItem::setScrollPoint(const IntPoint& point)
156 {
157     m_scrollPoint = point;
158 }
159
160 void HistoryItem::clearScrollPoint()
161 {
162     m_scrollPoint.setX(0);
163     m_scrollPoint.setY(0);
164 }
165
166 float HistoryItem::pageScaleFactor() const
167 {
168     return m_pageScaleFactor;
169 }
170
171 void HistoryItem::setPageScaleFactor(float scaleFactor)
172 {
173     m_pageScaleFactor = scaleFactor;
174 }
175
176 void HistoryItem::setDocumentState(const Vector<String>& state)
177 {
178     m_documentState = state;
179 }
180
181 const Vector<String>& HistoryItem::documentState() const
182 {
183     return m_documentState;
184 }
185
186 void HistoryItem::clearDocumentState()
187 {
188     m_documentState.clear();
189 }
190
191 void HistoryItem::setStateObject(PassRefPtr<SerializedScriptValue> object)
192 {
193     m_stateObject = object;
194 }
195
196 void HistoryItem::addChildItem(PassRefPtr<HistoryItem> child)
197 {
198     m_children.append(child);
199 }
200
201 const HistoryItemVector& HistoryItem::children() const
202 {
203     return m_children;
204 }
205
206 void HistoryItem::clearChildren()
207 {
208     m_children.clear();
209 }
210
211 const AtomicString& HistoryItem::formContentType() const
212 {
213     return m_formContentType;
214 }
215
216 void HistoryItem::setFormInfoFromRequest(const ResourceRequest& request)
217 {
218     if (equalIgnoringCase(request.httpMethod(), "POST")) {
219         // FIXME: Eventually we have to make this smart enough to handle the case where
220         // we have a stream for the body to handle the "data interspersed with files" feature.
221         m_formData = request.httpBody();
222         m_formContentType = request.httpContentType();
223     } else {
224         m_formData = 0;
225         m_formContentType = nullAtom;
226     }
227 }
228
229 void HistoryItem::setFormData(PassRefPtr<FormData> formData)
230 {
231     m_formData = formData;
232 }
233
234 void HistoryItem::setFormContentType(const AtomicString& formContentType)
235 {
236     m_formContentType = formContentType;
237 }
238
239 FormData* HistoryItem::formData()
240 {
241     return m_formData.get();
242 }
243
244 bool HistoryItem::isCurrentDocument(Document* doc) const
245 {
246     // FIXME: We should find a better way to check if this is the current document.
247     return equalIgnoringFragmentIdentifier(url(), doc->url());
248 }
249
250 } // namespace WebCore
251