Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / web / SharedWorkerRepositoryClientImpl.cpp
blobb7b59c401808f2a12666d919015e3bc42d747317
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/SharedWorkerRepositoryClientImpl.h"
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/dom/ExecutionContext.h"
38 #include "core/events/Event.h"
39 #include "core/frame/csp/ContentSecurityPolicy.h"
40 #include "core/inspector/InspectorInstrumentation.h"
41 #include "core/workers/SharedWorker.h"
42 #include "platform/network/ResourceResponse.h"
43 #include "public/platform/WebMessagePortChannel.h"
44 #include "public/platform/WebString.h"
45 #include "public/platform/WebURL.h"
46 #include "public/web/WebContentSecurityPolicy.h"
47 #include "public/web/WebFrameClient.h"
48 #include "public/web/WebKit.h"
49 #include "public/web/WebSharedWorker.h"
50 #include "public/web/WebSharedWorkerRepositoryClient.h"
51 #include "web/WebLocalFrameImpl.h"
53 namespace blink {
55 // Callback class that keeps the SharedWorker and WebSharedWorker objects alive while connecting.
56 class SharedWorkerConnector : private WebSharedWorkerConnector::ConnectListener {
57 public:
58 SharedWorkerConnector(SharedWorker* worker, const KURL& url, const String& name, PassOwnPtr<WebMessagePortChannel> channel, PassOwnPtr<WebSharedWorkerConnector> webWorkerConnector)
59 : m_worker(worker)
60 , m_url(url)
61 , m_name(name)
62 , m_webWorkerConnector(webWorkerConnector)
63 , m_channel(channel) { }
65 virtual ~SharedWorkerConnector();
66 void connect();
68 private:
69 // WebSharedWorkerConnector::ConnectListener overrides.
70 void connected() override;
71 void scriptLoadFailed() override;
73 Persistent<SharedWorker> m_worker;
74 KURL m_url;
75 String m_name;
76 OwnPtr<WebSharedWorkerConnector> m_webWorkerConnector;
77 OwnPtr<WebMessagePortChannel> m_channel;
80 SharedWorkerConnector::~SharedWorkerConnector()
82 m_worker->setIsBeingConnected(false);
85 void SharedWorkerConnector::connect()
87 m_worker->setIsBeingConnected(true);
88 m_webWorkerConnector->connect(m_channel.leakPtr(), this);
91 void SharedWorkerConnector::connected()
93 // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
94 delete this;
97 void SharedWorkerConnector::scriptLoadFailed()
99 m_worker->dispatchEvent(Event::createCancelable(EventTypeNames::error));
100 // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
101 delete this;
104 static WebSharedWorkerRepositoryClient::DocumentID getId(void* document)
106 ASSERT(document);
107 return reinterpret_cast<WebSharedWorkerRepositoryClient::DocumentID>(document);
110 void SharedWorkerRepositoryClientImpl::connect(SharedWorker* worker, PassOwnPtr<WebMessagePortChannel> port, const KURL& url, const String& name, ExceptionState& exceptionState)
112 ASSERT(m_client);
114 // No nested workers (for now) - connect() should only be called from document context.
115 ASSERT(worker->executionContext()->isDocument());
116 Document* document = toDocument(worker->executionContext());
118 // TODO(estark): this is broken, as it only uses the first header
119 // when multiple might have been sent. Fix by making the
120 // SharedWorkerConnector interface take a map that can contain
121 // multiple headers.
122 OwnPtr<Vector<CSPHeaderAndType>> headers = worker->executionContext()->contentSecurityPolicy()->headers();
123 WebString header;
124 WebContentSecurityPolicyType headerType = WebContentSecurityPolicyTypeReport;
126 if (headers->size() > 0) {
127 header = (*headers)[0].first;
128 headerType = static_cast<WebContentSecurityPolicyType>((*headers)[0].second);
131 OwnPtr<WebSharedWorkerConnector> webWorkerConnector = adoptPtr(m_client->createSharedWorkerConnector(url, name, getId(document), header, headerType));
132 if (!webWorkerConnector) {
133 // Existing worker does not match this url, so return an error back to the caller.
134 exceptionState.throwDOMException(URLMismatchError, "The location of the SharedWorker named '" + name + "' does not exactly match the provided URL ('" + url.elidedString() + "').");
135 return;
138 // The connector object manages its own lifecycle (and the lifecycles of the two worker objects).
139 // It will free itself once connecting is completed.
140 SharedWorkerConnector* connector = new SharedWorkerConnector(worker, url, name, port, webWorkerConnector.release());
141 connector->connect();
144 void SharedWorkerRepositoryClientImpl::documentDetached(Document* document)
146 ASSERT(m_client);
147 m_client->documentDetached(getId(document));
150 SharedWorkerRepositoryClientImpl::SharedWorkerRepositoryClientImpl(WebSharedWorkerRepositoryClient* client)
151 : m_client(client)
155 } // namespace blink