| OLD | NEW |
| (Empty) |
| 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 "web/SharedWorkerRepositoryClientImpl.h" | |
| 32 | |
| 33 #include <memory> | |
| 34 #include <utility> | |
| 35 #include "core/dom/ExecutionContext.h" | |
| 36 #include "core/events/Event.h" | |
| 37 #include "core/frame/UseCounter.h" | |
| 38 #include "core/frame/csp/ContentSecurityPolicy.h" | |
| 39 #include "core/probe/CoreProbes.h" | |
| 40 #include "core/workers/SharedWorker.h" | |
| 41 #include "platform/loader/fetch/ResourceResponse.h" | |
| 42 #include "platform/wtf/PtrUtil.h" | |
| 43 #include "public/platform/WebContentSecurityPolicy.h" | |
| 44 #include "public/platform/WebMessagePortChannel.h" | |
| 45 #include "public/platform/WebString.h" | |
| 46 #include "public/platform/WebURL.h" | |
| 47 #include "public/web/WebFrameClient.h" | |
| 48 #include "public/web/WebKit.h" | |
| 49 #include "public/web/WebSharedWorker.h" | |
| 50 #include "public/web/WebSharedWorkerConnectListener.h" | |
| 51 #include "public/web/WebSharedWorkerCreationErrors.h" | |
| 52 #include "public/web/WebSharedWorkerRepositoryClient.h" | |
| 53 #include "web/WebLocalFrameImpl.h" | |
| 54 | |
| 55 namespace blink { | |
| 56 | |
| 57 // Implementation of the callback interface passed to the embedder. This will be | |
| 58 // destructed when a connection to a shared worker is established. | |
| 59 class SharedWorkerConnectListener final | |
| 60 : public WebSharedWorkerConnectListener { | |
| 61 public: | |
| 62 explicit SharedWorkerConnectListener(SharedWorker* worker) | |
| 63 : worker_(worker) {} | |
| 64 | |
| 65 ~SharedWorkerConnectListener() override { | |
| 66 DCHECK(!worker_->IsBeingConnected()); | |
| 67 } | |
| 68 | |
| 69 // WebSharedWorkerConnectListener overrides. | |
| 70 | |
| 71 void WorkerCreated(WebWorkerCreationError creation_error) override { | |
| 72 worker_->SetIsBeingConnected(true); | |
| 73 | |
| 74 // No nested workers (for now) - connect() should only be called from | |
| 75 // document context. | |
| 76 DCHECK(worker_->GetExecutionContext()->IsDocument()); | |
| 77 Document* document = ToDocument(worker_->GetExecutionContext()); | |
| 78 bool is_secure_context = worker_->GetExecutionContext()->IsSecureContext(); | |
| 79 switch (creation_error) { | |
| 80 case kWebWorkerCreationErrorNone: | |
| 81 return; | |
| 82 case kWebWorkerCreationErrorSecureContextMismatch: | |
| 83 UseCounter::Feature feature = | |
| 84 is_secure_context | |
| 85 ? UseCounter::kNonSecureSharedWorkerAccessedFromSecureContext | |
| 86 : UseCounter::kSecureSharedWorkerAccessedFromNonSecureContext; | |
| 87 UseCounter::Count(document, feature); | |
| 88 return; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void ScriptLoadFailed() override { | |
| 93 worker_->DispatchEvent(Event::CreateCancelable(EventTypeNames::error)); | |
| 94 worker_->SetIsBeingConnected(false); | |
| 95 } | |
| 96 | |
| 97 void Connected() override { worker_->SetIsBeingConnected(false); } | |
| 98 | |
| 99 void CountFeature(uint32_t feature) override { | |
| 100 UseCounter::Count(worker_->GetExecutionContext(), | |
| 101 static_cast<UseCounter::Feature>(feature)); | |
| 102 } | |
| 103 | |
| 104 Persistent<SharedWorker> worker_; | |
| 105 }; | |
| 106 | |
| 107 static WebSharedWorkerRepositoryClient::DocumentID GetId(void* document) { | |
| 108 DCHECK(document); | |
| 109 return reinterpret_cast<WebSharedWorkerRepositoryClient::DocumentID>( | |
| 110 document); | |
| 111 } | |
| 112 | |
| 113 void SharedWorkerRepositoryClientImpl::Connect( | |
| 114 SharedWorker* worker, | |
| 115 std::unique_ptr<WebMessagePortChannel> port, | |
| 116 const KURL& url, | |
| 117 const String& name) { | |
| 118 DCHECK(client_); | |
| 119 | |
| 120 // No nested workers (for now) - connect() should only be called from document | |
| 121 // context. | |
| 122 DCHECK(worker->GetExecutionContext()->IsDocument()); | |
| 123 Document* document = ToDocument(worker->GetExecutionContext()); | |
| 124 | |
| 125 // TODO(estark): this is broken, as it only uses the first header | |
| 126 // when multiple might have been sent. Fix by making the | |
| 127 // SharedWorkerConnectListener interface take a map that can contain | |
| 128 // multiple headers. | |
| 129 std::unique_ptr<Vector<CSPHeaderAndType>> headers = | |
| 130 worker->GetExecutionContext()->GetContentSecurityPolicy()->Headers(); | |
| 131 WebString header; | |
| 132 WebContentSecurityPolicyType header_type = | |
| 133 kWebContentSecurityPolicyTypeReport; | |
| 134 | |
| 135 if (headers->size() > 0) { | |
| 136 header = (*headers)[0].first; | |
| 137 header_type = | |
| 138 static_cast<WebContentSecurityPolicyType>((*headers)[0].second); | |
| 139 } | |
| 140 | |
| 141 bool is_secure_context = worker->GetExecutionContext()->IsSecureContext(); | |
| 142 std::unique_ptr<WebSharedWorkerConnectListener> listener = | |
| 143 WTF::MakeUnique<SharedWorkerConnectListener>(worker); | |
| 144 client_->Connect( | |
| 145 url, name, GetId(document), header, header_type, | |
| 146 worker->GetExecutionContext()->GetSecurityContext().AddressSpace(), | |
| 147 is_secure_context ? kWebSharedWorkerCreationContextTypeSecure | |
| 148 : kWebSharedWorkerCreationContextTypeNonsecure, | |
| 149 std::move(port), std::move(listener)); | |
| 150 } | |
| 151 | |
| 152 void SharedWorkerRepositoryClientImpl::DocumentDetached(Document* document) { | |
| 153 DCHECK(client_); | |
| 154 client_->DocumentDetached(GetId(document)); | |
| 155 } | |
| 156 | |
| 157 SharedWorkerRepositoryClientImpl::SharedWorkerRepositoryClientImpl( | |
| 158 WebSharedWorkerRepositoryClient* client) | |
| 159 : client_(client) {} | |
| 160 | |
| 161 } // namespace blink | |
| OLD | NEW |