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 DOMWrapperWorld_h
32#define DOMWrapperWorld_h
33
34#include "bindings/core/v8/ScriptState.h"
35#include "platform/weborigin/SecurityOrigin.h"
36#include "wtf/MainThread.h"
37#include "wtf/PassRefPtr.h"
38#include "wtf/RefCounted.h"
39#include "wtf/RefPtr.h"
40#include "wtf/text/WTFString.h"
41#include <v8.h>
42
43namespace blink {
44
45class DOMDataStore;
46class ExecutionContext;
47class ScriptController;
48
49enum WorldIdConstants {
50    MainWorldId = 0,
51    // Embedder isolated worlds can use IDs in [1, 1<<29).
52    EmbedderWorldIdLimit = (1 << 29),
53    ScriptPreprocessorIsolatedWorldId,
54    PrivateScriptIsolatedWorldId,
55    IsolatedWorldIdLimit,
56    WorkerWorldId,
57    TestingWorldId,
58};
59
60// This class represent a collection of DOM wrappers for a specific world.
61class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
62public:
63    static PassRefPtr<DOMWrapperWorld> create(int worldId = -1, int extensionGroup = -1);
64
65    static const int mainWorldExtensionGroup = 0;
66    static const int privateScriptIsolatedWorldExtensionGroup = 1;
67    static PassRefPtr<DOMWrapperWorld> ensureIsolatedWorld(int worldId, int extensionGroup);
68    ~DOMWrapperWorld();
69    void dispose();
70
71    static bool isolatedWorldsExist() { return isolatedWorldCount; }
72    static void allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds);
73
74    static DOMWrapperWorld& world(v8::Handle<v8::Context> context)
75    {
76        return ScriptState::from(context)->world();
77    }
78
79    static DOMWrapperWorld& current(v8::Isolate* isolate)
80    {
81        if (isMainThread() && worldOfInitializingWindow) {
82            // It's possible that current() is being called while window is being initialized.
83            // In order to make current() workable during the initialization phase,
84            // we cache the world of the initializing window on worldOfInitializingWindow.
85            // If there is no initiazing window, worldOfInitializingWindow is 0.
86            return *worldOfInitializingWindow;
87        }
88        return world(isolate->GetCurrentContext());
89    }
90
91    static DOMWrapperWorld& mainWorld();
92    static DOMWrapperWorld& privateScriptIsolatedWorld();
93
94    static void setIsolatedWorldHumanReadableName(int worldID, const String&);
95    String isolatedWorldHumanReadableName();
96
97    // Associates an isolated world (see above for description) with a security
98    // origin. XMLHttpRequest instances used in that world will be considered
99    // to come from that origin, not the frame's.
100    static void setIsolatedWorldSecurityOrigin(int worldId, PassRefPtr<SecurityOrigin>);
101    SecurityOrigin* isolatedWorldSecurityOrigin();
102
103    // Associated an isolated world with a Content Security Policy. Resources
104    // embedded into the main world's DOM from script executed in an isolated
105    // world should be restricted based on the isolated world's DOM, not the
106    // main world's.
107    //
108    // FIXME: Right now, resource injection simply bypasses the main world's
109    // DOM. More work is necessary to allow the isolated world's policy to be
110    // applied correctly.
111    static void setIsolatedWorldContentSecurityPolicy(int worldId, const String& policy);
112    bool isolatedWorldHasContentSecurityPolicy();
113
114    bool isMainWorld() const { return m_worldId == MainWorldId; }
115    bool isPrivateScriptIsolatedWorld() const { return m_worldId == PrivateScriptIsolatedWorldId; }
116    bool isWorkerWorld() const { return m_worldId == WorkerWorldId; }
117    bool isIsolatedWorld() const { return MainWorldId < m_worldId  && m_worldId < IsolatedWorldIdLimit; }
118
119    int worldId() const { return m_worldId; }
120    int extensionGroup() const { return m_extensionGroup; }
121    DOMDataStore& domDataStore() const { return *m_domDataStore; }
122
123    static void setWorldOfInitializingWindow(DOMWrapperWorld* world)
124    {
125        ASSERT(isMainThread());
126        worldOfInitializingWindow = world;
127    }
128    // FIXME: Remove this method once we fix crbug.com/345014.
129    static bool windowIsBeingInitialized() { return !!worldOfInitializingWindow; }
130
131private:
132    class DOMObjectHolderBase {
133    public:
134        DOMObjectHolderBase(v8::Isolate* isolate, v8::Handle<v8::Value> wrapper)
135            : m_wrapper(isolate, wrapper)
136            , m_world(0)
137        {
138        }
139        virtual ~DOMObjectHolderBase() { }
140
141        DOMWrapperWorld* world() const { return m_world; }
142        void setWorld(DOMWrapperWorld* world) { m_world = world; }
143        void setWeak(void (*callback)(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&))
144        {
145            m_wrapper.setWeak(this, callback);
146        }
147
148    private:
149        ScopedPersistent<v8::Value> m_wrapper;
150        DOMWrapperWorld* m_world;
151    };
152
153    template<typename T>
154    class DOMObjectHolder : public DOMObjectHolderBase {
155    public:
156        static PassOwnPtr<DOMObjectHolder<T> > create(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
157        {
158            return adoptPtr(new DOMObjectHolder(isolate, object, wrapper));
159        }
160
161    private:
162        DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
163            : DOMObjectHolderBase(isolate, wrapper)
164            , m_object(object)
165        {
166        }
167
168        Persistent<T> m_object;
169    };
170
171public:
172    template<typename T>
173    void registerDOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
174    {
175        registerDOMObjectHolderInternal(DOMObjectHolder<T>::create(isolate, object, wrapper));
176    }
177
178private:
179    DOMWrapperWorld(int worldId, int extensionGroup);
180
181    static void weakCallbackForDOMObjectHolder(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&);
182    void registerDOMObjectHolderInternal(PassOwnPtr<DOMObjectHolderBase>);
183    void unregisterDOMObjectHolder(DOMObjectHolderBase*);
184
185    static unsigned isolatedWorldCount;
186    static DOMWrapperWorld* worldOfInitializingWindow;
187
188    const int m_worldId;
189    const int m_extensionGroup;
190    OwnPtr<DOMDataStore> m_domDataStore;
191    HashSet<OwnPtr<DOMObjectHolderBase> > m_domObjectHolders;
192};
193
194} // namespace blink
195
196#endif // DOMWrapperWorld_h
197