| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "chrome/worker/websharedworker_stub.h" | |
| 6 | |
| 7 #include "content/common/child_thread.h" | |
| 8 #include "content/common/file_system/file_system_dispatcher.h" | |
| 9 #include "content/common/webmessageportchannel_impl.h" | |
| 10 #include "content/common/worker_messages.h" | |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSharedWorker.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | |
| 14 | |
| 15 WebSharedWorkerStub::WebSharedWorkerStub( | |
| 16 const string16& name, int route_id, | |
| 17 const WorkerAppCacheInitInfo& appcache_init_info) | |
| 18 : WebWorkerStubBase(route_id, appcache_init_info), | |
| 19 name_(name), | |
| 20 started_(false) { | |
| 21 // TODO(atwilson): Add support for NaCl when they support MessagePorts. | |
| 22 impl_ = WebKit::WebSharedWorker::create(client()); | |
| 23 } | |
| 24 | |
| 25 WebSharedWorkerStub::~WebSharedWorkerStub() { | |
| 26 impl_->clientDestroyed(); | |
| 27 } | |
| 28 | |
| 29 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { | |
| 30 bool handled = true; | |
| 31 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) | |
| 32 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) | |
| 33 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, | |
| 34 OnTerminateWorkerContext) | |
| 35 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) | |
| 36 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 37 IPC_END_MESSAGE_MAP() | |
| 38 return handled; | |
| 39 } | |
| 40 | |
| 41 void WebSharedWorkerStub::OnChannelError() { | |
| 42 OnTerminateWorkerContext(); | |
| 43 } | |
| 44 | |
| 45 const GURL& WebSharedWorkerStub::url() const { | |
| 46 return url_; | |
| 47 } | |
| 48 | |
| 49 void WebSharedWorkerStub::OnStartWorkerContext( | |
| 50 const GURL& url, const string16& user_agent, const string16& source_code) { | |
| 51 // Ignore multiple attempts to start this worker (can happen if two pages | |
| 52 // try to start it simultaneously). | |
| 53 if (started_) | |
| 54 return; | |
| 55 | |
| 56 impl_->startWorkerContext(url, name_, user_agent, source_code, 0); | |
| 57 started_ = true; | |
| 58 url_ = url; | |
| 59 | |
| 60 // Process any pending connections. | |
| 61 for (PendingConnectInfoList::const_iterator iter = pending_connects_.begin(); | |
| 62 iter != pending_connects_.end(); | |
| 63 ++iter) { | |
| 64 OnConnect(iter->first, iter->second); | |
| 65 } | |
| 66 pending_connects_.clear(); | |
| 67 } | |
| 68 | |
| 69 void WebSharedWorkerStub::OnConnect(int sent_message_port_id, int routing_id) { | |
| 70 if (started_) { | |
| 71 WebKit::WebMessagePortChannel* channel = | |
| 72 new WebMessagePortChannelImpl(routing_id, sent_message_port_id); | |
| 73 impl_->connect(channel, NULL); | |
| 74 } else { | |
| 75 // If two documents try to load a SharedWorker at the same time, the | |
| 76 // WorkerMsg_Connect for one of the documents can come in before the | |
| 77 // worker is started. Just queue up the connect and deliver it once the | |
| 78 // worker starts. | |
| 79 PendingConnectInfo pending_connect(sent_message_port_id, routing_id); | |
| 80 pending_connects_.push_back(pending_connect); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void WebSharedWorkerStub::OnTerminateWorkerContext() { | |
| 85 impl_->terminateWorkerContext(); | |
| 86 | |
| 87 // Call the client to make sure context exits. | |
| 88 EnsureWorkerContextTerminates(); | |
| 89 started_ = false; | |
| 90 } | |
| OLD | NEW |