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" | |
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
| |
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[] | |
haraken
2015/06/27 08:09:33
Nit: Don't need the identation.
tdresser
2015/06/29 20:59:33
Done.
| |
16 = "applyScroll takes a ScrollState object."; | |
17 static const char distributeScrollRequiresScrollStateObject[] | |
18 = "distributeScroll takes a ScrollState object."; | |
19 | |
20 // Key in |data| passed to scrollStateFunctionCallback. | |
21 const char* kHandlerFunction = "handler_function"; | |
22 | |
23 } // namespace | |
24 | |
25 static void scrollStateFunctionCallback(const v8::FunctionCallbackInfo<v8::Value >& info) | |
26 { | |
27 v8::Isolate* isolate = info.GetIsolate(); | |
28 | |
29 v8::Local<v8::Object> data = info.Data().As<v8::Object>(); | |
30 v8::MaybeLocal<v8::Value> maybeScrollStateCallback = | |
31 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.
| |
32 v8::Local<v8::Value> localScrollStateCallback = | |
33 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
| |
34 | |
35 ASSERT(!localScrollStateCallback->IsUndefined()); | |
36 ASSERT(localScrollStateCallback->IsExternal()); | |
37 ScrollStateCallback* scrollStateCallback = static_cast<ScrollStateCallback*> ( | |
38 localScrollStateCallback.As<v8::External>()->Value()); | |
39 | |
40 ASSERT(scrollStateCallback->callbackType() != ScrollStateCallback::CallbackT ype::UNSET); | |
41 bool isDistributeScroll = scrollStateCallback->callbackType() | |
42 == ScrollStateCallback::CallbackType::DISTRIBUTE_SCROLL; | |
43 const char* errorMessage = isDistributeScroll | |
44 ? distributeScrollRequiresScrollStateObject | |
45 : applyScrollRequiresScrollStateObject; | |
46 if (!info[0]->IsObject()) { | |
47 info.GetIsolate()->ThrowException(v8::Exception::TypeError(v8::String::N ewFromUtf8( | |
48 isolate, errorMessage))); | |
49 return; | |
50 } | |
51 v8::Local<v8::Object> scrollStateObject = info[0]->ToObject(); | |
52 | |
53 if (!V8ScrollState::wrapperTypeInfo.domTemplateFunction(isolate)->HasInstanc e( | |
54 scrollStateObject)) { | |
55 info.GetIsolate()->ThrowException(v8::Exception::TypeError( | |
56 v8::String::NewFromUtf8(isolate, errorMessage))); | |
57 return; | |
58 } | |
59 | |
60 ScrollState* scrollState = toScriptWrappable(scrollStateObject)->toImpl<Scro llState>(); | |
61 ASSERT(scrollState); | |
62 | |
63 Element* targetElement = toScriptWrappable(info.Holder())->toImpl<Element>() ; | |
64 ASSERT(targetElement); | |
65 scrollStateCallback->handleEventForElement(*targetElement, scrollState); | |
66 } | |
67 | |
68 ScrollStateCallback::ScrollStateCallback() | |
69 : m_callbackType(CallbackType::UNSET) | |
70 { | |
71 } | |
72 | |
73 void ScrollStateCallback::handleEventForElement(Element& targetElement, ScrollSt ate* scrollState) | |
74 { | |
75 m_targetElement = &targetElement; | |
76 handleEvent(scrollState); | |
77 m_targetElement = nullptr; | |
78 } | |
79 | |
80 ScriptValue ScrollStateCallback::toScriptValue(ScriptState* scriptState) | |
81 { | |
82 v8::Isolate* isolate = scriptState->isolate(); | |
83 v8::Local<v8::Signature> signature | |
84 = v8::Signature::New(isolate, V8Element::wrapperTypeInfo.domTemplateFunc tion(isolate)); | |
85 | |
86 v8::Local<v8::Object> data = v8::Object::New(isolate); | |
87 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
| |
88 | |
89 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New ( | |
90 scriptState->isolate(), &scrollStateFunctionCallback, data, signature); | |
91 | |
92 ASSERT(callbackType() != CallbackType::UNSET); | |
93 | |
94 bool isDistributeScroll = callbackType() == CallbackType::DISTRIBUTE_SCROLL; | |
95 functionTemplate->SetClassName( | |
96 v8AtomicString(isolate, isDistributeScroll ? "distributeScroll" : "apply Scroll")); | |
97 v8::MaybeLocal<v8::Function> function = functionTemplate->GetFunction(script State->context()); | |
98 return ScriptValue(scriptState, function); | |
99 } | |
100 | |
101 } // namespace blink | |
OLD | NEW |