15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * Copyright (C) 2013 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) 3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#ifndef UnsafePersistent_h 3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#define UnsafePersistent_h 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include <v8.h> 355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore { 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// An unsafe way to pass Persistent handles around. Do not use unless you know 3953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// what you're doing. UnsafePersistent is only safe to use when we know that the 4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// memory pointed by the it is not going away: 1) When GC cannot happen while 4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// the UnsafePersistent is alive or 2) when there is a strong Persistent keeping 4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// the memory alive while the UnsafePersistent is alive. 4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)template<typename T> class UnsafePersistent { 445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public: 4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) UnsafePersistent() : m_value(0) { } 4693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) explicit UnsafePersistent(T* value) : m_value(value) { } 4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) explicit UnsafePersistent(v8::Persistent<T>& handle) 485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 4993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) m_value = handle.ClearAndLeak(); 505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) UnsafePersistent(v8::Isolate* isolate, v8::Handle<T>& handle) 5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) { 5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) v8::Persistent<T> persistent(isolate, handle); 5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) m_value = persistent.ClearAndLeak(); 5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) } 575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) T* value() const 5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) { 6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return m_value; 6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) } 625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) // This is incredibly unsafe: the handle is valid only when this 6493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) // UnsafePersistent is alive and valid (see class level comment). 6593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) v8::Persistent<T>* persistent() 6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) { 6793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) v8::Persistent<T>* handle = reinterpret_cast<v8::Persistent<T>*>(&m_value); 6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return handle; 6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) } 705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 710019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch // FIXME: Remove this function, replace the usages with newLocal(). Do not 720019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch // add code which calls this function. 730019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch v8::Handle<T> deprecatedHandle() 7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) { 7593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) v8::Handle<T>* handle = reinterpret_cast<v8::Handle<T>*>(&m_value); 7693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) return *handle; 7793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) } 7893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) 7993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) void dispose() 8093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) { 8193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) persistent()->Dispose(); 8293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) m_value = 0; 8393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) } 8493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) 855267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) void clear() 865267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) { 875267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_value = 0; 885267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 895267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 9093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) v8::Local<T> newLocal(v8::Isolate* isolate) 9193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) { 9293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) return v8::Local<T>::New(isolate, *persistent()); 9393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) } 9493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) 9593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) bool isEmpty() const 9693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) { 9793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) return !m_value; 9893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) } 9993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) 100926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private: 10153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) T* m_value; 1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} // namespace WebCore 105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 10653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#endif // UnsafePersistent_h 107