OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 10 matching lines...) Expand all Loading... |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef GCObservation_h | 31 #ifndef ScriptPromise_h |
32 #define GCObservation_h | 32 #define ScriptPromise_h |
33 | 33 |
34 #include "bindings/v8/ScopedPersistent.h" | 34 #include "bindings/v8/ScopedPersistent.h" |
35 #include "wtf/PassRefPtr.h" | 35 #include "bindings/v8/ScriptFunction.h" |
36 #include "wtf/RefCounted.h" | 36 #include "bindings/v8/V8ScriptRunner.h" |
37 #include <v8.h> | 37 #include <v8.h> |
38 | 38 |
39 namespace WebCore { | 39 namespace WebCore { |
40 | 40 |
41 class GCObservation : public RefCounted<GCObservation> { | 41 class ScriptPromise { |
42 public: | 42 public: |
43 static PassRefPtr<GCObservation> create(v8::Handle<v8::Value> observedValue)
{ return adoptRef(new GCObservation(observedValue)); } | 43 explicit ScriptPromise(ScriptValue promise) |
44 ~GCObservation() { } | 44 : m_promise(promise) |
| 45 { |
| 46 ASSERT(!m_promise.hasNoValue()); |
| 47 } |
45 | 48 |
46 // Caution: It is only feasible to determine whether an object was | 49 bool then(v8::Handle<v8::Function> function, ScriptValue& result) |
47 // "near death"; it may have been kept alive through a weak | 50 { |
48 // handle. After reaching near-death, having been collected is the | 51 if (!m_promise.isObject()) |
49 // common case. | 52 return false; |
50 bool wasCollected() const { return m_collected; } | 53 v8::Handle<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
51 void setWasCollected(); | 54 v8::Handle<v8::Value> then = promise->Get(v8::String::NewSymbol("then"))
; |
| 55 if (then.IsEmpty() || !then->IsFunction()) |
| 56 return false; |
| 57 |
| 58 v8::Handle<v8::Value> argv[] = { function }; |
| 59 result = V8ScriptRunner::callFunction(then.As<v8::Function>(), getScript
ExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv); |
| 60 return true; |
| 61 } |
52 | 62 |
53 private: | 63 private: |
54 explicit GCObservation(v8::Handle<v8::Value>); | 64 ScriptValue m_promise; |
55 | |
56 ScopedPersistent<v8::Value> m_observed; | |
57 bool m_collected; | |
58 }; | 65 }; |
59 | 66 |
60 } | 67 } // namespace WebCore |
61 | 68 |
62 #endif // GCObservation_h | 69 |
| 70 #endif // ScriptPromise_h |
OLD | NEW |