Chromium Code Reviews| Index: Source/core/page/scrolling/ScrollStateCallback.cpp |
| diff --git a/Source/core/page/scrolling/ScrollStateCallback.cpp b/Source/core/page/scrolling/ScrollStateCallback.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10ec8fc309a94846c89b8987d9b84ccf5b75ccd7 |
| --- /dev/null |
| +++ b/Source/core/page/scrolling/ScrollStateCallback.cpp |
| @@ -0,0 +1,101 @@ |
| +// 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" |
|
haraken
2015/06/27 08:09:33
This file uses V8 APIs. So this should go to Sourc
tdresser
2015/06/29 20:59:33
It looks like this is a bit tricky, as the idl fil
|
| + |
| +#include "bindings/core/v8/V8Element.h" |
| +#include "bindings/core/v8/V8ScrollState.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| + static const char applyScrollRequiresScrollStateObject[] |
|
haraken
2015/06/27 08:09:33
Nit: Don't need the identation.
tdresser
2015/06/29 20:59:33
Done.
|
| + = "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::MaybeLocal<v8::Value> maybeScrollStateCallback = |
| + data->Get(v8::String::NewFromUtf8(info.GetIsolate(), kHandlerFunction)); |
|
haraken
2015/06/27 08:09:33
Nit: You don't need to wrap lines at 80 characters
tdresser
2015/06/29 20:59:33
Ran git cl format.
|
| + v8::Local<v8::Value> localScrollStateCallback = |
| + maybeScrollStateCallback.ToLocalChecked(); |
|
haraken
2015/06/27 08:09:33
if (!data->Get(...).ToLocal(&localScrollStateCallb
tdresser
2015/06/29 20:59:33
Done. (I'm still using a temporary, as it looks li
|
| + |
| + 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); |
| +} |
| + |
| +ScrollStateCallback::ScrollStateCallback() |
| + : m_callbackType(CallbackType::UNSET) |
| +{ |
| +} |
| + |
| +void ScrollStateCallback::handleEventForElement(Element& targetElement, ScrollState* scrollState) |
| +{ |
| + m_targetElement = &targetElement; |
| + handleEvent(scrollState); |
| + m_targetElement = nullptr; |
| +} |
| + |
| +ScriptValue ScrollStateCallback::toScriptValue(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, this)); |
|
tdresser
2015/06/26 14:53:15
I'm fairly sure this is wrong. The v8::External do
haraken
2015/06/27 08:09:33
I don't understand why you need the complexity abo
tdresser
2015/06/29 20:59:33
I've written up a quick doc summarizing the requir
|
| + |
| + v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New( |
| + scriptState->isolate(), &scrollStateFunctionCallback, data, signature); |
| + |
| + ASSERT(callbackType() != CallbackType::UNSET); |
| + |
| + bool isDistributeScroll = callbackType() == 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 |