| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/renderer/websharedworker_proxy.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "content/child/webmessageportchannel_impl.h" | |
| 10 #include "content/common/view_messages.h" | |
| 11 #include "content/common/worker_messages.h" | |
| 12 #include "ipc/message_router.h" | |
| 13 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 14 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, | |
| 19 int route_id) | |
| 20 : route_id_(route_id), | |
| 21 router_(router), | |
| 22 connect_listener_(nullptr), | |
| 23 created_(false) { | |
| 24 DCHECK_NE(MSG_ROUTING_NONE, route_id_); | |
| 25 router_->AddRoute(route_id_, this); | |
| 26 } | |
| 27 | |
| 28 WebSharedWorkerProxy::~WebSharedWorkerProxy() { | |
| 29 router_->RemoveRoute(route_id_); | |
| 30 } | |
| 31 | |
| 32 bool WebSharedWorkerProxy::Send(std::unique_ptr<IPC::Message> message) { | |
| 33 // The worker object can be interacted with before the browser process told us | |
| 34 // that it started, in which case we want to queue the message. | |
| 35 if (!created_) { | |
| 36 queued_messages_.push_back(std::move(message)); | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 // For now we proxy all messages to the worker process through the browser. | |
| 41 // Revisit if we find this slow. | |
| 42 // TODO(jabdelmalek): handle sync messages if we need them. | |
| 43 return router_->Send(message.release()); | |
| 44 } | |
| 45 | |
| 46 void WebSharedWorkerProxy::SendQueuedMessages() { | |
| 47 DCHECK(created_); | |
| 48 DCHECK(queued_messages_.size()); | |
| 49 std::vector<std::unique_ptr<IPC::Message>> queued_messages; | |
| 50 queued_messages.swap(queued_messages_); | |
| 51 for (size_t i = 0; i < queued_messages.size(); ++i) { | |
| 52 queued_messages[i]->set_routing_id(route_id_); | |
| 53 Send(std::move(queued_messages[i])); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, | |
| 58 ConnectListener* listener) { | |
| 59 WebMessagePortChannelImpl* webchannel = | |
| 60 static_cast<WebMessagePortChannelImpl*>(channel); | |
| 61 | |
| 62 int message_port_id = webchannel->message_port_id(); | |
| 63 DCHECK_NE(MSG_ROUTING_NONE, message_port_id); | |
| 64 webchannel->QueueMessages(); | |
| 65 | |
| 66 Send(base::MakeUnique<ViewHostMsg_ConnectToWorker>(route_id_, | |
| 67 message_port_id)); | |
| 68 connect_listener_ = listener; | |
| 69 } | |
| 70 | |
| 71 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { | |
| 72 bool handled = true; | |
| 73 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) | |
| 74 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) | |
| 75 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, | |
| 76 OnWorkerScriptLoadFailed) | |
| 77 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, | |
| 78 OnWorkerConnected) | |
| 79 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 80 IPC_END_MESSAGE_MAP() | |
| 81 return handled; | |
| 82 } | |
| 83 | |
| 84 void WebSharedWorkerProxy::OnWorkerCreated() { | |
| 85 created_ = true; | |
| 86 // The worker is created - now send off the WorkerMsg_Connect message. | |
| 87 SendQueuedMessages(); | |
| 88 } | |
| 89 | |
| 90 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { | |
| 91 if (connect_listener_) { | |
| 92 // This can result in this object being freed. | |
| 93 connect_listener_->scriptLoadFailed(); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void WebSharedWorkerProxy::OnWorkerConnected() { | |
| 98 if (connect_listener_) { | |
| 99 // This can result in this object being freed. | |
| 100 connect_listener_->connected(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 } // namespace content | |
| OLD | NEW |