Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2012 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 "bindings/v8/CustomElementWrapper.h"
     33 
     34 #include "V8HTMLElement.h"
     35 #include "V8HTMLElementWrapperFactory.h"
     36 #include "V8SVGElement.h"
     37 #include "V8SVGElementWrapperFactory.h"
     38 #include "bindings/v8/DOMDataStore.h"
     39 #include "bindings/v8/DOMWrapperWorld.h"
     40 #include "bindings/v8/V8PerContextData.h"
     41 #include "core/dom/CustomElement.h"
     42 #include "core/html/HTMLElement.h"
     43 #include "core/html/HTMLUnknownElement.h"
     44 #include "core/svg/SVGElement.h"
     45 
     46 namespace WebCore {
     47 
     48 template<typename ElementType>
     49 v8::Handle<v8::Object> createDirectWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
     50 
     51 template<>
     52 v8::Handle<v8::Object> createDirectWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
     53 {
     54     return createV8HTMLDirectWrapper(element, creationContext, isolate);
     55 }
     56 
     57 template<>
     58 v8::Handle<v8::Object> createDirectWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
     59 {
     60     return createV8SVGDirectWrapper(element, creationContext, isolate);
     61 }
     62 
     63 template<typename ElementType>
     64 v8::Handle<v8::Object> createFallbackWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
     65 
     66 template<>
     67 v8::Handle<v8::Object> createFallbackWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
     68 {
     69     return createV8HTMLFallbackWrapper(toHTMLUnknownElement(element), creationContext, isolate);
     70 }
     71 
     72 template<>
     73 v8::Handle<v8::Object> createFallbackWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
     74 {
     75     return createV8SVGFallbackWrapper(element, creationContext, isolate);
     76 }
     77 
     78 template<typename ElementType>
     79 v8::Handle<v8::Object> createUpgradeCandidateWrapper(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
     80 {
     81     if (CustomElement::isValidName(element->localName()))
     82         return createDirectWrapper(element, creationContext, isolate);
     83     if (createSpecificWrapper)
     84         return createSpecificWrapper(element, creationContext, isolate);
     85     return createFallbackWrapper(element, creationContext, isolate);
     86 }
     87 
     88 template<typename ElementType, typename WrapperType>
     89 v8::Handle<v8::Object> CustomElementWrapper<ElementType, WrapperType>::wrap(PassRefPtr<ElementType> element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
     90 {
     91     ASSERT(DOMDataStore::getWrapper<V8Element>(element.get(), isolate).IsEmpty());
     92 
     93     // FIXME: creationContext.IsEmpty() should never happen. Remove
     94     // this when callers (like InspectorController::inspect) are fixed
     95     // to never pass an empty creation context.
     96     v8::Handle<v8::Context> context = creationContext.IsEmpty() ? isolate->GetCurrentContext() : creationContext->CreationContext();
     97 
     98     if (!element->isUpgradedCustomElement() || DOMWrapperWorld::isolatedWorld(context))
     99         return createUpgradeCandidateWrapper(element.get(), creationContext, isolate, createSpecificWrapper);
    100 
    101     V8PerContextData* perContextData = V8PerContextData::from(context);
    102     if (!perContextData)
    103         return v8::Handle<v8::Object>();
    104 
    105     CustomElementBinding* binding = perContextData->customElementBinding(CustomElement::definitionFor(element.get()));
    106     v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, binding->wrapperType(), element.get(), isolate);
    107     if (wrapper.IsEmpty())
    108         return v8::Handle<v8::Object>();
    109 
    110     wrapper->SetPrototype(binding->prototype());
    111 
    112     V8DOMWrapper::associateObjectWithWrapper<WrapperType>(element, binding->wrapperType(), wrapper, isolate, WrapperConfiguration::Dependent);
    113     return wrapper;
    114 }
    115 
    116 template
    117 class CustomElementWrapper<HTMLElement, V8HTMLElement>;
    118 
    119 template
    120 class CustomElementWrapper<SVGElement, V8SVGElement>;
    121 
    122 } // namespace WebCore
    123