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 "ScopedPersistent.h" | |
| 35 #include "ScriptFunction.h" | |
| 36 #include "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 void call(ScriptValue scriptValue) { | |
| 52 // m_receiver->HaveValue(scriptValue.v8Value().As<v8::Integer>()->Value()) | |
| 53 // m_value = | |
|
abarth-chromium
2013/08/09 21:46:52
This code same seems incomplete...
| |
| 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 // m_resultCallback = adoptRef(new MyCallback(this)); | |
|
abarth-chromium
2013/08/09 21:46:52
Doesn't this example code create a reference cycle
| |
| 63 // promise->then(m_resultCallback); | |
| 64 // } | |
| 65 // | |
| 66 // void MyClass::HaveValue(int value) { | |
| 67 // .. deal with the value here. | |
| 68 // } | |
| 69 class ScriptPromise { | |
| 70 public: | |
| 71 ScriptPromise() : m_isolate(0) { } | |
|
abarth-chromium
2013/08/09 21:46:52
Can we remove this constructor? ScriptPromise sho
| |
| 72 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
| |
| 73 : m_promise(promise) | |
| 74 , m_isolate(isolate) { } | |
| 75 | |
| 76 void Reset(v8::Handle<v8::Value> promise, v8::Isolate* isolate) | |
| 77 { | |
| 78 m_promise.set(isolate, promise); | |
| 79 m_isolate = isolate; | |
| 80 } | |
|
abarth-chromium
2013/08/09 21:46:52
I'd skip this function.
| |
| 81 | |
| 82 void clear() | |
| 83 { | |
| 84 m_isolate = 0; | |
| 85 m_promise.clear(); | |
| 86 } | |
|
abarth-chromium
2013/08/09 21:46:52
This one too.
| |
| 87 bool then(ScriptFunction* function, ScriptExecutionContext* context) const | |
|
abarth-chromium
2013/08/09 21:46:52
I'd probably take a PassRefPtr<ScriptFunction> fun
| |
| 88 { | |
| 89 v8::Handle<v8::Value> promise = m_promise.newLocal(m_isolate); | |
| 90 if (!promise->IsObject()) { | |
| 91 return false; | |
| 92 } | |
|
abarth-chromium
2013/08/09 21:46:52
No need for { }.
Also, don't we need to check pro
| |
| 93 v8::Handle<v8::Object> promiseObject = promise.As<v8::Object>(); | |
| 94 v8::Handle<v8::Value> then = promiseObject->Get(v8::String::NewSymbol("t hen")); | |
| 95 if (then.IsEmpty() || !then->IsFunction()) | |
| 96 return false; | |
| 97 | |
| 98 v8::Handle<v8::Value> argv[] = { | |
| 99 function->toV8(m_isolate) | |
| 100 }; | |
| 101 V8ScriptRunner::callFunction(then.As<v8::Function>(), context, promiseOb ject, WTF_ARRAY_LENGTH(argv), argv); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 private: | |
| 106 ScopedPersistent<v8::Value> m_promise; | |
| 107 v8::Isolate* m_isolate; | |
| 108 }; | |
| 109 | |
| 110 } // namespace WebCore | |
| 111 | |
| 112 | |
| 113 #endif // ScriptPromiseResolver_h | |
| OLD | NEW |