2 * Copyright (C) 2008, 2009, 2011 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "JSWorkerContext.h"
32 #include "ExceptionCode.h"
33 #include "JSDOMBinding.h"
34 #include "JSDOMGlobalObject.h"
35 #include "JSEventListener.h"
36 #include "JSEventSource.h"
37 #include "JSMessageChannel.h"
38 #include "JSMessagePort.h"
39 #include "JSWorkerLocation.h"
40 #include "JSWorkerNavigator.h"
41 #include "JSXMLHttpRequest.h"
42 #include "ScheduledAction.h"
43 #include "WorkerContext.h"
44 #include "WorkerLocation.h"
45 #include "WorkerNavigator.h"
46 #include <interpreter/Interpreter.h>
48 #if ENABLE(WEB_SOCKETS)
49 #include "JSWebSocket.h"
56 void JSWorkerContext::visitChildren(SlotVisitor& visitor)
58 ASSERT_GC_OBJECT_INHERITS(this, &s_info);
59 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
60 ASSERT(structure()->typeInfo().overridesVisitChildren());
61 Base::visitChildren(visitor);
63 if (WorkerLocation* location = impl()->optionalLocation())
64 visitor.addOpaqueRoot(location);
65 if (WorkerNavigator* navigator = impl()->optionalNavigator())
66 visitor.addOpaqueRoot(navigator);
68 impl()->visitJSEventListeners(visitor);
71 bool JSWorkerContext::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
73 // Look for overrides before looking at any of our own properties.
74 if (JSGlobalObject::getOwnPropertySlot(exec, propertyName, slot))
79 bool JSWorkerContext::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
81 // Look for overrides before looking at any of our own properties.
82 if (JSGlobalObject::getOwnPropertyDescriptor(exec, propertyName, descriptor))
87 #if ENABLE(EVENTSOURCE)
88 JSValue JSWorkerContext::eventSource(ExecState* exec) const
90 return getDOMConstructor<JSEventSourceConstructor>(exec, this);
94 JSValue JSWorkerContext::xmlHttpRequest(ExecState* exec) const
96 return getDOMConstructor<JSXMLHttpRequestConstructor>(exec, this);
99 #if ENABLE(WEB_SOCKETS)
100 JSValue JSWorkerContext::webSocket(ExecState* exec) const
102 return getDOMConstructor<JSWebSocketConstructor>(exec, this);
106 JSValue JSWorkerContext::importScripts(ExecState* exec)
108 if (!exec->argumentCount())
109 return jsUndefined();
112 for (unsigned i = 0; i < exec->argumentCount(); i++) {
113 urls.append(ustringToString(exec->argument(i).toString(exec)));
114 if (exec->hadException())
115 return jsUndefined();
117 ExceptionCode ec = 0;
119 impl()->importScripts(urls, ec);
120 setDOMException(exec, ec);
121 return jsUndefined();
124 JSValue JSWorkerContext::setTimeout(ExecState* exec)
126 // FIXME: Should we enforce a Content-Security-Policy on workers?
127 OwnPtr<ScheduledAction> action = ScheduledAction::create(exec, currentWorld(exec), 0);
128 if (exec->hadException())
129 return jsUndefined();
130 int delay = exec->argument(1).toInt32(exec);
131 return jsNumber(impl()->setTimeout(action.release(), delay));
134 JSValue JSWorkerContext::setInterval(ExecState* exec)
136 // FIXME: Should we enforce a Content-Security-Policy on workers?
137 OwnPtr<ScheduledAction> action = ScheduledAction::create(exec, currentWorld(exec), 0);
138 if (exec->hadException())
139 return jsUndefined();
140 int delay = exec->argument(1).toInt32(exec);
141 return jsNumber(impl()->setInterval(action.release(), delay));
145 #if ENABLE(CHANNEL_MESSAGING)
146 JSValue JSWorkerContext::messageChannel(ExecState* exec) const
148 return getDOMConstructor<JSMessageChannelConstructor>(exec, this);
152 } // namespace WebCore
154 #endif // ENABLE(WORKERS)