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

Side by Side Diff: third_party/WebKit/Source/modules/websockets/WorkerWebSocketChannel.cpp

Issue 2270693002: WebSocket: Consolidate inter-thread synchronous function calls (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comment Created 4 years, 3 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
« no previous file with comments | « third_party/WebKit/Source/modules/websockets/WorkerWebSocketChannel.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 } 102 }
103 103
104 std::unique_ptr<WaitableEvent> m_event; 104 std::unique_ptr<WaitableEvent> m_event;
105 bool m_connectRequestResult; 105 bool m_connectRequestResult;
106 }; 106 };
107 107
108 WorkerWebSocketChannel::WorkerWebSocketChannel(WorkerGlobalScope& workerGlobalSc ope, WebSocketChannelClient* client, std::unique_ptr<SourceLocation> location) 108 WorkerWebSocketChannel::WorkerWebSocketChannel(WorkerGlobalScope& workerGlobalSc ope, WebSocketChannelClient* client, std::unique_ptr<SourceLocation> location)
109 : m_bridge(new Bridge(client, workerGlobalScope)) 109 : m_bridge(new Bridge(client, workerGlobalScope))
110 , m_locationAtConnection(std::move(location)) 110 , m_locationAtConnection(std::move(location))
111 { 111 {
112 m_bridge->initialize(m_locationAtConnection->clone());
113 } 112 }
114 113
115 WorkerWebSocketChannel::~WorkerWebSocketChannel() 114 WorkerWebSocketChannel::~WorkerWebSocketChannel()
116 { 115 {
117 ASSERT(!m_bridge); 116 ASSERT(!m_bridge);
118 } 117 }
119 118
120 bool WorkerWebSocketChannel::connect(const KURL& url, const String& protocol) 119 bool WorkerWebSocketChannel::connect(const KURL& url, const String& protocol)
121 { 120 {
122 ASSERT(m_bridge); 121 ASSERT(m_bridge);
123 return m_bridge->connect(url, protocol); 122 return m_bridge->connect(m_locationAtConnection->clone(), url, protocol);
124 } 123 }
125 124
126 void WorkerWebSocketChannel::send(const CString& message) 125 void WorkerWebSocketChannel::send(const CString& message)
127 { 126 {
128 ASSERT(m_bridge); 127 ASSERT(m_bridge);
129 m_bridge->send(message); 128 m_bridge->send(message);
130 } 129 }
131 130
132 void WorkerWebSocketChannel::send(const DOMArrayBuffer& binaryData, unsigned byt eOffset, unsigned byteLength) 131 void WorkerWebSocketChannel::send(const DOMArrayBuffer& binaryData, unsigned byt eOffset, unsigned byteLength)
133 { 132 {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 bool Peer::initialize(std::unique_ptr<SourceLocation> location, ExecutionContext * context) 196 bool Peer::initialize(std::unique_ptr<SourceLocation> location, ExecutionContext * context)
198 { 197 {
199 ASSERT(isMainThread()); 198 ASSERT(isMainThread());
200 if (wasContextDestroyedBeforeObserverCreation()) 199 if (wasContextDestroyedBeforeObserverCreation())
201 return false; 200 return false;
202 Document* document = toDocument(context); 201 Document* document = toDocument(context);
203 m_mainWebSocketChannel = DocumentWebSocketChannel::create(document, this, st d::move(location)); 202 m_mainWebSocketChannel = DocumentWebSocketChannel::create(document, this, st d::move(location));
204 return true; 203 return true;
205 } 204 }
206 205
207 void Peer::connect(const KURL& url, const String& protocol) 206 bool Peer::connect(const KURL& url, const String& protocol)
208 { 207 {
209 ASSERT(isMainThread()); 208 ASSERT(isMainThread());
210 ASSERT(m_syncHelper); 209 if (!m_mainWebSocketChannel)
211 if (!m_mainWebSocketChannel) { 210 return false;
212 m_syncHelper->setConnectRequestResult(false); 211 return m_mainWebSocketChannel->connect(url, protocol);
213 } else {
214 bool connectRequestResult = m_mainWebSocketChannel->connect(url, protoco l);
215 m_syncHelper->setConnectRequestResult(connectRequestResult);
216 }
217 m_syncHelper->signalWorkerThread();
218 } 212 }
219 213
220 void Peer::sendTextAsCharVector(std::unique_ptr<Vector<char>> data) 214 void Peer::sendTextAsCharVector(std::unique_ptr<Vector<char>> data)
221 { 215 {
222 ASSERT(isMainThread()); 216 ASSERT(isMainThread());
223 if (m_mainWebSocketChannel) 217 if (m_mainWebSocketChannel)
224 m_mainWebSocketChannel->sendTextAsCharVector(std::move(data)); 218 m_mainWebSocketChannel->sendTextAsCharVector(std::move(data));
225 } 219 }
226 220
227 void Peer::sendBinaryAsCharVector(std::unique_ptr<Vector<char>> data) 221 void Peer::sendBinaryAsCharVector(std::unique_ptr<Vector<char>> data)
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy()) 380 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy())
387 , m_syncHelper(WebSocketChannelSyncHelper::create(wrapUnique(new WaitableEve nt()))) 381 , m_syncHelper(WebSocketChannelSyncHelper::create(wrapUnique(new WaitableEve nt())))
388 { 382 {
389 } 383 }
390 384
391 Bridge::~Bridge() 385 Bridge::~Bridge()
392 { 386 {
393 ASSERT(!m_peer); 387 ASSERT(!m_peer);
394 } 388 }
395 389
396 void Bridge::createPeerOnMainThread(std::unique_ptr<SourceLocation> location, Wo rkerThreadLifecycleContext* workerThreadLifecycleContext, ExecutionContext* cont ext) 390 void Bridge::connectOnMainThread(std::unique_ptr<SourceLocation> location, Worke rThreadLifecycleContext* workerThreadLifecycleContext, const KURL& url, const St ring& protocol, ExecutionContext* context)
397 { 391 {
398 DCHECK(isMainThread()); 392 DCHECK(isMainThread());
399 DCHECK(!m_peer); 393 DCHECK(!m_peer);
400 Peer* peer = new Peer(this, m_loaderProxy, m_syncHelper, workerThreadLifecyc leContext); 394 Peer* peer = new Peer(this, m_loaderProxy, m_syncHelper, workerThreadLifecyc leContext);
401 if (peer->initialize(std::move(location), context)) 395 if (peer->initialize(std::move(location), context)) {
402 m_peer = peer; 396 m_peer = peer;
397 bool result = m_peer->connect(url, protocol);
398 m_syncHelper->setConnectRequestResult(result);
399 }
403 m_syncHelper->signalWorkerThread(); 400 m_syncHelper->signalWorkerThread();
404 } 401 }
405 402
406 void Bridge::initialize(std::unique_ptr<SourceLocation> location) 403 bool Bridge::connect(std::unique_ptr<SourceLocation> location, const KURL& url, const String& protocol)
407 { 404 {
408 // Wait for completion of the task on the main thread because the connection
409 // must synchronously be established (see Bridge::connect).
410 if (!waitForMethodCompletion(BLINK_FROM_HERE, createCrossThreadTask(&Bridge: :createPeerOnMainThread, wrapCrossThreadPersistent(this), passed(location->clone ()), wrapCrossThreadPersistent(m_workerGlobalScope->thread()->getWorkerThreadLif ecycleContext())))) {
411 // The worker thread has been signalled to shutdown before method comple tion.
412 disconnect();
413 }
414 }
415
416 bool Bridge::connect(const KURL& url, const String& protocol)
417 {
418 if (!m_peer)
419 return false;
420
421 // Wait for completion of the task on the main thread because the mixed 405 // Wait for completion of the task on the main thread because the mixed
422 // content check must synchronously be conducted. 406 // content check must synchronously be conducted.
423 if (!waitForMethodCompletion(BLINK_FROM_HERE, createCrossThreadTask(&Peer::c onnect, wrapCrossThreadPersistent(m_peer.get()), url, protocol))) 407 if (!waitForMethodCompletion(BLINK_FROM_HERE, createCrossThreadTask(&Bridge: :connectOnMainThread, wrapCrossThreadPersistent(this), passed(location->clone()) , wrapCrossThreadPersistent(m_workerGlobalScope->thread()->getWorkerThreadLifecy cleContext()), url, protocol)))
424 return false; 408 return false;
425 409
426 return m_syncHelper->connectRequestResult(); 410 return m_syncHelper->connectRequestResult();
427 } 411 }
428 412
429 void Bridge::send(const CString& message) 413 void Bridge::send(const CString& message)
430 { 414 {
431 ASSERT(m_peer); 415 ASSERT(m_peer);
432 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(message.len gth())); 416 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(message.len gth()));
433 if (message.length()) 417 if (message.length())
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 } 483 }
500 484
501 DEFINE_TRACE(Bridge) 485 DEFINE_TRACE(Bridge)
502 { 486 {
503 visitor->trace(m_client); 487 visitor->trace(m_client);
504 visitor->trace(m_workerGlobalScope); 488 visitor->trace(m_workerGlobalScope);
505 visitor->trace(m_syncHelper); 489 visitor->trace(m_syncHelper);
506 } 490 }
507 491
508 } // namespace blink 492 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/websockets/WorkerWebSocketChannel.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698