1/*
2 * Copyright (C) 2012 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 V8DependentRetained_h
32#define V8DependentRetained_h
33
34#include "ScopedPersistent.h"
35#include "V8HiddenPropertyName.h"
36#include <v8.h>
37#include <wtf/Forward.h>
38#include <wtf/MainThread.h>
39#include <wtf/dtoa.h>
40
41namespace WebCore {
42
43#define V8_DEPENDENT_RETAINED "V8DependentRetained"
44
45class V8DependentRetained {
46public:
47 V8DependentRetained(v8::Handle<v8::Object> owner, v8::Handle<v8::Object> value)
48 : m_owner(owner)
49 , m_propertyName(createPropertyName())
50 {
51 ASSERT(!owner.IsEmpty());
52 ASSERT(!value.IsEmpty());
53 owner->SetHiddenValue(m_propertyName.get(), value);
54 m_owner.get().MakeWeak(this, &V8DependentRetained::weakCallback);
55 }
56
57 ~V8DependentRetained()
58 {
59 release();
60 }
61
62 v8::Local<v8::Object> get() const
63 {
64 return m_owner->GetHiddenValue(m_propertyName.get()).As<v8::Object>();
65 }
66
67 bool isEmpty() const
68 {
69 return m_owner.isEmpty() || get().IsEmpty();
70 }
71
72private:
73 static v8::Persistent<v8::String> createPropertyName()
74 {
75 ASSERT(isMainThread());
76 static double nextId = 0;
77 v8::HandleScope scope;
78 NumberToStringBuffer buffer;
79 Vector<char, 32> name;
80 const char* id = numberToString(nextId++, buffer);
81 name.append(V8_DEPENDENT_RETAINED, sizeof(V8_DEPENDENT_RETAINED) - 1);
82 name.append(id, strlen(id));
83 return v8::Persistent<v8::String>::New(V8HiddenPropertyName::hiddenReferenceName(name.data(), NewString));
84 }
85
86 static void weakCallback(v8::Persistent<v8::Value> object, void* parameter)
87 {
88 V8DependentRetained* value = static_cast<V8DependentRetained*>(parameter);
89 value->release();
90 }
91
92 void release()
93 {
94 if (!m_owner.isEmpty())
95 m_owner->DeleteHiddenValue(m_propertyName.get());
96 m_owner.clear();
97 }
98
99 ScopedPersistent<v8::Object> m_owner;
100 ScopedPersistent<v8::String> m_propertyName;
101};
102
103} // namespace WebCore
104
105#endif // V8DependentRetained_h