Chromium Code Reviews| Index: headless/lib/browser/headless_tab_socket_impl.cc |
| diff --git a/headless/lib/browser/headless_tab_socket_impl.cc b/headless/lib/browser/headless_tab_socket_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5dcee91deaee41339b6d55b0709781f74c1a010b |
| --- /dev/null |
| +++ b/headless/lib/browser/headless_tab_socket_impl.cc |
| @@ -0,0 +1,62 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "headless/lib/browser/headless_tab_socket_impl.h" |
| + |
| +namespace headless { |
| + |
| +HeadlessTabSocketImpl::HeadlessTabSocketImpl() : listener_(nullptr) {} |
| + |
| +HeadlessTabSocketImpl::~HeadlessTabSocketImpl() {} |
| + |
| +void HeadlessTabSocketImpl::SendMessageToTab(const std::string& message) { |
| + base::AutoLock lock(lock_); |
| + if (waiting_for_message_cb_.is_null() || !out_going_message_queue_.empty()) { |
| + out_going_message_queue_.push_back(message); |
| + } else { |
| + waiting_for_message_cb_.Run(message); |
|
Sami
2017/04/12 14:18:45
Ditto about locking.
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + waiting_for_message_cb_ = AwaitNextMessageFromEmbedderCallback(); |
| + DCHECK(waiting_for_message_cb_.is_null()); |
| + } |
| +} |
| + |
| +void HeadlessTabSocketImpl::SetListener(Listener* listener) { |
| + base::AutoLock lock(lock_); |
| + listener_ = listener; |
| + if (!listener) |
| + return; |
| + |
| + for (const std::string& message : incomming_message_queue_) { |
| + listener_->OnMessageFromTab(message); |
|
Sami
2017/04/12 14:18:45
Could we do this outside the lock? Otherwise you c
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
Sami
2017/04/12 16:09:06
Looks like we're still holding the lock here?
alex clarke (OOO till 29th)
2017/04/12 19:41:48
Oops done.
|
| + } |
| + incomming_message_queue_.clear(); |
| +} |
| + |
| +void HeadlessTabSocketImpl::SendMessageToEmbedder(const std::string& message) { |
| + base::AutoLock lock(lock_); |
| + if (listener_) { |
| + listener_->OnMessageFromTab(message); |
| + } else { |
| + incomming_message_queue_.push_back(message); |
| + } |
| +} |
| + |
| +void HeadlessTabSocketImpl::AwaitNextMessageFromEmbedder( |
| + const AwaitNextMessageFromEmbedderCallback& callback) { |
| + base::AutoLock lock(lock_); |
| + if (out_going_message_queue_.empty()) { |
| + waiting_for_message_cb_ = callback; |
| + DCHECK(!waiting_for_message_cb_.is_null()); |
| + } else { |
| + callback.Run(out_going_message_queue_.front()); |
|
Sami
2017/04/12 14:18:45
Ditto about locking.
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + out_going_message_queue_.pop_front(); |
| + } |
| +} |
| + |
| +void HeadlessTabSocketImpl::CreateMojoService( |
| + mojo::InterfaceRequest<TabSocket> request) { |
| + mojo_bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +} // namespace headless |