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 "V8BindingMacros.h"
     41 #include "V8Proxy.h"
     42 #include "V8Utilities.h"
     43 #include "V8WorkerContextEventListener.h"
     44 #include "WebSocket.h"
     45 #include "WorkerContext.h"
     46 #include "WorkerContextExecutionProxy.h"
     47 
     48 namespace WebCore {
     49 
     50 v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot)
     51 {
     52     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
     53 
     54     int argumentCount = args.Length();
     55     if (argumentCount < 1)
     56         return v8::Undefined();
     57 
     58     v8::Handle<v8::Value> function = args[0];
     59     int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0;
     60     int timerId;
     61 
     62     WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
     63     if (!proxy)
     64         return v8::Undefined();
     65 
     66     v8::Handle<v8::Context> v8Context = proxy->context();
     67     if (function->IsString()) {
     68         WTF::String stringFunction = toWebCoreString(function);
     69         timerId = DOMTimer::install(workerContext, new ScheduledAction(v8Context, stringFunction, workerContext->url()), timeout, singleShot);
     70     } else if (function->IsFunction()) {
     71         size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
     72         v8::Local<v8::Value>* params = 0;
     73         if (paramCount > 0) {
     74             params = new v8::Local<v8::Value>[paramCount];
     75             for (size_t i = 0; i < paramCount; ++i)
     76                 params[i] = args[i+2];
     77         }
     78         // ScheduledAction takes ownership of actual params and releases them in its destructor.
     79         ScheduledAction* action = new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params);
     80         delete [] params;
     81         timerId = DOMTimer::install(workerContext, action, timeout, singleShot);
     82     } else
     83         return v8::Undefined();
     84 
     85     return v8::Integer::New(timerId);
     86 }
     87 
     88 v8::Handle<v8::Value> V8WorkerContext::importScriptsCallback(const v8::Arguments& args)
     89 {
     90     INC_STATS(L"DOM.WorkerContext.importScripts()");
     91     if (!args.Length())
     92         return v8::Undefined();
     93 
     94     Vector<String> urls;
     95     for (int i = 0; i < args.Length(); i++) {
     96         v8::TryCatch tryCatch;
     97         v8::Handle<v8::String> scriptUrl = args[i]->ToString();
     98         if (tryCatch.HasCaught() || scriptUrl.IsEmpty())
     99             return v8::Undefined();
    100         urls.append(toWebCoreString(scriptUrl));
    101     }
    102 
    103     WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
    104 
    105     ExceptionCode ec = 0;
    106     workerContext->importScripts(urls, ec);
    107 
    108     if (ec)
    109         return throwError(ec);
    110 
    111     return v8::Undefined();
    112 }
    113 
    114 v8::Handle<v8::Value> V8WorkerContext::setTimeoutCallback(const v8::Arguments& args)
    115 {
    116     INC_STATS(L"DOM.WorkerContext.setTimeout()");
    117     return SetTimeoutOrInterval(args, true);
    118 }
    119 
    120 v8::Handle<v8::Value> V8WorkerContext::setIntervalCallback(const v8::Arguments& args)
    121 {
    122     INC_STATS(L"DOM.WorkerContext.setInterval()");
    123     return SetTimeoutOrInterval(args, false);
    124 }
    125 
    126 v8::Handle<v8::Value> toV8(WorkerContext* impl)
    127 {
    128     if (!impl)
    129         return v8::Null();
    130 
    131     WorkerContextExecutionProxy* proxy = impl->script()->proxy();
    132     if (!proxy)
    133         return v8::Null();
    134 
    135     v8::Handle<v8::Object> global = proxy->context()->Global();
    136     ASSERT(!global.IsEmpty());
    137     return global;
    138 }
    139 
    140 } // namespace WebCore
    141 
    142 #endif // ENABLE(WORKERS)
    143