[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 1 | // Copyright (c) 2009 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 | |
[email protected] | 6f51608 | 2011-03-17 19:15:35 | [diff] [blame] | 5 | #include "content/renderer/websharedworker_proxy.h" |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 6 | |
[email protected] | 59f4f2fa | 2011-03-23 01:00:55 | [diff] [blame] | 7 | #include "content/common/view_messages.h" |
[email protected] | e93e04e | 2011-03-14 00:27:10 | [diff] [blame] | 8 | #include "content/common/webmessageportchannel_impl.h" |
[email protected] | 4d223d3 | 2011-03-12 06:38:56 | [diff] [blame] | 9 | #include "content/common/worker_messages.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 10 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 11 | |
| 12 | WebSharedWorkerProxy::WebSharedWorkerProxy(ChildThread* child_thread, |
[email protected] | 0791d3a | 2010-01-28 01:28:49 | [diff] [blame] | 13 | unsigned long long document_id, |
[email protected] | 6de0bcf | 2010-02-17 22:00:51 | [diff] [blame] | 14 | bool exists, |
[email protected] | 9c00f00 | 2009-11-05 22:37:42 | [diff] [blame] | 15 | int route_id, |
| 16 | int render_view_route_id) |
[email protected] | 6de0bcf | 2010-02-17 22:00:51 | [diff] [blame] | 17 | : WebWorkerBase(child_thread, |
| 18 | document_id, |
| 19 | exists ? route_id : MSG_ROUTING_NONE, |
[email protected] | 14396e9 | 2010-05-06 20:40:56 | [diff] [blame] | 20 | render_view_route_id, |
[email protected] | 55c76bd | 2011-10-07 07:41:04 | [diff] [blame] | 21 | 0), |
[email protected] | 6de0bcf | 2010-02-17 22:00:51 | [diff] [blame] | 22 | pending_route_id_(route_id), |
[email protected] | 30447b6 | 2009-11-13 01:13:37 | [diff] [blame] | 23 | connect_listener_(NULL) { |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | bool WebSharedWorkerProxy::isStarted() { |
| 27 | return IsStarted(); |
| 28 | } |
| 29 | |
| 30 | void WebSharedWorkerProxy::startWorkerContext( |
| 31 | const WebKit::WebURL& script_url, |
| 32 | const WebKit::WebString& name, |
| 33 | const WebKit::WebString& user_agent, |
[email protected] | b7ab026 | 2010-05-05 21:54:21 | [diff] [blame] | 34 | const WebKit::WebString& source_code, |
| 35 | long long script_resource_appcache_id) { |
[email protected] | 30447b6 | 2009-11-13 01:13:37 | [diff] [blame] | 36 | DCHECK(!isStarted()); |
[email protected] | 14396e9 | 2010-05-06 20:40:56 | [diff] [blame] | 37 | CreateSharedWorkerContext(script_url, name, user_agent, source_code, |
| 38 | pending_route_id_, script_resource_appcache_id); |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 9c00f00 | 2009-11-05 22:37:42 | [diff] [blame] | 41 | void WebSharedWorkerProxy::terminateWorkerContext() { |
| 42 | // This API should only be invoked from worker context. |
| 43 | NOTREACHED(); |
| 44 | } |
| 45 | |
| 46 | void WebSharedWorkerProxy::clientDestroyed() { |
| 47 | // This API should only be invoked from worker context. |
| 48 | NOTREACHED(); |
| 49 | } |
| 50 | |
[email protected] | 70269042 | 2009-11-09 04:23:26 | [diff] [blame] | 51 | void WebSharedWorkerProxy::connect(WebKit::WebMessagePortChannel* channel, |
| 52 | ConnectListener* listener) { |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 53 | WebMessagePortChannelImpl* webchannel = |
| 54 | static_cast<WebMessagePortChannelImpl*>(channel); |
| 55 | |
| 56 | int message_port_id = webchannel->message_port_id(); |
| 57 | DCHECK(message_port_id != MSG_ROUTING_NONE); |
| 58 | webchannel->QueueMessages(); |
| 59 | |
| 60 | Send(new WorkerMsg_Connect(route_id_, message_port_id, MSG_ROUTING_NONE)); |
[email protected] | 70269042 | 2009-11-09 04:23:26 | [diff] [blame] | 61 | if (HasQueuedMessages()) { |
[email protected] | 30447b6 | 2009-11-13 01:13:37 | [diff] [blame] | 62 | connect_listener_ = listener; |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 63 | } else { |
[email protected] | 70269042 | 2009-11-09 04:23:26 | [diff] [blame] | 64 | listener->connected(); |
| 65 | // The listener may free this object, so do not access the object after |
| 66 | // this point. |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 67 | } |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 70 | bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { |
| 71 | bool handled = true; |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 72 | IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) |
| 73 | IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 74 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 75 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 76 | return handled; |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void WebSharedWorkerProxy::OnWorkerCreated() { |
| 80 | // The worker is created - now send off the CreateWorkerContext message and |
| 81 | // any other queued messages |
| 82 | SendQueuedMessages(); |
[email protected] | 70269042 | 2009-11-09 04:23:26 | [diff] [blame] | 83 | |
| 84 | // Inform any listener that the pending connect event has been sent |
| 85 | // (this can result in this object being freed). |
[email protected] | 30447b6 | 2009-11-13 01:13:37 | [diff] [blame] | 86 | if (connect_listener_) { |
| 87 | connect_listener_->connected(); |
[email protected] | 70269042 | 2009-11-09 04:23:26 | [diff] [blame] | 88 | } |
[email protected] | 6898bbe | 2009-11-05 16:27:06 | [diff] [blame] | 89 | } |