• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 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 V8Utilities_h
32 #define V8Utilities_h
33 
34 #include <wtf/Forward.h>
35 #include <v8.h>
36 
37 namespace WebCore {
38 
39     class EventListener;
40     class Frame;
41     class KURL;
42     class ScriptExecutionContext;
43     class ScriptState;
44 
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);
48 
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);
51 
52     bool processingUserGesture();
53     bool shouldAllowNavigation(Frame*);
54     KURL completeURL(const String& relativeURL);
55 
56     ScriptExecutionContext* getScriptExecutionContext();
57 
58     void throwTypeMismatchException();
59 
60     enum CallbackAllowedValueFlag {
61         CallbackAllowUndefined = 1,
62         CallbackAllowNull = 1 << 1
63     };
64 
65     typedef unsigned CallbackAllowedValueFlags;
66 
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)
70     {
71         succeeded = true;
72 
73         if (value->IsUndefined() && (acceptedValues & CallbackAllowUndefined))
74             return 0;
75 
76         if (value->IsNull() && (acceptedValues & CallbackAllowNull))
77             return 0;
78 
79         if (!value->IsFunction()) {
80             succeeded = false;
81             throwTypeMismatchException();
82             return 0;
83         }
84 
85         return V8CallbackType::create(value, getScriptExecutionContext());
86     }
87 
88     class AllowAllocation {
89     public:
AllowAllocation()90         inline AllowAllocation()
91         {
92             m_previous = m_current;
93             m_current = true;
94         }
95 
~AllowAllocation()96         inline ~AllowAllocation()
97         {
98             m_current = m_previous;
99         }
100 
101         static bool m_current;
102 
103     private:
104         bool m_previous;
105     };
106 
107     class SafeAllocation {
108      public:
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[]);
112     };
113 
newInstance(v8::Handle<v8::Function> function)114     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function)
115     {
116         if (function.IsEmpty())
117             return v8::Local<v8::Object>();
118         AllowAllocation allow;
119         return function->NewInstance();
120     }
121 
newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)122     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)
123     {
124         if (objectTemplate.IsEmpty())
125             return v8::Local<v8::Object>();
126         AllowAllocation allow;
127         return objectTemplate->NewInstance();
128     }
129 
newInstance(v8::Handle<v8::Function> function,int argc,v8::Handle<v8::Value> argv[])130     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
131     {
132         if (function.IsEmpty())
133             return v8::Local<v8::Object>();
134         AllowAllocation allow;
135         return function->NewInstance(argc, argv);
136     }
137 
138 } // namespace WebCore
139 
140 #endif // V8Utilities_h
141