Home | History | Annotate | Download | only in workers
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  * Copyright (C) 2010 Apple 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 are
      7  * met:
      8  *
      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
     14  * distribution.
     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.
     18  *
     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.
     30  */
     31 
     32 #include "config.h"
     33 #include "core/workers/SharedWorker.h"
     34 
     35 #include "bindings/core/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"
     48 
     49 namespace blink {
     50 
     51 inline SharedWorker::SharedWorker(ExecutionContext* context)
     52     : AbstractWorker(context)
     53     , m_isBeingConnected(false)
     54 {
     55 }
     56 
     57 PassRefPtrWillBeRawPtr<SharedWorker> SharedWorker::create(ExecutionContext* context, const String& url, const String& name, ExceptionState& exceptionState)
     58 {
     59     ASSERT(isMainThread());
     60     ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument());
     61 
     62     UseCounter::count(context, UseCounter::SharedWorkerStart);
     63 
     64     RefPtrWillBeRawPtr<SharedWorker> worker = adoptRefWillBeNoop(new SharedWorker(context));
     65 
     66     RefPtrWillBeRawPtr<MessageChannel> channel = MessageChannel::create(context);
     67     worker->m_port = channel->port1();
     68     OwnPtr<blink::WebMessagePortChannel> remotePort = channel->port2()->disentangle();
     69     ASSERT(remotePort);
     70 
     71     worker->suspendIfNeeded();
     72 
     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() + "'.");
     77         return nullptr;
     78     }
     79 
     80     KURL scriptURL = worker->resolveURL(url, exceptionState);
     81     if (scriptURL.isEmpty())
     82         return nullptr;
     83 
     84     if (document->frame()->loader().client()->sharedWorkerRepositoryClient())
     85         document->frame()->loader().client()->sharedWorkerRepositoryClient()->connect(worker.get(), remotePort.release(), scriptURL, name, exceptionState);
     86 
     87     return worker.release();
     88 }
     89 
     90 SharedWorker::~SharedWorker()
     91 {
     92 }
     93 
     94 const AtomicString& SharedWorker::interfaceName() const
     95 {
     96     return EventTargetNames::SharedWorker;
     97 }
     98 
     99 bool SharedWorker::hasPendingActivity() const
    100 {
    101     return m_isBeingConnected;
    102 }
    103 
    104 void SharedWorker::trace(Visitor* visitor)
    105 {
    106 #if ENABLE(OILPAN)
    107     visitor->trace(m_port);
    108     HeapSupplementable<SharedWorker>::trace(visitor);
    109 #endif
    110     AbstractWorker::trace(visitor);
    111 }
    112 
    113 } // namespace blink
    114