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.
30 #include "core/frame/Location.h"
32 #include "bindings/v8/ExceptionState.h"
33 #include "core/dom/DOMURLUtilsReadOnly.h"
34 #include "core/dom/Document.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/frame/DOMWindow.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/loader/FrameLoader.h"
39 #include "platform/weborigin/KURL.h"
40 #include "platform/weborigin/SecurityOrigin.h"
44 Location::Location(LocalFrame* frame)
45 : DOMWindowProperty(frame)
47 ScriptWrappable::init(this);
50 inline const KURL& Location::url() const
54 const KURL& url = m_frame->document()->url();
56 return blankURL(); // Use "about:blank" while the page is still loading (before we have a frame).
61 String Location::href() const
66 return url().string();
69 String Location::protocol() const
73 return DOMURLUtilsReadOnly::protocol(url());
76 String Location::host() const
80 return DOMURLUtilsReadOnly::host(url());
83 String Location::hostname() const
87 return DOMURLUtilsReadOnly::hostname(url());
90 String Location::port() const
94 return DOMURLUtilsReadOnly::port(url());
97 String Location::pathname() const
101 return DOMURLUtilsReadOnly::pathname(url());
104 String Location::search() const
108 return DOMURLUtilsReadOnly::search(url());
111 String Location::origin() const
115 return DOMURLUtilsReadOnly::origin(url());
118 PassRefPtrWillBeRawPtr<DOMStringList> Location::ancestorOrigins() const
120 RefPtrWillBeRawPtr<DOMStringList> origins = DOMStringList::create();
122 return origins.release();
123 for (LocalFrame* frame = m_frame->tree().parent(); frame; frame = frame->tree().parent())
124 origins->append(frame->document()->securityOrigin()->toString());
125 return origins.release();
128 String Location::hash() const
133 return DOMURLUtilsReadOnly::hash(url());
136 void Location::setHref(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& url)
140 setLocation(url, callingWindow, enteredWindow);
143 void Location::setProtocol(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& protocol, ExceptionState& exceptionState)
147 KURL url = m_frame->document()->url();
148 if (!url.setProtocol(protocol)) {
149 exceptionState.throwDOMException(SyntaxError, "'" + protocol + "' is an invalid protocol.");
152 setLocation(url.string(), callingWindow, enteredWindow);
155 void Location::setHost(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& host)
159 KURL url = m_frame->document()->url();
160 url.setHostAndPort(host);
161 setLocation(url.string(), callingWindow, enteredWindow);
164 void Location::setHostname(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& hostname)
168 KURL url = m_frame->document()->url();
169 url.setHost(hostname);
170 setLocation(url.string(), callingWindow, enteredWindow);
173 void Location::setPort(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& portString)
177 KURL url = m_frame->document()->url();
178 url.setPort(portString);
179 setLocation(url.string(), callingWindow, enteredWindow);
182 void Location::setPathname(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& pathname)
186 KURL url = m_frame->document()->url();
187 url.setPath(pathname);
188 setLocation(url.string(), callingWindow, enteredWindow);
191 void Location::setSearch(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& search)
195 KURL url = m_frame->document()->url();
196 url.setQuery(search);
197 setLocation(url.string(), callingWindow, enteredWindow);
200 void Location::setHash(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& hash)
204 KURL url = m_frame->document()->url();
205 String oldFragmentIdentifier = url.fragmentIdentifier();
206 String newFragmentIdentifier = hash;
208 newFragmentIdentifier = hash.substring(1);
209 url.setFragmentIdentifier(newFragmentIdentifier);
210 // Note that by parsing the URL and *then* comparing fragments, we are
211 // comparing fragments post-canonicalization, and so this handles the
212 // cases where fragment identifiers are ignored or invalid.
213 if (equalIgnoringNullity(oldFragmentIdentifier, url.fragmentIdentifier()))
215 setLocation(url.string(), callingWindow, enteredWindow);
218 void Location::assign(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& url)
222 setLocation(url, callingWindow, enteredWindow);
225 void Location::replace(DOMWindow* callingWindow, DOMWindow* enteredWindow, const String& url)
229 // Note: We call DOMWindow::setLocation directly here because replace() always operates on the current frame.
230 m_frame->domWindow()->setLocation(url, callingWindow, enteredWindow, LockHistoryAndBackForwardList);
233 void Location::reload(DOMWindow* callingWindow)
237 if (protocolIsJavaScript(m_frame->document()->url()))
239 m_frame->navigationScheduler().scheduleRefresh();
242 void Location::setLocation(const String& url, DOMWindow* callingWindow, DOMWindow* enteredWindow)
245 LocalFrame* frame = m_frame->loader().findFrameForNavigation(nullAtom, callingWindow->document());
248 frame->domWindow()->setLocation(url, callingWindow, enteredWindow);
251 } // namespace WebCore