Source/bindings/v8/custom/V8WindowCustom.cpp
// FIXME: There is a lot of duplication with SetTimeoutOrInterval() in V8WorkerGlobalScopeCustom.cpp.
// We should refactor this.
There is a lot of duplication with SetTimeoutOrInterval() in V8WorkerGlobalScopeCustom.cpp.
We should refactor this.
There is a lot of duplication with SetTimeoutOrInterval () in V8WorkerGlobalScopeCustom . cpp . We should refactor this .
static void windowSetTimeoutImpl(const v8::FunctionCallbackInfo<v8::Value>& info, bool singleShot, ExceptionState& exceptionState)
{
    int argumentCount = info.Length();
    if (argumentCount < 1)
        return;
    LocalDOMWindow* impl = V8Window::toNative(info.Holder());
    if (!impl->frame() || !impl->document()) {
        exceptionState.throwDOMException(InvalidAccessError, "No script context is available in which to execute the script.");
        return;
    }
    ScriptState* scriptState = ScriptState::current(info.GetIsolate());
    v8::Handle<v8::Value> function = info[0];
    String functionString;
    if (!function->IsFunction()) {
        if (function->IsString()) {
            functionString = toCoreString(function.As<v8::String>());
        } else {
            v8::Handle<v8::String> v8String = function->ToString();
            if (v8String.IsEmpty())
                return;
            functionString = toCoreString(v8String);
        }
        if (!functionString.length())
            return;
    }
    if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), impl->frame(), exceptionState))
        return;
    OwnPtr<ScheduledAction> action;
    if (function->IsFunction()) {
        int paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
        OwnPtr<v8::Local<v8::Value>[]> params;
        if (paramCount > 0) {
            params = adoptArrayPtr(new v8::Local<v8::Value>[paramCount]);
            for (int i = 0; i < paramCount; i++) {
                params[i] = info[i+2];
            }
        }
        ASSERT(impl->frame());
        action = adoptPtr(new ScheduledAction(scriptState, v8::Handle<v8::Function>::Cast(function), paramCount, params.get(), info.GetIsolate()));
    } else {
        if (impl->document() && !impl->document()->contentSecurityPolicy()->allowEval()) {
            v8SetReturnValue(info, 0);
            return;
        }
        ASSERT(impl->frame());
        action = adoptPtr(new ScheduledAction(scriptState, functionString, KURL(), info.GetIsolate()));
    }
    int32_t timeout = argumentCount >= 2 ? info[1]->Int32Value() : 0;
    int timerId;
    if (singleShot)
        timerId = DOMWindowTimers::setTimeout(*impl, action.release(), timeout);
    else
        timerId = DOMWindowTimers::setInterval(*impl, action.release(), timeout);
    if (timeout >= 0) {
        double maximumFireInterval = static_cast<double>(timeout) / 1000 / 2;
        V8GCForContextDispose::instanceTemplate().notifyIdleSooner(maximumFireInterval);
    }
    v8SetReturnValue(info, timerId);
}
static void windowSetTimeoutImpl ( const v8 :: FunctionCallbackInfo < v8 :: Value > & info , bool singleShot , ExceptionState & exceptionState ) { int argumentCount = info . Length ( ) ; if ( argumentCount < 1 ) return ; LocalDOMWindow * impl = V8Window :: toNative ( info . Holder ( ) ) ; if ( ! impl -> frame ( ) || ! impl -> document ( ) ) { exceptionState . throwDOMException ( InvalidAccessError , " " ) ; return ; } ScriptState * scriptState = ScriptState :: current ( info . GetIsolate ( ) ) ; v8 :: Handle < v8 :: Value > function = info [ 0 ] ; String functionString ; if ( ! function -> IsFunction ( ) ) { if ( function -> IsString ( ) ) { functionString = toCoreString ( function . As < v8 :: String > ( ) ) ; } else { v8 :: Handle < v8 :: String > v8String = function -> ToString ( ) ; if ( v8String . IsEmpty ( ) ) return ; functionString = toCoreString ( v8String ) ; } if ( ! functionString . length ( ) ) return ; } if ( ! BindingSecurity :: shouldAllowAccessToFrame ( info . GetIsolate ( ) , impl -> frame ( ) , exceptionState ) ) return ; OwnPtr < ScheduledAction > action ; if ( function -> IsFunction ( ) ) { int paramCount = argumentCount >= 2 ? argumentCount - 2 : 0 ; OwnPtr < v8 :: Local < v8 :: Value > [ ] > params ; if ( paramCount > 0 ) { params = adoptArrayPtr ( new v8 :: Local < v8 :: Value > [ paramCount ] ) ; for ( int i = 0 ; i < paramCount ; i ++ ) { params [ i ] = info [ i + 2 ] ; } } ASSERT ( impl -> frame ( ) ) ; action = adoptPtr ( new ScheduledAction ( scriptState , v8 :: Handle < v8 :: Function > :: Cast ( function ) , paramCount , params . get ( ) , info . GetIsolate ( ) ) ) ; } else { if ( impl -> document ( ) && ! impl -> document ( ) -> contentSecurityPolicy ( ) -> allowEval ( ) ) { v8SetReturnValue ( info , 0 ) ; return ; } ASSERT ( impl -> frame ( ) ) ; action = adoptPtr ( new ScheduledAction ( scriptState , functionString , KURL ( ) , info . GetIsolate ( ) ) ) ; } int32_t timeout = argumentCount >= 2 ? info [ 1 ] -> Int32Value ( ) : 0 ; int timerId ; if ( singleShot ) timerId = DOMWindowTimers :: setTimeout ( * impl , action . release ( ) , timeout ) ; else timerId = DOMWindowTimers :: setInterval ( * impl , action . release ( ) , timeout ) ; if ( timeout >= 0 ) { double maximumFireInterval = static_cast < double > ( timeout ) / 1000 / 2 ; V8GCForContextDispose :: instanceTemplate ( ) . notifyIdleSooner ( maximumFireInterval ) ; } v8SetReturnValue ( info , timerId ) ; }
FIXME: There is a lot of duplication with SetTimeoutOrInterval() in V8WorkerGlobalScopeCustom.cpp.
FIXME : There is a lot of duplication with SetTimeoutOrInterval () in V8WorkerGlobalScopeCustom . cpp .
// Bail out if string conversion failed. // Don't allow setting timeouts to run empty functions! // (Bug 1009597) // parameters must be globalized // params is passed to action, and released in action's destructor // Try to do the idle notification before the timeout expires to get better // use of any idle time. Aim for the middle of the interval for simplicity.