Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / SharedWorkerRepositoryClientImpl.cpp
1 /*
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"
32 #include "SharedWorkerRepositoryClientImpl.h"
33
34 #include "WebContentSecurityPolicy.h"
35 #include "WebFrameClient.h"
36 #include "WebFrameImpl.h"
37 #include "WebKit.h"
38 #include "WebSharedWorker.h"
39 #include "WebSharedWorkerRepositoryClient.h"
40 #include "bindings/v8/ExceptionMessages.h"
41 #include "bindings/v8/ExceptionState.h"
42 #include "core/dom/ExceptionCode.h"
43 #include "core/dom/ExecutionContext.h"
44 #include "core/events/Event.h"
45 #include "core/events/ThreadLocalEventNames.h"
46 #include "core/frame/ContentSecurityPolicy.h"
47 #include "core/inspector/InspectorInstrumentation.h"
48 #include "core/workers/SharedWorker.h"
49 #include "core/workers/WorkerScriptLoader.h"
50 #include "core/workers/WorkerScriptLoaderClient.h"
51 #include "platform/network/ResourceResponse.h"
52 #include "public/platform/WebMessagePortChannel.h"
53 #include "public/platform/WebString.h"
54 #include "public/platform/WebURL.h"
55
56 using namespace WebCore;
57
58 namespace blink {
59
60 // Callback class that keeps the SharedWorker and WebSharedWorker objects alive while connecting.
61 class SharedWorkerConnector : private WebSharedWorkerConnector::ConnectListener {
62 public:
63     SharedWorkerConnector(PassRefPtr<SharedWorker> worker, const KURL& url, const String& name, PassOwnPtr<WebMessagePortChannel> channel, PassOwnPtr<WebSharedWorkerConnector> webWorkerConnector)
64         : m_worker(worker)
65         , m_url(url)
66         , m_name(name)
67         , m_webWorkerConnector(webWorkerConnector)
68         , m_channel(channel) { }
69
70     virtual ~SharedWorkerConnector();
71     void connect();
72
73 private:
74     // WebSharedWorkerConnector::ConnectListener overrides.
75     virtual void connected() OVERRIDE;
76     virtual void scriptLoadFailed() OVERRIDE;
77
78     RefPtr<SharedWorker> m_worker;
79     KURL m_url;
80     String m_name;
81     OwnPtr<WebSharedWorkerConnector> m_webWorkerConnector;
82     OwnPtr<WebMessagePortChannel> m_channel;
83 };
84
85 SharedWorkerConnector::~SharedWorkerConnector()
86 {
87     m_worker->unsetPreventGC();
88 }
89 void SharedWorkerConnector::connect()
90 {
91     m_worker->setPreventGC();
92     m_webWorkerConnector->connect(m_channel.leakPtr(), this);
93 }
94
95 void SharedWorkerConnector::connected()
96 {
97     // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
98     delete this;
99 }
100
101 void SharedWorkerConnector::scriptLoadFailed()
102 {
103     m_worker->dispatchEvent(Event::createCancelable(EventTypeNames::error));
104     // Free ourselves (this releases the SharedWorker so it can be freed as well if unreferenced).
105     delete this;
106 }
107
108 static WebSharedWorkerRepositoryClient::DocumentID getId(void* document)
109 {
110     ASSERT(document);
111     return reinterpret_cast<WebSharedWorkerRepositoryClient::DocumentID>(document);
112 }
113
114 void SharedWorkerRepositoryClientImpl::connect(PassRefPtr<SharedWorker> worker, PassOwnPtr<WebMessagePortChannel> port, const KURL& url, const String& name, ExceptionState& exceptionState)
115 {
116     ASSERT(m_client);
117
118     // No nested workers (for now) - connect() should only be called from document context.
119     ASSERT(worker->executionContext()->isDocument());
120     Document* document = toDocument(worker->executionContext());
121     OwnPtr<WebSharedWorkerConnector> webWorkerConnector = adoptPtr(m_client->createSharedWorkerConnector(url, name, getId(document), worker->executionContext()->contentSecurityPolicy()->deprecatedHeader(), static_cast<blink::WebContentSecurityPolicyType>(worker->executionContext()->contentSecurityPolicy()->deprecatedHeaderType())));
122     if (!webWorkerConnector) {
123         // Existing worker does not match this url, so return an error back to the caller.
124         exceptionState.throwDOMException(URLMismatchError, "The location of the SharedWorker named '" + name + "' does not exactly match the provided URL ('" + url.elidedString() + "').");
125         return;
126     }
127
128     // The connector object manages its own lifecycle (and the lifecycles of the two worker objects).
129     // It will free itself once connecting is completed.
130     SharedWorkerConnector* connector = new SharedWorkerConnector(worker, url, name, port, webWorkerConnector.release());
131     connector->connect();
132 }
133
134 void SharedWorkerRepositoryClientImpl::documentDetached(Document* document)
135 {
136     ASSERT(m_client);
137     m_client->documentDetached(getId(document));
138 }
139
140 SharedWorkerRepositoryClientImpl::SharedWorkerRepositoryClientImpl(WebSharedWorkerRepositoryClient* client)
141     : m_client(client)
142 {
143 }
144
145 } // namespace blink