1/*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include "config.h"
29
30#if ENABLE(WORKERS)
31
32#include "JSWorkerContextBase.h"
33
34#include "DOMWrapperWorld.h"
35#include "JSDedicatedWorkerContext.h"
36#include "JSSharedWorkerContext.h"
37#include "JSWorkerContext.h"
38#include "WorkerContext.h"
39
40using namespace JSC;
41
42namespace WebCore {
43
44ASSERT_CLASS_FITS_IN_CELL(JSWorkerContextBase);
45
46const ClassInfo JSWorkerContextBase::s_info = { "WorkerContext", &JSDOMGlobalObject::s_info, 0, 0 };
47
48JSWorkerContextBase::JSWorkerContextBase(JSC::JSGlobalData& globalData, JSC::Structure* structure, PassRefPtr<WorkerContext> impl)
49    : JSDOMGlobalObject(globalData, structure, normalWorld(globalData), this)
50    , m_impl(impl)
51{
52    ASSERT(inherits(&s_info));
53}
54
55JSWorkerContextBase::~JSWorkerContextBase()
56{
57}
58
59ScriptExecutionContext* JSWorkerContextBase::scriptExecutionContext() const
60{
61    return m_impl.get();
62}
63
64JSValue toJS(ExecState* exec, JSDOMGlobalObject*, WorkerContext* workerContext)
65{
66    return toJS(exec, workerContext);
67}
68
69JSValue toJS(ExecState*, WorkerContext* workerContext)
70{
71    if (!workerContext)
72        return jsNull();
73    WorkerScriptController* script = workerContext->script();
74    if (!script)
75        return jsNull();
76    JSWorkerContext* contextWrapper = script->workerContextWrapper();
77    ASSERT(contextWrapper);
78    return contextWrapper;
79}
80
81JSDedicatedWorkerContext* toJSDedicatedWorkerContext(JSValue value)
82{
83    if (!value.isObject())
84        return 0;
85    const ClassInfo* classInfo = asObject(value)->classInfo();
86    if (classInfo == &JSDedicatedWorkerContext::s_info)
87        return static_cast<JSDedicatedWorkerContext*>(asObject(value));
88    return 0;
89}
90
91#if ENABLE(SHARED_WORKERS)
92JSSharedWorkerContext* toJSSharedWorkerContext(JSValue value)
93{
94    if (!value.isObject())
95        return 0;
96    const ClassInfo* classInfo = asObject(value)->classInfo();
97    if (classInfo == &JSSharedWorkerContext::s_info)
98        return static_cast<JSSharedWorkerContext*>(asObject(value));
99    return 0;
100}
101#endif
102
103JSWorkerContext* toJSWorkerContext(JSValue value)
104{
105    JSWorkerContext* context = toJSDedicatedWorkerContext(value);
106#if ENABLE(SHARED_WORKERS)
107    if (!context)
108        context = toJSSharedWorkerContext(value);
109#endif
110    return context;
111}
112
113} // namespace WebCore
114
115#endif // ENABLE(WORKERS)
116