OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2008, 2010 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 * | |
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. | |
16 * | |
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. | |
27 */ | |
28 | |
29 #include "config.h" | |
30 #include "core/page/Location.h" | |
31 | |
32 #include "bindings/v8/ExceptionMessages.h" | |
33 #include "bindings/v8/ExceptionState.h" | |
34 #include "core/dom/Document.h" | |
35 #include "core/dom/ExceptionCode.h" | |
36 #include "core/loader/FrameLoader.h" | |
37 #include "core/page/DOMWindow.h" | |
38 #include "core/page/Frame.h" | |
39 #include "weborigin/KURL.h" | |
40 #include "weborigin/SecurityOrigin.h" | |
41 | |
42 namespace WebCore { | |
43 | |
44 Location::Location(Frame* frame) | |
45 : DOMWindowProperty(frame) | |
46 { | |
47 ScriptWrappable::init(this); | |
48 } | |
49 | |
50 inline const KURL& Location::url() const | |
51 { | |
52 ASSERT(m_frame); | |
53 | |
54 const KURL& url = m_frame->document()->url(); | |
55 if (!url.isValid()) | |
56 return blankURL(); // Use "about:blank" while the page is still loading
(before we have a frame). | |
57 | |
58 return url; | |
59 } | |
60 | |
61 String Location::href() const | |
62 { | |
63 if (!m_frame) | |
64 return String(); | |
65 | |
66 return url().string(); | |
67 } | |
68 | |
69 String Location::protocol() const | |
70 { | |
71 if (!m_frame) | |
72 return String(); | |
73 | |
74 return url().protocol() + ":"; | |
75 } | |
76 | |
77 String Location::host() const | |
78 { | |
79 if (!m_frame) | |
80 return String(); | |
81 | |
82 // Note: this is the IE spec. The NS spec swaps the two, it says | |
83 // "The hostname property is the concatenation of the host and port properti
es, separated by a colon." | |
84 const KURL& url = this->url(); | |
85 return url.hasPort() ? url.host() + ":" + String::number(url.port()) : url.h
ost(); | |
86 } | |
87 | |
88 String Location::hostname() const | |
89 { | |
90 if (!m_frame) | |
91 return String(); | |
92 | |
93 return url().host(); | |
94 } | |
95 | |
96 String Location::port() const | |
97 { | |
98 if (!m_frame) | |
99 return String(); | |
100 | |
101 const KURL& url = this->url(); | |
102 return url.hasPort() ? String::number(url.port()) : ""; | |
103 } | |
104 | |
105 String Location::pathname() const | |
106 { | |
107 if (!m_frame) | |
108 return String(); | |
109 | |
110 const KURL& url = this->url(); | |
111 return url.path().isEmpty() ? "/" : url.path(); | |
112 } | |
113 | |
114 String Location::search() const | |
115 { | |
116 if (!m_frame) | |
117 return String(); | |
118 | |
119 const KURL& url = this->url(); | |
120 return url.query().isEmpty() ? emptyString() : "?" + url.query(); | |
121 } | |
122 | |
123 String Location::origin() const | |
124 { | |
125 if (!m_frame) | |
126 return String(); | |
127 return SecurityOrigin::create(url())->toString(); | |
128 } | |
129 | |
130 PassRefPtr<DOMStringList> Location::ancestorOrigins() const | |
131 { | |
132 RefPtr<DOMStringList> origins = DOMStringList::create(); | |
133 if (!m_frame) | |
134 return origins.release(); | |
135 for (Frame* frame = m_frame->tree()->parent(); frame; frame = frame->tree()-
>parent()) | |
136 origins->append(frame->document()->securityOrigin()->toString()); | |
137 return origins.release(); | |
138 } | |
139 | |
140 String Location::hash() const | |
141 { | |
142 if (!m_frame) | |
143 return String(); | |
144 | |
145 const String& fragmentIdentifier = url().fragmentIdentifier(); | |
146 return fragmentIdentifier.isEmpty() ? emptyString() : "#" + fragmentIdentifi
er; | |
147 } | |
148 | |
149 void Location::setHref(DOMWindow* activeWindow, DOMWindow* firstWindow, const St
ring& url) | |
150 { | |
151 if (!m_frame) | |
152 return; | |
153 setLocation(url, activeWindow, firstWindow); | |
154 } | |
155 | |
156 void Location::setProtocol(DOMWindow* activeWindow, DOMWindow* firstWindow, cons
t String& protocol, ExceptionState& es) | |
157 { | |
158 if (!m_frame) | |
159 return; | |
160 KURL url = m_frame->document()->url(); | |
161 if (!url.setProtocol(protocol)) { | |
162 es.throwDOMException(SyntaxError, ExceptionMessages::failedToSet("protoc
ol", "Location", "'" + protocol + "' is an invalid protocol.")); | |
163 return; | |
164 } | |
165 setLocation(url.string(), activeWindow, firstWindow); | |
166 } | |
167 | |
168 void Location::setHost(DOMWindow* activeWindow, DOMWindow* firstWindow, const St
ring& host) | |
169 { | |
170 if (!m_frame) | |
171 return; | |
172 KURL url = m_frame->document()->url(); | |
173 url.setHostAndPort(host); | |
174 setLocation(url.string(), activeWindow, firstWindow); | |
175 } | |
176 | |
177 void Location::setHostname(DOMWindow* activeWindow, DOMWindow* firstWindow, cons
t String& hostname) | |
178 { | |
179 if (!m_frame) | |
180 return; | |
181 KURL url = m_frame->document()->url(); | |
182 url.setHost(hostname); | |
183 setLocation(url.string(), activeWindow, firstWindow); | |
184 } | |
185 | |
186 void Location::setPort(DOMWindow* activeWindow, DOMWindow* firstWindow, const St
ring& portString) | |
187 { | |
188 if (!m_frame) | |
189 return; | |
190 KURL url = m_frame->document()->url(); | |
191 int port = portString.toInt(); | |
192 if (port < 0 || port > 0xFFFF || portString.isEmpty()) | |
193 url.removePort(); | |
194 else | |
195 url.setPort(port); | |
196 setLocation(url.string(), activeWindow, firstWindow); | |
197 } | |
198 | |
199 void Location::setPathname(DOMWindow* activeWindow, DOMWindow* firstWindow, cons
t String& pathname) | |
200 { | |
201 if (!m_frame) | |
202 return; | |
203 KURL url = m_frame->document()->url(); | |
204 url.setPath(pathname); | |
205 setLocation(url.string(), activeWindow, firstWindow); | |
206 } | |
207 | |
208 void Location::setSearch(DOMWindow* activeWindow, DOMWindow* firstWindow, const
String& search) | |
209 { | |
210 if (!m_frame) | |
211 return; | |
212 KURL url = m_frame->document()->url(); | |
213 url.setQuery(search); | |
214 setLocation(url.string(), activeWindow, firstWindow); | |
215 } | |
216 | |
217 void Location::setHash(DOMWindow* activeWindow, DOMWindow* firstWindow, const St
ring& hash) | |
218 { | |
219 if (!m_frame) | |
220 return; | |
221 KURL url = m_frame->document()->url(); | |
222 String oldFragmentIdentifier = url.fragmentIdentifier(); | |
223 String newFragmentIdentifier = hash; | |
224 if (hash[0] == '#') | |
225 newFragmentIdentifier = hash.substring(1); | |
226 url.setFragmentIdentifier(newFragmentIdentifier); | |
227 // Note that by parsing the URL and *then* comparing fragments, we are | |
228 // comparing fragments post-canonicalization, and so this handles the | |
229 // cases where fragment identifiers are ignored or invalid. | |
230 if (equalIgnoringNullity(oldFragmentIdentifier, url.fragmentIdentifier())) | |
231 return; | |
232 setLocation(url.string(), activeWindow, firstWindow); | |
233 } | |
234 | |
235 void Location::assign(DOMWindow* activeWindow, DOMWindow* firstWindow, const Str
ing& url) | |
236 { | |
237 if (!m_frame) | |
238 return; | |
239 setLocation(url, activeWindow, firstWindow); | |
240 } | |
241 | |
242 void Location::replace(DOMWindow* activeWindow, DOMWindow* firstWindow, const St
ring& url) | |
243 { | |
244 if (!m_frame) | |
245 return; | |
246 // Note: We call DOMWindow::setLocation directly here because replace() alwa
ys operates on the current frame. | |
247 m_frame->domWindow()->setLocation(url, activeWindow, firstWindow, LockHistor
yAndBackForwardList); | |
248 } | |
249 | |
250 void Location::reload(DOMWindow* activeWindow) | |
251 { | |
252 if (!m_frame) | |
253 return; | |
254 if (protocolIsJavaScript(m_frame->document()->url())) | |
255 return; | |
256 m_frame->navigationScheduler()->scheduleRefresh(); | |
257 } | |
258 | |
259 void Location::setLocation(const String& url, DOMWindow* activeWindow, DOMWindow
* firstWindow) | |
260 { | |
261 ASSERT(m_frame); | |
262 // We call findFrameForNavigation to handle the case of a seamless iframe co
rrectly. | |
263 Frame* frame = m_frame->loader()->findFrameForNavigation(String(), activeWin
dow->document()); | |
264 if (!frame) | |
265 return; | |
266 frame->domWindow()->setLocation(url, activeWindow, firstWindow); | |
267 } | |
268 | |
269 } // namespace WebCore | |
OLD | NEW |