- add sources.
[platform/framework/web/crosswalk.git] / src / content / worker / websharedworkerclient_proxy.cc
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.
4
5 #include "content/worker/websharedworkerclient_proxy.h"
6
7 #include "base/bind.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"
24
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;
33
34 namespace content {
35
36 // How long to wait for worker to finish after it's been told to terminate.
37 #define kMaxTimeForRunawayWorkerSeconds 3
38
39 WebSharedWorkerClientProxy::WebSharedWorkerClientProxy(
40     int route_id, WebSharedWorkerStub* stub)
41     : route_id_(route_id),
42       appcache_host_id_(0),
43       stub_(stub),
44       weak_factory_(this),
45       devtools_agent_(NULL) {
46 }
47
48 WebSharedWorkerClientProxy::~WebSharedWorkerClientProxy() {
49 }
50
51 void WebSharedWorkerClientProxy::workerContextClosed() {
52   Send(new WorkerHostMsg_WorkerContextClosed(route_id_));
53 }
54
55 void WebSharedWorkerClientProxy::workerContextDestroyed() {
56   Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_));
57   // Tell the stub that the worker has shutdown - frees this object.
58   if (stub_)
59     stub_->Shutdown();
60 }
61
62 WebKit::WebNotificationPresenter*
63 WebSharedWorkerClientProxy::notificationPresenter() {
64   // TODO(johnnyg): Notifications are not yet hooked up to workers.
65   // Coming soon.
66   NOTREACHED();
67   return NULL;
68 }
69
70 WebApplicationCacheHost* WebSharedWorkerClientProxy::createApplicationCacheHost(
71     WebKit::WebApplicationCacheHostClient* client) {
72   WorkerWebApplicationCacheHostImpl* host =
73       new WorkerWebApplicationCacheHostImpl(stub_->appcache_init_info(),
74                                             client);
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();
78   return host;
79 }
80
81 WebKit::WebWorkerPermissionClientProxy*
82 WebSharedWorkerClientProxy::createWorkerPermissionClientProxy(
83     const WebKit::WebSecurityOrigin& origin) {
84   if (origin.isUnique())
85     return NULL;
86   return new SharedWorkerPermissionClientProxy(
87       GURL(origin.toString()), route_id_,
88       ChildThread::current()->thread_safe_sender());
89 }
90
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) {
96   return false;
97 }
98
99 bool WebSharedWorkerClientProxy::allowFileSystem() {
100   return false;
101 }
102
103 bool WebSharedWorkerClientProxy::allowIndexedDB(const WebKit::WebString& name) {
104   return false;
105 }
106
107 void WebSharedWorkerClientProxy::dispatchDevToolsMessage(
108     const WebString& message) {
109   if (devtools_agent_)
110     devtools_agent_->SendDevToolsMessage(message);
111 }
112
113 void WebSharedWorkerClientProxy::saveDevToolsAgentState(
114     const WebKit::WebString& state) {
115   if (devtools_agent_)
116     devtools_agent_->SaveDevToolsAgentState(state);
117 }
118
119 bool WebSharedWorkerClientProxy::Send(IPC::Message* message) {
120   return WorkerThread::current()->Send(message);
121 }
122
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(
129       FROM_HERE,
130       base::Bind(&WebSharedWorkerClientProxy::workerContextDestroyed,
131                  weak_factory_.GetWeakPtr()),
132       base::TimeDelta::FromSeconds(kMaxTimeForRunawayWorkerSeconds));
133 }
134
135 }  // namespace content