| 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..f0c92dee86e65d7829a9a24b6151e5854d427c11
|
| --- /dev/null
|
| +++ b/Source/bindings/v8/ScriptPromise.h
|
| @@ -0,0 +1,105 @@
|
| +/*
|
| + * 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 "bindings/v8/ScopedPersistent.h"
|
| +#include "bindings/v8/ScriptFunction.h"
|
| +#include "bindings/v8/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 ScriptValue call(ScriptValue scriptValue) {
|
| +// m_receiver->HaveValue(scriptValue.v8Value().As<v8::Integer>()->Value())
|
| +// return scriptValue;
|
| +// }
|
| +// int value() const { return m_value; }
|
| +//
|
| +// private:
|
| +// RefPtr<MyClass> m_receiver;
|
| +// };
|
| +//
|
| +// void MyClass::saveValue(ScriptPromise* promise) {
|
| +// promise->then(adoptRef(new MyCallback(this)));
|
| +// }
|
| +//
|
| +// void MyClass::HaveValue(int value) {
|
| +// .. deal with the value here.
|
| +// }
|
| +class ScriptPromise {
|
| +public:
|
| + explicit ScriptPromise(ScriptValue promise)
|
| + : m_promise(promise.v8Value()) { }
|
| +
|
| + bool then(PassRefPtr<ScriptFunction> prpFunction)
|
| + {
|
| + if (m_promise.isEmpty())
|
| + return false;
|
| + v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
| + v8::Handle<v8::Value> promise = m_promise.newLocal(isolate);
|
| + if (!promise->IsObject())
|
| + return false;
|
| +
|
| + 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;
|
| +
|
| + RefPtr<ScriptFunction> function(prpFunction);
|
| + v8::Handle<v8::Value> argv[] = {
|
| + function->toV8(isolate)
|
| + };
|
| + m_result.set(isolate, V8ScriptRunner::callFunction(then.As<v8::Function>(), getScriptExecutionContext(), promiseObject, WTF_ARRAY_LENGTH(argv), argv));
|
| + return true;
|
| + }
|
| +
|
| + ScriptValue ResultPromise() { return ScriptValue(m_result.newLocal(v8::Isolate::GetCurrent())); }
|
| +
|
| +private:
|
| + ScopedPersistent<v8::Value> m_promise;
|
| + ScopedPersistent<v8::Value> m_result;
|
| +};
|
| +
|
| +} // namespace WebCore
|
| +
|
| +
|
| +#endif // ScriptPromise_h
|
|
|