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: address review comments 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/StdLibExtras.h" 44 #include "wtf/StdLibExtras.h"
45 #include <memory> 45 #include <memory>
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::isolatedWorldCount = 0; 84 unsigned DOMWrapperWorld::isolatedWorldCount = 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 }
98 } 102 }
99 103
100 DOMWrapperWorld& DOMWrapperWorld::mainWorld() { 104 DOMWrapperWorld& DOMWrapperWorld::mainWorld() {
101 ASSERT(isMainThread()); 105 ASSERT(isMainThread());
102 DEFINE_STATIC_REF( 106 DEFINE_STATIC_REF(
103 DOMWrapperWorld, cachedMainWorld, 107 DOMWrapperWorld, cachedMainWorld,
104 (DOMWrapperWorld::create(v8::Isolate::GetCurrent(), MainWorldId))); 108 (DOMWrapperWorld::create(v8::Isolate::GetCurrent(), WorldType::Main)));
105 return *cachedMainWorld; 109 return *cachedMainWorld;
106 } 110 }
107 111
108 DOMWrapperWorld*& DOMWrapperWorld::workerWorld() { 112 DOMWrapperWorld*& DOMWrapperWorld::workerWorld() {
109 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<DOMWrapperWorld*>, workerWorld, 113 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<DOMWrapperWorld*>, workerWorld,
110 new ThreadSpecific<DOMWrapperWorld*>); 114 new ThreadSpecific<DOMWrapperWorld*>);
111 return *workerWorld; 115 return *workerWorld;
112 } 116 }
113 117
114 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::fromWorldId(v8::Isolate* isolate, 118 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::fromWorldId(v8::Isolate* isolate,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 188
185 void DOMWrapperWorld::dispose() { 189 void DOMWrapperWorld::dispose() {
186 m_domObjectHolders.clear(); 190 m_domObjectHolders.clear();
187 m_domDataStore.reset(); 191 m_domDataStore.reset();
188 if (isWorkerWorld()) 192 if (isWorkerWorld())
189 workerWorld() = nullptr; 193 workerWorld() = nullptr;
190 } 194 }
191 195
192 #if DCHECK_IS_ON() 196 #if DCHECK_IS_ON()
193 static bool isIsolatedWorldId(int worldId) { 197 static bool isIsolatedWorldId(int worldId) {
194 return MainWorldId < worldId && worldId < IsolatedWorldIdLimit; 198 return DOMWrapperWorld::MainWorldId < worldId &&
199 worldId < DOMWrapperWorld::IsolatedWorldIdLimit;
195 } 200 }
196 #endif 201 #endif
197 202
198 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld( 203 PassRefPtr<DOMWrapperWorld> DOMWrapperWorld::ensureIsolatedWorld(
199 v8::Isolate* isolate, 204 v8::Isolate* isolate,
200 int worldId) { 205 int worldId) {
201 ASSERT(isIsolatedWorldId(worldId)); 206 ASSERT(isIsolatedWorldId(worldId));
202 207
203 WorldMap& map = isolatedWorldMap(); 208 WorldMap& map = isolatedWorldMap();
204 WorldMap::AddResult result = map.insert(worldId, nullptr); 209 WorldMap::AddResult result = map.insert(worldId, nullptr);
205 RefPtr<DOMWrapperWorld> world = result.storedValue->value; 210 RefPtr<DOMWrapperWorld> world = result.storedValue->value;
206 if (world) { 211 if (world) {
207 ASSERT(world->worldId() == worldId); 212 ASSERT(world->worldId() == worldId);
208 return world.release(); 213 return world.release();
209 } 214 }
210 215
211 world = DOMWrapperWorld::create(isolate, worldId); 216 world = adoptRef(new DOMWrapperWorld(isolate, WorldType::Isolated, worldId));
212 result.storedValue->value = world.get(); 217 result.storedValue->value = world.get();
213 isolatedWorldCount++; 218 isolatedWorldCount++;
214 return world.release(); 219 return world.release();
215 } 220 }
216 221
217 typedef HashMap<int, RefPtr<SecurityOrigin>> IsolatedWorldSecurityOriginMap; 222 typedef HashMap<int, RefPtr<SecurityOrigin>> IsolatedWorldSecurityOriginMap;
218 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins() { 223 static IsolatedWorldSecurityOriginMap& isolatedWorldSecurityOrigins() {
219 ASSERT(isMainThread()); 224 ASSERT(isMainThread());
220 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ()); 225 DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ());
221 return map; 226 return map;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 ASSERT(m_domObjectHolders.contains(holderBase)); 313 ASSERT(m_domObjectHolders.contains(holderBase));
309 m_domObjectHolders.erase(holderBase); 314 m_domObjectHolders.erase(holderBase);
310 } 315 }
311 316
312 void DOMWrapperWorld::weakCallbackForDOMObjectHolder( 317 void DOMWrapperWorld::weakCallbackForDOMObjectHolder(
313 const v8::WeakCallbackInfo<DOMObjectHolderBase>& data) { 318 const v8::WeakCallbackInfo<DOMObjectHolderBase>& data) {
314 DOMObjectHolderBase* holderBase = data.GetParameter(); 319 DOMObjectHolderBase* holderBase = data.GetParameter();
315 holderBase->world()->unregisterDOMObjectHolder(holderBase); 320 holderBase->world()->unregisterDOMObjectHolder(holderBase);
316 } 321 }
317 322
323 int DOMWrapperWorld::getWorldIdForType(WorldType worldType) {
Yuki 2017/03/08 14:07:51 IIUC, this function is going to be used to get an
nhiroki 2017/03/09 03:40:28 I'd prefer to rename this in my second patch that
324 switch (worldType) {
325 case WorldType::Unknown:
326 return UnknownWorldId;
Yuki 2017/03/08 14:07:51 I think "Unknown" is not a good name. It seems th
nhiroki 2017/03/09 03:40:28 Done.
327 case WorldType::Main:
328 return MainWorldId;
329 case WorldType::Isolated:
330 NOTREACHED();
Yuki 2017/03/08 14:07:51 Could you add a comment about the reason why we do
nhiroki 2017/03/09 03:40:28 Done.
331 return UnknownWorldId;
332 // Currently, WorldId for a worker/worklet is a fixed value, but this
333 // doesn't work when multiple worklets are created on a thread.
334 // TODO(nhiroki): Expand the identifier space for workers/worklets.
335 case WorldType::Worker:
336 return WorkerWorldId;
337 }
338 NOTREACHED();
339 return UnknownWorldId;
340 }
341
318 } // namespace blink 342 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698