2 * Copyright (C) 2009 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #import "WebScriptWorld.h"
27 #import "WebScriptWorldInternal.h"
28 #import <WebCore/JSDOMBinding.h>
29 #import <WebCore/ScriptController.h>
30 #import <JavaScriptCore/APICast.h>
32 #import <wtf/RefPtr.h>
34 using namespace WebCore;
36 @interface WebScriptWorldPrivate : NSObject {
38 RefPtr<DOMWrapperWorld> world;
42 @implementation WebScriptWorldPrivate
45 typedef HashMap<DOMWrapperWorld*, WebScriptWorld*> WorldMap;
46 static WorldMap& allWorlds()
48 static WorldMap& map = *new WorldMap;
52 @implementation WebScriptWorld
54 - (id)initWithWorld:(PassRefPtr<DOMWrapperWorld>)world
56 ASSERT_ARG(world, world);
64 _private = [[WebScriptWorldPrivate alloc] init];
65 _private->world = world;
67 ASSERT_ARG(world, !allWorlds().contains(_private->world.get()));
68 allWorlds().add(_private->world.get(), self);
75 return [self initWithWorld:ScriptController::createWorld()];
78 - (void)unregisterWorld
80 _private->world->clearWrappers();
85 ASSERT(allWorlds().contains(_private->world.get()));
86 allWorlds().remove(_private->world.get());
93 + (WebScriptWorld *)standardWorld
95 static WebScriptWorld *world = [[WebScriptWorld alloc] initWithWorld:mainThreadNormalWorld()];
99 + (WebScriptWorld *)world
101 return [[[self alloc] init] autorelease];
104 + (WebScriptWorld *)scriptWorldForGlobalContext:(JSGlobalContextRef)context
106 return [self findOrCreateWorld:currentWorld(toJS(context))];
111 @implementation WebScriptWorld (WebInternal)
113 DOMWrapperWorld* core(WebScriptWorld *world)
115 return world ? world->_private->world.get() : 0;
118 + (WebScriptWorld *)findOrCreateWorld:(DOMWrapperWorld*) world
120 ASSERT_ARG(world, world);
122 if (world == mainThreadNormalWorld())
123 return [self standardWorld];
125 if (WebScriptWorld *existingWorld = allWorlds().get(world))
126 return existingWorld;
128 return [[[self alloc] initWithWorld:world] autorelease];