1 // Copyright (c) 2012 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/worker/websharedworkerclient_proxy.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/child/webmessageportchannel_impl.h"
11 #include "content/common/worker_messages.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/worker/shared_worker_devtools_agent.h"
14 #include "content/worker/shared_worker_permission_client_proxy.h"
15 #include "content/worker/websharedworker_stub.h"
16 #include "content/worker/worker_thread.h"
17 #include "content/worker/worker_webapplicationcachehost_impl.h"
18 #include "ipc/ipc_logging.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
25 using WebKit::WebApplicationCacheHost;
26 using WebKit::WebFrame;
27 using WebKit::WebMessagePortChannel;
28 using WebKit::WebMessagePortChannelArray;
29 using WebKit::WebSecurityOrigin;
30 using WebKit::WebString;
31 using WebKit::WebWorker;
32 using WebKit::WebSharedWorkerClient;
36 // How long to wait for worker to finish after it's been told to terminate.
37 #define kMaxTimeForRunawayWorkerSeconds 3
39 WebSharedWorkerClientProxy::WebSharedWorkerClientProxy(
40 int route_id, WebSharedWorkerStub* stub)
41 : route_id_(route_id),
45 devtools_agent_(NULL) {
48 WebSharedWorkerClientProxy::~WebSharedWorkerClientProxy() {
51 void WebSharedWorkerClientProxy::workerContextClosed() {
52 Send(new WorkerHostMsg_WorkerContextClosed(route_id_));
55 void WebSharedWorkerClientProxy::workerContextDestroyed() {
56 Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_));
57 // Tell the stub that the worker has shutdown - frees this object.
62 WebKit::WebNotificationPresenter*
63 WebSharedWorkerClientProxy::notificationPresenter() {
64 // TODO(johnnyg): Notifications are not yet hooked up to workers.
70 WebApplicationCacheHost* WebSharedWorkerClientProxy::createApplicationCacheHost(
71 WebKit::WebApplicationCacheHostClient* client) {
72 WorkerWebApplicationCacheHostImpl* host =
73 new WorkerWebApplicationCacheHostImpl(stub_->appcache_init_info(),
75 // Remember the id of the instance we create so we have access to that
76 // value when creating nested dedicated workers in createWorker.
77 appcache_host_id_ = host->host_id();
81 WebKit::WebWorkerPermissionClientProxy*
82 WebSharedWorkerClientProxy::createWorkerPermissionClientProxy(
83 const WebKit::WebSecurityOrigin& origin) {
84 if (origin.isUnique())
86 return new SharedWorkerPermissionClientProxy(
87 GURL(origin.toString()), route_id_,
88 ChildThread::current()->thread_safe_sender());
91 // TODO(kinuko): Deprecate these methods.
92 bool WebSharedWorkerClientProxy::allowDatabase(WebFrame* frame,
93 const WebString& name,
94 const WebString& display_name,
95 unsigned long estimated_size) {
99 bool WebSharedWorkerClientProxy::allowFileSystem() {
103 bool WebSharedWorkerClientProxy::allowIndexedDB(const WebKit::WebString& name) {
107 void WebSharedWorkerClientProxy::dispatchDevToolsMessage(
108 const WebString& message) {
110 devtools_agent_->SendDevToolsMessage(message);
113 void WebSharedWorkerClientProxy::saveDevToolsAgentState(
114 const WebKit::WebString& state) {
116 devtools_agent_->SaveDevToolsAgentState(state);
119 bool WebSharedWorkerClientProxy::Send(IPC::Message* message) {
120 return WorkerThread::current()->Send(message);
123 void WebSharedWorkerClientProxy::EnsureWorkerContextTerminates() {
124 // This shuts down the process cleanly from the perspective of the browser
125 // process, and avoids the crashed worker infobar from appearing to the new
126 // page. It's ok to post several of theese, because the first executed task
127 // will exit the message loop and subsequent ones won't be executed.
128 base::MessageLoop::current()->PostDelayedTask(
130 base::Bind(&WebSharedWorkerClientProxy::workerContextDestroyed,
131 weak_factory_.GetWeakPtr()),
132 base::TimeDelta::FromSeconds(kMaxTimeForRunawayWorkerSeconds));
135 } // namespace content