Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / shared_worker / embedded_shared_worker_stub.cc
blobd556d37749fd1d4305e438955f83edc2995472a3
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_handle_id() !=
94 kInvalidServiceWorkerHandleId;
97 virtual int64_t serviceWorkerID(
98 blink::WebDataSource& data_source) {
99 ServiceWorkerNetworkProvider* provider =
100 GetNetworkProviderFromDataSource(&data_source);
101 if (provider->context()->controller())
102 return provider->context()->controller()->version_id();
103 return kInvalidServiceWorkerVersionId;
106 private:
107 ServiceWorkerNetworkProvider* GetNetworkProviderFromDataSource(
108 const blink::WebDataSource* data_source) {
109 return ServiceWorkerNetworkProvider::FromDocumentState(
110 static_cast<DataSourceExtraData*>(data_source->extraData()));
114 } // namespace
116 EmbeddedSharedWorkerStub::EmbeddedSharedWorkerStub(
117 const GURL& url,
118 const base::string16& name,
119 const base::string16& content_security_policy,
120 blink::WebContentSecurityPolicyType security_policy_type,
121 bool pause_on_start,
122 int route_id)
123 : route_id_(route_id),
124 name_(name),
125 url_(url) {
126 RenderThreadImpl::current()->AddEmbeddedWorkerRoute(route_id_, this);
127 impl_ = blink::WebSharedWorker::create(this);
128 if (pause_on_start) {
129 // Pause worker context when it starts and wait until either DevTools client
130 // is attached or explicit resume notification is received.
131 impl_->pauseWorkerContextOnStart();
133 worker_devtools_agent_.reset(
134 new SharedWorkerDevToolsAgent(route_id, impl_));
135 impl_->startWorkerContext(url, name_,
136 content_security_policy, security_policy_type);
139 EmbeddedSharedWorkerStub::~EmbeddedSharedWorkerStub() {
140 RenderThreadImpl::current()->RemoveEmbeddedWorkerRoute(route_id_);
141 DCHECK(!impl_);
144 bool EmbeddedSharedWorkerStub::OnMessageReceived(
145 const IPC::Message& message) {
146 if (worker_devtools_agent_->OnMessageReceived(message))
147 return true;
148 bool handled = true;
149 IPC_BEGIN_MESSAGE_MAP(EmbeddedSharedWorkerStub, message)
150 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext,
151 OnTerminateWorkerContext)
152 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect)
153 IPC_MESSAGE_UNHANDLED(handled = false)
154 IPC_END_MESSAGE_MAP()
155 return handled;
158 void EmbeddedSharedWorkerStub::OnChannelError() {
159 OnTerminateWorkerContext();
162 void EmbeddedSharedWorkerStub::workerReadyForInspection() {
163 Send(new WorkerHostMsg_WorkerReadyForInspection(route_id_));
166 void EmbeddedSharedWorkerStub::workerScriptLoaded() {
167 Send(new WorkerHostMsg_WorkerScriptLoaded(route_id_));
168 running_ = true;
169 // Process any pending connections.
170 for (PendingChannelList::const_iterator iter = pending_channels_.begin();
171 iter != pending_channels_.end();
172 ++iter) {
173 ConnectToChannel(*iter);
175 pending_channels_.clear();
178 void EmbeddedSharedWorkerStub::workerScriptLoadFailed() {
179 Send(new WorkerHostMsg_WorkerScriptLoadFailed(route_id_));
180 for (PendingChannelList::const_iterator iter = pending_channels_.begin();
181 iter != pending_channels_.end();
182 ++iter) {
183 blink::WebMessagePortChannel* channel = *iter;
184 channel->destroy();
186 pending_channels_.clear();
187 Shutdown();
190 void EmbeddedSharedWorkerStub::workerContextClosed() {
191 Send(new WorkerHostMsg_WorkerContextClosed(route_id_));
194 void EmbeddedSharedWorkerStub::workerContextDestroyed() {
195 Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_));
196 Shutdown();
199 void EmbeddedSharedWorkerStub::selectAppCacheID(long long app_cache_id) {
200 if (app_cache_host_) {
201 // app_cache_host_ could become stale as it's owned by blink's
202 // DocumentLoader. This method is assumed to be called while it's valid.
203 app_cache_host_->backend()->SelectCacheForSharedWorker(
204 app_cache_host_->host_id(), app_cache_id);
208 blink::WebNotificationPresenter*
209 EmbeddedSharedWorkerStub::notificationPresenter() {
210 // TODO(horo): delete this method if we have no plan to implement this.
211 NOTREACHED();
212 return NULL;
215 blink::WebApplicationCacheHost*
216 EmbeddedSharedWorkerStub::createApplicationCacheHost(
217 blink::WebApplicationCacheHostClient* client) {
218 app_cache_host_ = new SharedWorkerWebApplicationCacheHostImpl(client);
219 return app_cache_host_;
222 blink::WebWorkerContentSettingsClientProxy*
223 EmbeddedSharedWorkerStub::createWorkerContentSettingsClientProxy(
224 const blink::WebSecurityOrigin& origin) {
225 return new EmbeddedSharedWorkerContentSettingsClientProxy(
226 GURL(origin.toString()),
227 origin.isUnique(),
228 route_id_,
229 ChildThreadImpl::current()->thread_safe_sender());
232 blink::WebServiceWorkerNetworkProvider*
233 EmbeddedSharedWorkerStub::createServiceWorkerNetworkProvider(
234 blink::WebDataSource* data_source) {
235 // Create a content::ServiceWorkerNetworkProvider for this data source so
236 // we can observe its requests.
237 scoped_ptr<ServiceWorkerNetworkProvider> provider(
238 new ServiceWorkerNetworkProvider(
239 route_id_, SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER));
241 // The provider is kept around for the lifetime of the DataSource
242 // and ownership is transferred to the DataSource.
243 DataSourceExtraData* extra_data = new DataSourceExtraData();
244 data_source->setExtraData(extra_data);
245 ServiceWorkerNetworkProvider::AttachToDocumentState(
246 extra_data, provider.Pass());
248 // Blink is responsible for deleting the returned object.
249 return new WebServiceWorkerNetworkProviderImpl();
252 void EmbeddedSharedWorkerStub::sendDevToolsMessage(
253 int call_id,
254 const blink::WebString& message,
255 const blink::WebString& state) {
256 worker_devtools_agent_->SendDevToolsMessage(call_id, message, state);
259 void EmbeddedSharedWorkerStub::Shutdown() {
260 // WebSharedWorker must be already deleted in the blink side
261 // when this is called.
262 impl_ = nullptr;
263 delete this;
266 bool EmbeddedSharedWorkerStub::Send(IPC::Message* message) {
267 return RenderThreadImpl::current()->Send(message);
270 void EmbeddedSharedWorkerStub::ConnectToChannel(
271 WebMessagePortChannelImpl* channel) {
272 impl_->connect(channel);
273 Send(
274 new WorkerHostMsg_WorkerConnected(channel->message_port_id(), route_id_));
277 void EmbeddedSharedWorkerStub::OnConnect(int sent_message_port_id,
278 int routing_id) {
279 TransferredMessagePort port;
280 port.id = sent_message_port_id;
281 WebMessagePortChannelImpl* channel = new WebMessagePortChannelImpl(
282 routing_id, port, base::ThreadTaskRunnerHandle::Get().get());
283 if (running_) {
284 ConnectToChannel(channel);
285 } else {
286 // If two documents try to load a SharedWorker at the same time, the
287 // WorkerMsg_Connect for one of the documents can come in before the
288 // worker is started. Just queue up the connect and deliver it once the
289 // worker starts.
290 pending_channels_.push_back(channel);
294 void EmbeddedSharedWorkerStub::OnTerminateWorkerContext() {
295 // After this we wouldn't get any IPC for this stub.
296 running_ = false;
297 impl_->terminateWorkerContext();
300 } // namespace content