Home | History | Annotate | Download | only in js
      1 /*
      2  * Copyright (C) 2008, 2009 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::markChildren(MarkStack& markStack)
     57 {
     58     Base::markChildren(markStack);
     59 
     60     JSGlobalData& globalData = this->globalData();
     61 
     62     markActiveObjectsForContext(markStack, globalData, scriptExecutionContext());
     63 
     64     markDOMObjectWrapper(markStack, globalData, impl()->optionalLocation());
     65     markDOMObjectWrapper(markStack, globalData, impl()->optionalNavigator());
     66 
     67     impl()->markJSEventListeners(markStack);
     68 }
     69 
     70 bool JSWorkerContext::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
     71 {
     72     // Look for overrides before looking at any of our own properties.
     73     if (JSGlobalObject::getOwnPropertySlot(exec, propertyName, slot))
     74         return true;
     75     return false;
     76 }
     77 
     78 bool JSWorkerContext::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
     79 {
     80     // Look for overrides before looking at any of our own properties.
     81     if (JSGlobalObject::getOwnPropertyDescriptor(exec, propertyName, descriptor))
     82         return true;
     83     return false;
     84 }
     85 
     86 #if ENABLE(EVENTSOURCE)
     87 JSValue JSWorkerContext::eventSource(ExecState* exec) const
     88 {
     89     return getDOMConstructor<JSEventSourceConstructor>(exec, this);
     90 }
     91 #endif
     92 
     93 JSValue JSWorkerContext::xmlHttpRequest(ExecState* exec) const
     94 {
     95     return getDOMConstructor<JSXMLHttpRequestConstructor>(exec, this);
     96 }
     97 
     98 #if ENABLE(WEB_SOCKETS)
     99 JSValue JSWorkerContext::webSocket(ExecState* exec) const
    100 {
    101     return getDOMConstructor<JSWebSocketConstructor>(exec, this);
    102 }
    103 #endif
    104 
    105 JSValue JSWorkerContext::importScripts(ExecState* exec)
    106 {
    107     if (!exec->argumentCount())
    108         return jsUndefined();
    109 
    110     Vector<String> urls;
    111     for (unsigned i = 0; i < exec->argumentCount(); i++) {
    112         urls.append(ustringToString(exec->argument(i).toString(exec)));
    113         if (exec->hadException())
    114             return jsUndefined();
    115     }
    116     ExceptionCode ec = 0;
    117 
    118     impl()->importScripts(urls, ec);
    119     setDOMException(exec, ec);
    120     return jsUndefined();
    121 }
    122 
    123 JSValue JSWorkerContext::setTimeout(ExecState* exec)
    124 {
    125     // FIXME: Should we enforce a Content-Security-Policy on workers?
    126     OwnPtr<ScheduledAction> action = ScheduledAction::create(exec, currentWorld(exec), 0);
    127     if (exec->hadException())
    128         return jsUndefined();
    129     int delay = exec->argument(1).toInt32(exec);
    130     return jsNumber(impl()->setTimeout(action.release(), delay));
    131 }
    132 
    133 JSValue JSWorkerContext::setInterval(ExecState* exec)
    134 {
    135     // FIXME: Should we enforce a Content-Security-Policy on workers?
    136     OwnPtr<ScheduledAction> action = ScheduledAction::create(exec, currentWorld(exec), 0);
    137     if (exec->hadException())
    138         return jsUndefined();
    139     int delay = exec->argument(1).toInt32(exec);
    140     return jsNumber(impl()->setInterval(action.release(), delay));
    141 }
    142 
    143 
    144 #if ENABLE(CHANNEL_MESSAGING)
    145 JSValue JSWorkerContext::messageChannel(ExecState* exec) const
    146 {
    147     return getDOMConstructor<JSMessageChannelConstructor>(exec, this);
    148 }
    149 #endif
    150 
    151 } // namespace WebCore
    152 
    153 #endif // ENABLE(WORKERS)
    154