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 "JSDOMBinding.h"
     33 #include "JSDOMGlobalObject.h"
     34 #include "JSEventListener.h"
     35 #include "JSEventSourceConstructor.h"
     36 #include "JSMessageChannelConstructor.h"
     37 #include "JSMessagePort.h"
     38 #include "JSWebSocketConstructor.h"
     39 #include "JSWorkerLocation.h"
     40 #include "JSWorkerNavigator.h"
     41 #include "JSXMLHttpRequestConstructor.h"
     42 #include "ScheduledAction.h"
     43 #include "WorkerContext.h"
     44 #include "WorkerLocation.h"
     45 #include "WorkerNavigator.h"
     46 #include <interpreter/Interpreter.h>
     47 
     48 using namespace JSC;
     49 
     50 namespace WebCore {
     51 
     52 void JSWorkerContext::markChildren(MarkStack& markStack)
     53 {
     54     Base::markChildren(markStack);
     55 
     56     JSGlobalData& globalData = *this->globalData();
     57 
     58     markActiveObjectsForContext(markStack, globalData, scriptExecutionContext());
     59 
     60     markDOMObjectWrapper(markStack, globalData, impl()->optionalLocation());
     61     markDOMObjectWrapper(markStack, globalData, impl()->optionalNavigator());
     62 
     63     impl()->markJSEventListeners(markStack);
     64 }
     65 
     66 bool JSWorkerContext::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
     67 {
     68     // Look for overrides before looking at any of our own properties.
     69     if (JSGlobalObject::getOwnPropertySlot(exec, propertyName, slot))
     70         return true;
     71     return false;
     72 }
     73 
     74 bool JSWorkerContext::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
     75 {
     76     // Look for overrides before looking at any of our own properties.
     77     if (JSGlobalObject::getOwnPropertyDescriptor(exec, propertyName, descriptor))
     78         return true;
     79     return false;
     80 }
     81 
     82 #if ENABLE(EVENTSOURCE)
     83 JSValue JSWorkerContext::eventSource(ExecState* exec) const
     84 {
     85     return getDOMConstructor<JSEventSourceConstructor>(exec, this);
     86 }
     87 #endif
     88 
     89 JSValue JSWorkerContext::xmlHttpRequest(ExecState* exec) const
     90 {
     91     return getDOMConstructor<JSXMLHttpRequestConstructor>(exec, this);
     92 }
     93 
     94 #if ENABLE(WEB_SOCKETS)
     95 JSValue JSWorkerContext::webSocket(ExecState* exec) const
     96 {
     97     return getDOMConstructor<JSWebSocketConstructor>(exec, this);
     98 }
     99 #endif
    100 
    101 JSValue JSWorkerContext::importScripts(ExecState* exec, const ArgList& args)
    102 {
    103     if (!args.size())
    104         return jsUndefined();
    105 
    106     Vector<String> urls;
    107     for (unsigned i = 0; i < args.size(); i++) {
    108         urls.append(args.at(i).toString(exec));
    109         if (exec->hadException())
    110             return jsUndefined();
    111     }
    112     ExceptionCode ec = 0;
    113     int signedLineNumber;
    114     intptr_t sourceID;
    115     UString sourceURL;
    116     JSValue function;
    117     exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function);
    118 
    119     impl()->importScripts(urls, sourceURL, signedLineNumber >= 0 ? signedLineNumber : 0, ec);
    120     setDOMException(exec, ec);
    121     return jsUndefined();
    122 }
    123 
    124 JSValue JSWorkerContext::addEventListener(ExecState* exec, const ArgList& args)
    125 {
    126     JSValue listener = args.at(1);
    127     if (!listener.isObject())
    128         return jsUndefined();
    129 
    130     impl()->addEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)), args.at(2).toBoolean(exec));
    131     return jsUndefined();
    132 }
    133 
    134 JSValue JSWorkerContext::removeEventListener(ExecState* exec, const ArgList& args)
    135 {
    136     JSValue listener = args.at(1);
    137     if (!listener.isObject())
    138         return jsUndefined();
    139 
    140     impl()->removeEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)).get(), args.at(2).toBoolean(exec));
    141     return jsUndefined();
    142 }
    143 
    144 JSValue JSWorkerContext::setTimeout(ExecState* exec, const ArgList& args)
    145 {
    146     ScheduledAction* action = ScheduledAction::create(exec, args, currentWorld(exec));
    147     if (exec->hadException())
    148         return jsUndefined();
    149     int delay = args.at(1).toInt32(exec);
    150     return jsNumber(exec, impl()->setTimeout(action, delay));
    151 }
    152 
    153 JSValue JSWorkerContext::setInterval(ExecState* exec, const ArgList& args)
    154 {
    155     ScheduledAction* action = ScheduledAction::create(exec, args, currentWorld(exec));
    156     if (exec->hadException())
    157         return jsUndefined();
    158     int delay = args.at(1).toInt32(exec);
    159     return jsNumber(exec, impl()->setInterval(action, delay));
    160 }
    161 
    162 
    163 #if ENABLE(CHANNEL_MESSAGING)
    164 JSValue JSWorkerContext::messageChannel(ExecState* exec) const
    165 {
    166     return getDOMConstructor<JSMessageChannelConstructor>(exec, this);
    167 }
    168 #endif
    169 
    170 } // namespace WebCore
    171 
    172 #endif // ENABLE(WORKERS)
    173