Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: content/renderer/shared_worker/websharedworker_proxy.cc

Issue 2627123003: SharedWorker: Simplify connection sequence (Closed)
Patch Set: maybe fix win compile failures Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/shared_worker/websharedworker_proxy.h" 5 #include "content/renderer/shared_worker/websharedworker_proxy.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "content/child/child_thread_impl.h"
9 #include "content/child/webmessageportchannel_impl.h" 10 #include "content/child/webmessageportchannel_impl.h"
10 #include "content/common/view_messages.h" 11 #include "content/common/view_messages.h"
11 #include "content/common/worker_messages.h" 12 #include "content/common/worker_messages.h"
12 #include "ipc/message_router.h" 13 #include "ipc/message_router.h"
13 14
14 namespace content { 15 namespace content {
15 16
16 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, 17 WebSharedWorkerProxy::WebSharedWorkerProxy(
17 int route_id) 18 std::unique_ptr<blink::WebSharedWorkerConnectListener> listener)
18 : route_id_(route_id), 19 : route_id_(MSG_ROUTING_NONE),
19 router_(router), 20 router_(ChildThreadImpl::current()->GetRouter()),
20 message_port_id_(MSG_ROUTING_NONE), 21 message_port_id_(MSG_ROUTING_NONE),
21 connect_listener_(nullptr) { 22 listener_(std::move(listener)) {}
22 DCHECK_NE(MSG_ROUTING_NONE, route_id_);
23 router_->AddRoute(route_id_, this);
24 }
25 23
26 WebSharedWorkerProxy::~WebSharedWorkerProxy() { 24 WebSharedWorkerProxy::~WebSharedWorkerProxy() {
25 DCHECK_NE(MSG_ROUTING_NONE, route_id_);
27 router_->RemoveRoute(route_id_); 26 router_->RemoveRoute(route_id_);
28 } 27 }
29 28
30 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, 29 void WebSharedWorkerProxy::connect(ViewHostMsg_CreateWorker_Params params,
31 ConnectListener* listener) { 30 blink::WebMessagePortChannel* channel) {
31 // Send synchronous IPC to get |route_id|.
32 // TODO(nhiroki): Stop using synchronous IPC (https://crbug.com/679654).
33 ViewHostMsg_CreateWorker_Reply reply;
34 router_->Send(new ViewHostMsg_CreateWorker(params, &reply));
35 route_id_ = reply.route_id;
36 router_->AddRoute(route_id_, this);
37 listener_->workerCreated(reply.error);
38
32 DCHECK_EQ(MSG_ROUTING_NONE, message_port_id_); 39 DCHECK_EQ(MSG_ROUTING_NONE, message_port_id_);
33 WebMessagePortChannelImpl* webchannel = 40 WebMessagePortChannelImpl* webchannel =
34 static_cast<WebMessagePortChannelImpl*>(channel); 41 static_cast<WebMessagePortChannelImpl*>(channel);
35 message_port_id_ = webchannel->message_port_id(); 42 message_port_id_ = webchannel->message_port_id();
36 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); 43 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_);
37 webchannel->QueueMessages(); 44 webchannel->QueueMessages();
38 connect_listener_ = listener; 45 // |webchannel| is intentionally leaked here: it'll be removed at
46 // WebMessagePortChannelImpl::OnMessagesQueued().
47
39 // An actual connection request will be issued on OnWorkerCreated(). 48 // An actual connection request will be issued on OnWorkerCreated().
40 } 49 }
41 50
42 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { 51 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) {
43 bool handled = true; 52 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) 53 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message)
45 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) 54 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated)
46 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, 55 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed,
47 OnWorkerScriptLoadFailed) 56 OnWorkerScriptLoadFailed)
48 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, 57 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected,
49 OnWorkerConnected) 58 OnWorkerConnected)
50 IPC_MESSAGE_UNHANDLED(handled = false) 59 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP() 60 IPC_END_MESSAGE_MAP()
52 return handled; 61 return handled;
53 } 62 }
54 63
55 void WebSharedWorkerProxy::OnWorkerCreated() { 64 void WebSharedWorkerProxy::OnWorkerCreated() {
56 // connect() should be called before.
57 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_);
58
59 // The worker is created - now send off the connection request. 65 // The worker is created - now send off the connection request.
60 router_->Send( 66 router_->Send(
61 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_)); 67 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_));
62 } 68 }
63 69
64 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { 70 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() {
65 if (connect_listener_) { 71 listener_->scriptLoadFailed();
66 // This can result in this object being freed. 72 delete this;
67 connect_listener_->scriptLoadFailed();
68 }
69 } 73 }
70 74
71 void WebSharedWorkerProxy::OnWorkerConnected() { 75 void WebSharedWorkerProxy::OnWorkerConnected() {
72 if (connect_listener_) { 76 listener_->connected();
73 // This can result in this object being freed. 77 delete this;
74 connect_listener_->connected();
75 }
76 } 78 }
77 79
78 } // namespace content 80 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698