[V8] Get rid of function-level static FunctionTemplates in generated bindings code
[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     // This is only for getting a unique pointer which we can pass to privateTemplate.
143     static String privateTemplateUniqueKey = "reloadPrivateTemplate";
144     WrapperWorldType currentWorldType = worldType(isolate);
145     v8::Persistent<v8::FunctionTemplate> privateTemplate = V8PerIsolateData::from(isolate)->privateTemplate(currentWorldType, &privateTemplateUniqueKey, V8Location::reloadMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
146
147     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, currentWorldType));
148     if (holder.IsEmpty()) {
149         // can only reach here by 'object.__proto__.func', and it should passed
150         // domain security check already
151         return privateTemplate->GetFunction();
152     }
153     Location* imp = V8Location::toNative(holder);
154     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
155         static String sharedTemplateUniqueKey = "reloadSharedTemplate";
156         v8::Persistent<v8::FunctionTemplate> sharedTemplate = V8PerIsolateData::from(info.GetIsolate())->privateTemplate(currentWorldType, &sharedTemplateUniqueKey, V8Location::reloadMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
157         return sharedTemplate->GetFunction();
158     }
159     return privateTemplate->GetFunction();
160 }
161
162 v8::Handle<v8::Value> V8Location::replaceAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info)
163 {
164     v8::Isolate* isolate = info.GetIsolate();
165     // This is only for getting a unique pointer which we can pass to privateTemplateMap.
166     static String privateTemplateUniqueKey = "replacePrivateTemplate";
167     WrapperWorldType currentWorldType = worldType(info.GetIsolate());
168     v8::Persistent<v8::FunctionTemplate> privateTemplate = V8PerIsolateData::from(isolate)->privateTemplate(currentWorldType, &privateTemplateUniqueKey, V8Location::replaceMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
169
170     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, currentWorldType));
171     if (holder.IsEmpty()) {
172         // can only reach here by 'object.__proto__.func', and it should passed
173         // domain security check already
174         return privateTemplate->GetFunction();
175     }
176     Location* imp = V8Location::toNative(holder);
177     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
178         static String sharedTemplateUniqueKey = "replaceSharedTemplate";
179         v8::Persistent<v8::FunctionTemplate> sharedTemplate = V8PerIsolateData::from(info.GetIsolate())->privateTemplate(currentWorldType, &sharedTemplateUniqueKey, V8Location::replaceMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
180         return sharedTemplate->GetFunction();
181     }
182     return privateTemplate->GetFunction();
183 }
184
185 v8::Handle<v8::Value> V8Location::assignAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info)
186 {
187     v8::Isolate* isolate = info.GetIsolate();
188     // This is only for getting a unique pointer which we can pass to privateTemplateMap.
189     static String privateTemplateUniqueKey = "assignPrivateTemplate";
190     WrapperWorldType currentWorldType = worldType(info.GetIsolate());
191     v8::Persistent<v8::FunctionTemplate> privateTemplate = V8PerIsolateData::from(isolate)->privateTemplate(currentWorldType, &privateTemplateUniqueKey, V8Location::assignMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
192
193     v8::Handle<v8::Object> holder = info.This()->FindInstanceInPrototypeChain(V8Location::GetTemplate(isolate, currentWorldType));
194     if (holder.IsEmpty()) {
195         // can only reach here by 'object.__proto__.func', and it should passed
196         // domain security check already
197         return privateTemplate->GetFunction();
198     }
199     Location* imp = V8Location::toNative(holder);
200     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame(), DoNotReportSecurityError)) {
201         static String sharedTemplateUniqueKey = "assignSharedTemplate";
202         v8::Persistent<v8::FunctionTemplate> sharedTemplate = V8PerIsolateData::from(info.GetIsolate())->privateTemplate(currentWorldType, &sharedTemplateUniqueKey, V8Location::assignMethodCustom, v8Undefined(), v8::Signature::New(V8Location::GetRawTemplate(isolate)));
203         return sharedTemplate->GetFunction();
204     }
205     return privateTemplate->GetFunction();
206 }
207
208 v8::Handle<v8::Value> V8Location::reloadMethodCustom(const v8::Arguments& args)
209 {
210     Location* impl = V8Location::toNative(args.Holder());
211     BindingState* state = BindingState::instance();
212
213     impl->reload(activeDOMWindow(state));
214     return v8::Undefined();
215 }
216
217 v8::Handle<v8::Value> V8Location::replaceMethodCustom(const v8::Arguments& args)
218 {
219     Location* impl = V8Location::toNative(args.Holder());
220     BindingState* state = BindingState::instance();
221
222     // FIXME: Handle exceptions correctly.
223     String urlString = toWebCoreString(args[0]);
224
225     impl->replace(urlString, activeDOMWindow(state), firstDOMWindow(state));
226     return v8::Undefined();
227 }
228
229 v8::Handle<v8::Value> V8Location::assignMethodCustom(const v8::Arguments& args)
230 {
231     Location* impl = V8Location::toNative(args.Holder());
232     BindingState* state = BindingState::instance();
233
234     // FIXME: Handle exceptions correctly.
235     String urlString = toWebCoreString(args[0]);
236
237     impl->assign(urlString, activeDOMWindow(state), firstDOMWindow(state));
238     return v8::Undefined();
239 }
240
241 v8::Handle<v8::Value> V8Location::valueOfMethodCustom(const v8::Arguments& args)
242 {
243     // Just return the this object the way the normal valueOf function
244     // on the Object prototype would.  The valueOf function is only
245     // added to make sure that it cannot be overwritten on location
246     // objects, since that would provide a hook to change the string
247     // conversion behavior of location objects.
248     return args.This();
249 }
250
251 v8::Handle<v8::Value> V8Location::toStringMethodCustom(const v8::Arguments& args)
252 {
253     v8::Handle<v8::Object> holder = args.Holder();
254     Location* imp = V8Location::toNative(holder);
255     if (!BindingSecurity::shouldAllowAccessToFrame(BindingState::instance(), imp->frame()))
256         return v8::Undefined();
257     String result = imp->href();
258     return v8String(result, args.GetIsolate());
259 }
260
261 }  // namespace WebCore