text: // Copyright 2014 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  // This file has been auto-generated by code_generator_v8.py. DO NOT MODIFY!
  
  #include "config.h"
  #include "V8CSSStyleSheet.h"
  
  #include "bindings/core/v8/V8CSSRule.h"
  #include "bindings/core/v8/V8CSSRuleList.h"
  #include "bindings/v8/ExceptionState.h"
  #include "bindings/v8/V8DOMConfiguration.h"
  #include "bindings/v8/V8GCController.h"
  #include "bindings/v8/V8HiddenValue.h"
  #include "bindings/v8/V8ObjectConstructor.h"
  #include "core/dom/ContextFeatures.h"
  #include "core/dom/Document.h"
  #include "core/dom/Element.h"
  #include "core/frame/UseCounter.h"
  #include "platform/RuntimeEnabledFeatures.h"
  #include "platform/TraceEvent.h"
  #include "wtf/GetPtr.h"
  #include "wtf/RefPtr.h"
  
  namespace WebCore {
  
  static void initializeScriptWrappableForInterface(CSSStyleSheet* object)
  {
      if (ScriptWrappable::wrapperCanBeStoredInObject(object))
          ScriptWrappable::fromObject(object)->setTypeInfo(&V8CSSStyleSheet::wrapperTypeInfo);
      else
          ASSERT_NOT_REACHED();
  }
  
  } // namespace WebCore
  
  void webCoreInitializeScriptWrappableForInterface(WebCore::CSSStyleSheet* object)
  {
      WebCore::initializeScriptWrappableForInterface(object);
  }
  
  namespace WebCore {
  const WrapperTypeInfo V8CSSStyleSheet::wrapperTypeInfo = { gin::kEmbedderBlink, V8CSSStyleSheet::domTemplate, V8CSSStyleSheet::derefObject, 0, 0, V8CSSStyleSheet::visitDOMWrapper, V8CSSStyleSheet::installPerContextEnabledMethods, &V8StyleSheet::wrapperTypeInfo, WrapperTypeObjectPrototype, WillBeGarbageCollectedObject };
  
  namespace CSSStyleSheetV8Internal {
  
  template <typename T> void V8_USE(T) { }
  
  static void ownerRuleAttributeGetter(const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      v8::Handle<v8::Object> holder = info.Holder();
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(holder);
      RefPtrWillBeRawPtr<CSSRule> result(impl->ownerRule());
      if (result && DOMDataStore::setReturnValueFromWrapper<V8CSSRule>(info.GetReturnValue(), result.get()))
          return;
      v8::Handle<v8::Value> wrapper = toV8(result.get(), holder, info.GetIsolate());
      if (!wrapper.IsEmpty()) {
          V8HiddenValue::setHiddenValue(info.GetIsolate(), holder, v8AtomicString(info.GetIsolate(), "ownerRule"), wrapper);
          v8SetReturnValue(info, wrapper);
      }
  }
  
  static void ownerRuleAttributeGetterCallback(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMGetter");
      CSSStyleSheetV8Internal::ownerRuleAttributeGetter(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void cssRulesAttributeGetter(const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      v8::Handle<v8::Object> holder = info.Holder();
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(holder);
      RefPtrWillBeRawPtr<CSSRuleList> result(impl->cssRules());
      if (result && DOMDataStore::setReturnValueFromWrapper<V8CSSRuleList>(info.GetReturnValue(), result.get()))
          return;
      v8::Handle<v8::Value> wrapper = toV8(result.get(), holder, info.GetIsolate());
      if (!wrapper.IsEmpty()) {
          V8HiddenValue::setHiddenValue(info.GetIsolate(), holder, v8AtomicString(info.GetIsolate(), "cssRules"), wrapper);
          v8SetReturnValue(info, wrapper);
      }
  }
  
  static void cssRulesAttributeGetterCallback(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMGetter");
      CSSStyleSheetV8Internal::cssRulesAttributeGetter(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void rulesAttributeGetter(const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      v8::Handle<v8::Object> holder = info.Holder();
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(holder);
      RefPtrWillBeRawPtr<CSSRuleList> result(impl->rules());
      if (result && DOMDataStore::setReturnValueFromWrapper<V8CSSRuleList>(info.GetReturnValue(), result.get()))
          return;
      v8::Handle<v8::Value> wrapper = toV8(result.get(), holder, info.GetIsolate());
      if (!wrapper.IsEmpty()) {
          V8HiddenValue::setHiddenValue(info.GetIsolate(), holder, v8AtomicString(info.GetIsolate(), "rules"), wrapper);
          v8SetReturnValue(info, wrapper);
      }
  }
  
  static void rulesAttributeGetterCallback(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMGetter");
      UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::CSSStyleSheetRules);
      CSSStyleSheetV8Internal::rulesAttributeGetter(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void insertRuleMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      ExceptionState exceptionState(ExceptionState::ExecutionContext, "insertRule", "CSSStyleSheet", info.Holder(), info.GetIsolate());
      if (UNLIKELY(info.Length() < 1)) {
          throwMinimumArityTypeError(exceptionState, 1, info.Length());
          return;
      }
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(info.Holder());
      V8StringResource<> rule;
      unsigned index;
      {
          v8::TryCatch block;
          V8RethrowTryCatchScope rethrow(block);
          TOSTRING_VOID_INTERNAL(rule, info[0]);
          if (UNLIKELY(info.Length() <= 1)) {
              unsigned result = impl->insertRule(rule, exceptionState);
              if (exceptionState.hadException()) {
                  exceptionState.throwIfNeeded();
                  return;
              }
              v8SetReturnValueUnsigned(info, result);
              return;
          }
          TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(index, toUInt32(info[1], exceptionState), exceptionState);
      }
      unsigned result = impl->insertRule(rule, index, exceptionState);
      if (exceptionState.hadException()) {
          exceptionState.throwIfNeeded();
          return;
      }
      v8SetReturnValueUnsigned(info, result);
  }
  
  static void insertRuleMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMMethod");
      CSSStyleSheetV8Internal::insertRuleMethod(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void deleteRuleMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      ExceptionState exceptionState(ExceptionState::ExecutionContext, "deleteRule", "CSSStyleSheet", info.Holder(), info.GetIsolate());
      if (UNLIKELY(info.Length() < 1)) {
          throwMinimumArityTypeError(exceptionState, 1, info.Length());
          return;
      }
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(info.Holder());
      unsigned index;
      {
          v8::TryCatch block;
          V8RethrowTryCatchScope rethrow(block);
          TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(index, toUInt32(info[0], exceptionState), exceptionState);
      }
      impl->deleteRule(index, exceptionState);
      if (exceptionState.hadException()) {
          exceptionState.throwIfNeeded();
          return;
      }
  }
  
  static void deleteRuleMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMMethod");
      CSSStyleSheetV8Internal::deleteRuleMethod(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void addRuleMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      ExceptionState exceptionState(ExceptionState::ExecutionContext, "addRule", "CSSStyleSheet", info.Holder(), info.GetIsolate());
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(info.Holder());
      V8StringResource<> selector;
      V8StringResource<> style;
      unsigned index;
      {
          v8::TryCatch block;
          V8RethrowTryCatchScope rethrow(block);
          TOSTRING_VOID_INTERNAL(selector, info[0]);
          TOSTRING_VOID_INTERNAL(style, info[1]);
          if (UNLIKELY(info.Length() <= 2)) {
              int result = impl->addRule(selector, style, exceptionState);
              if (exceptionState.hadException()) {
                  exceptionState.throwIfNeeded();
                  return;
              }
              v8SetReturnValueInt(info, result);
              return;
          }
          TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(index, toUInt32(info[2], exceptionState), exceptionState);
      }
      int result = impl->addRule(selector, style, index, exceptionState);
      if (exceptionState.hadException()) {
          exceptionState.throwIfNeeded();
          return;
      }
      v8SetReturnValueInt(info, result);
  }
  
  static void addRuleMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMMethod");
      UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::CSSStyleSheetAddRule);
      CSSStyleSheetV8Internal::addRuleMethod(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  static void removeRuleMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      ExceptionState exceptionState(ExceptionState::ExecutionContext, "removeRule", "CSSStyleSheet", info.Holder(), info.GetIsolate());
      CSSStyleSheet* impl = V8CSSStyleSheet::toNative(info.Holder());
      unsigned index;
      {
          v8::TryCatch block;
          V8RethrowTryCatchScope rethrow(block);
          TONATIVE_VOID_EXCEPTIONSTATE_INTERNAL(index, toUInt32(info[0], exceptionState), exceptionState);
      }
      impl->removeRule(index, exceptionState);
      if (exceptionState.hadException()) {
          exceptionState.throwIfNeeded();
          return;
      }
  }
  
  static void removeRuleMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
  {
      TRACE_EVENT_SET_SAMPLING_STATE("Blink", "DOMMethod");
      UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::CSSStyleSheetRemoveRule);
      CSSStyleSheetV8Internal::removeRuleMethod(info);
      TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8Execution");
  }
  
  } // namespace CSSStyleSheetV8Internal
  
  void V8CSSStyleSheet::visitDOMWrapper(void* object, const v8::Persistent<v8::Object>& wrapper, v8::Isolate* isolate)
  {
      CSSStyleSheet* impl = fromInternalPointer(object);
      // The ownerNode() method may return a reference or a pointer.
      if (Node* owner = WTF::getPtr(impl->ownerNode())) {
          Node* root = V8GCController::opaqueRootForGC(owner, isolate);
          isolate->SetReferenceFromGroup(v8::UniqueId(reinterpret_cast<intptr_t>(root)), wrapper);
          return;
      }
      setObjectGroup(object, wrapper, isolate);
  }
  
  static const V8DOMConfiguration::AttributeConfiguration V8CSSStyleSheetAttributes[] = {
      {"ownerRule", CSSStyleSheetV8Internal::ownerRuleAttributeGetterCallback, 0, 0, 0, 0, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
      {"cssRules", CSSStyleSheetV8Internal::cssRulesAttributeGetterCallback, 0, 0, 0, 0, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
      {"rules", CSSStyleSheetV8Internal::rulesAttributeGetterCallback, 0, 0, 0, 0, static_cast<v8::AccessControl>(v8::DEFAULT), static_cast<v8::PropertyAttribute>(v8::None), 0 /* on instance */},
  };
  
  static const V8DOMConfiguration::MethodConfiguration V8CSSStyleSheetMethods[] = {
      {"insertRule", CSSStyleSheetV8Internal::insertRuleMethodCallback, 0, 1},
      {"deleteRule", CSSStyleSheetV8Internal::deleteRuleMethodCallback, 0, 1},
      {"addRule", CSSStyleSheetV8Internal::addRuleMethodCallback, 0, 0},
      {"removeRule", CSSStyleSheetV8Internal::removeRuleMethodCallback, 0, 0},
  };
  
  static void configureV8CSSStyleSheetTemplate(v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Isolate* isolate)
  {
      functionTemplate->ReadOnlyPrototype();
  
      v8::Local<v8::Signature> defaultSignature;
      defaultSignature = V8DOMConfiguration::installDOMClassTemplate(functionTemplate, "CSSStyleSheet", V8StyleSheet::domTemplate(isolate), V8CSSStyleSheet::internalFieldCount,
          V8CSSStyleSheetAttributes, WTF_ARRAY_LENGTH(V8CSSStyleSheetAttributes),
          0, 0,
          V8CSSStyleSheetMethods, WTF_ARRAY_LENGTH(V8CSSStyleSheetMethods),
          isolate);
      v8::Local<v8::ObjectTemplate> instanceTemplate ALLOW_UNUSED = functionTemplate->InstanceTemplate();
      v8::Local<v8::ObjectTemplate> prototypeTemplate ALLOW_UNUSED = functionTemplate->PrototypeTemplate();
  
      // Custom toString template
      functionTemplate->Set(v8AtomicString(isolate, "toString"), V8PerIsolateData::from(isolate)->toStringTemplate());
  }
  
  v8::Handle<v8::FunctionTemplate> V8CSSStyleSheet::domTemplate(v8::Isolate* isolate)
  {
      return V8DOMConfiguration::domClassTemplate(isolate, const_cast<WrapperTypeInfo*>(&wrapperTypeInfo), configureV8CSSStyleSheetTemplate);
  }
  
  bool V8CSSStyleSheet::hasInstance(v8::Handle<v8::Value> v8Value, v8::Isolate* isolate)
  {
      return V8PerIsolateData::from(isolate)->hasInstance(&wrapperTypeInfo, v8Value);
  }
  
  v8::Handle<v8::Object> V8CSSStyleSheet::findInstanceInPrototypeChain(v8::Handle<v8::Value> v8Value, v8::Isolate* isolate)
  {
      return V8PerIsolateData::from(isolate)->findInstanceInPrototypeChain(&wrapperTypeInfo, v8Value);
  }
  
  CSSStyleSheet* V8CSSStyleSheet::toNativeWithTypeCheck(v8::Isolate* isolate, v8::Handle<v8::Value> value)
  {
      return hasInstance(value, isolate) ? fromInternalPointer(v8::Handle<v8::Object>::Cast(value)->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)) : 0;
  }
  
  v8::Handle<v8::Object> wrap(CSSStyleSheet* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
  {
      ASSERT(impl);
      ASSERT(!DOMDataStore::containsWrapper<V8CSSStyleSheet>(impl, isolate));
      return V8CSSStyleSheet::createWrapper(impl, creationContext, isolate);
  }
  
  v8::Handle<v8::Object> V8CSSStyleSheet::createWrapper(PassRefPtrWillBeRawPtr<CSSStyleSheet> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
  {
      ASSERT(impl);
      ASSERT(!DOMDataStore::containsWrapper<V8CSSStyleSheet>(impl.get(), isolate));
      if (ScriptWrappable::wrapperCanBeStoredInObject(impl.get())) {
          const WrapperTypeInfo* actualInfo = ScriptWrappable::fromObject(impl.get())->typeInfo();
          // Might be a XXXConstructor::wrapperTypeInfo instead of an XXX::wrapperTypeInfo. These will both have
          // the same object de-ref functions, though, so use that as the basis of the check.
          RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(actualInfo->derefObjectFunction == wrapperTypeInfo.derefObjectFunction);
      }
  
      v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, &wrapperTypeInfo, toInternalPointer(impl.get()), isolate);
      if (UNLIKELY(wrapper.IsEmpty()))
          return wrapper;
  
      installPerContextEnabledProperties(wrapper, impl.get(), isolate);
      V8DOMWrapper::associateObjectWithWrapper<V8CSSStyleSheet>(impl, &wrapperTypeInfo, wrapper, isolate, WrapperConfiguration::Dependent);
      return wrapper;
  }
  
  void V8CSSStyleSheet::derefObject(void* object)
  {
  #if !ENABLE(OILPAN)
      fromInternalPointer(object)->deref();
  #endif // !ENABLE(OILPAN)
  }
  
  template<>
  v8::Handle<v8::Value> toV8NoInline(CSSStyleSheet* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
  {
      return toV8(impl, creationContext, isolate);
  }
  
  } // namespace WebCore
  