2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include <wtf/Forward.h>
42 class ScriptExecutionContext;
45 // Use an array to hold dependents. It works like a ref-counted scheme. A value can be added more than once to the DOM object.
46 void createHiddenDependency(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex);
47 void removeHiddenDependency(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex);
49 // Combo create/remove, for generated event-handler-setter bindings:
50 void transferHiddenDependency(v8::Handle<v8::Object>, EventListener* oldValue, v8::Local<v8::Value> newValue, int cacheIndex);
52 bool processingUserGesture();
53 bool shouldAllowNavigation(Frame*);
54 KURL completeURL(const String& relativeURL);
56 ScriptExecutionContext* getScriptExecutionContext();
58 void throwTypeMismatchException();
60 enum CallbackAllowedValueFlag {
61 CallbackAllowUndefined = 1,
62 CallbackAllowNull = 1 << 1
65 typedef unsigned CallbackAllowedValueFlags;
67 // 'FunctionOnly' is assumed for the created callback.
68 template <typename V8CallbackType>
69 PassRefPtr<V8CallbackType> createFunctionOnlyCallback(v8::Local<v8::Value> value, bool& succeeded, CallbackAllowedValueFlags acceptedValues = 0)
73 if (value->IsUndefined() && (acceptedValues & CallbackAllowUndefined))
76 if (value->IsNull() && (acceptedValues & CallbackAllowNull))
79 if (!value->IsFunction()) {
81 throwTypeMismatchException();
85 return V8CallbackType::create(value, getScriptExecutionContext());
88 class AllowAllocation {
90 inline AllowAllocation()
92 m_previous = m_current;
96 inline ~AllowAllocation()
98 m_current = m_previous;
101 static bool m_current;
107 class SafeAllocation {
109 static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>);
110 static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::ObjectTemplate>);
111 static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]);
114 v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function)
116 if (function.IsEmpty())
117 return v8::Local<v8::Object>();
118 AllowAllocation allow;
119 return function->NewInstance();
122 v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)
124 if (objectTemplate.IsEmpty())
125 return v8::Local<v8::Object>();
126 AllowAllocation allow;
127 return objectTemplate->NewInstance();
130 v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
132 if (function.IsEmpty())
133 return v8::Local<v8::Object>();
134 AllowAllocation allow;
135 return function->NewInstance(argc, argv);
138 } // namespace WebCore
140 #endif // V8Utilities_h