Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / shared_worker / embedded_shared_worker_stub.cc
blob6988540481225f3b7b0e8444c629c71650f24c2f
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/shared_worker/embedded_shared_worker_stub.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "content/child/appcache/appcache_dispatcher.h"
9 #include "content/child/appcache/web_application_cache_host_impl.h"
10 #include "content/child/request_extra_data.h"
11 #include "content/child/scoped_child_process_reference.h"
12 #include "content/child/service_worker/service_worker_handle_reference.h"
13 #include "content/child/service_worker/service_worker_network_provider.h"
14 #include "content/child/service_worker/service_worker_provider_context.h"
15 #include "content/child/shared_worker_devtools_agent.h"
16 #include "content/child/webmessageportchannel_impl.h"
17 #include "content/common/worker_messages.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "content/renderer/shared_worker/embedded_shared_worker_content_settings_client_proxy.h"
20 #include "ipc/ipc_message_macros.h"
21 #include "third_party/WebKit/public/platform/WebURLRequest.h"
22 #include "third_party/WebKit/public/web/WebDataSource.h"
23 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
24 #include "third_party/WebKit/public/web/WebSharedWorker.h"
25 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h"
26 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerNetworkProvider.h"
28 namespace content {
30 namespace {
32 class SharedWorkerWebApplicationCacheHostImpl
33 : public WebApplicationCacheHostImpl {
34 public:
35 SharedWorkerWebApplicationCacheHostImpl(
36 blink::WebApplicationCacheHostClient* client)
37 : WebApplicationCacheHostImpl(client,
38 RenderThreadImpl::current()
39 ->appcache_dispatcher()
40 ->backend_proxy()) {}
42 // Main resource loading is different for workers. The main resource is
43 // loaded by the worker using WorkerScriptLoader.
44 // These overrides are stubbed out.
45 virtual void willStartMainResourceRequest(
46 blink::WebURLRequest&,
47 const blink::WebApplicationCacheHost*) {}
48 virtual void didReceiveResponseForMainResource(const blink::WebURLResponse&) {
50 virtual void didReceiveDataForMainResource(const char* data, unsigned len) {}
51 virtual void didFinishLoadingMainResource(bool success) {}
53 // Cache selection is also different for workers. We know at construction
54 // time what cache to select and do so then.
55 // These overrides are stubbed out.
56 virtual void selectCacheWithoutManifest() {}
57 virtual bool selectCacheWithManifest(const blink::WebURL& manifestURL) {
58 return true;
62 // We store an instance of this class in the "extra data" of the WebDataSource
63 // and attach a ServiceWorkerNetworkProvider to it as base::UserData.
64 // (see createServiceWorkerNetworkProvider).
65 class DataSourceExtraData
66 : public blink::WebDataSource::ExtraData,
67 public base::SupportsUserData {
68 public:
69 DataSourceExtraData() {}
70 virtual ~DataSourceExtraData() {}
73 // Called on the main thread only and blink owns it.
74 class WebServiceWorkerNetworkProviderImpl
75 : public blink::WebServiceWorkerNetworkProvider {
76 public:
77 // Blink calls this method for each request starting with the main script,
78 // we tag them with the provider id.
79 virtual void willSendRequest(
80 blink::WebDataSource* data_source,
81 blink::WebURLRequest& request) {
82 ServiceWorkerNetworkProvider* provider =
83 GetNetworkProviderFromDataSource(data_source);
84 scoped_ptr<RequestExtraData> extra_data(new RequestExtraData);
85 extra_data->set_service_worker_provider_id(provider->provider_id());
86 request.setExtraData(extra_data.release());
89 virtual bool isControlledByServiceWorker(
90 blink::WebDataSource& data_source) {
91 ServiceWorkerNetworkProvider* provider =
92 GetNetworkProviderFromDataSource(&data_source);
93 return provider->context()->controller() != nullptr;
96 virtual int64_t serviceWorkerID(
97 blink::WebDataSource& data_source) {
98 ServiceWorkerNetworkProvider* provider =
99 GetNetworkProviderFromDataSource(&data_source);
100 if (provider->context()->controller())
101 return provider->context()->controller()->version_id();
102 return kInvalidServiceWorkerVersionId;
105 private:
106 ServiceWorkerNetworkProvider* GetNetworkProviderFromDataSource(
107 const blink::WebDataSource* data_source) {
108 return ServiceWorkerNetworkProvider::FromDocumentState(
109 static_cast<DataSourceExtraData*>(data_source->extraData()));
113 } // namespace
115 EmbeddedSharedWorkerStub::EmbeddedSharedWorkerStub(
116 const GURL& url,
117 const base::string16& name,
118 const base::string16& content_security_policy,
119 blink::WebContentSecurityPolicyType security_policy_type,
120 bool pause_on_start,
121 int route_id)
122 : route_id_(route_id),
123 name_(name),
124 url_(url) {
125 RenderThreadImpl::current()->AddEmbeddedWorkerRoute(route_id_, this);
126 impl_ = blink::WebSharedWorker::create(this);
127 if (pause_on_start) {
128 // Pause worker context when it starts and wait until either DevTools client
129 // is attached or explicit resume notification is received.
130 impl_->pauseWorkerContextOnStart();
132 worker_devtools_agent_.reset(
133 new SharedWorkerDevToolsAgent(route_id, impl_));
134 impl_->startWorkerContext(url, name_,
135 content_security_policy, security_policy_type);
138 EmbeddedSharedWorkerStub::~EmbeddedSharedWorkerStub() {
139 RenderThreadImpl::current()->RemoveEmbeddedWorkerRoute(route_id_);
140 DCHECK(!impl_);
143 bool EmbeddedSharedWorkerStub::OnMessageReceived(
144 const IPC::Message& message) {
145 if (worker_devtools_agent_->OnMessageReceived(message))
146 return true;
147 bool handled = true;
148 IPC_BEGIN_MESSAGE_MAP(EmbeddedSharedWorkerStub, message)
149 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext,
150 OnTerminateWorkerContext)
151 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect)
152 IPC_MESSAGE_UNHANDLED(handled = false)
153 IPC_END_MESSAGE_MAP()
154 return handled;
157 void EmbeddedSharedWorkerStub::OnChannelError() {
158 OnTerminateWorkerContext();
161 void EmbeddedSharedWorkerStub::workerReadyForInspection() {
162 Send(new WorkerHostMsg_WorkerReadyForInspection(route_id_));
165 void EmbeddedSharedWorkerStub::workerScriptLoaded() {
166 Send(new WorkerHostMsg_WorkerScriptLoaded(route_id_));
167 running_ = true;
168 // Process any pending connections.
169 for (PendingChannelList::const_iterator iter = pending_channels_.begin();
170 iter != pending_channels_.end();
171 ++iter) {
172 ConnectToChannel(*iter);
174 pending_channels_.clear();
177 void EmbeddedSharedWorkerStub::workerScriptLoadFailed() {
178 Send(new WorkerHostMsg_WorkerScriptLoadFailed(route_id_));
179 for (PendingChannelList::const_iterator iter = pending_channels_.begin();
180 iter != pending_channels_.end();
181 ++iter) {
182 blink::WebMessagePortChannel* channel = *iter;
183 channel->destroy();
185 pending_channels_.clear();
186 Shutdown();
189 void EmbeddedSharedWorkerStub::workerContextClosed() {
190 Send(new WorkerHostMsg_WorkerContextClosed(route_id_));
193 void EmbeddedSharedWorkerStub::workerContextDestroyed() {
194 Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_));
195 Shutdown();
198 void EmbeddedSharedWorkerStub::selectAppCacheID(long long app_cache_id) {
199 if (app_cache_host_) {
200 // app_cache_host_ could become stale as it's owned by blink's
201 // DocumentLoader. This method is assumed to be called while it's valid.
202 app_cache_host_->backend()->SelectCacheForSharedWorker(
203 app_cache_host_->host_id(), app_cache_id);
207 blink::WebNotificationPresenter*
208 EmbeddedSharedWorkerStub::notificationPresenter() {
209 // TODO(horo): delete this method if we have no plan to implement this.
210 NOTREACHED();
211 return NULL;
214 blink::WebApplicationCacheHost*
215 EmbeddedSharedWorkerStub::createApplicationCacheHost(
216 blink::WebApplicationCacheHostClient* client) {
217 app_cache_host_ = new SharedWorkerWebApplicationCacheHostImpl(client);
218 return app_cache_host_;
221 blink::WebWorkerContentSettingsClientProxy*
222 EmbeddedSharedWorkerStub::createWorkerContentSettingsClientProxy(
223 const blink::WebSecurityOrigin& origin) {
224 return new EmbeddedSharedWorkerContentSettingsClientProxy(
225 GURL(origin.toString()),
226 origin.isUnique(),
227 route_id_,
228 ChildThreadImpl::current()->thread_safe_sender());
231 blink::WebServiceWorkerNetworkProvider*
232 EmbeddedSharedWorkerStub::createServiceWorkerNetworkProvider(
233 blink::WebDataSource* data_source) {
234 // Create a content::ServiceWorkerNetworkProvider for this data source so
235 // we can observe its requests.
236 scoped_ptr<ServiceWorkerNetworkProvider> provider(
237 new ServiceWorkerNetworkProvider(
238 route_id_, SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER));
240 // The provider is kept around for the lifetime of the DataSource
241 // and ownership is transferred to the DataSource.
242 DataSourceExtraData* extra_data = new DataSourceExtraData();
243 data_source->setExtraData(extra_data);
244 ServiceWorkerNetworkProvider::AttachToDocumentState(
245 extra_data, provider.Pass());
247 // Blink is responsible for deleting the returned object.
248 return new WebServiceWorkerNetworkProviderImpl();
251 void EmbeddedSharedWorkerStub::sendDevToolsMessage(
252 int call_id,
253 const blink::WebString& message,
254 const blink::WebString& state) {
255 worker_devtools_agent_->SendDevToolsMessage(call_id, message, state);
258 void EmbeddedSharedWorkerStub::Shutdown() {
259 // WebSharedWorker must be already deleted in the blink side
260 // when this is called.
261 impl_ = nullptr;
262 delete this;
265 bool EmbeddedSharedWorkerStub::Send(IPC::Message* message) {
266 return RenderThreadImpl::current()->Send(message);
269 void EmbeddedSharedWorkerStub::ConnectToChannel(
270 WebMessagePortChannelImpl* channel) {
271 impl_->connect(channel);
272 Send(
273 new WorkerHostMsg_WorkerConnected(channel->message_port_id(), route_id_));
276 void EmbeddedSharedWorkerStub::OnConnect(int sent_message_port_id,
277 int routing_id) {
278 TransferredMessagePort port;
279 port.id = sent_message_port_id;
280 WebMessagePortChannelImpl* channel = new WebMessagePortChannelImpl(
281 routing_id, port, base::ThreadTaskRunnerHandle::Get().get());
282 if (running_) {
283 ConnectToChannel(channel);
284 } else {
285 // If two documents try to load a SharedWorker at the same time, the
286 // WorkerMsg_Connect for one of the documents can come in before the
287 // worker is started. Just queue up the connect and deliver it once the
288 // worker starts.
289 pending_channels_.push_back(channel);
293 void EmbeddedSharedWorkerStub::OnTerminateWorkerContext() {
294 // After this we wouldn't get any IPC for this stub.
295 running_ = false;
296 impl_->terminateWorkerContext();
299 } // namespace content