2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2010 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "core/workers/SharedWorker.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/dom/ExecutionContext.h"
38 #include "core/dom/MessageChannel.h"
39 #include "core/dom/MessagePort.h"
40 #include "core/frame/LocalFrame.h"
41 #include "core/frame/UseCounter.h"
42 #include "core/inspector/InspectorInstrumentation.h"
43 #include "core/loader/FrameLoader.h"
44 #include "core/loader/FrameLoaderClient.h"
45 #include "core/workers/SharedWorkerRepositoryClient.h"
46 #include "platform/weborigin/KURL.h"
47 #include "platform/weborigin/SecurityOrigin.h"
51 inline SharedWorker::SharedWorker(ExecutionContext* context)
52 : AbstractWorker(context)
54 ScriptWrappable::init(this);
57 PassRefPtrWillBeRawPtr<SharedWorker> SharedWorker::create(ExecutionContext* context, const String& url, const String& name, ExceptionState& exceptionState)
59 ASSERT(isMainThread());
60 ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument());
62 UseCounter::count(context, UseCounter::SharedWorkerStart);
64 RefPtrWillBeRawPtr<SharedWorker> worker = adoptRefWillBeRefCountedGarbageCollected(new SharedWorker(context));
66 RefPtr<MessageChannel> channel = MessageChannel::create(context);
67 worker->m_port = channel->port1();
68 OwnPtr<blink::WebMessagePortChannel> remotePort = channel->port2()->disentangle();
71 worker->suspendIfNeeded();
73 // We don't currently support nested workers, so workers can only be created from documents.
74 Document* document = toDocument(context);
75 if (!document->securityOrigin()->canAccessSharedWorkers()) {
76 exceptionState.throwSecurityError("Access to shared workers is denied to origin '" + document->securityOrigin()->toString() + "'.");
80 KURL scriptURL = worker->resolveURL(url, exceptionState);
81 if (scriptURL.isEmpty())
84 if (document->frame()->loader().client()->sharedWorkerRepositoryClient())
85 document->frame()->loader().client()->sharedWorkerRepositoryClient()->connect(worker.get(), remotePort.release(), scriptURL, name, exceptionState);
87 return worker.release();
90 SharedWorker::~SharedWorker()
94 const AtomicString& SharedWorker::interfaceName() const
96 return EventTargetNames::SharedWorker;
99 void SharedWorker::setPreventGC()
101 setPendingActivity(this);
104 void SharedWorker::unsetPreventGC()
106 unsetPendingActivity(this);
109 void SharedWorker::trace(Visitor* visitor)
111 AbstractWorker::trace(visitor);
112 WillBeHeapSupplementable<SharedWorker>::trace(visitor);
115 } // namespace WebCore