initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSWorkerContextCustom.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2011 Apple 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
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(WORKERS)
29
30 #include "JSWorkerContext.h"
31
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>
47
48 #if ENABLE(WEB_SOCKETS)
49 #include "JSWebSocket.h"
50 #endif
51
52 using namespace JSC;
53
54 namespace WebCore {
55
56 void JSWorkerContext::visitChildren(SlotVisitor& visitor)
57 {
58     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
59     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
60     ASSERT(structure()->typeInfo().overridesVisitChildren());
61     Base::visitChildren(visitor);
62
63     if (WorkerLocation* location = impl()->optionalLocation())
64         visitor.addOpaqueRoot(location);
65     if (WorkerNavigator* navigator = impl()->optionalNavigator())
66         visitor.addOpaqueRoot(navigator);
67
68     impl()->visitJSEventListeners(visitor);
69 }
70
71 bool JSWorkerContext::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
72 {
73     // Look for overrides before looking at any of our own properties.
74     if (JSGlobalObject::getOwnPropertySlot(exec, propertyName, slot))
75         return true;
76     return false;
77 }
78
79 bool JSWorkerContext::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
80 {
81     // Look for overrides before looking at any of our own properties.
82     if (JSGlobalObject::getOwnPropertyDescriptor(exec, propertyName, descriptor))
83         return true;
84     return false;
85 }
86
87 #if ENABLE(EVENTSOURCE)
88 JSValue JSWorkerContext::eventSource(ExecState* exec) const
89 {
90     return getDOMConstructor<JSEventSourceConstructor>(exec, this);
91 }
92 #endif
93
94 JSValue JSWorkerContext::xmlHttpRequest(ExecState* exec) const
95 {
96     return getDOMConstructor<JSXMLHttpRequestConstructor>(exec, this);
97 }
98
99 #if ENABLE(WEB_SOCKETS)
100 JSValue JSWorkerContext::webSocket(ExecState* exec) const
101 {
102     return getDOMConstructor<JSWebSocketConstructor>(exec, this);
103 }
104 #endif
105
106 JSValue JSWorkerContext::importScripts(ExecState* exec)
107 {
108     if (!exec->argumentCount())
109         return jsUndefined();
110
111     Vector<String> urls;
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();
116     }
117     ExceptionCode ec = 0;
118
119     impl()->importScripts(urls, ec);
120     setDOMException(exec, ec);
121     return jsUndefined();
122 }
123
124 JSValue JSWorkerContext::setTimeout(ExecState* exec)
125 {
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));
132 }
133
134 JSValue JSWorkerContext::setInterval(ExecState* exec)
135 {
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));
142 }
143
144
145 #if ENABLE(CHANNEL_MESSAGING)
146 JSValue JSWorkerContext::messageChannel(ExecState* exec) const
147 {
148     return getDOMConstructor<JSMessageChannelConstructor>(exec, this);
149 }
150 #endif
151
152 } // namespace WebCore
153
154 #endif // ENABLE(WORKERS)