15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions of source code must retain the above copyright 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions in binary form must reproduce the above 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission. 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef V8NodeFilterCondition_h 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define V8NodeFilterCondition_h 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/ScopedPersistent.h" 3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/NodeFilterCondition.h" 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <v8.h> 3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/PassRefPtr.h" 385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore { 405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class Node; 425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class ScriptState; 435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 44f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// V8NodeFilterCondition maintains a Javascript implemented callback for 45f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// filtering Node returned by NodeIterator/TreeWalker. 46f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// A NodeFilterCondition is referenced by a NodeFilter, and A NodeFilter is 47f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// referenced by a NodeIterator/TreeWalker. As V8NodeFilterCondition maintains 48f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// a Javascript callback which may reference Document, we need to avoid circular 49f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// reference spanning V8/Blink object space. 50f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// To address this issue, V8NodeFilterCondition holds a weak reference to 51f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// |m_filter|, the Javascript value, and the whole reference is exposed to V8 to 52f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// let V8 GC handle collection of |m_filter|. 53f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// (DOM) 54f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// NodeIterator ----RefPtr----> NodeFilter ----RefPtr----> NodeFilterCondition 55f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// | ^ | ^ | 56f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// weak | weak | ScopedPersistent(weak) 57f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// | RefPtr | RefPtr | 58f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// v | v | v 59f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// NodeIterator --HiddenValue--> NodeFilter --HiddenValue--> JS Callback 60f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)// (V8) 615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class V8NodeFilterCondition : public NodeFilterCondition { 625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public: 639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles) static PassRefPtr<V8NodeFilterCondition> create(v8::Handle<v8::Value> filter, v8::Handle<v8::Object> owner, v8::Isolate* isolate) 645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles) return adoptRef(new V8NodeFilterCondition(filter, owner, isolate)); 665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) virtual ~V8NodeFilterCondition(); 695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) virtual short acceptNode(ScriptState*, Node*) const; 715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private: 73f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) // As the value |filter| is maintained by V8GC, the |owner| which references 74f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) // V8NodeFilterCondition, usually a wrapper of NodeFilter, is specified here 75f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) // to hold a strong reference to |filter|. 769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles) V8NodeFilterCondition(v8::Handle<v8::Value> filter, v8::Handle<v8::Object> owner, v8::Isolate*); 77f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) 78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static void setWeakCallback(const v8::WeakCallbackData<v8::Value, V8NodeFilterCondition>&); 795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ScopedPersistent<v8::Value> m_filter; 815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace WebCore 845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // V8NodeFilterCondition_h 86