Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "core/page/scrolling/ScrollStateCallback.h" | |
| 7 | |
| 8 #include "bindings/core/v8/V8Element.h" | |
| 9 #include "bindings/core/v8/V8ScrollState.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 static const char applyScrollRequiresScrollStateObject[] = "applyScroll takes a ScrollState object."; | |
| 16 static const char distributeScrollRequiresScrollStateObject[] = "distributeScrol l takes a ScrollState object."; | |
| 17 | |
| 18 // Key in |data| passed to scrollStateFunctionCallback. | |
| 19 const char* kHandlerFunction = "handler_function"; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 static void scrollStateFunctionCallback(const v8::FunctionCallbackInfo<v8::Value >& info) | |
| 24 { | |
| 25 v8::Isolate* isolate = info.GetIsolate(); | |
| 26 | |
| 27 v8::Local<v8::Object> data = info.Data().As<v8::Object>(); | |
| 28 | |
| 29 v8::Local<v8::Value> localScrollStateCallback; | |
| 30 v8::MaybeLocal<v8::Value> maybeLocalScrollStateCallback | |
| 31 = data->Get(v8::String::NewFromUtf8(info.GetIsolate(), kHandlerFunction) ); | |
| 32 if (!maybeLocalScrollStateCallback.ToLocal(&localScrollStateCallback)) | |
|
haraken
2015/06/30 07:08:30
if (data->Get(...).ToLocal())
Nit: We want to avo
| |
| 33 return; | |
| 34 | |
| 35 ASSERT(!localScrollStateCallback->IsUndefined()); | |
| 36 ASSERT(localScrollStateCallback->IsExternal()); | |
| 37 ScrollStateCallback* scrollStateCallback | |
| 38 = static_cast<ScrollStateCallback*>(localScrollStateCallback.As<v8::Exte rnal>()->Value()); | |
| 39 | |
| 40 ASSERT(scrollStateCallback->callbackType() != ScrollStateCallback::CallbackT ype::UNSET); | |
| 41 bool isDistributeScroll | |
| 42 = scrollStateCallback->callbackType() == ScrollStateCallback::CallbackTy pe::DISTRIBUTE_SCROLL; | |
| 43 const char* errorMessage | |
| 44 = isDistributeScroll ? distributeScrollRequiresScrollStateObject : apply ScrollRequiresScrollStateObject; | |
| 45 if (!info[0]->IsObject()) { | |
| 46 info.GetIsolate()->ThrowException(v8::Exception::TypeError(v8::String::N ewFromUtf8(isolate, errorMessage))); | |
| 47 return; | |
| 48 } | |
| 49 v8::Local<v8::Object> scrollStateObject = info[0]->ToObject(); | |
| 50 | |
| 51 if (!V8ScrollState::wrapperTypeInfo.domTemplateFunction(isolate)->HasInstanc e(scrollStateObject)) { | |
| 52 info.GetIsolate()->ThrowException(v8::Exception::TypeError(v8::String::N ewFromUtf8(isolate, errorMessage))); | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 ScrollState* scrollState = toScriptWrappable(scrollStateObject)->toImpl<Scro llState>(); | |
| 57 ASSERT(scrollState); | |
| 58 | |
| 59 Element* targetElement = toScriptWrappable(info.Holder())->toImpl<Element>() ; | |
| 60 ASSERT(targetElement); | |
| 61 scrollStateCallback->handleEventForElement(*targetElement, scrollState); | |
| 62 } | |
| 63 | |
| 64 ScriptValue scrollStateCallbackToScriptValue(ScrollStateCallback& scrollStateCal lback, ScriptState* scriptState) | |
| 65 { | |
| 66 v8::Isolate* isolate = scriptState->isolate(); | |
| 67 v8::Local<v8::Signature> signature | |
| 68 = v8::Signature::New(isolate, V8Element::wrapperTypeInfo.domTemplateFunc tion(isolate)); | |
| 69 | |
| 70 v8::Local<v8::Object> data = v8::Object::New(isolate); | |
| 71 data->Set(v8::String::NewFromUtf8(isolate, kHandlerFunction), v8::External:: New(isolate, &scrollStateCallback)); | |
| 72 | |
| 73 v8::Local<v8::FunctionTemplate> functionTemplate | |
| 74 = v8::FunctionTemplate::New(scriptState->isolate(), &scrollStateFunction Callback, data, signature); | |
| 75 | |
| 76 ASSERT(scrollStateCallback.callbackType() != ScrollStateCallback::CallbackTy pe::UNSET); | |
| 77 | |
| 78 bool isDistributeScroll = scrollStateCallback.callbackType() | |
| 79 == ScrollStateCallback::CallbackType::DISTRIBUTE_SCROLL; | |
| 80 functionTemplate->SetClassName(v8AtomicString(isolate, isDistributeScroll ? "distributeScroll" : "applyScroll")); | |
| 81 v8::MaybeLocal<v8::Function> function = functionTemplate->GetFunction(script State->context()); | |
| 82 return ScriptValue(scriptState, function); | |
| 83 } | |
| 84 | |
| 85 } // namespace blink | |
| OLD | NEW |