Oilpan: fix build after r202625.
[chromium-blink-merge.git] / third_party / WebKit / Source / core / frame / Location.cpp
blobe0922ef92ef2dfbaca7008e65bd3354ea743e91d
1 /*
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
6 * are met:
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.
29 #include "config.h"
30 #include "core/frame/Location.h"
32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/V8DOMActivityLogger.h"
34 #include "core/dom/DOMURLUtilsReadOnly.h"
35 #include "core/dom/Document.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/frame/LocalDOMWindow.h"
38 #include "core/frame/LocalFrame.h"
39 #include "core/loader/FrameLoader.h"
40 #include "platform/weborigin/KURL.h"
41 #include "platform/weborigin/SecurityOrigin.h"
43 namespace blink {
45 Location::Location(Frame* frame)
46 : m_frame(frame)
50 DEFINE_TRACE(Location)
52 visitor->trace(m_frame);
55 inline const KURL& Location::url() const
57 const KURL& url = toLocalFrame(m_frame)->document()->url();
58 if (!url.isValid())
59 return blankURL(); // Use "about:blank" while the page is still loading (before we have a frame).
61 return url;
64 String Location::href() const
66 if (!m_frame)
67 return String();
69 return url().strippedForUseAsHref();
72 String Location::protocol() const
74 if (!m_frame)
75 return String();
76 return DOMURLUtilsReadOnly::protocol(url());
79 String Location::host() const
81 if (!m_frame)
82 return String();
83 return DOMURLUtilsReadOnly::host(url());
86 String Location::hostname() const
88 if (!m_frame)
89 return String();
90 return DOMURLUtilsReadOnly::hostname(url());
93 String Location::port() const
95 if (!m_frame)
96 return String();
97 return DOMURLUtilsReadOnly::port(url());
100 String Location::pathname() const
102 if (!m_frame)
103 return String();
104 return DOMURLUtilsReadOnly::pathname(url());
107 String Location::search() const
109 if (!m_frame)
110 return String();
111 return DOMURLUtilsReadOnly::search(url());
114 String Location::origin() const
116 if (!m_frame)
117 return String();
118 return DOMURLUtilsReadOnly::origin(url());
121 PassRefPtrWillBeRawPtr<DOMStringList> Location::ancestorOrigins() const
123 RefPtrWillBeRawPtr<DOMStringList> origins = DOMStringList::create(DOMStringList::Location);
124 if (!m_frame)
125 return origins.release();
126 for (Frame* frame = m_frame->tree().parent(); frame; frame = frame->tree().parent())
127 origins->append(frame->securityContext()->securityOrigin()->toString());
128 return origins.release();
131 String Location::hash() const
133 if (!m_frame)
134 return String();
136 return DOMURLUtilsReadOnly::hash(url());
139 void Location::setHref(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& url)
141 if (!m_frame)
142 return;
143 setLocation(url, callingWindow, enteredWindow);
146 void Location::setProtocol(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& protocol, ExceptionState& exceptionState)
148 if (!m_frame)
149 return;
150 KURL url = toLocalFrame(m_frame)->document()->url();
151 if (!url.setProtocol(protocol)) {
152 exceptionState.throwDOMException(SyntaxError, "'" + protocol + "' is an invalid protocol.");
153 return;
155 setLocation(url.string(), callingWindow, enteredWindow);
158 void Location::setHost(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& host)
160 if (!m_frame)
161 return;
162 KURL url = toLocalFrame(m_frame)->document()->url();
163 url.setHostAndPort(host);
164 setLocation(url.string(), callingWindow, enteredWindow);
167 void Location::setHostname(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& hostname)
169 if (!m_frame)
170 return;
171 KURL url = toLocalFrame(m_frame)->document()->url();
172 url.setHost(hostname);
173 setLocation(url.string(), callingWindow, enteredWindow);
176 void Location::setPort(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& portString)
178 if (!m_frame)
179 return;
180 KURL url = toLocalFrame(m_frame)->document()->url();
181 url.setPort(portString);
182 setLocation(url.string(), callingWindow, enteredWindow);
185 void Location::setPathname(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& pathname)
187 if (!m_frame)
188 return;
189 KURL url = toLocalFrame(m_frame)->document()->url();
190 url.setPath(pathname);
191 setLocation(url.string(), callingWindow, enteredWindow);
194 void Location::setSearch(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& search)
196 if (!m_frame)
197 return;
198 KURL url = toLocalFrame(m_frame)->document()->url();
199 url.setQuery(search);
200 setLocation(url.string(), callingWindow, enteredWindow);
203 void Location::setHash(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& hash)
205 if (!m_frame)
206 return;
207 KURL url = toLocalFrame(m_frame)->document()->url();
208 String oldFragmentIdentifier = url.fragmentIdentifier();
209 String newFragmentIdentifier = hash;
210 if (hash[0] == '#')
211 newFragmentIdentifier = hash.substring(1);
212 url.setFragmentIdentifier(newFragmentIdentifier);
213 // Note that by parsing the URL and *then* comparing fragments, we are
214 // comparing fragments post-canonicalization, and so this handles the
215 // cases where fragment identifiers are ignored or invalid.
216 if (equalIgnoringNullity(oldFragmentIdentifier, url.fragmentIdentifier()))
217 return;
218 setLocation(url.string(), callingWindow, enteredWindow);
221 void Location::assign(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& url)
223 if (!m_frame)
224 return;
225 setLocation(url, callingWindow, enteredWindow);
228 void Location::replace(LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, const String& url)
230 if (!m_frame)
231 return;
232 setLocation(url, callingWindow, enteredWindow, SetLocation::ReplaceThisFrame);
235 void Location::reload(LocalDOMWindow* callingWindow)
237 if (!m_frame)
238 return;
239 if (protocolIsJavaScript(toLocalFrame(m_frame)->document()->url()))
240 return;
241 m_frame->reload(FrameLoadTypeReload, ClientRedirect);
244 void Location::setLocation(const String& url, LocalDOMWindow* callingWindow, LocalDOMWindow* enteredWindow, SetLocation locationPolicy)
246 ASSERT(m_frame);
247 if (!m_frame || !m_frame->host())
248 return;
250 if (!callingWindow->frame() || !callingWindow->frame()->canNavigate(*m_frame))
251 return;
253 Document* enteredDocument = enteredWindow->document();
254 if (!enteredDocument)
255 return;
257 KURL completedURL = enteredDocument->completeURL(url);
258 if (completedURL.isNull())
259 return;
261 if (m_frame->domWindow()->isInsecureScriptAccess(*callingWindow, completedURL))
262 return;
264 V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
265 if (activityLogger) {
266 Vector<String> argv;
267 argv.append("LocalDOMWindow");
268 argv.append("url");
269 argv.append(enteredDocument->url());
270 argv.append(completedURL);
271 activityLogger->logEvent("blinkSetAttribute", argv.size(), argv.data());
273 m_frame->navigate(*callingWindow->document(), completedURL, locationPolicy == SetLocation::ReplaceThisFrame, UserGestureStatus::None);
276 } // namespace blink