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