OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009 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 23 matching lines...) Expand all Loading... |
34 #include "bindings/v8/ScopedPersistent.h" | 34 #include "bindings/v8/ScopedPersistent.h" |
35 #include "core/dom/NodeFilterCondition.h" | 35 #include "core/dom/NodeFilterCondition.h" |
36 #include <v8.h> | 36 #include <v8.h> |
37 #include "wtf/PassRefPtr.h" | 37 #include "wtf/PassRefPtr.h" |
38 | 38 |
39 namespace WebCore { | 39 namespace WebCore { |
40 | 40 |
41 class Node; | 41 class Node; |
42 class ScriptState; | 42 class ScriptState; |
43 | 43 |
| 44 // V8NodeFilterCondition maintains a Javascript implemented callback for |
| 45 // filtering Node returned by NodeIterator/TreeWalker. |
| 46 // A NodeFilterCondition is referenced by a NodeFilter, and A NodeFilter is |
| 47 // referenced by a NodeIterator/TreeWalker. As V8NodeFilterCondition maintains |
| 48 // a Javascript callback which may reference Document, we need to avoid circular |
| 49 // reference spanning V8/Blink object space. |
| 50 // To address this issue, V8NodeFilterCondition holds a weak reference to |
| 51 // |m_filter|, the Javascript value, and the whole reference is exposed to V8 to |
| 52 // let V8 GC handle collection of |m_filter|. |
| 53 // (DOM) |
| 54 // NodeIterator ----RefPtr----> NodeFilter ----RefPtr----> NodeFilterCondition |
| 55 // | ^ | ^ | |
| 56 // weak | weak | ScopedPersistent(weak) |
| 57 // | RefPtr | RefPtr | |
| 58 // v | v | v |
| 59 // NodeIterator --HiddenValue--> NodeFilter --HiddenValue--> JS Callback |
| 60 // (V8) |
44 class V8NodeFilterCondition : public NodeFilterCondition { | 61 class V8NodeFilterCondition : public NodeFilterCondition { |
45 public: | 62 public: |
46 static PassRefPtr<V8NodeFilterCondition> create(v8::Handle<v8::Value> filter
) | 63 static PassRefPtr<V8NodeFilterCondition> create(v8::Handle<v8::Value> filter
, v8::Handle<v8::Object> owner) |
47 { | 64 { |
48 return adoptRef(new V8NodeFilterCondition(filter)); | 65 return adoptRef(new V8NodeFilterCondition(filter, owner)); |
49 } | 66 } |
50 | 67 |
51 virtual ~V8NodeFilterCondition(); | 68 virtual ~V8NodeFilterCondition(); |
52 | 69 |
53 virtual short acceptNode(ScriptState*, Node*) const; | 70 virtual short acceptNode(ScriptState*, Node*) const; |
54 | 71 |
55 private: | 72 private: |
56 explicit V8NodeFilterCondition(v8::Handle<v8::Value> filter); | 73 // As the value |filter| is maintained by V8GC, the |owner| which references |
| 74 // V8NodeFilterCondition, usually a wrapper of NodeFilter, is specified here |
| 75 // to hold a strong reference to |filter|. |
| 76 V8NodeFilterCondition(v8::Handle<v8::Value> filter, v8::Handle<v8::Object> o
wner); |
| 77 |
| 78 static void makeWeakCallback(v8::Isolate*, v8::Persistent<v8::Value>*, V8Nod
eFilterCondition*); |
57 | 79 |
58 ScopedPersistent<v8::Value> m_filter; | 80 ScopedPersistent<v8::Value> m_filter; |
59 }; | 81 }; |
60 | 82 |
61 } // namespace WebCore | 83 } // namespace WebCore |
62 | 84 |
63 #endif // V8NodeFilterCondition_h | 85 #endif // V8NodeFilterCondition_h |
OLD | NEW |