Skip to content

Commit

Permalink
[mojo-bindings] Use Watch API instead of MessagePumpMojo
Browse files Browse the repository at this point in the history
This removes the C++ bindings dependency on MessagePumpMojo,
consuming the new Watch API instead.

For convenience a new mojo::Watcher is added to the public
Mojo C++ API library, and this is used by Connector.

BUG=590495
R=yzshen@chromium.org
TBR=blundell@chromium.org for rename affecting components/message_port.gypi
TBR=jam@chromium.org - added a missing header to new url tests

Committed: https://crrev.com/d06373e7cd8b4ad725ed5c64c958f2de13585add
Cr-Commit-Position: refs/heads/master@{#379402}

Review URL: https://codereview.chromium.org/1759783003

Cr-Commit-Position: refs/heads/master@{#379463}
  • Loading branch information
krockot authored and Commit bot committed Mar 5, 2016
1 parent 30abe5a commit 2cdb2f6
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 43 deletions.
2 changes: 1 addition & 1 deletion components/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test("components_unittests") {
"//components/variations:unit_tests",
"//components/variations/service:unit_tests",
"//components/web_resource:unit_tests",
"//mojo/edk/embedder:headers",
"//mojo/edk/system",
"//net",
"//testing/gtest",
"//ui/base",
Expand Down
2 changes: 1 addition & 1 deletion components/message_port.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'dependencies': [
'../base/base.gyp:base',
'../mojo/mojo_public.gyp:mojo_message_pump_lib',
'../mojo/mojo_public.gyp:mojo_system_cpp_headers',
'../mojo/mojo_public.gyp:mojo_cpp_system',
'../third_party/WebKit/public/blink.gyp:blink',
],
'include_dirs': [
Expand Down
1 change: 1 addition & 0 deletions mojo/mojo_edk_tests.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'dependencies': [
'../testing/gtest.gyp:gtest',
'mojo_edk.gyp:mojo_run_all_unittests',
'mojo_public.gyp:mojo_cpp_system',
'mojo_public.gyp:mojo_public_test_utils',
],
'sources': [
Expand Down
9 changes: 6 additions & 3 deletions mojo/mojo_public.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
},
{
# GN version: //mojo/public/cpp/system
'target_name': 'mojo_system_cpp_headers',
'type': 'none',
'target_name': 'mojo_cpp_system',
'type': 'static_library',
'sources': [
'public/cpp/system/buffer.h',
'public/cpp/system/core.h',
Expand All @@ -84,8 +84,11 @@
'public/cpp/system/handle.h',
'public/cpp/system/macros.h',
'public/cpp/system/message_pipe.h',
'public/cpp/system/watcher.cc',
'public/cpp/system/watcher.h',
],
'dependencies': [
'../base/base.gyp:base',
'mojo_system_headers',
],
},
Expand Down Expand Up @@ -187,7 +190,7 @@
],
'dependencies': [
'../base/base.gyp:base',
'mojo_message_pump_lib',
'mojo_cpp_system',
'mojo_interface_bindings_cpp_sources',
],
},
Expand Down
1 change: 1 addition & 0 deletions mojo/mojo_variables.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'<(DEPTH)/mojo/public/c/system/tests/macros_unittest.cc',
'<(DEPTH)/mojo/public/cpp/system/tests/core_unittest.cc',
'<(DEPTH)/mojo/public/cpp/system/tests/macros_unittest.cc',
'<(DEPTH)/mojo/public/cpp/system/tests/watcher_unittest.cc',
],
},
}
1 change: 0 additions & 1 deletion mojo/public/cpp/bindings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ source_set("bindings") {

deps = [
"//base",
"//mojo/message_pump",
"//mojo/public/interfaces/bindings:bindings_cpp_sources",
]
}
Expand Down
43 changes: 18 additions & 25 deletions mojo/public/cpp/bindings/lib/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "base/thread_task_runner_handle.h"
#include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h"

