WebCore/bindings/v8/V8DOMWindowShell.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 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 V8DOMWindowShell_h
       
    32 #define V8DOMWindowShell_h
       
    33 
       
    34 #include "WrapperTypeInfo.h"
       
    35 #include <wtf/HashMap.h>
       
    36 #include <wtf/PassRefPtr.h>
       
    37 #include <wtf/RefCounted.h>
       
    38 #include <wtf/RefPtr.h>
       
    39 
       
    40 namespace WebCore {
       
    41 
       
    42 class DOMWindow;
       
    43 class Frame;
       
    44 class String;
       
    45 
       
    46 // V8WindowShell represents all the per-global object state for a Frame that
       
    47 // persist between navigations.
       
    48 class V8DOMWindowShell : public RefCounted<V8DOMWindowShell> {
       
    49 public:
       
    50     static PassRefPtr<V8DOMWindowShell> create(Frame*);
       
    51 
       
    52     v8::Handle<v8::Context> context() const { return m_context; }
       
    53 
       
    54     // Update document object of the frame.
       
    55     void updateDocument();
       
    56 
       
    57     // Update the security origin of a document
       
    58     // (e.g., after setting docoument.domain).
       
    59     void updateSecurityOrigin();
       
    60 
       
    61     bool isContextInitialized();
       
    62 
       
    63     v8::Persistent<v8::Context> createNewContext(v8::Handle<v8::Object> global, int extensionGroup);
       
    64     void setContext(v8::Handle<v8::Context>);
       
    65     static bool installDOMWindow(v8::Handle<v8::Context> context, DOMWindow*);
       
    66 
       
    67     void initContextIfNeeded();
       
    68     void updateDocumentWrapper(v8::Handle<v8::Object> wrapper);
       
    69 
       
    70     void clearForNavigation();
       
    71     void clearForClose();
       
    72 
       
    73     void destroyGlobal();
       
    74 
       
    75     static v8::Handle<v8::Value> getHiddenObjectPrototype(v8::Handle<v8::Context>);
       
    76     // WARNING: Call |installHiddenObjectPrototype| only on fresh contexts!
       
    77     static bool installHiddenObjectPrototype(v8::Handle<v8::Context>);
       
    78 
       
    79     // To create JS Wrapper objects, we create a cache of a 'boiler plate'
       
    80     // object, and then simply Clone that object each time we need a new one.
       
    81     // This is faster than going through the full object creation process.
       
    82     v8::Local<v8::Object> createWrapperFromCache(WrapperTypeInfo* type)
       
    83     {
       
    84         v8::Persistent<v8::Object> boilerplate = m_wrapperBoilerplates.get(type);
       
    85         return boilerplate.IsEmpty() ? createWrapperFromCacheSlowCase(type) : boilerplate->Clone();
       
    86     }
       
    87 
       
    88     static void setLocation(DOMWindow*, const String& relativeURL);
       
    89 
       
    90 private:
       
    91     V8DOMWindowShell(Frame*);
       
    92 
       
    93     void disposeContextHandles();
       
    94 
       
    95     void setSecurityToken();
       
    96     void clearDocumentWrapper();
       
    97 
       
    98     // The JavaScript wrapper for the document object is cached on the global
       
    99     // object for fast access. UpdateDocumentWrapperCache sets the wrapper
       
   100     // for the current document on the global object. ClearDocumentWrapperCache
       
   101     // deletes the document wrapper from the global object.
       
   102     void updateDocumentWrapperCache();
       
   103     void clearDocumentWrapperCache();
       
   104 
       
   105     v8::Local<v8::Object> createWrapperFromCacheSlowCase(WrapperTypeInfo*);
       
   106 
       
   107     Frame* m_frame;
       
   108 
       
   109     // For each possible type of wrapper, we keep a boilerplate object.
       
   110     // The boilerplate is used to create additional wrappers of the same type.
       
   111     typedef WTF::HashMap<WrapperTypeInfo*, v8::Persistent<v8::Object> > WrapperBoilerplateMap;
       
   112     WrapperBoilerplateMap m_wrapperBoilerplates;
       
   113 
       
   114     v8::Persistent<v8::Context> m_context;
       
   115     v8::Persistent<v8::Object> m_global;
       
   116     v8::Persistent<v8::Object> m_document;
       
   117 };
       
   118 
       
   119 } // namespace WebCore
       
   120 
       
   121 #endif // V8DOMWindowShell_h