Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2009, 2010 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/custom/V8HTMLImageElementConstructor.h"
     33 
     34 #include "HTMLNames.h"
     35 #include "V8Document.h"
     36 #include "V8HTMLImageElement.h"
     37 #include "bindings/v8/BindingSecurity.h"
     38 #include "bindings/v8/V8Binding.h"
     39 #include "bindings/v8/V8ObjectConstructor.h"
     40 #include "core/dom/Document.h"
     41 #include "core/html/HTMLImageElement.h"
     42 #include "core/page/Frame.h"
     43 #include "wtf/RefPtr.h"
     44 
     45 namespace WebCore {
     46 
     47 WrapperTypeInfo V8HTMLImageElementConstructor::info = { V8HTMLImageElementConstructor::GetTemplate, V8HTMLImageElement::derefObject, 0, V8HTMLImageElement::toEventTarget, 0, V8HTMLImageElement::installPerContextPrototypeProperties, 0, WrapperTypeObjectPrototype };
     48 
     49 static void v8HTMLImageElementConstructorMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
     50 {
     51     if (!args.IsConstructCall()) {
     52         throwTypeError("DOM object constructor cannot be called as a function.", args.GetIsolate());
     53         return;
     54     }
     55 
     56     if (ConstructorMode::current() == ConstructorMode::WrapExistingObject) {
     57         v8SetReturnValue(args, args.Holder());
     58         return;
     59     }
     60 
     61     Document* document = currentDocument();
     62 
     63     // Make sure the document is added to the DOM Node map. Otherwise, the HTMLImageElement instance
     64     // may end up being the only node in the map and get garbage-collected prematurely.
     65     // FIXME: The correct way to do this would be to make HTMLImageElement derive from
     66     // ActiveDOMObject and use its interface to keep its wrapper alive. Then we would
     67     // remove this code and the special case in isObservableThroughDOM.
     68     toV8(document, args.Holder(), args.GetIsolate());
     69 
     70     int width;
     71     int height;
     72     int* optionalWidth = 0;
     73     int* optionalHeight = 0;
     74     if (args.Length() > 0) {
     75         width = toInt32(args[0]);
     76         optionalWidth = &width;
     77     }
     78     if (args.Length() > 1) {
     79         height = toInt32(args[1]);
     80         optionalHeight = &height;
     81     }
     82 
     83     RefPtr<HTMLImageElement> image = HTMLImageElement::createForJSConstructor(document, optionalWidth, optionalHeight);
     84     v8::Handle<v8::Object> wrapper = args.Holder();
     85     V8DOMWrapper::associateObjectWithWrapper<V8HTMLImageElement>(image.release(), &V8HTMLImageElementConstructor::info, wrapper, args.GetIsolate(), WrapperConfiguration::Dependent);
     86     v8SetReturnValue(args, wrapper);
     87 }
     88 
     89 v8::Handle<v8::FunctionTemplate> V8HTMLImageElementConstructor::GetTemplate(v8::Isolate* isolate, WrapperWorldType currentWorldType)
     90 {
     91     // This is only for getting a unique pointer which we can pass to privateTemplate.
     92     static const char* privateTemplateUniqueKey = "V8HTMLImageElementConstructor::GetTemplatePrivateTemplate";
     93     V8PerIsolateData* data = V8PerIsolateData::from(isolate);
     94     v8::Handle<v8::FunctionTemplate> result = data->privateTemplateIfExists(currentWorldType, &privateTemplateUniqueKey);
     95     if (!result.IsEmpty())
     96         return result;
     97 
     98     v8::HandleScope scope(isolate);
     99     result = v8::FunctionTemplate::New(v8HTMLImageElementConstructorMethodCustom);
    100 
    101     v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate();
    102     instance->SetInternalFieldCount(V8HTMLImageElement::internalFieldCount);
    103     result->SetClassName(v8::String::NewSymbol("HTMLImageElement"));
    104     result->Inherit(V8HTMLImageElement::GetTemplate(isolate, currentWorldType));
    105 
    106     data->setPrivateTemplate(currentWorldType, &privateTemplateUniqueKey, result);
    107     return scope.Close(result);
    108 }
    109 
    110 } // namespace WebCore
    111