Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #if ENABLE(WORKERS)
     34 #include "V8WorkerContext.h"
     35 
     36 #include "DOMTimer.h"
     37 #include "ExceptionCode.h"
     38 #include "ScheduledAction.h"
     39 #include "V8Binding.h"
     40 #include "V8Proxy.h"
     41 #include "V8Utilities.h"
     42 #include "V8WorkerContextEventListener.h"
     43 #include "WebSocket.h"
     44 #include "WorkerContext.h"
     45 #include "WorkerContextExecutionProxy.h"
     46 
     47 namespace WebCore {
     48 
     49 v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot)
     50 {
     51     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
     52 
     53     int argumentCount = args.Length();
     54     if (argumentCount < 1)
     55         return v8::Undefined();
     56 
     57     v8::Handle<v8::Value> function = args[0];
     58     int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0;
     59     int timerId;
     60 
     61     v8::Handle<v8::Context> v8Context = workerContext->script()->proxy()->context();
     62     if (function->IsString()) {
     63         WebCore::String stringFunction = toWebCoreString(function);
     64         timerId = DOMTimer::install(workerContext, new ScheduledAction(v8Context, stringFunction, workerContext->url()), timeout, singleShot);
     65     } else if (function->IsFunction()) {
     66         size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
     67         v8::Local<v8::Value>* params = 0;
     68         if (paramCount > 0) {
     69             params = new v8::Local<v8::Value>[paramCount];
     70             for (size_t i = 0; i < paramCount; ++i)
     71                 params[i] = args[i+2];
     72         }
     73         // ScheduledAction takes ownership of actual params and releases them in its destructor.
     74         ScheduledAction* action = new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params);
     75         delete [] params;
     76         timerId = DOMTimer::install(workerContext, action, timeout, singleShot);
     77     } else
     78         return v8::Undefined();
     79 
     80     return v8::Integer::New(timerId);
     81 }
     82 
     83 v8::Handle<v8::Value> V8WorkerContext::importScriptsCallback(const v8::Arguments& args)
     84 {
     85     INC_STATS(L"DOM.WorkerContext.importScripts()");
     86     if (!args.Length())
     87         return v8::Undefined();
     88 
     89     String callerURL;
     90     if (!V8Proxy::sourceName(callerURL))
     91         return v8::Undefined();
     92     int callerLine;
     93     if (!V8Proxy::sourceLineNumber(callerLine))
     94         return v8::Undefined();
     95     callerLine += 1;
     96 
     97     Vector<String> urls;
     98     for (int i = 0; i < args.Length(); i++) {
     99         v8::TryCatch tryCatch;
    100         v8::Handle<v8::String> scriptUrl = args[i]->ToString();
    101         if (tryCatch.HasCaught() || scriptUrl.IsEmpty())
    102             return v8::Undefined();
    103         urls.append(toWebCoreString(scriptUrl));
    104     }
    105 
    106     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
    107 
    108     ExceptionCode ec = 0;
    109     workerContext->importScripts(urls, callerURL, callerLine, ec);
    110 
    111     if (ec)
    112         return throwError(ec);
    113 
    114     return v8::Undefined();
    115 }
    116 
    117 v8::Handle<v8::Value> V8WorkerContext::setTimeoutCallback(const v8::Arguments& args)
    118 {
    119     INC_STATS(L"DOM.WorkerContext.setTimeout()");
    120     return SetTimeoutOrInterval(args, true);
    121 }
    122 
    123 v8::Handle<v8::Value> V8WorkerContext::setIntervalCallback(const v8::Arguments& args)
    124 {
    125     INC_STATS(L"DOM.WorkerContext.setInterval()");
    126     return SetTimeoutOrInterval(args, false);
    127 }
    128 
    129 v8::Handle<v8::Value> V8WorkerContext::addEventListenerCallback(const v8::Arguments& args)
    130 {
    131     INC_STATS(L"DOM.WorkerContext.addEventListener()");
    132     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
    133 
    134     RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(workerContext, args[1], false, ListenerFindOrCreate);
    135     if (listener) {
    136         String type = toWebCoreString(args[0]);
    137         bool useCapture = args[2]->BooleanValue();
    138         workerContext->addEventListener(type, listener, useCapture);
    139 
    140         createHiddenDependency(args.Holder(), args[1], cacheIndex);
    141     }
    142     return v8::Undefined();
    143 }
    144 
    145 v8::Handle<v8::Value> V8WorkerContext::removeEventListenerCallback(const v8::Arguments& args)
    146 {
    147     INC_STATS(L"DOM.WorkerContext.removeEventListener()");
    148     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
    149 
    150     RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(workerContext, args[1], false, ListenerFindOnly);
    151     if (listener) {
    152         String type = toWebCoreString(args[0]);
    153         bool useCapture = args[2]->BooleanValue();
    154         workerContext->removeEventListener(type, listener.get(), useCapture);
    155 
    156         removeHiddenDependency(args.Holder(), args[1], cacheIndex);
    157     }
    158     return v8::Undefined();
    159 }
    160 
    161 v8::Handle<v8::Value> toV8(WorkerContext* impl)
    162 {
    163     if (!impl)
    164         return v8::Null();
    165 
    166     v8::Handle<v8::Object> global = impl->script()->proxy()->context()->Global();
    167     ASSERT(!global.IsEmpty());
    168     return global;
    169 }
    170 
    171 } // namespace WebCore
    172 
    173 #endif // ENABLE(WORKERS)
    174