Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: webkit/port/bindings/v8/v8_proxy.h

Issue 3195: Use static type information from IDL to streamline the wrapping and unwrappin... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_PROXY_H__ 5 #ifndef V8_PROXY_H__
6 #define V8_PROXY_H__ 6 #define V8_PROXY_H__
7 7
8 #include <v8.h> 8 #include <v8.h>
9 #include "v8_index.h" 9 #include "v8_index.h"
10 #include "v8_custom.h" 10 #include "v8_custom.h"
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 static bool IsFromSameOrigin(Frame* target, bool report_error); 269 static bool IsFromSameOrigin(Frame* target, bool report_error);
270 270
271 // Check if it is safe to access the given node from the 271 // Check if it is safe to access the given node from the
272 // current security context. 272 // current security context.
273 static bool CheckNodeSecurity(Node* node); 273 static bool CheckNodeSecurity(Node* node);
274 274
275 // Return true if the current security context can access the target frame. 275 // Return true if the current security context can access the target frame.
276 static bool CanAccess(Frame* target); 276 static bool CanAccess(Frame* target);
277 277
278 // Create a V8 wrapper for a C pointer 278 // Create a V8 wrapper for a C pointer
279 static v8::Handle<v8::Value> WrapCPointer(void* cptr); 279 static inline v8::Handle<v8::Value> WrapCPointer(void* cptr);
280 280
281 static v8::Handle<v8::Value> CheckNewLegal(const v8::Arguments& args); 281 static v8::Handle<v8::Value> CheckNewLegal(const v8::Arguments& args);
282 282
283 // Take C pointer out of a v8 wrapper 283 // Take C pointer out of a v8 wrapper
284 template <class C> 284 template <class C>
285 static C* ExtractCPointer(v8::Handle<v8::Value> obj) { 285 static C* ExtractCPointer(v8::Handle<v8::Value> obj) {
286 return static_cast<C*>(ExtractCPointerImpl(obj)); 286 return static_cast<C*>(ExtractCPointerImpl(obj));
287 } 287 }
288 288
289 289
290 static v8::Handle<v8::Script> CompileScript(v8::Handle<v8::String> code, 290 static v8::Handle<v8::Script> CompileScript(v8::Handle<v8::String> code,
291 const String& fileName, 291 const String& fileName,
292 int baseLine); 292 int baseLine);
293 293
294
295 #ifndef NDEBUG
294 // Checks if a v8 value can be a DOM wrapper 296 // Checks if a v8 value can be a DOM wrapper
295 static bool MaybeDOMWrapper(v8::Handle<v8::Value> obj); 297 static bool MaybeDOMWrapper(v8::Handle<v8::Value> value);
298 #endif
296 299
297 // Sets contents of a DOM wrapper, returns false if 300 // Sets contents of a DOM wrapper.
298 // obj is not a DOM wrapper type 301 static void SetDOMWrapper(v8::Handle<v8::Object> obj, int type, void* ptr);
299 static bool SetDOMWrapper(v8::Handle<v8::Object> obj, int type, void* ptr);
300 302
301 static v8::Handle<v8::Object> LookupDOMWrapper( 303 static v8::Handle<v8::Object> LookupDOMWrapper(
302 V8ClassIndex::V8WrapperType type, v8::Handle<v8::Value> value); 304 V8ClassIndex::V8WrapperType type, v8::Handle<v8::Value> value);
303 305
304 // A helper function extract native object pointer from a DOM wrapper 306 // A helper function extract native object pointer from a DOM wrapper
305 // and cast to the specified type. 307 // and cast to the specified type.
306 template <class C> 308 template <class C>
307 static C* DOMWrapperToNative(v8::Handle<v8::Value> object) { 309 static C* DOMWrapperToNative(v8::Handle<v8::Value> object) {
308 if (!MaybeDOMWrapper(object))
309 return 0;
310 return ExtractCPointer<C>(
311 v8::Handle<v8::Object>::Cast(object)->GetInternalField(0));
312 }
313
314 // A helper function extract native object pointer from a DOM wrapper
315 // and cast to the specified type.
316 template <class C>
317 static C* FastDOMWrapperToNative(v8::Handle<v8::Value> object) {
318 ASSERT(MaybeDOMWrapper(object)); 310 ASSERT(MaybeDOMWrapper(object));
319 return ExtractCPointer<C>( 311 return ExtractCPointer<C>(
320 v8::Handle<v8::Object>::Cast(object)->GetInternalField(0)); 312 v8::Handle<v8::Object>::Cast(object)->GetInternalField(0));
321 } 313 }
322 314
323 // A help function extract a node type pointer from a DOM wrapper. 315 // A help function extract a node type pointer from a DOM wrapper.
324 // Wrapped pointer must be cast to Node* first. 316 // Wrapped pointer must be cast to Node* first.
325 template <class C> 317 template <class C>
326 static C* DOMWrapperToNode(v8::Handle<v8::Value> object) { 318 static C* DOMWrapperToNode(v8::Handle<v8::Value> value) {
327 if (!MaybeDOMWrapper(object)) 319 ASSERT(MaybeDOMWrapper(value));
328 return 0; 320
321 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
322
323 ASSERT(GetDOMWrapperType(object) == V8ClassIndex::NODE);
324
329 v8::Handle<v8::Value> wrapper = 325 v8::Handle<v8::Value> wrapper =
330 v8::Handle<v8::Object>::Cast(object)->GetInternalField(0); 326 object->GetInternalField(V8Custom::kDOMWrapperObjectIndex);
331 return static_cast<C*>(ExtractCPointer<Node>(wrapper)); 327 return static_cast<C*>(ExtractCPointer<Node>(wrapper));
332 } 328 }
333 329
334 static v8::Handle<v8::Value> ToV8Object(V8ClassIndex::V8WrapperType type, 330 static v8::Handle<v8::Value> ToV8Object(V8ClassIndex::V8WrapperType type,
335 void* imp); 331 void* imp);
336 332 // Fast-path for Node objects.
337 template <class C> 333 static v8::Handle<v8::Value> NodeToV8Object(Node* node);
338 static C* FastToNativeObject(V8ClassIndex::V8WrapperType type,
339 v8::Handle<v8::Value> object) {
340 return static_cast<C*>(FastToNativeObjectImpl(type, object));
341 }
342 334
343 template <class C> 335 template <class C>
344 static C* ToNativeObject(V8ClassIndex::V8WrapperType type, 336 static C* ToNativeObject(V8ClassIndex::V8WrapperType type,
345 v8::Handle<v8::Value> object) { 337 v8::Handle<v8::Value> object) {
346 return static_cast<C*>(ToNativeObjectImpl(type, object)); 338 return static_cast<C*>(ToNativeObjectImpl(type, object));
347 } 339 }
348 340
349 static V8ClassIndex::V8WrapperType GetDOMWrapperType( 341 static V8ClassIndex::V8WrapperType GetDOMWrapperType(
350 v8::Handle<v8::Object> object); 342 v8::Handle<v8::Object> object);
351 343
352 // If the exception code is different from zero, a DOM exception is 344 // If the exception code is different from zero, a DOM exception is
353 // schedule to be thrown. 345 // schedule to be thrown.
354 static void SetDOMException(int exception_code); 346 static void SetDOMException(int exception_code);
355 347
356 // Schedule an error object to be thrown. 348 // Schedule an error object to be thrown.
357 static v8::Handle<v8::Value> ThrowError(ErrorType type, const char* message); 349 static v8::Handle<v8::Value> ThrowError(ErrorType type, const char* message);
358 350
359 // Create an instance of a function descriptor and set to the global object 351 // Create an instance of a function descriptor and set to the global object
360 // as a named property. Used by v8_test_shell. 352 // as a named property. Used by v8_test_shell.
361 static void BindJSObjectToWindow(Frame* frame, 353 static void BindJSObjectToWindow(Frame* frame,
362 const char* name, 354 const char* name,
363 int type, 355 int type,
364 v8::Handle<v8::FunctionTemplate> desc, 356 v8::Handle<v8::FunctionTemplate> desc,
365 void* imp); 357 void* imp);
366 358
367 static v8::Handle<v8::Value> EventToV8Object(Event* event); 359 static v8::Handle<v8::Value> EventToV8Object(Event* event);
368 static Event* ToNativeEvent(v8::Handle<v8::Value> jsevent) { 360 static Event* ToNativeEvent(v8::Handle<v8::Value> jsevent) {
361 if (!IsDOMEventWrapper(jsevent)) return 0;
369 return DOMWrapperToNative<Event>(jsevent); 362 return DOMWrapperToNative<Event>(jsevent);
370 } 363 }
371 364
372 static v8::Handle<v8::Value> EventTargetToV8Object(EventTarget* target); 365 static v8::Handle<v8::Value> EventTargetToV8Object(EventTarget* target);
373 // Wrap and unwrap JS event listeners 366 // Wrap and unwrap JS event listeners
374 static v8::Handle<v8::Value> EventListenerToV8Object(EventListener* target); 367 static v8::Handle<v8::Value> EventListenerToV8Object(EventListener* target);
375 368
376 // DOMImplementation is a singleton and it is handled in a special 369 // DOMImplementation is a singleton and it is handled in a special
377 // way. A wrapper is generated per document and stored in an 370 // way. A wrapper is generated per document and stored in an
378 // internal field of the document. When wrapping the 371 // internal field of the document. When wrapping the
(...skipping 28 matching lines...) Expand all
407 void* host, 400 void* host,
408 v8::Persistent<v8::Value> handle); 401 v8::Persistent<v8::Value> handle);
409 static void UnregisterGlobalHandle(void* host, 402 static void UnregisterGlobalHandle(void* host,
410 v8::Persistent<v8::Value> handle); 403 v8::Persistent<v8::Value> handle);
411 #endif 404 #endif
412 405
413 private: 406 private:
414 void initContextIfNeeded(); 407 void initContextIfNeeded();
415 void DisconnectEventListeners(); 408 void DisconnectEventListeners();
416 409
410 // Check whether a V8 value is a DOM Event wrapper
411 static bool IsDOMEventWrapper(v8::Handle<v8::Value> obj);
412
417 static void* ToNativeObjectImpl(V8ClassIndex::V8WrapperType type, 413 static void* ToNativeObjectImpl(V8ClassIndex::V8WrapperType type,
418 v8::Handle<v8::Value> object); 414 v8::Handle<v8::Value> object);
419 static void* FastToNativeObjectImpl(V8ClassIndex::V8WrapperType type,
420 v8::Handle<v8::Value> object);
421 415
422 // Take C pointer out of a v8 wrapper 416 // Take C pointer out of a v8 wrapper
423 static void* ExtractCPointerImpl(v8::Handle<v8::Value> obj); 417 static inline void* ExtractCPointerImpl(v8::Handle<v8::Value> obj);
424 418
425 static v8::Handle<v8::Object> NodeToV8Object(Node* node); 419 static v8::Handle<v8::Value> StyleSheetToV8Object(StyleSheet* sheet);
426 static v8::Handle<v8::Object> StyleSheetToV8Object(StyleSheet* sheet); 420 static v8::Handle<v8::Value> CSSValueToV8Object(CSSValue* value);
427 static v8::Handle<v8::Object> CSSValueToV8Object(CSSValue* value); 421 static v8::Handle<v8::Value> CSSRuleToV8Object(CSSRule* rule);
428 static v8::Handle<v8::Object> CSSRuleToV8Object(CSSRule* rule);
429 // Returns the JS wrapper of a window object, initializes the environment 422 // Returns the JS wrapper of a window object, initializes the environment
430 // of the window frame if needed. 423 // of the window frame if needed.
431 static v8::Handle<v8::Object> WindowToV8Object(DOMWindow* window); 424 static v8::Handle<v8::Value> WindowToV8Object(DOMWindow* window);
432 425
433 #if ENABLE(SVG) 426 #if ENABLE(SVG)
434 static v8::Handle<v8::Object> SVGElementInstanceToV8Object( 427 static v8::Handle<v8::Value> SVGElementInstanceToV8Object(
435 SVGElementInstance* instance); 428 SVGElementInstance* instance);
436 static v8::Handle<v8::Object> SVGObjectWithContextToV8Object( 429 static v8::Handle<v8::Value> SVGObjectWithContextToV8Object(
437 Peerable* object, V8ClassIndex::V8WrapperType type); 430 Peerable* object, V8ClassIndex::V8WrapperType type);
438 #endif 431 #endif
439 432
440 // Set hidden references in a DOMWindow object of a frame. 433 // Set hidden references in a DOMWindow object of a frame.
441 static void SetHiddenWindowReference(Frame* frame, 434 static void SetHiddenWindowReference(Frame* frame,
442 const int internal_index, 435 const int internal_index,
443 v8::Handle<v8::Object> jsobj); 436 v8::Handle<v8::Object> jsobj);
444 437
445 static V8ClassIndex::V8WrapperType GetHTMLElementType(HTMLElement* elm); 438 static V8ClassIndex::V8WrapperType GetHTMLElementType(HTMLElement* elm);
446 439
440 // The first parameter, desc_type, specifies the function descriptor
441 // used to create JS object. The second parameter, cptr_type, specifies
442 // the type of third parameter, impl, for type casting.
443 // For example, a HTML element has HTMLELEMENT desc_type, but always
444 // use NODE as cptr_type. JS wrapper has store cptr_type and impl as
Mads Ager (chromium) 2008/09/23 06:45:09 JS wrapper has store -> JS wrappers store
Feng Qian 2008/09/23 21:46:04 Done.
445 // internal fields.
447 static v8::Local<v8::Object> InstantiateV8Object( 446 static v8::Local<v8::Object> InstantiateV8Object(
448 V8ClassIndex::V8WrapperType type, void* impl); 447 V8ClassIndex::V8WrapperType desc_type,
448 V8ClassIndex::V8WrapperType cptr_type,
449 void* impl);
449 450
450 static const char* GetRangeExceptionName(int exception_code); 451 static const char* GetRangeExceptionName(int exception_code);
451 static const char* GetEventExceptionName(int exception_code); 452 static const char* GetEventExceptionName(int exception_code);
452 static const char* GetXMLHttpRequestExceptionName(int exception_code); 453 static const char* GetXMLHttpRequestExceptionName(int exception_code);
453 static const char* GetDOMExceptionName(int exception_code); 454 static const char* GetDOMExceptionName(int exception_code);
454 455
455 #if ENABLE(XPATH) 456 #if ENABLE(XPATH)
456 static const char* GetXPathExceptionName(int exception_code); 457 static const char* GetXPathExceptionName(int exception_code);
457 #endif 458 #endif
458 459
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 V8Proxy::SetDOMWrapper(args.Holder(), tag, obj); 513 V8Proxy::SetDOMWrapper(args.Holder(), tag, obj);
513 V8Proxy::SetJSWrapperForDOMObject( 514 V8Proxy::SetJSWrapperForDOMObject(
514 obj, v8::Persistent<v8::Object>::New(args.Holder())); 515 obj, v8::Persistent<v8::Object>::New(args.Holder()));
515 return args.Holder(); 516 return args.Holder();
516 } 517 }
517 518
518 } // namespace WebCore 519 } // namespace WebCore
519 520
520 #endif // V8_PROXY_H__ 521 #endif // V8_PROXY_H__
521 522
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698