Chromium Code Reviews| Index: Source/bindings/v8/ScriptPromise.h |
| diff --git a/Source/bindings/v8/ScriptPromise.h b/Source/bindings/v8/ScriptPromise.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6bd3e1fb8a64fea9533c790ced135e9461655dc3 |
| --- /dev/null |
| +++ b/Source/bindings/v8/ScriptPromise.h |
| @@ -0,0 +1,113 @@ |
| +/* |
| + * Copyright (C) 2013 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#ifndef ScriptPromise_h |
| +#define ScriptPromise_h |
| + |
| +#include "ScopedPersistent.h" |
| +#include "ScriptFunction.h" |
| +#include "V8ScriptRunner.h" |
| + |
| +#include <v8.h> |
| + |
| +namespace WebCore { |
| + |
| +// ScriptPromise is a class for hooking into the then() chain for a |
| +// Promise from the C++ world. |
| +// |
| +// To use this, provide a subclass of ScriptFunction where the 'call' |
| +// method the function to pass to 'Promise.then'. |
| +// |
| +// class MyCallback : public ScriptFunction { |
| +// public: |
| +// MyCallback(PassRefPtr<MyClass> reciever) : m_receiver(receiver) { } |
| +// virtual void call(ScriptValue scriptValue) { |
| +// m_receiver->HaveValue(scriptValue.v8Value().As<v8::Integer>()->Value()) |
| +// m_value = |
|
abarth-chromium
2013/08/09 21:46:52
This code same seems incomplete...
|
| +// } |
| +// int value() const { return m_value; } |
| +// |
| +// private: |
| +// RefPtr<MyClass> m_receiver; |
| +// }; |
| +// |
| +// void MyClass::saveValue(ScriptPromise* promise) { |
| +// m_resultCallback = adoptRef(new MyCallback(this)); |
|
abarth-chromium
2013/08/09 21:46:52
Doesn't this example code create a reference cycle
|
| +// promise->then(m_resultCallback); |
| +// } |
| +// |
| +// void MyClass::HaveValue(int value) { |
| +// .. deal with the value here. |
| +// } |
| +class ScriptPromise { |
| +public: |
| + ScriptPromise() : m_isolate(0) { } |
|
abarth-chromium
2013/08/09 21:46:52
Can we remove this constructor? ScriptPromise sho
|
| + explicit ScriptPromise(v8::Handle<v8::Value> promise, v8::Isolate* isolate) |
|
abarth-chromium
2013/08/09 21:46:52
I would have expected this object to take a Script
|
| + : m_promise(promise) |
| + , m_isolate(isolate) { } |
| + |
| + void Reset(v8::Handle<v8::Value> promise, v8::Isolate* isolate) |
| + { |
| + m_promise.set(isolate, promise); |
| + m_isolate = isolate; |
| + } |
|
abarth-chromium
2013/08/09 21:46:52
I'd skip this function.
|
| + |
| + void clear() |
| + { |
| + m_isolate = 0; |
| + m_promise.clear(); |
| + } |
|
abarth-chromium
2013/08/09 21:46:52
This one too.
|
| + bool then(ScriptFunction* function, ScriptExecutionContext* context) const |
|
abarth-chromium
2013/08/09 21:46:52
I'd probably take a PassRefPtr<ScriptFunction> fun
|
| + { |
| + v8::Handle<v8::Value> promise = m_promise.newLocal(m_isolate); |
| + if (!promise->IsObject()) { |
| + return false; |
| + } |
|
abarth-chromium
2013/08/09 21:46:52
No need for { }.
Also, don't we need to check pro
|
| + v8::Handle<v8::Object> promiseObject = promise.As<v8::Object>(); |
| + v8::Handle<v8::Value> then = promiseObject->Get(v8::String::NewSymbol("then")); |
| + if (then.IsEmpty() || !then->IsFunction()) |
| + return false; |
| + |
| + v8::Handle<v8::Value> argv[] = { |
| + function->toV8(m_isolate) |
| + }; |
| + V8ScriptRunner::callFunction(then.As<v8::Function>(), context, promiseObject, WTF_ARRAY_LENGTH(argv), argv); |
| + return true; |
| + } |
| + |
| +private: |
| + ScopedPersistent<v8::Value> m_promise; |
| + v8::Isolate* m_isolate; |
| +}; |
| + |
| +} // namespace WebCore |
| + |
| + |
| +#endif // ScriptPromiseResolver_h |