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