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#include "core/workers/DedicatedWorkerGlobalScope.h"
33
34#include "bindings/core/v8/ExceptionState.h"
35#include "bindings/core/v8/SerializedScriptValue.h"
36#include "core/frame/LocalDOMWindow.h"
37#include "core/workers/DedicatedWorkerThread.h"
38#include "core/workers/WorkerClients.h"
39#include "core/workers/WorkerObjectProxy.h"
40#include "core/workers/WorkerThreadStartupData.h"
41
42namespace blink {
43
44PassRefPtrWillBeRawPtr<DedicatedWorkerGlobalScope> DedicatedWorkerGlobalScope::create(DedicatedWorkerThread* thread, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData, double timeOrigin)
45{
46    RefPtrWillBeRawPtr<DedicatedWorkerGlobalScope> context = adoptRefWillBeNoop(new DedicatedWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, timeOrigin, startupData->m_workerClients.release()));
47    context->applyContentSecurityPolicyFromString(startupData->m_contentSecurityPolicy, startupData->m_contentSecurityPolicyType);
48    return context.release();
49}
50
51DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(const KURL& url, const String& userAgent, DedicatedWorkerThread* thread, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients> workerClients)
52    : WorkerGlobalScope(url, userAgent, thread, timeOrigin, workerClients)
53{
54}
55
56DedicatedWorkerGlobalScope::~DedicatedWorkerGlobalScope()
57{
58}
59
60const AtomicString& DedicatedWorkerGlobalScope::interfaceName() const
61{
62    return EventTargetNames::DedicatedWorkerGlobalScope;
63}
64
65void DedicatedWorkerGlobalScope::postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState)
66{
67    // Disentangle the port in preparation for sending it to the remote context.
68    OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(ports, exceptionState);
69    if (exceptionState.hadException())
70        return;
71    thread()->workerObjectProxy().postMessageToWorkerObject(message, channels.release());
72}
73
74void DedicatedWorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState& exceptionState)
75{
76    Base::importScripts(urls, exceptionState);
77    thread()->workerObjectProxy().reportPendingActivity(hasPendingActivity());
78}
79
80DedicatedWorkerThread* DedicatedWorkerGlobalScope::thread() const
81{
82    return static_cast<DedicatedWorkerThread*>(Base::thread());
83}
84
85class UseCounterTask : public ExecutionContextTask {
86public:
87    static PassOwnPtr<UseCounterTask> createCount(UseCounter::Feature feature) { return adoptPtr(new UseCounterTask(feature, false)); }
88    static PassOwnPtr<UseCounterTask> createDeprecation(UseCounter::Feature feature) { return adoptPtr(new UseCounterTask(feature, true)); }
89
90private:
91    UseCounterTask(UseCounter::Feature feature, bool isDeprecation)
92        : m_feature(feature)
93        , m_isDeprecation(isDeprecation)
94    {
95    }
96
97    virtual void performTask(ExecutionContext* context) OVERRIDE
98    {
99        ASSERT(context->isDocument());
100        if (m_isDeprecation)
101            UseCounter::countDeprecation(context, m_feature);
102        else
103            UseCounter::count(context, m_feature);
104    }
105
106    UseCounter::Feature m_feature;
107    bool m_isDeprecation;
108};
109
110void DedicatedWorkerGlobalScope::countFeature(UseCounter::Feature feature) const
111{
112    thread()->workerObjectProxy().postTaskToMainExecutionContext(UseCounterTask::createCount(feature));
113}
114
115void DedicatedWorkerGlobalScope::countDeprecation(UseCounter::Feature feature) const
116{
117    thread()->workerObjectProxy().postTaskToMainExecutionContext(UseCounterTask::createDeprecation(feature));
118}
119
120void DedicatedWorkerGlobalScope::trace(Visitor* visitor)
121{
122    WorkerGlobalScope::trace(visitor);
123}
124
125} // namespace blink
126