namespace mojo {
Expand Down Expand Up @@ -247,7 +248,7 @@ bool Connector::RunSyncHandleWatch(const bool* should_stop) {
return SyncHandleWatcher::current()->WatchAllHandles(should_stop_array, 2);
}

void Connector::OnHandleWatcherHandleReady(MojoResult result) {
void Connector::OnWatcherHandleReady(MojoResult result) {
OnHandleReadyInternal(result);
}

Expand All @@ -273,12 +274,21 @@ void Connector::OnHandleReadyInternal(MojoResult result) {
}

void Connector::WaitToReadMore() {
CHECK(!handle_watcher_.is_watching());
CHECK(!paused_);
handle_watcher_.Start(message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE,
base::Bind(&Connector::OnHandleWatcherHandleReady,
base::Unretained(this)));
DCHECK(!handle_watcher_.IsWatching());

MojoResult rv = handle_watcher_.Start(
message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
base::Bind(&Connector::OnWatcherHandleReady,
base::Unretained(this)));

if (rv != MOJO_RESULT_OK) {
// If the watch failed because the handle is invalid or its conditions can
// no longer be met, we signal the error asynchronously to avoid reentry.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&Connector::OnWatcherHandleReady,
weak_factory_.GetWeakPtr(), rv));
}

if (register_sync_handle_watch_count_ > 0 &&
!registered_with_sync_handle_watcher_) {
Expand All @@ -304,13 +314,6 @@ bool Connector::ReadSingleMessage(MojoResult* read_result) {
*read_result = rv;

if (rv == MOJO_RESULT_OK) {
// Dispatching the message may spin in a nested message loop. To ensure we
// continue dispatching messages when this happens start listening for
// messagse now.
if (!handle_watcher_.is_watching()) {
// TODO: Need to evaluate the perf impact of this.
WaitToReadMore();
}
receiver_result =
incoming_receiver_ && incoming_receiver_->Accept(&message);
}
Expand Down Expand Up @@ -344,21 +347,13 @@ void Connector::ReadAllAvailableMessages() {
if (paused_)
return;

if (rv == MOJO_RESULT_SHOULD_WAIT) {
// ReadSingleMessage could end up calling HandleError which resets
// message_pipe_ to a dummy one that is closed. The old EDK will see the
// that the peer is closed immediately, while the new one is asynchronous
// because of thread hops. In that case, there'll still be an async
// waiter.
if (!handle_watcher_.is_watching())
WaitToReadMore();
if (rv == MOJO_RESULT_SHOULD_WAIT)
break;
}
}
}

void Connector::CancelWait() {
handle_watcher_.Stop();
handle_watcher_.Cancel();

if (registered_with_sync_handle_watcher_) {
SyncHandleWatcher::current()->UnregisterHandle(message_pipe_.get());
Expand Down Expand Up @@ -394,8 +389,6 @@ void Connector::HandleError(bool force_pipe_reset, bool force_async_handler) {
}

if (force_async_handler) {
// |dummy_pipe.handle1| has been destructed. Reading the pipe will
// eventually cause a read error on |message_pipe_| and set error state.
if (!paused_)
WaitToReadMore();
} else {
Expand Down
8 changes: 4 additions & 4 deletions mojo/public/cpp/bindings/lib/connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "mojo/message_pump/handle_watcher.h"
#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/system/watcher.h"

namespace base {
class Lock;
Expand Down Expand Up @@ -141,8 +141,8 @@ class Connector : public MessageReceiver {
}

private:
// Callback of mojo::common::HandleWatcher.
void OnHandleWatcherHandleReady(MojoResult result);
// Callback of mojo::Watcher.
void OnWatcherHandleReady(MojoResult result);
// Callback of SyncHandleWatcher.
void OnSyncHandleWatcherHandleReady(MojoResult result);
void OnHandleReadyInternal(MojoResult result);
Expand All @@ -169,7 +169,7 @@ class Connector : public MessageReceiver {
ScopedMessagePipeHandle message_pipe_;
MessageReceiver* incoming_receiver_;

common::HandleWatcher handle_watcher_;
Watcher handle_watcher_;

bool error_;
bool drop_writes_;
Expand Down
2 changes: 2 additions & 0 deletions mojo/public/cpp/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ source_set("system") {
"handle.h",
"macros.h",
"message_pipe.h",
"watcher.cc",
"watcher.h",
]

public_deps = [
Expand Down
2 changes: 2 additions & 0 deletions mojo/public/cpp/system/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ source_set("tests") {
sources = [
"core_unittest.cc",
"macros_unittest.cc",
"watcher_unittest.cc",
]

deps = [
"//base",
"//mojo/public/c/system/tests",
"//mojo/public/cpp/environment:standalone",
"//mojo/public/cpp/system",
Expand Down
Loading

0 comments on commit 2cdb2f6

Please sign in to comment.