blob: 56bf4adbf33048ec407cb110cf58424980873981 [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
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"
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000032#include "SharedWorkerRepositoryClientImpl.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010033
34#include "WebContentSecurityPolicy.h"
35#include "WebFrameClient.h"
36#include "WebFrameImpl.h"
37#include "WebKit.h"
38#include "WebSharedWorker.h"
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000039#include "WebSharedWorkerRepositoryClient.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010040#include "bindings/v8/ExceptionMessages.h"
Ben Murdoch7757ec22013-07-23 11:17:36 +010041#include "bindings/v8/ExceptionState.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010042#include "core/dom/ExceptionCode.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010043#include "core/dom/ExecutionContext.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010044#include "core/events/Event.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000045#include "core/frame/csp/ContentSecurityPolicy.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010046#include "core/inspector/InspectorInstrumentation.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010047#include "core/workers/SharedWorker.h"
48#include "core/workers/WorkerScriptLoader.h"
49#include "core/workers/WorkerScriptLoaderClient.h"
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +010050#include "platform/network/ResourceResponse.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010051#include "public/platform/WebMessagePortChannel.h"
52#include "public/platform/WebString.h"
53#include "public/platform/WebURL.h"
54
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000055using namespace WebCore;
56
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000057namespace blink {
Ben Murdoche69819b2013-07-17 14:56:49 +010058
Torne (Richard Coles)09380292014-02-21 12:17:33 +000059// Callback class that keeps the SharedWorker and WebSharedWorker objects alive while connecting.
60class SharedWorkerConnector : private WebSharedWorkerConnector::ConnectListener {
Ben Murdoche69819b2013-07-17 14:56:49 +010061public:
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000062 SharedWorkerConnector(PassRefPtrWillBeRawPtr<SharedWorker> worker, const KURL& url, const String& name, PassOwnPtr<WebMessagePortChannel> channel, PassOwnPtr<WebSharedWorkerConnector> webWorkerConnector)
Ben Murdoche69819b2013-07-17 14:56:49 +010063 : m_worker(worker)
64 , m_url(url)
65 , m_name(name)
Torne (Richard Coles)09380292014-02-21 12:17:33 +000066 , m_webWorkerConnector(webWorkerConnector)
67 , m_channel(channel) { }
Ben Murdoche69819b2013-07-17 14:56:49 +010068
Torne (Richard Coles)09380292014-02-21 12:17:33 +000069 virtual ~SharedWorkerConnector();
70 void connect();
Ben Murdoche69819b2013-07-17 14:56:49 +010071
72private:
Torne (Richard Coles)09380292014-02-21 12:17:33 +000073 // WebSharedWorkerConnector::ConnectListener overrides.
74 virtual void connected() OVERRIDE;
75 virtual void scriptLoadFailed() OVERRIDE;
Ben Murdoche69819b2013-07-17 14:56:49 +010076
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000077 RefPtrWillBePersistent<SharedWorker> m_worker;
Ben Murdoche69819b2013-07-17 14:56:49 +010078 KURL m_url;
79 String m_name;
Torne (Richard Coles)09380292014-02-21 12:17:33 +000080 OwnPtr<WebSharedWorkerConnector> m_webWorkerConnector;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000081 OwnPtr<WebMessagePortChannel> m_channel;
Ben Murdoche69819b2013-07-17 14:56:49 +010082};
83
Torne (Richard Coles)09380292014-02-21 12:17:33 +000084SharedWorkerConnector::~SharedWorkerConnector()
Ben Murdoche69819b2013-07-17 14:56:49 +010085{
Torne (Richard Coles)09380292014-02-21 12:17:33 +000086 m_worker->unsetPreventGC();
87}
88void SharedWorkerConnector::connect()
89{
90 m_worker->setPreventGC();
91 m_webWorkerConnector->connect(m_channel.leakPtr(), this);
Ben Murdoche69819b2013-07-17 14:56:49 +010092}
93
Torne (Richard Coles)09380292014-02-21 12:17:33 +000094void SharedWorkerConnector::connected()
Ben Murdoche69819b2013-07-17 14:56:49 +010095{
Torne (Richard Coles)09380292014-02-21 12:17:33 +000096 // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
97 delete this;
Ben Murdoche69819b2013-07-17 14:56:49 +010098}
99
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000100void SharedWorkerConnector::scriptLoadFailed()
Ben Murdoche69819b2013-07-17 14:56:49 +0100101{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000102 m_worker->dispatchEvent(Event::createCancelable(EventTypeNames::error));
103 // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
Ben Murdoche69819b2013-07-17 14:56:49 +0100104 delete this;
105}
106
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000107static WebSharedWorkerRepositoryClient::DocumentID getId(void* document)
Ben Murdoche69819b2013-07-17 14:56:49 +0100108{
109 ASSERT(document);
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000110 return reinterpret_cast<WebSharedWorkerRepositoryClient::DocumentID>(document);
Ben Murdoche69819b2013-07-17 14:56:49 +0100111}
112
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000113void SharedWorkerRepositoryClientImpl::connect(PassRefPtrWillBeRawPtr<SharedWorker> worker, PassOwnPtr<WebMessagePortChannel> port, const KURL& url, const String& name, ExceptionState& exceptionState)
Ben Murdoche69819b2013-07-17 14:56:49 +0100114{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000115 ASSERT(m_client);
Ben Murdoche69819b2013-07-17 14:56:49 +0100116
117 // No nested workers (for now) - connect() should only be called from document context.
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100118 ASSERT(worker->executionContext()->isDocument());
119 Document* document = toDocument(worker->executionContext());
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000120 OwnPtr<WebSharedWorkerConnector> webWorkerConnector = adoptPtr(m_client->createSharedWorkerConnector(url, name, getId(document), worker->executionContext()->contentSecurityPolicy()->deprecatedHeader(), static_cast<blink::WebContentSecurityPolicyType>(worker->executionContext()->contentSecurityPolicy()->deprecatedHeaderType())));
121 if (!webWorkerConnector) {
Ben Murdoche69819b2013-07-17 14:56:49 +0100122 // Existing worker does not match this url, so return an error back to the caller.
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000123 exceptionState.throwDOMException(URLMismatchError, "The location of the SharedWorker named '" + name + "' does not exactly match the provided URL ('" + url.elidedString() + "').");
Ben Murdoche69819b2013-07-17 14:56:49 +0100124 return;
125 }
126
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000127 // The connector object manages its own lifecycle (and the lifecycles of the two worker objects).
128 // It will free itself once connecting is completed.
129 SharedWorkerConnector* connector = new SharedWorkerConnector(worker, url, name, port, webWorkerConnector.release());
130 connector->connect();
Ben Murdoche69819b2013-07-17 14:56:49 +0100131}
132
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000133void SharedWorkerRepositoryClientImpl::documentDetached(Document* document)
Ben Murdoche69819b2013-07-17 14:56:49 +0100134{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000135 ASSERT(m_client);
136 m_client->documentDetached(getId(document));
Ben Murdoche69819b2013-07-17 14:56:49 +0100137}
138
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000139SharedWorkerRepositoryClientImpl::SharedWorkerRepositoryClientImpl(WebSharedWorkerRepositoryClient* client)
140 : m_client(client)
Ben Murdoche69819b2013-07-17 14:56:49 +0100141{
Ben Murdoche69819b2013-07-17 14:56:49 +0100142}
143
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000144} // namespace blink