Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #ifndef ScriptPromise_h | |
| 32 #define ScriptPromise_h | |
| 33 | |
| 34 #include "bindings/v8/ScopedPersistent.h" | |
| 35 #include "bindings/v8/ScriptFunction.h" | |
| 36 #include "bindings/v8/V8ScriptRunner.h" | |
| 37 | |
| 38 #include <v8.h> | |
| 39 | |
| 40 namespace WebCore { | |
| 41 | |
| 42 // ScriptPromise is a class for hooking into the then() chain for a | |
| 43 // Promise from the C++ world. | |
| 44 // | |
| 45 // To use this, provide a subclass of ScriptFunction where the 'call' | |
| 46 // method the function to pass to 'Promise.then'. | |
| 47 // | |
| 48 // class MyCallback : public ScriptFunction { | |
| 49 // public: | |
| 50 // MyCallback(PassRefPtr<MyClass> reciever) : m_receiver(receiver) { } | |
| 51 // virtual ScriptValue call(ScriptValue scriptValue) { | |
| 52 // m_receiver->HaveValue(scriptValue.v8Value().As<v8::Integer>()->Value()) | |
|
abarth-chromium
2013/08/15 00:35:15
This example code is no good. Why do you assume t
| |
| 53 // return scriptValue; | |
| 54 // } | |
| 55 // int value() const { return m_value; } | |
| 56 // | |
| 57 // private: | |
| 58 // RefPtr<MyClass> m_receiver; | |
| 59 // }; | |
| 60 // | |
| 61 // void MyClass::saveValue(ScriptPromise* promise) { | |
| 62 // promise->then(adoptRef(new MyCallback(this))); | |
|
abarth-chromium
2013/08/15 00:35:15
adoptRef(new MyCallback(this)) should be MyCall
| |
| 63 // } | |
| 64 // | |
| 65 // void MyClass::HaveValue(int value) { | |
| 66 // .. deal with the value here. | |
| 67 // } | |
| 68 class ScriptPromise { | |
| 69 public: | |
| 70 explicit ScriptPromise(ScriptValue promise) | |
| 71 : m_promise(promise.v8Value()) { } | |
| 72 | |
| 73 bool then(PassRefPtr<ScriptFunction> prpFunction) | |
| 74 { | |
| 75 if (m_promise.isEmpty()) | |
| 76 return false; | |
| 77 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 78 v8::Handle<v8::Value> promise = m_promise.newLocal(isolate); | |
| 79 if (!promise->IsObject()) | |
| 80 return false; | |
| 81 | |
| 82 v8::Handle<v8::Object> promiseObject = promise.As<v8::Object>(); | |
| 83 v8::Handle<v8::Value> then = promiseObject->Get(v8::String::NewSymbol("t hen")); | |
| 84 if (then.IsEmpty() || !then->IsFunction()) | |
| 85 return false; | |
| 86 | |
| 87 RefPtr<ScriptFunction> function(prpFunction); | |
| 88 v8::Handle<v8::Value> argv[] = { | |
| 89 function->toV8() | |
| 90 }; | |
| 91 m_result.set(isolate, V8ScriptRunner::callFunction(then.As<v8::Function> (), getScriptExecutionContext(), promiseObject, WTF_ARRAY_LENGTH(argv), argv)); | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 ScriptValue ResultPromise() { return ScriptValue(m_result.newLocal(v8::Isola te::GetCurrent())); } | |
|
abarth-chromium
2013/08/15 00:35:15
ResultPromise -> resultPromise
| |
| 96 | |
| 97 private: | |
| 98 ScopedPersistent<v8::Value> m_promise; | |
| 99 ScopedPersistent<v8::Value> m_result; | |
|
abarth-chromium
2013/08/15 00:35:15
Rather than storing m_result as a member, why not
| |
| 100 }; | |
| 101 | |
| 102 } // namespace WebCore | |
| 103 | |
| 104 | |
| 105 #endif // ScriptPromise_h | |
| OLD | NEW |