Index: Source/bindings/core/v8/ScrollStateCallbackToScriptValue.cpp |
diff --git a/Source/bindings/core/v8/ScrollStateCallbackToScriptValue.cpp b/Source/bindings/core/v8/ScrollStateCallbackToScriptValue.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..11e8091195b951970026fce9b559954e0779adae |
--- /dev/null |
+++ b/Source/bindings/core/v8/ScrollStateCallbackToScriptValue.cpp |
@@ -0,0 +1,85 @@ |
+// Copyright 2015 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. |
+ |
+#include "config.h" |
+#include "core/page/scrolling/ScrollStateCallback.h" |
+ |
+#include "bindings/core/v8/V8Element.h" |
+#include "bindings/core/v8/V8ScrollState.h" |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+static const char applyScrollRequiresScrollStateObject[] = "applyScroll takes a ScrollState object."; |
+static const char distributeScrollRequiresScrollStateObject[] = "distributeScroll takes a ScrollState object."; |
+ |
+// Key in |data| passed to scrollStateFunctionCallback. |
+const char* kHandlerFunction = "handler_function"; |
+ |
+} // namespace |
+ |
+static void scrollStateFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
+{ |
+ v8::Isolate* isolate = info.GetIsolate(); |
+ |
+ v8::Local<v8::Object> data = info.Data().As<v8::Object>(); |
+ |
+ v8::Local<v8::Value> localScrollStateCallback; |
+ v8::MaybeLocal<v8::Value> maybeLocalScrollStateCallback |
+ = data->Get(v8::String::NewFromUtf8(info.GetIsolate(), kHandlerFunction)); |
+ if (!maybeLocalScrollStateCallback.ToLocal(&localScrollStateCallback)) |
haraken
2015/06/30 07:08:30
if (data->Get(...).ToLocal())
Nit: We want to avo
|
+ return; |
+ |
+ ASSERT(!localScrollStateCallback->IsUndefined()); |
+ ASSERT(localScrollStateCallback->IsExternal()); |
+ ScrollStateCallback* scrollStateCallback |
+ = static_cast<ScrollStateCallback*>(localScrollStateCallback.As<v8::External>()->Value()); |
+ |
+ ASSERT(scrollStateCallback->callbackType() != ScrollStateCallback::CallbackType::UNSET); |
+ bool isDistributeScroll |
+ = scrollStateCallback->callbackType() == ScrollStateCallback::CallbackType::DISTRIBUTE_SCROLL; |
+ const char* errorMessage |
+ = isDistributeScroll ? distributeScrollRequiresScrollStateObject : applyScrollRequiresScrollStateObject; |
+ if (!info[0]->IsObject()) { |
+ info.GetIsolate()->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, errorMessage))); |
+ return; |
+ } |
+ v8::Local<v8::Object> scrollStateObject = info[0]->ToObject(); |
+ |
+ if (!V8ScrollState::wrapperTypeInfo.domTemplateFunction(isolate)->HasInstance(scrollStateObject)) { |
+ info.GetIsolate()->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, errorMessage))); |
+ return; |
+ } |
+ |
+ ScrollState* scrollState = toScriptWrappable(scrollStateObject)->toImpl<ScrollState>(); |
+ ASSERT(scrollState); |
+ |
+ Element* targetElement = toScriptWrappable(info.Holder())->toImpl<Element>(); |
+ ASSERT(targetElement); |
+ scrollStateCallback->handleEventForElement(*targetElement, scrollState); |
+} |
+ |
+ScriptValue scrollStateCallbackToScriptValue(ScrollStateCallback& scrollStateCallback, ScriptState* scriptState) |
+{ |
+ v8::Isolate* isolate = scriptState->isolate(); |
+ v8::Local<v8::Signature> signature |
+ = v8::Signature::New(isolate, V8Element::wrapperTypeInfo.domTemplateFunction(isolate)); |
+ |
+ v8::Local<v8::Object> data = v8::Object::New(isolate); |
+ data->Set(v8::String::NewFromUtf8(isolate, kHandlerFunction), v8::External::New(isolate, &scrollStateCallback)); |
+ |
+ v8::Local<v8::FunctionTemplate> functionTemplate |
+ = v8::FunctionTemplate::New(scriptState->isolate(), &scrollStateFunctionCallback, data, signature); |
+ |
+ ASSERT(scrollStateCallback.callbackType() != ScrollStateCallback::CallbackType::UNSET); |
+ |
+ bool isDistributeScroll = scrollStateCallback.callbackType() |
+ == ScrollStateCallback::CallbackType::DISTRIBUTE_SCROLL; |
+ functionTemplate->SetClassName(v8AtomicString(isolate, isDistributeScroll ? "distributeScroll" : "applyScroll")); |
+ v8::MaybeLocal<v8::Function> function = functionTemplate->GetFunction(scriptState->context()); |
+ return ScriptValue(scriptState, function); |
+} |
+ |
+} // namespace blink |