6b6fe6f615de736dfdaf475a142073f3e7e3fa36
[WebKit-https.git] / Source / WebCore / bindings / v8 / custom / V8LocationCustom.cpp
1 /*
2  * Copyright (C) 2009 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "V8Location.h"
33
34 #include "BindingState.h"
35 #include "Document.h"
36 #include "Frame.h"
37 #include "FrameLoader.h"
38 #include "KURL.h"
39 #include "Location.h"
40 #include "V8Binding.h"
41 #include "V8DOMWindow.h"
42 #include "V8EventListener.h"
43 #include "V8Utilities.h"
44 #include <wtf/text/WTFString.h>
45
46 namespace WebCore {
47
48 void V8Location::hashAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
49 {
50     Location* impl = V8Location::toNative(info.Holder());
51     BindingState* state = BindingState::instance();
52
53     // FIXME: Handle exceptions correctly.
54     String hash = toWebCoreString(value);
55
56     impl->setHash(hash, activeDOMWindow(state), firstDOMWindow(state));
57 }
58
59 void V8Location::hostAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
60 {
61     Location* impl = V8Location::toNative(info.Holder());
62     BindingState* state = BindingState::instance();
63
64     // FIXME: Handle exceptions correctly.
65     String host = toWebCoreString(value);
66
67     impl->setHost(host, activeDOMWindow(state), firstDOMWindow(state));
68 }
69
70 void V8Location::hostnameAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
71 {
72     Location* impl = V8Location::toNative(info.Holder());
73     BindingState* state = BindingState::instance();
74
75     // FIXME: Handle exceptions correctly.
76     String hostname = toWebCoreString(value);
77
78     impl->setHostname(hostname, activeDOMWindow(state), firstDOMWindow(state));
79 }
80
81 void V8Location::hrefAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
82 {
83     Location* impl = V8Location::toNative(info.Holder());
84     BindingState* state = BindingState::instance();
85
86     // FIXME: Handle exceptions correctly.
87     String href = toWebCoreString(value);
88
89     impl->setHref(href, activeDOMWindow(state), firstDOMWindow(state));
90 }
91
92 void V8Location::pathnameAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
93 {
94     Location* impl = V8Location::toNative(info.Holder());
95     BindingState* state = BindingState::instance();
96
97     // FIXME: Handle exceptions correctly.
98     String pathname = toWebCoreString(value);
99
100     impl->setPathname(pathname, activeDOMWindow(state), firstDOMWindow(state));
101 }
102
103 void V8Location::portAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
104 {
105     Location* impl = V8Location::toNative(info.Holder());
106     BindingState* state = BindingState::instance();
107
108     // FIXME: Handle exceptions correctly.
109     String port = toWebCoreString(value);
110
111     impl->setPort(port, activeDOMWindow(state), firstDOMWindow(state));
112 }
113
114 void V8Location::protocolAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
115 {
116     Location* impl = V8Location::toNative(info.Holder());
117     BindingState* state = BindingState::instance();
118
119     // FIXME: Handle exceptions correctly.
120     String protocol = toWebCoreString(value);
121
122     ExceptionCode ec = 0;
123     impl->setProtocol(protocol, activeDOMWindow(state), firstDOMWindow(state), ec);
124     if (UNLIKELY(ec))
125         setDOMException(ec, info.GetIsolate());
126 }
127
128 void V8Location::searchAttrSetterCustom(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
129 {
130     Location* impl = V8Location::toNative(info.Holder());
131     BindingState* state = BindingState::instance();
132
133     // FIXME: Handle exceptions correctly.
134     String search = toWebCoreString(value);
135
136     impl->setSearch(search, activeDOMWindow(state), firstDOMWindow(state));
137 }
138
139 v8::Handle<v8::Value> V8Location::reloadAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info)
140 {
141     v8::Isolate* isolate = info.GetIsolate();
142     static v8::Persistent<v8::FunctionTemplate> privateTemplate = v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::reloadMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
143     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, worldType(isolate)));
144     if (holder.IsEmpty()) {
145         // can only reach here by 'object.__proto__.func', and it should passed
146         // domain security check already
147         return privateTemplate->GetFunction();
148     }
149     Location* imp = V8Location::toNative(holder);
150     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
151         static v8::Persistent<v8::FunctionTemplate> sharedTemplate = v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::reloadMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
152         return sharedTemplate->GetFunction();
153     }
154     return privateTemplate->GetFunction();
155 }
156
157 v8::Handle<v8::Value> V8Location::replaceAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info)
158 {
159     v8::Isolate* isolate = info.GetIsolate();
160     static v8::Persistent<v8::FunctionTemplate> privateTemplate = v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::replaceMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
161     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, worldType(isolate)));
162     if (holder.IsEmpty()) {
163         // can only reach here by 'object.__proto__.func', and it should passed
164         // domain security check already
165         return privateTemplate->GetFunction();
166     }
167     Location* imp = V8Location::toNative(holder);
168     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
169         static v8::Persistent<v8::FunctionTemplate> sharedTemplate = v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::replaceMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
170         return sharedTemplate->GetFunction();
171     }
172     return privateTemplate->GetFunction();
173 }
174
175 v8::Handle<v8::Value> V8Location::assignAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info)
176 {
177     v8::Isolate* isolate = info.GetIsolate();
178     static v8::Persistent<v8::FunctionTemplate> privateTemplate =
179         v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::assignMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
180     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, worldType(isolate)));
181     if (holder.IsEmpty()) {
182         // can only reach here by 'object.__proto__.func', and it should passed
183         // domain security check already
184         return privateTemplate->GetFunction();
185     }
186     Location* imp = V8Location::toNative(holder);
187     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
188         static v8::Persistent<v8::FunctionTemplate> sharedTemplate = v8::Persistent<v8::FunctionTemplate>::New(isolate, v8::FunctionTemplate::New(V8Location::assignMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate))));
189         return sharedTemplate->GetFunction();
190     }
191     return privateTemplate->GetFunction();
192 }
193
194 v8::Handle<v8::Value> V8Location::reloadMethodCustom(const v8::Arguments& args)
195 {
196     Location* impl = V8Location::toNative(args.Holder());
197     BindingState* state = BindingState::instance();
198
199     impl->reload(activeDOMWindow(state));
200     return v8::Undefined();
201 }
202
203 v8::Handle<v8::Value> V8Location::replaceMethodCustom(const v8::Arguments& args)
204 {
205     Location* impl = V8Location::toNative(args.Holder());
206     BindingState* state = BindingState::instance();
207
208     // FIXME: Handle exceptions correctly.
209     String urlString = toWebCoreString(args[0]);
210
211     impl->replace(urlString, activeDOMWindow(state), firstDOMWindow(state));
212     return v8::Undefined();
213 }
214
215 v8::Handle<v8::Value> V8Location::assignMethodCustom(const v8::Arguments& args)
216 {
217     Location* impl = V8Location::toNative(args.Holder());
218     BindingState* state = BindingState::instance();
219
220     // FIXME: Handle exceptions correctly.
221     String urlString = toWebCoreString(args[0]);
222
223     impl->assign(urlString, activeDOMWindow(state), firstDOMWindow(state));
224     return v8::Undefined();
225 }
226
227 v8::Handle<v8::Value> V8Location::valueOfMethodCustom(const v8::Arguments& args)
228 {
229     // Just return the this object the way the normal valueOf function
230     // on the Object prototype would.  The valueOf function is only
231     // added to make sure that it cannot be overwritten on location
232     // objects, since that would provide a hook to change the string
233     // conversion behavior of location objects.
234     return args.This();
235 }
236
237 v8::Handle<v8::Value> V8Location::toStringMethodCustom(const v8::Arguments& args)
238 {
239     v8::Handle<v8::Object> holder = args.Holder();
240     Location* imp = V8Location::toNative(holder);
241     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame()))
242         return v8::Undefined();
243     String result = imp->href();
244     return v8String(result, args.GetIsolate());
245 }
246
247 }  // namespace WebCore