OLD | NEW |
| (Empty) |
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 "WebBindings.h" | |
33 | |
34 #include "npruntime_impl.h" | |
35 #include "npruntime_priv.h" | |
36 #include "webkit/api/public/WebDragData.h" | |
37 #include "webkit/api/public/WebRange.h" | |
38 | |
39 #if USE(V8) | |
40 #include "ChromiumDataObject.h" | |
41 #include "ClipboardChromium.h" | |
42 #include "EventNames.h" | |
43 #include "MouseEvent.h" | |
44 #include "NPV8Object.h" // for PrivateIdentifier | |
45 #include "Range.h" | |
46 #include "V8DOMWrapper.h" | |
47 #include "V8Helpers.h" | |
48 #include "V8Proxy.h" | |
49 #elif USE(JSC) | |
50 #include "bridge/c/c_utility.h" | |
51 #endif | |
52 | |
53 #if USE(JAVASCRIPTCORE_BINDINGS) | |
54 using JSC::Bindings::PrivateIdentifier; | |
55 #endif | |
56 | |
57 using namespace WebCore; | |
58 | |
59 namespace WebKit { | |
60 | |
61 bool WebBindings::construct(NPP npp, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant* result) | |
62 { | |
63 return _NPN_Construct(npp, npobj, args, argCount, result); | |
64 } | |
65 | |
66 NPObject* WebBindings::createObject(NPP npp, NPClass* npClass) | |
67 { | |
68 return _NPN_CreateObject(npp, npClass); | |
69 } | |
70 | |
71 bool WebBindings::enumerate(NPP id, NPObject* obj, NPIdentifier** identifier, uint32_t* val) | |
72 { | |
73 return _NPN_Enumerate(id, obj, identifier, val); | |
74 } | |
75 | |
76 bool WebBindings::evaluate(NPP npp, NPObject* npObject, NPString* npScript, NPVariant* result) | |
77 { | |
78 return _NPN_Evaluate(npp, npObject, npScript, result); | |
79 } | |
80 | |
81 bool WebBindings::evaluateHelper(NPP npp, bool popups_allowed, NPObject* npobj, NPString* npscript, NPVariant* result) | |
82 { | |
83 return _NPN_EvaluateHelper(npp, popups_allowed, npobj, npscript, result); | |
84 } | |
85 | |
86 NPIdentifier WebBindings::getIntIdentifier(int32_t number) | |
87 { | |
88 return _NPN_GetIntIdentifier(number); | |
89 } | |
90 | |
91 bool WebBindings::getProperty(NPP npp, NPObject* obj, NPIdentifier propertyName, NPVariant *result) | |
92 { | |
93 return _NPN_GetProperty(npp, obj, propertyName, result); | |
94 } | |
95 | |
96 NPIdentifier WebBindings::getStringIdentifier(const NPUTF8* string) | |
97 { | |
98 return _NPN_GetStringIdentifier(string); | |
99 } | |
100 | |
101 void WebBindings::getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers) | |
102 { | |
103 _NPN_GetStringIdentifiers(names, nameCount, identifiers); | |
104 } | |
105 | |
106 bool WebBindings::hasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName) | |
107 { | |
108 return _NPN_HasMethod(npp, npObject, methodName); | |
109 } | |
110 | |
111 bool WebBindings::hasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName) | |
112 { | |
113 return _NPN_HasProperty(npp, npObject, propertyName); | |
114 } | |
115 | |
116 bool WebBindings::identifierIsString(NPIdentifier identifier) | |
117 { | |
118 return _NPN_IdentifierIsString(identifier); | |
119 } | |
120 | |
121 int32_t WebBindings::intFromIdentifier(NPIdentifier identifier) | |
122 { | |
123 return _NPN_IntFromIdentifier(identifier); | |
124 } | |
125 | |
126 void WebBindings::initializeVariantWithStringCopy(NPVariant* variant, const NPString* value) | |
127 { | |
128 #if USE(V8) | |
129 _NPN_InitializeVariantWithStringCopy(variant, value); | |
130 #else | |
131 NPN_InitializeVariantWithStringCopy(variant, value); | |
132 #endif | |
133 } | |
134 | |
135 bool WebBindings::invoke(NPP npp, NPObject* npObject, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result) | |
136 { | |
137 return _NPN_Invoke(npp, npObject, methodName, arguments, argumentCount, result); | |
138 } | |
139 | |
140 bool WebBindings::invokeDefault(NPP id, NPObject* obj, const NPVariant* args, uint32_t count, NPVariant* result) | |
141 { | |
142 return _NPN_InvokeDefault(id, obj, args, count, result); | |
143 } | |
144 | |
145 void WebBindings::releaseObject(NPObject* npObject) | |
146 { | |
147 return _NPN_ReleaseObject(npObject); | |
148 } | |
149 | |
150 void WebBindings::releaseVariantValue(NPVariant* variant) | |
151 { | |
152 _NPN_ReleaseVariantValue(variant); | |
153 } | |
154 | |
155 bool WebBindings::removeProperty(NPP id, NPObject* object, NPIdentifier identifier) | |
156 { | |
157 return _NPN_RemoveProperty(id, object, identifier); | |
158 } | |
159 | |
160 NPObject* WebBindings::retainObject(NPObject* npObject) | |
161 { | |
162 return _NPN_RetainObject(npObject); | |
163 } | |
164 | |
165 void WebBindings::setException(NPObject* obj, const NPUTF8* message) | |
166 { | |
167 _NPN_SetException(obj, message); | |
168 } | |
169 | |
170 bool WebBindings::setProperty(NPP id, NPObject* obj, NPIdentifier identifier, const NPVariant* variant) | |
171 { | |
172 return _NPN_SetProperty(id, obj, identifier, variant); | |
173 } | |
174 | |
175 void WebBindings::unregisterObject(NPObject* npObject) | |
176 { | |
177 #if USE(V8) | |
178 _NPN_UnregisterObject(npObject); | |
179 #endif | |
180 } | |
181 | |
182 NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier) | |
183 { | |
184 return _NPN_UTF8FromIdentifier(identifier); | |
185 } | |
186 | |
187 void WebBindings::extractIdentifierData(const NPIdentifier& identifier, const NPUTF8*& string, int32_t& number, bool& isString) | |
188 { | |
189 PrivateIdentifier* priv = static_cast<PrivateIdentifier*>(identifier); | |
190 if (!priv) { | |
191 isString = false; | |
192 number = 0; | |
193 return; | |
194 } | |
195 | |
196 isString = priv->isString; | |
197 if (isString) | |
198 string = priv->value.string; | |
199 else | |
200 number = priv->value.number; | |
201 } | |
202 | |
203 #if USE(V8) | |
204 | |
205 static v8::Local<v8::Value> getEvent(const v8::Handle<v8::Context>& context) | |
206 { | |
207 static v8::Persistent<v8::String> eventSymbol(v8::Persistent<v8::String>::New(v8::String::NewSymbol("event"))); | |
208 return context->Global()->GetHiddenValue(eventSymbol); | |
209 } | |
210 | |
211 static bool getDragDataImpl(NPObject* npobj, int* eventId, WebDragData* data) | |
212 { | |
213 if (!npobj) | |
214 return false; | |
215 if (npobj->_class != npScriptObjectClass) | |
216 return false; | |
217 | |
218 v8::HandleScope handleScope; | |
219 v8::Handle<v8::Context> context = v8::Context::GetEntered(); | |
220 if (context.IsEmpty()) | |
221 return false; | |
222 | |
223 // Get the current WebCore event. | |
224 v8::Handle<v8::Value> currentEvent(getEvent(context)); | |
225 Event* event = V8DOMWrapper::convertToNativeEvent(currentEvent); | |
226 if (!event) | |
227 return false; | |
228 | |
229 // Check that the given npobj is that event. | |
230 V8NPObject* object = reinterpret_cast<V8NPObject*>(npobj); | |
231 Event* given = V8DOMWrapper::convertToNativeEvent(object->v8Object); | |
232 if (given != event) | |
233 return false; | |
234 | |
235 // Check the execution frames are same origin. | |
236 V8Proxy* current = V8Proxy::retrieve(V8Proxy::retrieveFrameForCurrentContext()); | |
237 Frame* frame = V8Proxy::retrieveFrame(context); | |
238 if (!current || !current->canAccessFrame(frame, false)) | |
239 return false; | |
240 | |
241 const EventNames& names(eventNames()); | |
242 const AtomicString& eventType(event->type()); | |
243 | |
244 enum DragTargetMouseEventId { | |
245 DragEnterId = 1, DragOverId = 2, DragLeaveId = 3, DropId = 4 | |
246 }; | |
247 | |
248 // The event type should be a drag event. | |
249 if (eventType == names.dragenterEvent) | |
250 *eventId = DragEnterId; | |
251 else if (eventType == names.dragoverEvent) | |
252 *eventId = DragOverId; | |
253 else if (eventType == names.dragleaveEvent) | |
254 *eventId = DragLeaveId; | |
255 else if (eventType == names.dropEvent) | |
256 *eventId = DropId; | |
257 else | |
258 return false; | |
259 | |
260 // Drag events are mouse events and should have a clipboard. | |
261 MouseEvent* me = static_cast<MouseEvent*>(event); | |
262 Clipboard* clipboard = me->clipboard(); | |
263 if (!clipboard) | |
264 return false; | |
265 | |
266 // And that clipboard should be accessible by WebKit policy. | |
267 ClipboardChromium* chrome = static_cast<ClipboardChromium*>(clipboard); | |
268 HashSet<String> accessible(chrome->types()); | |
269 if (accessible.isEmpty()) | |
270 return false; | |
271 | |
272 RefPtr<ChromiumDataObject> dataObject(chrome->dataObject()); | |
273 if (dataObject && data) | |
274 *data = WebDragData(dataObject); | |
275 | |
276 return dataObject; | |
277 } | |
278 | |
279 static bool getRangeImpl(NPObject* npobj, WebRange* range) | |
280 { | |
281 V8NPObject* v8npobject = reinterpret_cast<V8NPObject*>(npobj); | |
282 v8::Handle<v8::Object> v8object(v8npobject->v8Object); | |
283 if (V8ClassIndex::RANGE != V8DOMWrapper::domWrapperType(v8object)) | |
284 return false; | |
285 | |
286 Range* native = V8DOMWrapper::convertToNativeObject<WebCore::Range>(V8ClassIndex::RANGE, v8object); | |
287 if (!native) | |
288 return false; | |
289 | |
290 *range = WebRange(native); | |
291 return true; | |
292 } | |
293 | |
294 #endif | |
295 | |
296 bool WebBindings::getDragData(NPObject* event, int* eventId, WebDragData* data) | |
297 { | |
298 #if USE(V8) | |
299 return getDragDataImpl(event, eventId, data); | |
300 #else | |
301 // Not supported on other ports (JSC, etc). | |
302 return false; | |
303 #endif | |
304 } | |
305 | |
306 bool WebBindings::isDragEvent(NPObject* event) | |
307 { | |
308 int eventId; | |
309 return getDragData(event, &eventId, 0); | |
310 } | |
311 | |
312 bool WebBindings::getRange(NPObject* range, WebRange* webrange) | |
313 { | |
314 #if USE(V8) | |
315 return getRangeImpl(range, webrange); | |
316 #else | |
317 // Not supported on other ports (JSC, etc). | |
318 return false; | |
319 #endif | |
320 } | |
321 | |
322 } // namespace WebKit | |
OLD | NEW |