2 * Copyright (C) 2008, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "DOMWindow.h"
33 #include "ExceptionCode.h"
35 #include "FrameLoader.h"
40 Location::Location(Frame* frame)
45 void Location::disconnectFrame()
50 inline const KURL& Location::url() const
54 const KURL& url = m_frame->loader()->url();
56 return blankURL(); // Use "about:blank" while the page is still loading (before we have a frame).
61 String Location::href() const
66 const KURL& url = this->url();
67 return url.hasPath() ? url.prettyURL() : url.prettyURL() + "/";
70 String Location::protocol() const
75 return url().protocol() + ":";
78 String Location::host() const
83 // Note: this is the IE spec. The NS spec swaps the two, it says
84 // "The hostname property is the concatenation of the host and port properties, separated by a colon."
85 const KURL& url = this->url();
86 return url.port() ? url.host() + ":" + String::number(url.port()) : url.host();
89 String Location::hostname() const
97 String Location::port() const
102 const KURL& url = this->url();
103 return url.port() ? String::number(url.port()) : "";
106 String Location::pathname() const
111 const KURL& url = this->url();
112 return url.path().isEmpty() ? "/" : url.path();
115 String Location::search() const
120 const KURL& url = this->url();
121 return url.query().isEmpty() ? "" : "?" + url.query();
124 String Location::origin() const
128 return SecurityOrigin::create(url())->toString();
131 String Location::hash() const
136 const String& fragmentIdentifier = url().fragmentIdentifier();
137 return fragmentIdentifier.isEmpty() ? "" : "#" + fragmentIdentifier;
140 String Location::getParameter(const String& name) const
145 ParsedURLParameters parameters;
146 url().copyParsedQueryTo(parameters);
147 return parameters.get(name);
150 String Location::toString() const
155 const KURL& url = this->url();
156 return url.hasPath() ? url.prettyURL() : url.prettyURL() + "/";
159 void Location::setHref(const String& urlString, DOMWindow* activeWindow, DOMWindow* firstWindow)
163 m_frame->domWindow()->setLocation(urlString, activeWindow, firstWindow);
166 void Location::setProtocol(const String& protocol, DOMWindow* activeWindow, DOMWindow* firstWindow, ExceptionCode& ec)
170 KURL url = m_frame->loader()->url();
171 if (!url.setProtocol(protocol)) {
175 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
178 void Location::setHost(const String& host, DOMWindow* activeWindow, DOMWindow* firstWindow)
182 KURL url = m_frame->loader()->url();
183 url.setHostAndPort(host);
184 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
187 void Location::setHostname(const String& hostname, DOMWindow* activeWindow, DOMWindow* firstWindow)
191 KURL url = m_frame->loader()->url();
192 url.setHost(hostname);
193 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
196 void Location::setPort(const String& portString, DOMWindow* activeWindow, DOMWindow* firstWindow)
200 KURL url = m_frame->loader()->url();
201 int port = portString.toInt();
202 if (port < 0 || port > 0xFFFF)
206 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
209 void Location::setPathname(const String& pathname, DOMWindow* activeWindow, DOMWindow* firstWindow)
213 KURL url = m_frame->loader()->url();
214 url.setPath(pathname);
215 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
218 void Location::setSearch(const String& search, DOMWindow* activeWindow, DOMWindow* firstWindow)
222 KURL url = m_frame->loader()->url();
223 url.setQuery(search);
224 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
227 void Location::setHash(const String& hash, DOMWindow* activeWindow, DOMWindow* firstWindow)
231 KURL url = m_frame->loader()->url();
232 String oldFragmentIdentifier = url.fragmentIdentifier();
233 String newFragmentIdentifier = hash;
235 newFragmentIdentifier = hash.substring(1);
236 url.setFragmentIdentifier(newFragmentIdentifier);
237 // Note that by parsing the URL and *then* comparing fragments, we are
238 // comparing fragments post-canonicalization, and so this handles the
239 // cases where fragment identifiers are ignored or invalid.
240 if (equalIgnoringNullity(oldFragmentIdentifier, url.fragmentIdentifier()))
242 m_frame->domWindow()->setLocation(url.string(), activeWindow, firstWindow);
245 void Location::assign(const String& urlString, DOMWindow* activeWindow, DOMWindow* firstWindow)
249 m_frame->domWindow()->setLocation(urlString, activeWindow, firstWindow);
252 void Location::replace(const String& urlString, DOMWindow* activeWindow, DOMWindow* firstWindow)
256 m_frame->domWindow()->setLocation(urlString, activeWindow, firstWindow, LockHistoryAndBackForwardList);
259 void Location::reload(DOMWindow* activeWindow)
263 // FIXME: It's not clear this cross-origin security check is valuable.
264 // We allow one page to change the location of another. Why block attempts to reload?
265 // Other location operations simply block use of JavaScript URLs cross origin.
266 DOMWindow* targetWindow = m_frame->domWindow();
267 if (!activeWindow->securityOrigin()->canAccess(targetWindow->securityOrigin())) {
268 targetWindow->printErrorMessage(targetWindow->crossDomainAccessErrorMessage(activeWindow));
271 if (protocolIsJavaScript(m_frame->loader()->url()))
273 m_frame->navigationScheduler()->scheduleRefresh();
276 } // namespace WebCore