OLD | NEW |
1 /* | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * Copyright (C) 2009 Google Inc. All Rights Reserved. | 3 // found in the LICENSE file. |
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 | 4 |
28 #include "config.h" | 5 #include "config.h" |
29 #include "core/workers/Worker.h" | 6 #include "core/workers/Worker.h" |
30 | 7 |
31 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
32 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
33 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
34 #include "core/events/MessageEvent.h" | |
35 #include "core/fetch/ResourceFetcher.h" | |
36 #include "core/inspector/InspectorInstrumentation.h" | |
37 #include "core/frame/LocalDOMWindow.h" | |
38 #include "core/frame/UseCounter.h" | 11 #include "core/frame/UseCounter.h" |
39 #include "core/workers/WorkerGlobalScopeProxy.h" | 12 #include "core/workers/WorkerGlobalScopeProxy.h" |
40 #include "core/workers/WorkerGlobalScopeProxyProvider.h" | 13 #include "core/workers/WorkerGlobalScopeProxyProvider.h" |
41 #include "core/workers/WorkerScriptLoader.h" | |
42 #include "core/workers/WorkerThread.h" | |
43 #include "wtf/MainThread.h" | |
44 | 14 |
45 namespace blink { | 15 namespace blink { |
46 | 16 |
47 Worker::Worker(ExecutionContext* context) | 17 Worker::Worker(ExecutionContext* context) |
48 : AbstractWorker(context) | 18 : InProcessWorkerBase(context) |
49 , m_contextProxy(nullptr) | |
50 { | 19 { |
51 } | 20 } |
52 | 21 |
53 PassRefPtrWillBeRawPtr<Worker> Worker::create(ExecutionContext* context, const S
tring& url, ExceptionState& exceptionState) | 22 PassRefPtrWillBeRawPtr<Worker> Worker::create(ExecutionContext* context, const S
tring& url, ExceptionState& exceptionState) |
54 { | 23 { |
55 ASSERT(isMainThread()); | 24 ASSERT(isMainThread()); |
56 Document* document = toDocument(context); | 25 Document* document = toDocument(context); |
57 UseCounter::count(context, UseCounter::WorkerStart); | 26 UseCounter::count(context, UseCounter::WorkerStart); |
58 if (!document->page()) { | 27 if (!document->page()) { |
59 exceptionState.throwDOMException(InvalidAccessError, "The context provid
ed is invalid."); | 28 exceptionState.throwDOMException(InvalidAccessError, "The context provid
ed is invalid."); |
60 return nullptr; | 29 return nullptr; |
61 } | 30 } |
62 RefPtrWillBeRawPtr<Worker> worker = adoptRefWillBeNoop(new Worker(context)); | 31 RefPtrWillBeRawPtr<Worker> worker = adoptRefWillBeNoop(new Worker(context)); |
63 if (worker->initialize(context, url, exceptionState)) | 32 if (worker->initialize(context, url, exceptionState)) |
64 return worker.release(); | 33 return worker.release(); |
65 return nullptr; | 34 return nullptr; |
66 } | 35 } |
67 | 36 |
68 Worker::~Worker() | 37 Worker::~Worker() |
69 { | 38 { |
70 ASSERT(isMainThread()); | 39 ASSERT(isMainThread()); |
71 if (!m_contextProxy) | |
72 return; | |
73 m_contextProxy->workerObjectDestroyed(); | |
74 } | 40 } |
75 | 41 |
76 const AtomicString& Worker::interfaceName() const | 42 const AtomicString& Worker::interfaceName() const |
77 { | 43 { |
78 return EventTargetNames::Worker; | 44 return EventTargetNames::Worker; |
79 } | 45 } |
80 | 46 |
81 void Worker::postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValue> me
ssage, const MessagePortArray* ports, ExceptionState& exceptionState) | |
82 { | |
83 ASSERT(m_contextProxy); | |
84 // Disentangle the port in preparation for sending it to the remote context. | |
85 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); | |
86 if (exceptionState.hadException()) | |
87 return; | |
88 m_contextProxy->postMessageToWorkerGlobalScope(message, channels.release()); | |
89 } | |
90 | |
91 bool Worker::initialize(ExecutionContext* context, const String& url, ExceptionS
tate& exceptionState) | |
92 { | |
93 suspendIfNeeded(); | |
94 | |
95 KURL scriptURL = resolveURL(url, exceptionState); | |
96 if (scriptURL.isEmpty()) | |
97 return false; | |
98 | |
99 m_scriptLoader = WorkerScriptLoader::create(); | |
100 m_scriptLoader->loadAsynchronously(*context, scriptURL, DenyCrossOriginReque
sts, this); | |
101 | |
102 m_contextProxy = createWorkerGlobalScopeProxy(context); | |
103 | |
104 return true; | |
105 } | |
106 | |
107 WorkerGlobalScopeProxy* Worker::createWorkerGlobalScopeProxy(ExecutionContext* c
ontext) | 47 WorkerGlobalScopeProxy* Worker::createWorkerGlobalScopeProxy(ExecutionContext* c
ontext) |
108 { | 48 { |
109 Document* document = toDocument(context); | 49 Document* document = toDocument(context); |
110 WorkerGlobalScopeProxyProvider* proxyProvider = WorkerGlobalScopeProxyProvid
er::from(*document->page()); | 50 WorkerGlobalScopeProxyProvider* proxyProvider = WorkerGlobalScopeProxyProvid
er::from(*document->page()); |
111 ASSERT(proxyProvider); | 51 ASSERT(proxyProvider); |
112 return proxyProvider->createWorkerGlobalScopeProxy(this); | 52 return proxyProvider->createWorkerGlobalScopeProxy(this); |
113 } | 53 } |
114 | 54 |
115 void Worker::terminate() | |
116 { | |
117 if (m_contextProxy) | |
118 m_contextProxy->terminateWorkerGlobalScope(); | |
119 } | |
120 | |
121 void Worker::stop() | |
122 { | |
123 terminate(); | |
124 } | |
125 | |
126 bool Worker::hasPendingActivity() const | |
127 { | |
128 // The worker context does not exist while loading, so we must ensure that t
he worker object is not collected, nor are its event listeners. | |
129 return (m_contextProxy && m_contextProxy->hasPendingActivity()) || m_scriptL
oader; | |
130 } | |
131 | |
132 void Worker::didReceiveResponse(unsigned long identifier, const ResourceResponse
&) | |
133 { | |
134 InspectorInstrumentation::didReceiveScriptResponse(executionContext(), ident
ifier); | |
135 } | |
136 | |
137 void Worker::notifyFinished() | |
138 { | |
139 if (m_scriptLoader->failed()) { | |
140 dispatchEvent(Event::createCancelable(EventTypeNames::error)); | |
141 } else { | |
142 ASSERT(m_contextProxy); | |
143 WorkerThreadStartMode startMode = DontPauseWorkerGlobalScopeOnStart; | |
144 if (InspectorInstrumentation::shouldPauseDedicatedWorkerOnStart(executio
nContext())) | |
145 startMode = PauseWorkerGlobalScopeOnStart; | |
146 m_contextProxy->startWorkerGlobalScope(m_scriptLoader->url(), executionC
ontext()->userAgent(m_scriptLoader->url()), m_scriptLoader->script(), startMode)
; | |
147 InspectorInstrumentation::scriptImported(executionContext(), m_scriptLoa
der->identifier(), m_scriptLoader->script()); | |
148 } | |
149 m_scriptLoader = nullptr; | |
150 } | |
151 | |
152 DEFINE_TRACE(Worker) | |
153 { | |
154 AbstractWorker::trace(visitor); | |
155 } | |
156 | |
157 } // namespace blink | 55 } // namespace blink |
OLD | NEW |