Home | History | Annotate | Download | only in v8
      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 <v8.h>
     35 
     36 namespace WebCore {
     37 
     38     class EventListener;
     39     class Frame;
     40     class KURL;
     41     class ScriptExecutionContext;
     42     class ScriptState;
     43     class String;
     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     void navigateIfAllowed(Frame*, const KURL&, bool lockHistory, bool lockBackForwardList);
     56 
     57     ScriptExecutionContext* getScriptExecutionContext(ScriptState*);
     58     inline ScriptExecutionContext* getScriptExecutionContext() {
     59         return getScriptExecutionContext(0);
     60     }
     61 
     62     void reportException(ScriptState*, v8::TryCatch&);
     63 
     64     class AllowAllocation {
     65     public:
     66         inline AllowAllocation()
     67         {
     68             m_previous = m_current;
     69             m_current = true;
     70         }
     71 
     72         inline ~AllowAllocation()
     73         {
     74             m_current = m_previous;
     75         }
     76 
     77         static bool m_current;
     78 
     79     private:
     80         bool m_previous;
     81     };
     82 
     83     class SafeAllocation {
     84      public:
     85       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>);
     86       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::ObjectTemplate>);
     87       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]);
     88     };
     89 
     90     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function)
     91     {
     92         if (function.IsEmpty())
     93             return v8::Local<v8::Object>();
     94         AllowAllocation allow;
     95         return function->NewInstance();
     96     }
     97 
     98     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)
     99     {
    100         if (objectTemplate.IsEmpty())
    101             return v8::Local<v8::Object>();
    102         AllowAllocation allow;
    103         return objectTemplate->NewInstance();
    104     }
    105 
    106     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
    107     {
    108         if (function.IsEmpty())
    109             return v8::Local<v8::Object>();
    110         AllowAllocation allow;
    111         return function->NewInstance(argc, argv);
    112     }
    113 
    114 } // namespace WebCore
    115 
    116 #endif // V8Utilities_h
    117