Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2007-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 #include "ScheduledAction.h"
     33 
     34 #include "Document.h"
     35 #include "ScriptExecutionContext.h"
     36 #include "ScriptSourceCode.h"
     37 #include "ScriptValue.h"
     38 
     39 #include "V8Binding.h"
     40 #include "V8Proxy.h"
     41 #include "WorkerContext.h"
     42 #include "WorkerContextExecutionProxy.h"
     43 #include "WorkerThread.h"
     44 
     45 namespace WebCore {
     46 
     47 ScheduledAction::ScheduledAction(v8::Handle<v8::Context> context, v8::Handle<v8::Function> func, int argc, v8::Handle<v8::Value> argv[])
     48     : m_context(context)
     49     , m_code(String(), KURL(), 0)
     50 {
     51     m_function = v8::Persistent<v8::Function>::New(func);
     52 
     53 #ifndef NDEBUG
     54     V8GCController::registerGlobalHandle(SCHEDULED_ACTION, this, m_function);
     55 #endif
     56 
     57     m_argc = argc;
     58     if (argc > 0) {
     59         m_argv = new v8::Persistent<v8::Value>[argc];
     60         for (int i = 0; i < argc; i++) {
     61             m_argv[i] = v8::Persistent<v8::Value>::New(argv[i]);
     62 
     63 #ifndef NDEBUG
     64     V8GCController::registerGlobalHandle(SCHEDULED_ACTION, this, m_argv[i]);
     65 #endif
     66         }
     67     } else
     68         m_argv = 0;
     69 }
     70 
     71 ScheduledAction::~ScheduledAction()
     72 {
     73     if (m_function.IsEmpty())
     74         return;
     75 
     76 #ifndef NDEBUG
     77     V8GCController::unregisterGlobalHandle(this, m_function);
     78 #endif
     79     m_function.Dispose();
     80 
     81     for (int i = 0; i < m_argc; i++) {
     82 #ifndef NDEBUG
     83         V8GCController::unregisterGlobalHandle(this, m_argv[i]);
     84 #endif
     85         m_argv[i].Dispose();
     86     }
     87 
     88     if (m_argc > 0)
     89         delete[] m_argv;
     90 }
     91 
     92 void ScheduledAction::execute(ScriptExecutionContext* context)
     93 {
     94     V8Proxy* proxy = V8Proxy::retrieve(context);
     95     if (proxy)
     96         execute(proxy);
     97 #if ENABLE(WORKERS)
     98     else if (context->isWorkerContext())
     99         execute(static_cast<WorkerContext*>(context));
    100 #endif
    101     // It's possible that Javascript is disabled and that we have neither a V8Proxy
    102     // nor a WorkerContext.  Do nothing in that case.
    103 }
    104 
    105 void ScheduledAction::execute(V8Proxy* proxy)
    106 {
    107     ASSERT(proxy);
    108 
    109     v8::HandleScope handleScope;
    110     v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context.get());
    111     if (v8Context.IsEmpty())
    112         return; // JS may not be enabled.
    113 
    114     v8::Context::Scope scope(v8Context);
    115 
    116     proxy->setTimerCallback(true);
    117 
    118     // FIXME: Need to implement timeouts for preempting a long-running script.
    119     if (!m_function.IsEmpty() && m_function->IsFunction()) {
    120         proxy->callFunction(v8::Persistent<v8::Function>::Cast(m_function), v8Context->Global(), m_argc, m_argv);
    121         Document::updateStyleForAllDocuments();
    122     } else
    123         proxy->evaluate(m_code, 0);
    124 
    125     proxy->setTimerCallback(false);
    126 }
    127 
    128 #if ENABLE(WORKERS)
    129 void ScheduledAction::execute(WorkerContext* workerContext)
    130 {
    131     // In a Worker, the execution should always happen on a worker thread.
    132     ASSERT(workerContext->thread()->threadID() == currentThread());
    133 
    134     WorkerScriptController* scriptController = workerContext->script();
    135 
    136     if (!m_function.IsEmpty() && m_function->IsFunction()) {
    137         v8::HandleScope handleScope;
    138         v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context.get());
    139         ASSERT(!v8Context.IsEmpty());
    140         v8::Context::Scope scope(v8Context);
    141         m_function->Call(v8Context->Global(), m_argc, m_argv);
    142     } else
    143         scriptController->evaluate(m_code);
    144 }
    145 #endif
    146 
    147 } // namespace WebCore
    148