~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/bindings/v8/custom/V8WorkerContextCustom.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "ContentSecurityPolicy.h"
 
37
#include "DOMTimer.h"
 
38
#include "ExceptionCode.h"
 
39
#include "ScheduledAction.h"
 
40
#include "ScriptCallStack.h"
 
41
#include "V8Binding.h"
 
42
#include "V8Utilities.h"
 
43
#include "V8WorkerContextEventListener.h"
 
44
#include "WebSocket.h"
 
45
#include "WorkerContext.h"
 
46
#include "WorkerScriptController.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
    WorkerScriptController* script = workerContext->script();
 
63
    if (!script)
 
64
        return v8::Undefined();
 
65
 
 
66
    v8::Handle<v8::Context> v8Context = script->context();
 
67
    if (function->IsString()) {
 
68
        if (ContentSecurityPolicy* policy = workerContext->contentSecurityPolicy()) {
 
69
            if (!policy->allowEval())
 
70
                return v8Integer(0, args.GetIsolate());
 
71
        }
 
72
        WTF::String stringFunction = toWebCoreString(function);
 
73
        timerId = DOMTimer::install(workerContext, adoptPtr(new ScheduledAction(v8Context, stringFunction, workerContext->url())), timeout, singleShot);
 
74
    } else if (function->IsFunction()) {
 
75
        size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
 
76
        v8::Local<v8::Value>* params = 0;
 
77
        if (paramCount > 0) {
 
78
            params = new v8::Local<v8::Value>[paramCount];
 
79
            for (size_t i = 0; i < paramCount; ++i)
 
80
                params[i] = args[i+2];
 
81
        }
 
82
        // ScheduledAction takes ownership of actual params and releases them in its destructor.
 
83
        OwnPtr<ScheduledAction> action = adoptPtr(new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params));
 
84
        // FIXME: We should use a OwnArrayPtr for params.
 
85
        delete [] params;
 
86
        timerId = DOMTimer::install(workerContext, action.release(), timeout, singleShot);
 
87
    } else
 
88
        return v8::Undefined();
 
89
 
 
90
    return v8Integer(timerId, args.GetIsolate());
 
91
}
 
92
 
 
93
v8::Handle<v8::Value> V8WorkerContext::importScriptsCallback(const v8::Arguments& args)
 
94
{
 
95
    INC_STATS("DOM.WorkerContext.importScripts()");
 
96
    if (!args.Length())
 
97
        return v8::Undefined();
 
98
 
 
99
    Vector<String> urls;
 
100
    for (int i = 0; i < args.Length(); i++) {
 
101
        V8TRYCATCH(v8::Handle<v8::String>, scriptUrl, args[i]->ToString());
 
102
        if (scriptUrl.IsEmpty())
 
103
            return v8::Undefined();
 
104
        urls.append(toWebCoreString(scriptUrl));
 
105
    }
 
106
 
 
107
    WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
 
108
 
 
109
    ExceptionCode ec = 0;
 
110
    workerContext->importScripts(urls, ec);
 
111
 
 
112
    if (ec)
 
113
        return setDOMException(ec, args.GetIsolate());
 
114
 
 
115
    return v8::Undefined();
 
116
}
 
117
 
 
118
v8::Handle<v8::Value> V8WorkerContext::setTimeoutCallback(const v8::Arguments& args)
 
119
{
 
120
    INC_STATS("DOM.WorkerContext.setTimeout()");
 
121
    return SetTimeoutOrInterval(args, true);
 
122
}
 
123
 
 
124
v8::Handle<v8::Value> V8WorkerContext::setIntervalCallback(const v8::Arguments& args)
 
125
{
 
126
    INC_STATS("DOM.WorkerContext.setInterval()");
 
127
    return SetTimeoutOrInterval(args, false);
 
128
}
 
129
 
 
130
v8::Handle<v8::Value> toV8(WorkerContext* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
 
131
{
 
132
    // Notice that we explicitly ignore creationContext because the WorkerContext is its own creationContext.
 
133
 
 
134
    if (!impl)
 
135
        return v8NullWithCheck(isolate);
 
136
 
 
137
    WorkerScriptController* script = impl->script();
 
138
    if (!script)
 
139
        return v8NullWithCheck(isolate);
 
140
 
 
141
    v8::Handle<v8::Object> global = script->context()->Global();
 
142
    ASSERT(!global.IsEmpty());
 
143
    return global;
 
144
}
 
145
 
 
146
} // namespace WebCore
 
147
 
 
148
#endif // ENABLE(WORKERS)