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 "V8Index.h"
     35 #include <wtf/PassRefPtr.h>
     36 #include <wtf/RefCounted.h>
     37 #include <wtf/RefPtr.h>
     38 
     39 namespace WebCore {
     40 
     41 class DOMWindow;
     42 class Frame;
     43 class String;
     44 
     45 // V8WindowShell represents all the per-global object state for a Frame that
     46 // persist between navigations.
     47 class V8DOMWindowShell : public RefCounted<V8DOMWindowShell> {
     48 public:
     49     static PassRefPtr<V8DOMWindowShell> create(Frame*);
     50 
     51     v8::Handle<v8::Context> context() const { return m_context; }
     52 
     53     // Update document object of the frame.
     54     void updateDocument();
     55 
     56     // Update the security origin of a document
     57     // (e.g., after setting docoument.domain).
     58     void updateSecurityOrigin();
     59 
     60     bool isContextInitialized();
     61 
     62     v8::Persistent<v8::Context> createNewContext(v8::Handle<v8::Object> global, int extensionGroup);
     63     void setContext(v8::Handle<v8::Context>);
     64     static bool installDOMWindow(v8::Handle<v8::Context> context, DOMWindow*);
     65 
     66     void initContextIfNeeded();
     67     void updateDocumentWrapper(v8::Handle<v8::Object> wrapper);
     68 
     69     void clearForNavigation();
     70     void clearForClose();
     71 
     72     void destroyGlobal();
     73 
     74     static v8::Handle<v8::Value> getHiddenObjectPrototype(v8::Handle<v8::Context>);
     75     // WARNING: Call |installHiddenObjectPrototype| only on fresh contexts!
     76     static void installHiddenObjectPrototype(v8::Handle<v8::Context>);
     77 
     78     // To create JS Wrapper objects, we create a cache of a 'boiler plate'
     79     // object, and then simply Clone that object each time we need a new one.
     80     // This is faster than going through the full object creation process.
     81     v8::Local<v8::Object> createWrapperFromCache(V8ClassIndex::V8WrapperType type)
     82     {
     83         int classIndex = V8ClassIndex::ToInt(type);
     84         v8::Local<v8::Object> clone(m_wrapperBoilerplates->CloneElementAt(classIndex));
     85         return clone.IsEmpty() ? createWrapperFromCacheSlowCase(type) : 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(V8ClassIndex::V8WrapperType);
    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
    111     // type.  We keep a single persistent handle to an array of the
    112     // activated boilerplates.
    113     v8::Persistent<v8::Array> m_wrapperBoilerplates;
    114 
    115     v8::Persistent<v8::Context> m_context;
    116     v8::Persistent<v8::Object> m_global;
    117     v8::Persistent<v8::Object> m_document;
    118 };
    119 
    120 } // namespace WebCore
    121 
    122 #endif // V8DOMWindowShell_h
    123