Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / DOMWrapperWorld.h
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 <v8.h>
41
42 namespace blink {
43
44 class DOMDataStore;
45
46 enum WorldIdConstants {
47     MainWorldId = 0,
48     // Embedder isolated worlds can use IDs in [1, 1<<29).
49     EmbedderWorldIdLimit = (1 << 29),
50     ScriptPreprocessorIsolatedWorldId,
51     PrivateScriptIsolatedWorldId,
52     IsolatedWorldIdLimit,
53     WorkerWorldId,
54     TestingWorldId,
55 };
56
57 // This class represent a collection of DOM wrappers for a specific world.
58 class DOMWrapperWorld : public RefCounted<DOMWrapperWorld> {
59 public:
60     static PassRefPtr<DOMWrapperWorld> create(int worldId = -1, int extensionGroup = -1);
61
62     static const int mainWorldExtensionGroup = 0;
63     static const int privateScriptIsolatedWorldExtensionGroup = 1;
64     static PassRefPtr<DOMWrapperWorld> ensureIsolatedWorld(int worldId, int extensionGroup);
65     ~DOMWrapperWorld();
66     void dispose();
67
68     static bool isolatedWorldsExist() { return isolatedWorldCount; }
69     static void allWorldsInMainThread(Vector<RefPtr<DOMWrapperWorld> >& worlds);
70
71     static DOMWrapperWorld& world(v8::Handle<v8::Context> context)
72     {
73         return ScriptState::from(context)->world();
74     }
75
76     static DOMWrapperWorld& current(v8::Isolate* isolate)
77     {
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;
84         }
85         return world(isolate->GetCurrentContext());
86     }
87
88     static DOMWrapperWorld& mainWorld();
89     static DOMWrapperWorld& privateScriptIsolatedWorld();
90
91     static void setIsolatedWorldHumanReadableName(int worldID, const String&);
92     String isolatedWorldHumanReadableName();
93
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();
99
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
103     // main world's.
104     //
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();
110
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; }
115
116     int worldId() const { return m_worldId; }
117     int extensionGroup() const { return m_extensionGroup; }
118     DOMDataStore& domDataStore() const { return *m_domDataStore; }
119
120     static void setWorldOfInitializingWindow(DOMWrapperWorld* world)
121     {
122         ASSERT(isMainThread());
123         worldOfInitializingWindow = world;
124     }
125     // FIXME: Remove this method once we fix crbug.com/345014.
126     static bool windowIsBeingInitialized() { return !!worldOfInitializingWindow; }
127
128 private:
129     class DOMObjectHolderBase {
130     public:
131         DOMObjectHolderBase(v8::Isolate* isolate, v8::Handle<v8::Value> wrapper)
132             : m_wrapper(isolate, wrapper)
133             , m_world(0)
134         {
135         }
136         virtual ~DOMObjectHolderBase() { }
137
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>&))
141         {
142             m_wrapper.setWeak(this, callback);
143         }
144
145     private:
146         ScopedPersistent<v8::Value> m_wrapper;
147         DOMWrapperWorld* m_world;
148     };
149
150     template<typename T>
151     class DOMObjectHolder : public DOMObjectHolderBase {
152     public:
153         static PassOwnPtr<DOMObjectHolder<T> > create(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
154         {
155             return adoptPtr(new DOMObjectHolder(isolate, object, wrapper));
156         }
157
158     private:
159         DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
160             : DOMObjectHolderBase(isolate, wrapper)
161             , m_object(object)
162         {
163         }
164
165         Persistent<T> m_object;
166     };
167
168 public:
169     template<typename T>
170     void registerDOMObjectHolder(v8::Isolate* isolate, T* object, v8::Handle<v8::Value> wrapper)
171     {
172         registerDOMObjectHolderInternal(DOMObjectHolder<T>::create(isolate, object, wrapper));
173     }
174
175 private:
176     DOMWrapperWorld(int worldId, int extensionGroup);
177
178     static void weakCallbackForDOMObjectHolder(const v8::WeakCallbackData<v8::Value, DOMObjectHolderBase>&);
179     void registerDOMObjectHolderInternal(PassOwnPtr<DOMObjectHolderBase>);
180     void unregisterDOMObjectHolder(DOMObjectHolderBase*);
181
182     static unsigned isolatedWorldCount;
183     static DOMWrapperWorld* worldOfInitializingWindow;
184
185     const int m_worldId;
186     const int m_extensionGroup;
187     OwnPtr<DOMDataStore> m_domDataStore;
188     HashSet<OwnPtr<DOMObjectHolderBase> > m_domObjectHolders;
189 };
190
191 } // namespace blink
192
193 #endif // DOMWrapperWorld_h