Home | History | Annotate | Download | only in custom
      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 "V8CustomEvent.h"
     33 
     34 #include "V8Event.h"
     35 #include "bindings/v8/Dictionary.h"
     36 #include "bindings/v8/ScriptState.h"
     37 #include "bindings/v8/SerializedScriptValue.h"
     38 #include "bindings/v8/V8Binding.h"
     39 #include "bindings/v8/V8DOMWrapper.h"
     40 #include "bindings/v8/V8HiddenPropertyName.h"
     41 #include "core/dom/ContextFeatures.h"
     42 #include "core/page/Frame.h"
     43 #include "RuntimeEnabledFeatures.h"
     44 
     45 namespace WebCore {
     46 
     47 static v8::Handle<v8::Value> cacheState(v8::Handle<v8::Object> customEvent, v8::Handle<v8::Value> detail)
     48 {
     49     customEvent->SetHiddenValue(V8HiddenPropertyName::detail(), detail);
     50     return detail;
     51 }
     52 
     53 
     54 void V8CustomEvent::detailAttrGetterCustom(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
     55 {
     56     CustomEvent* event = V8CustomEvent::toNative(info.Holder());
     57 
     58     v8::Handle<v8::Value> result = info.Holder()->GetHiddenValue(V8HiddenPropertyName::detail());
     59 
     60     if (!result.IsEmpty()) {
     61         v8SetReturnValue(info, result);
     62         return;
     63     }
     64 
     65     if (!event->serializedDetail()) {
     66         // If we're in an isolated world and the event was created in the main world,
     67         // we need to find the 'detail' property on the main world wrapper and clone it.
     68         v8::Local<v8::Value> mainWorldDetail = getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenPropertyName::detail());
     69         if (!mainWorldDetail.IsEmpty())
     70             event->setSerializedDetail(SerializedScriptValue::createAndSwallowExceptions(mainWorldDetail, info.GetIsolate()));
     71     }
     72 
     73     if (event->serializedDetail()) {
     74         result = event->serializedDetail()->deserialize();
     75         v8SetReturnValue(info, cacheState(info.Holder(), result));
     76         return;
     77     }
     78 
     79     v8SetReturnValue(info, cacheState(info.Holder(), v8::Null(info.GetIsolate())));
     80 }
     81 
     82 void V8CustomEvent::initCustomEventMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
     83 {
     84     CustomEvent* event = V8CustomEvent::toNative(args.Holder());
     85     ASSERT(!event->serializedDetail());
     86 
     87     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, typeArg, args[0]);
     88     V8TRYCATCH_VOID(bool, canBubbleArg, args[1]->BooleanValue());
     89     V8TRYCATCH_VOID(bool, cancelableArg, args[2]->BooleanValue());
     90     v8::Handle<v8::Value> detailsArg = args[3];
     91 
     92     event->initEvent(typeArg, canBubbleArg, cancelableArg);
     93 
     94     if (!detailsArg.IsEmpty()) {
     95         args.Holder()->SetHiddenValue(V8HiddenPropertyName::detail(), detailsArg);
     96         if (isolatedWorldForIsolate(args.GetIsolate()))
     97             event->setSerializedDetail(SerializedScriptValue::createAndSwallowExceptions(detailsArg, args.GetIsolate()));
     98     }
     99 }
    100 
    101 } // namespace WebCore
    102