Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/DOMWrapperWorld.cpp

Issue 2735973006: Bindings: Separate WorldIdConstants into WorldTypes and WorldId (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "wtf/PtrUtil.h" 44 #include "wtf/PtrUtil.h"
45 #include "wtf/StdLibExtras.h" 45 #include "wtf/StdLibExtras.h"
46 46
47 namespace blink { 47 namespace blink {
48 48
49 class DOMObjectHolderBase { 49 class DOMObjectHolderBase {
50 USING_FAST_MALLOC(DOMObjectHolderBase); 50 USING_FAST_MALLOC(DOMObjectHolderBase);
51 51
52 public: 52 public:
53 DOMObjectHolderBase(v8::Isolate* isolate, v8::Local<v8::Value> wrapper) 53 DOMObjectHolderBase(v8::Isolate* isolate, v8::Local<v8::Value> wrapper)
54 : m_wrapper(isolate, wrapper), m_world(0) {} 54 : m_wrapper(isolate, wrapper), m_world(nullptr) {}
55 virtual ~DOMObjectHolderBase() {} 55 virtual ~DOMObjectHolderBase() {}
56 56
57 DOMWrapperWorld* world() const { return m_world; } 57 DOMWrapperWorld* world() const { return m_world; }
58 void setWorld(DOMWrapperWorld* world) { m_world = world; } 58 void setWorld(DOMWrapperWorld* world) { m_world = world; }
59 void setWeak( 59 void setWeak(
60 void (*callback)(const v8::WeakCallbackInfo<DOMObjectHolderBase>&)) { 60 void (*callback)(const v8::WeakCallbackInfo<DOMObjectHolderBase>&)) {
61 m_wrapper.setWeak(this, callback); 61 m_wrapper.setWeak(this, callback);
62 } 62 }
63 63
64 private: 64 private:
(...skipping 12 matching lines...) Expand all
77 private: 77 private:
78 DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Local<v8::Value> wrapper) 78 DOMObjectHolder(v8::Isolate* isolate, T* object, v8::Local<v8::Value> wrapper)
79 : DOMObjectHolderBase(isolate, wrapper), m_object(object) {} 79 : DOMObjectHolderBase(isolate, wrapper), m_object(object) {}
80 80
81 Persistent<T> m_object; 81 Persistent<T> m_object;
82 }; 82 };
83 83
84 unsigned DOMWrapperWorld::s_numberOfNonMainWorldsInMainThread = 0; 84 unsigned DOMWrapperWorld::s_numberOfNonMainWorldsInMainThread = 0;
85 85
86 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::create(v8::Isolate* isolate, 86 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::create(v8::Isolate* isolate,
87 int worldId) { 87 WorldType worldType) {
88 return adoptRef(new DOMWrapperWorld(isolate, worldId)); 88 DCHECK_NE(WorldType::Isolated, worldType);
89 return adoptRef(
90 new DOMWrapperWorld(isolate, worldType, getWorldIdForType(worldType)));
89 } 91 }
90 92
91 DOMWrapperWorld::DOMWrapperWorld(v8::Isolate* isolate, int worldId) 93 DOMWrapperWorld::DOMWrapperWorld(v8::Isolate* isolate,
92 : m_worldId(worldId), 94 WorldType worldType,
95 int worldId)
96 : m_worldType(worldType),
97 m_worldId(worldId),
93 m_domDataStore( 98 m_domDataStore(
94 WTF::wrapUnique(new DOMDataStore(isolate, isMainWorld()))) { 99 WTF::wrapUnique(new DOMDataStore(isolate, isMainWorld()))) {
95 if (worldId == WorkerWorldId) { 100 if (isWorkerWorld())
96 workerWorld() = this; 101 workerWorld() = this;
97 } 102 if (worldId != WorldId::MainWorldId && isMainThread())
98 if (worldId != MainWorldId && isMainThread())
99 s_numberOfNonMainWorldsInMainThread++; 103 s_numberOfNonMainWorldsInMainThread++;
100 } 104 }
101 105
102 DOMWrapperWorld& DOMWrapperWorld::mainWorld() { 106 DOMWrapperWorld& DOMWrapperWorld::mainWorld() {
103 ASSERT(isMainThread()); 107 ASSERT(isMainThread());
104 DEFINE_STATIC_REF( 108 DEFINE_STATIC_REF(
105 DOMWrapperWorld, cachedMainWorld, 109 DOMWrapperWorld, cachedMainWorld,
106 (DOMWrapperWorld::create(v8::Isolate::GetCurrent(), MainWorldId))); 110 (DOMWrapperWorld::create(v8::Isolate::GetCurrent(), WorldType::Main)));
107 return *cachedMainWorld; 111 return *cachedMainWorld;
108 } 112 }
109 113
110 DOMWrapperWorld*& DOMWrapperWorld::workerWorld() { 114 DOMWrapperWorld*& DOMWrapperWorld::workerWorld() {
111 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<DOMWrapperWorld*>, workerWorld, 115 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<DOMWrapperWorld*>, workerWorld,
112 new ThreadSpecific<DOMWrapperWorld*>); 116 new ThreadSpecific<DOMWrapperWorld*>);
113 return *workerWorld; 117 return *workerWorld;
114 } 118 }
115 119
116 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::fromWorldId(v8::Isolate* isolate, 120 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::fromWorldId(v8::Isolate* isolate,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 192
189 void DOMWrapperWorld::dispose() { 193 void DOMWrapperWorld::dispose() {
190 m_domObjectHolders.clear(); 194 m_domObjectHolders.clear();
191 m_domDataStore.reset(); 195 m_domDataStore.reset();
192 if (isWorkerWorld()) 196 if (isWorkerWorld())
193 workerWorld() = nullptr; 197 workerWorld() = nullptr;
194 } 198 }
195 199
196 #if DCHECK_IS_ON() 200 #if DCHECK_IS_ON()
197 static bool isIsolatedWorldId(int worldId) { 201 static bool isIsolatedWorldId(int worldId) {
198 return MainWorldId < worldId && worldId < IsolatedWorldIdLimit; 202 return DOMWrapperWorld::MainWorldId < worldId &&
203 worldId < DOMWrapperWorld::IsolatedWorldIdLimit;
199 } 204 }
200 #endif 205 #endif
201 206
202 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld( 207 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(
203 v8::Isolate* isolate, 208 v8::Isolate* isolate,
204 int worldId) { 209 int worldId) {
205 ASSERT(isIsolatedWorldId(worldId)); 210 ASSERT(isIsolatedWorldId(worldId));
206 211
207 WorldMap& map = isolatedWorldMap(); 212 WorldMap& map = isolatedWorldMap();
208 WorldMap::AddResult result = map.insert(worldId, nullptr); 213 WorldMap::AddResult result = map.insert(worldId, nullptr);
209 RefPtr<DOMWrapperWorld> world = result.storedValue->value; 214 RefPtr<DOMWrapperWorld> world = result.storedValue->value;
210 if (world) { 215 if (world) {
211 ASSERT(world->worldId() == worldId); 216 ASSERT(world->worldId() == worldId);
212 return world.release(); 217 return world.release();
213 } 218 }
214 219
215 world = DOMWrapperWorld::create(isolate, worldId); 220 world = adoptRef(new DOMWrapperWorld(isolate, WorldType::Isolated, worldId));
216 result.storedValue->value = world.get(); 221 result.storedValue->value = world.get();
217 return world.release(); 222 return world.release();
218 } 223 }
219 224
220 typedef HashMap<int, RefPtr<SecurityOrigin>> IsolatedWorldSecurityOriginMap; 225 typedef HashMap<int, RefPtr<SecurityOrigin>> IsolatedWorldSecurityOriginMap;
221 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins() { 226 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins() {
222 ASSERT(isMainThread()); 227 ASSERT(isMainThread());
223 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ()); 228 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ());
224 return map; 229 return map;
225 } 230 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 ASSERT(m_domObjectHolders.contains(holderBase)); 316 ASSERT(m_domObjectHolders.contains(holderBase));
312 m_domObjectHolders.erase(holderBase); 317 m_domObjectHolders.erase(holderBase);
313 } 318 }
314 319
315 void DOMWrapperWorld::weakCallbackForDOMObjectHolder( 320 void DOMWrapperWorld::weakCallbackForDOMObjectHolder(
316 const v8::WeakCallbackInfo<DOMObjectHolderBase>& data) { 321 const v8::WeakCallbackInfo<DOMObjectHolderBase>& data) {
317 DOMObjectHolderBase* holderBase = data.GetParameter(); 322 DOMObjectHolderBase* holderBase = data.GetParameter();
318 holderBase->world()->unregisterDOMObjectHolder(holderBase); 323 holderBase->world()->unregisterDOMObjectHolder(holderBase);
319 } 324 }
320 325
326 int DOMWrapperWorld::getWorldIdForType(WorldType worldType) {
327 switch (worldType) {
328 case WorldType::Main:
329 return MainWorldId;
330 case WorldType::Isolated:
331 // This function should not be called for IsolatedWorld because an
332 // identifier for the world is given from out of DOMWrapperWorld.
333 NOTREACHED();
334 return InvalidWorldId;
335 case WorldType::GarbageCollector:
336 return GarbageCollectorWorldId;
337 case WorldType::RegExp:
338 return RegExpWorldId;
339 case WorldType::Testing:
340 return TestingWorldId;
341 // Currently, WorldId for a worker/worklet is a fixed value, but this
342 // doesn't work when multiple worklets are created on a thread.
343 // TODO(nhiroki): Expand the identifier space for workers/worklets.
344 // (https://crbug.com/697622)
345 case WorldType::Worker:
346 return WorkerWorldId;
347 }
348 NOTREACHED();
349 return InvalidWorldId;
350 }
351
321 } // namespace blink 352 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698