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"
46 enum WorldIdConstants {
48 // Embedder isolated worlds can use IDs in [1, 1<<29).
49 EmbedderWorldIdLimit = (1 << 29),
50 ScriptPreprocessorIsolatedWorldId,
51 PrivateScriptIsolatedWorldId,
57 // This class represent a collection of DOM wrappers for a specific world.
58 class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
60 static PassRefPtr<DOMWrapperWorld> create(int worldId = -1, int extensionGroup = -1);
62 static const int mainWorldExtensionGroup = 0;
63 static const int privateScriptIsolatedWorldExtensionGroup = 1;
64 static PassRefPtr<DOMWrapperWorld> ensureIsolatedWorld(int worldId, int extensionGroup);
68 static bool isolatedWorldsExist() { return isolatedWorldCount; }
69 static void allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds);
71 static DOMWrapperWorld& world(v8::Handle<v8::Context> context)
73 return ScriptState::from(context)->world();
76 static DOMWrapperWorld& current(v8::Isolate* isolate)
78 if (isMainThread() && worldOfInitializingWindow) {
79 // It's possible that current() is being called while window is being initialized.
80 // In order to make current() workable during the initialization phase,
81 // we cache the world of the initializing window on worldOfInitializingWindow.
82 // If there is no initiazing window, worldOfInitializingWindow is 0.
83 return *worldOfInitializingWindow;
85 return world(isolate->GetCurrentContext());
88 static DOMWrapperWorld& mainWorld();
89 static DOMWrapperWorld& privateScriptIsolatedWorld();
91 static void setIsolatedWorldHumanReadableName(int worldID, const String&);
92 String isolatedWorldHumanReadableName();
94 // Associates an isolated world (see above for description) with a security
95 // origin. XMLHttpRequest instances used in that world will be considered
96 // to come from that origin, not the frame's.
97 static void setIsolatedWorldSecurityOrigin(int worldId, PassRefPtr<SecurityOrigin>);
98 SecurityOrigin* isolatedWorldSecurityOrigin();
100 // Associated an isolated world with a Content Security Policy. Resources
101 // embedded into the main world's DOM from script executed in an isolated
102 // world should be restricted based on the isolated world's DOM, not the
105 // FIXME: Right now, resource injection simply bypasses the main world's
106 // DOM. More work is necessary to allow the isolated world's policy to be
107 // applied correctly.
108 static void setIsolatedWorldContentSecurityPolicy(int worldId, const String& policy);
109 bool isolatedWorldHasContentSecurityPolicy();
111 bool isMainWorld() const { return m_worldId == MainWorldId; }
112 bool isPrivateScriptIsolatedWorld() const { return m_worldId == PrivateScriptIsolatedWorldId; }
113 bool isWorkerWorld() const { return m_worldId == WorkerWorldId; }
114 bool isIsolatedWorld() const { return MainWorldId < m_worldId && m_worldId < IsolatedWorldIdLimit; }
116 int worldId() const { return m_worldId; }
117 int extensionGroup() const { return m_extensionGroup; }
118 DOMDataStore& domDataStore() const { return *m_domDataStore; }
120 static void setWorldOfInitializingWindow(DOMWrapperWorld* world)
122 ASSERT(isMainThread());
123 worldOfInitializingWindow = world;
125 // FIXME: Remove this method once we fix crbug.com/345014.
126 static bool windowIsBeingInitialized() { return !!worldOfInitializingWindow; }
129 class DOMObjectHolderBase {
131 DOMObjectHolderBase(v8::Isolate* isolate, v8::Handle<v8::Value> wrapper)
132 : m_wrapper(isolate, wrapper)
136 virtual ~DOMObjectHolderBase() { }
138 DOMWrapperWorld* world() const { return m_world; }
139 void setWorld(DOMWrapperWorld* world) { m_world = world; }
140 void setWeak(void (*callback)(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&))
142 m_wrapper.setWeak(this, callback);
146 ScopedPersistent<v8::Value> m_wrapper;
147 DOMWrapperWorld* m_world;
151 class DOMObjectHolder : public DOMObjectHolderBase {
153 static PassOwnPtr<DOMObjectHolder<T> > create(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
155 return adoptPtr(new DOMObjectHolder(isolate, object, wrapper));
159 DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
160 : DOMObjectHolderBase(isolate, wrapper)
165 Persistent<T> m_object;
170 void registerDOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
172 registerDOMObjectHolderInternal(DOMObjectHolder<T>::create(isolate, object, wrapper));
176 DOMWrapperWorld(int worldId, int extensionGroup);
178 static void weakCallbackForDOMObjectHolder(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&);
179 void registerDOMObjectHolderInternal(PassOwnPtr<DOMObjectHolderBase>);
180 void unregisterDOMObjectHolder(DOMObjectHolderBase*);
182 static unsigned isolatedWorldCount;
183 static DOMWrapperWorld* worldOfInitializingWindow;
186 const int m_extensionGroup;
187 OwnPtr<DOMDataStore> m_domDataStore;
188 HashSet<OwnPtr<DOMObjectHolderBase> > m_domObjectHolders;
193 #endif // DOMWrapperWorld_h