2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef DOMWrapperWorld_h
32 #define DOMWrapperWorld_h
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"
46 class ExecutionContext;
47 class ScriptController;
49 enum WorldIdConstants {
51 // Embedder isolated worlds can use IDs in [1, 1<<29).
52 EmbedderWorldIdLimit = (1 << 29),
53 ScriptPreprocessorIsolatedWorldId,
54 PrivateScriptIsolatedWorldId,
60 // This class represent a collection of DOM wrappers for a specific world.
61 class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
63 static PassRefPtr<DOMWrapperWorld> create(int worldId = -1, int extensionGroup = -1);
65 static const int mainWorldExtensionGroup = 0;
66 static const int privateScriptIsolatedWorldExtensionGroup = 1;
67 static PassRefPtr<DOMWrapperWorld> ensureIsolatedWorld(int worldId, int extensionGroup);
71 static bool isolatedWorldsExist() { return isolatedWorldCount; }
72 static void allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds);
74 static DOMWrapperWorld& world(v8::Handle<v8::Context> context)
76 return ScriptState::from(context)->world();
79 static DOMWrapperWorld& current(v8::Isolate* isolate)
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;
88 return world(isolate->GetCurrentContext());
91 static DOMWrapperWorld& mainWorld();
92 static DOMWrapperWorld& privateScriptIsolatedWorld();
94 static void setIsolatedWorldHumanReadableName(int worldID, const String&);
95 String isolatedWorldHumanReadableName();
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();
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
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();
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; }
119 int worldId() const { return m_worldId; }
120 int extensionGroup() const { return m_extensionGroup; }
121 DOMDataStore& domDataStore() const { return *m_domDataStore; }
123 static void setWorldOfInitializingWindow(DOMWrapperWorld* world)
125 ASSERT(isMainThread());
126 worldOfInitializingWindow = world;
128 // FIXME: Remove this method once we fix crbug.com/345014.
129 static bool windowIsBeingInitialized() { return !!worldOfInitializingWindow; }
132 class DOMObjectHolderBase {
134 DOMObjectHolderBase(v8::Isolate* isolate, v8::Handle<v8::Value> wrapper)
135 : m_wrapper(isolate, wrapper)
139 virtual ~DOMObjectHolderBase() { }
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>&))
145 m_wrapper.setWeak(this, callback);
149 ScopedPersistent<v8::Value> m_wrapper;
150 DOMWrapperWorld* m_world;
154 class DOMObjectHolder : public DOMObjectHolderBase {
156 static PassOwnPtr<DOMObjectHolder<T> > create(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
158 return adoptPtr(new DOMObjectHolder(isolate, object, wrapper));
162 DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
163 : DOMObjectHolderBase(isolate, wrapper)
168 Persistent<T> m_object;
173 void registerDOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
175 registerDOMObjectHolderInternal(DOMObjectHolder<T>::create(isolate, object, wrapper));
179 DOMWrapperWorld(int worldId, int extensionGroup);
181 static void weakCallbackForDOMObjectHolder(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&);
182 void registerDOMObjectHolderInternal(PassOwnPtr<DOMObjectHolderBase>);
183 void unregisterDOMObjectHolder(DOMObjectHolderBase*);
185 static unsigned isolatedWorldCount;
186 static DOMWrapperWorld* worldOfInitializingWindow;
189 const int m_extensionGroup;
190 OwnPtr<DOMDataStore> m_domDataStore;
191 HashSet<OwnPtr<DOMObjectHolderBase> > m_domObjectHolders;
196 #endif // DOMWrapperWorld_h