2 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 * Copyright (C) 2004-2007 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "WMLPageState.h"
27 #include "BackForwardList.h"
30 #include "HistoryItem.h"
33 #include <wtf/text/CString.h>
37 WMLPageState::WMLPageState(Page* page)
39 , m_hasAccessControlData(false)
43 WMLPageState::~WMLPageState()
49 // Debugging helper for use within gdb
50 void WMLPageState::dump()
52 WMLVariableMap::iterator it = m_variables.begin();
53 WMLVariableMap::iterator end = m_variables.end();
55 fprintf(stderr, "Dumping WMLPageState (this=%p) associated with Page (page=%p)...\n", this, m_page);
56 for (; it != end; ++it)
57 fprintf(stderr, "\t-> name: '%s'\tvalue: '%s'\n", (*it).first.latin1().data(), (*it).second.latin1().data());
61 void WMLPageState::reset()
63 // Remove all the variables
66 // Clear the navigation history state
67 if (BackForwardList* list = m_page ? m_page->backForwardList() : 0)
68 list->clearWMLPageHistory();
71 static inline String normalizedHostName(const String& passedHost)
73 if (passedHost.contains("127.0.0.1")) {
74 String host = passedHost;
75 return host.replace("127.0.0.1", "localhost");
81 static inline String hostFromURL(const KURL& url)
83 // Default to "localhost"
84 String host = normalizedHostName(url.host());
85 return host.isEmpty() ? "localhost" : host;
88 static KURL urlForHistoryItem(Frame* frame, HistoryItem* item)
90 // For LayoutTests we need to find the corresponding WML frame in the test document
91 // to be able to test access-control correctly. Remember that WML is never supposed
92 // to be embedded anywhere, so the purpose is to simulate a standalone WML document.
93 if (frame->document()->isWMLDocument())
96 const HistoryItemVector& childItems = item->children();
97 HistoryItemVector::const_iterator it = childItems.begin();
98 const HistoryItemVector::const_iterator end = childItems.end();
100 for (; it != end; ++it) {
101 const RefPtr<HistoryItem> childItem = *it;
102 Frame* childFrame = frame->tree()->child(childItem->target());
106 if (Document* childDocument = childFrame->document()) {
107 if (childDocument->isWMLDocument())
108 return childItem->url();
115 static bool tryAccessHistoryURLs(Page* page, KURL& previousURL, KURL& currentURL)
120 Frame* frame = page->mainFrame();
121 if (!frame || !frame->document())
124 BackForwardList* list = page->backForwardList();
128 HistoryItem* previousItem = list->backItem();
132 HistoryItem* currentItem = list->currentItem();
136 previousURL = urlForHistoryItem(frame, previousItem);
137 currentURL = urlForHistoryItem(frame, currentItem);
142 bool WMLPageState::processAccessControlData(const String& domain, const String& path)
144 if (m_hasAccessControlData)
147 m_hasAccessControlData = true;
149 KURL previousURL, currentURL;
150 if (!tryAccessHistoryURLs(m_page, previousURL, currentURL))
153 // Spec: The path attribute defaults to the value "/"
154 m_accessPath = path.isEmpty() ? "/" : path;
156 // Spec: The domain attribute defaults to the current decks domain.
157 String previousHost = hostFromURL(previousURL);
158 m_accessDomain = domain.isEmpty() ? previousHost : normalizedHostName(domain);
160 // Spec: To simplify the development of applications that may not know the absolute path to the
161 // current deck, the path attribute accepts relative URIs. The user agent converts the relative
162 // path to an absolute path and then performs prefix matching against the PATH attribute.
163 Document* document = m_page->mainFrame() ? m_page->mainFrame()->document() : 0;
164 if (document && previousHost == m_accessDomain && !m_accessPath.startsWith("/")) {
165 String currentPath = currentURL.path();
167 size_t index = currentPath.reverseFind('/');
168 if (index != WTF::notFound)
169 m_accessPath = document->completeURL(currentPath.left(index + 1) + m_accessPath).path();
175 void WMLPageState::resetAccessControlData()
177 m_hasAccessControlData = false;
178 m_accessDomain = String();
179 m_accessPath = String();
182 bool WMLPageState::canAccessDeck() const
184 if (!m_hasAccessControlData)
187 KURL previousURL, currentURL;
188 if (!tryAccessHistoryURLs(m_page, previousURL, currentURL))
191 if (equalIgnoringFragmentIdentifier(previousURL, currentURL))
194 return hostIsAllowedToAccess(hostFromURL(previousURL)) && pathIsAllowedToAccess(previousURL.path());
197 bool WMLPageState::hostIsAllowedToAccess(const String& host) const
199 // Spec: The access domain is suffix-matched against the domain name portion of the referring URI
200 Vector<String> subdomainsAllowed;
201 if (m_accessDomain.contains('.'))
202 m_accessDomain.split('.', subdomainsAllowed);
204 subdomainsAllowed.append(m_accessDomain);
206 Vector<String> subdomainsCheck;
207 if (host.contains('.'))
208 host.split('.', subdomainsCheck);
210 subdomainsCheck.append(host);
212 Vector<String>::iterator itAllowed = subdomainsAllowed.end() - 1;
213 Vector<String>::iterator beginAllowed = subdomainsAllowed.begin();
215 Vector<String>::iterator itCheck = subdomainsCheck.end() - 1;
216 Vector<String>::iterator beginCheck = subdomainsCheck.begin();
219 for (; itAllowed >= beginAllowed && itCheck >= beginCheck; ) {
220 if (*itAllowed != *itCheck) {
232 bool WMLPageState::pathIsAllowedToAccess(const String& path) const
234 // Spec: The access path is prefix matched against the path portion of the referring URI
235 Vector<String> subpathsAllowed;
236 if (m_accessPath.contains('/'))
237 m_accessPath.split('/', subpathsAllowed);
239 subpathsAllowed.append(m_accessPath);
241 Vector<String> subpathsCheck;
242 if (path.contains('/'))
243 path.split('/', subpathsCheck);
245 subpathsCheck.append(path);
247 Vector<String>::iterator itAllowed = subpathsAllowed.begin();
248 Vector<String>::iterator endAllowed = subpathsAllowed.end();
250 Vector<String>::iterator itCheck = subpathsCheck.begin();
251 Vector<String>::iterator endCheck = subpathsCheck.end();
254 for (; itAllowed != endAllowed && itCheck != endCheck; ) {
255 if (*itAllowed != *itCheck) {