More SynchronousInterfacePtr conversion + some related pexe content handler cleanup.

R=smklein@chromium.org, vardhan@google.com

Review URL: https://codereview.chromium.org/1932713003 .
diff --git a/mojo/nacl/nonsfi/irt_pnacl_translator_compile.cc b/mojo/nacl/nonsfi/irt_pnacl_translator_compile.cc
index 77a7ef8..37d9426 100644
--- a/mojo/nacl/nonsfi/irt_pnacl_translator_compile.cc
+++ b/mojo/nacl/nonsfi/irt_pnacl_translator_compile.cc
@@ -21,9 +21,10 @@
 // with the nexe compiler service.
 class PexeCompilerImpl : public mojo::nacl::PexeCompiler {
  public:
-  PexeCompilerImpl(mojo::ScopedMessagePipeHandle handle,
-                   const struct nacl_irt_pnacl_compile_funcs* funcs)
-      : funcs_(funcs), strong_binding_(this, handle.Pass()) {}
+  PexeCompilerImpl(
+      mojo::InterfaceRequest<mojo::nacl::PexeCompiler> compiler_request,
+      const struct nacl_irt_pnacl_compile_funcs* funcs)
+      : funcs_(funcs), strong_binding_(this, compiler_request.Pass()) {}
   void PexeCompile(const mojo::String& pexe_file_name, const
                    mojo::Callback<void(mojo::Array<mojo::String>)>& callback)
       override {
@@ -117,7 +118,8 @@
   // Convert the MojoHandle into a ScopedMessagePipeHandle, and use that to
   // implement the PexeCompiler interface.
   PexeCompilerImpl impl(
-      mojo::ScopedMessagePipeHandle(mojo::MessagePipeHandle(handle)).Pass(),
+      mojo::MakeRequest<mojo::nacl::PexeCompiler>(
+          mojo::ScopedMessagePipeHandle(mojo::MessagePipeHandle(handle))),
       funcs);
   mojo::RunLoop::current()->Run();
 }
diff --git a/services/http_server/BUILD.gn b/services/http_server/BUILD.gn
index 3ac29a9..4b2c724 100644
--- a/services/http_server/BUILD.gn
+++ b/services/http_server/BUILD.gn
@@ -60,8 +60,11 @@
     "//mojo/public/cpp/system",
     "//mojo/services/http_server/cpp",
     "//mojo/services/http_server/interfaces",
+    "//mojo/services/http_server/interfaces:interfaces_sync",
     "//mojo/services/network/interfaces",
   ]
 
-  data_deps = [ ":http_server($default_toolchain)" ]
+  data_deps = [
+    ":http_server($default_toolchain)",
+  ]
 }
diff --git a/services/http_server/http_server_apptest.cc b/services/http_server/http_server_apptest.cc
index 280a867..80c3795 100644
--- a/services/http_server/http_server_apptest.cc
+++ b/services/http_server/http_server_apptest.cc
@@ -9,9 +9,10 @@
 #include "mojo/public/cpp/application/application_impl.h"
 #include "mojo/public/cpp/application/application_test_base.h"
 #include "mojo/public/cpp/application/connect.h"
+#include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "mojo/services/http_server/cpp/http_server_util.h"
-#include "mojo/services/http_server/interfaces/http_server.mojom.h"
+#include "mojo/services/http_server/interfaces/http_server.mojom-sync.h"
 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h"
 #include "mojo/services/network/interfaces/net_address.mojom.h"
 #include "mojo/services/network/interfaces/network_service.mojom.h"
@@ -20,6 +21,7 @@
 namespace http_server {
 
 namespace {
+
 void WriteMessageToDataPipe(
     const std::string message,
     mojo::ScopedDataPipeConsumerHandle* data_pipe_consumer) {
@@ -40,7 +42,8 @@
   ASSERT_EQ(message.size(), bytes);
 }
 
-const char* kExampleMessage = "Hello, world!";
+const char kExampleMessage[] = "Hello, world!";
+
 }  // namespace
 
 // Test handler that responds to all requests with the status OK and
@@ -104,7 +107,7 @@
                            GetProxy(&network_service_));
   }
 
-  http_server::HttpServerPtr CreateHttpServer();
+  mojo::SynchronousInterfacePtr<http_server::HttpServer> CreateHttpServer();
 
   http_server::HttpServerFactoryPtr http_server_factory_;
   mojo::NetworkServicePtr network_service_;
@@ -113,8 +116,9 @@
   MOJO_DISALLOW_COPY_AND_ASSIGN(HttpServerApplicationTest);
 };
 
-http_server::HttpServerPtr HttpServerApplicationTest::CreateHttpServer() {
-  http_server::HttpServerPtr http_server;
+mojo::SynchronousInterfacePtr<http_server::HttpServer>
+HttpServerApplicationTest::CreateHttpServer() {
+  mojo::SynchronousInterfacePtr<http_server::HttpServer> http_server;
   mojo::NetAddressPtr local_address(mojo::NetAddress::New());
   local_address->family = mojo::NetAddressFamily::IPV4;
   local_address->ipv4 = mojo::NetAddressIPv4::New();
@@ -124,7 +128,7 @@
   local_address->ipv4->addr[2] = 0;
   local_address->ipv4->addr[3] = 1;
   local_address->ipv4->port = 0;
-  http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(),
+  http_server_factory_->CreateHttpServer(GetSynchronousProxy(&http_server),
                                          local_address.Pass());
   return http_server;
 }
@@ -140,18 +144,19 @@
 // Verifies that the server responds to http GET requests using example
 // GetHandler.
 TEST_F(HttpServerApplicationTest, ServerResponse) {
-  http_server::HttpServerPtr http_server(CreateHttpServer());
-  uint16_t assigned_port;
-  http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; });
-  http_server.WaitForIncomingResponse();
+  auto http_server = CreateHttpServer();
+  uint16_t assigned_port = 0;
+  EXPECT_TRUE(http_server->GetPort(&assigned_port));
+  EXPECT_NE(assigned_port, 0u);
 
   HttpHandlerPtr http_handler_ptr;
   GetHandler handler(GetProxy(&http_handler_ptr).Pass());
 
   // Set the test handler and wait for confirmation.
-  http_server->SetHandler("/test", http_handler_ptr.Pass(),
-                          [](bool result) { EXPECT_TRUE(result); });
-  http_server.WaitForIncomingResponse();
+  bool result = false;
+  EXPECT_TRUE(
+      http_server->SetHandler("/test", http_handler_ptr.Pass(), &result));
+  EXPECT_TRUE(result);
 
   mojo::URLLoaderPtr url_loader;
   network_service_->CreateURLLoader(GetProxy(&url_loader));
@@ -167,18 +172,19 @@
 // Verifies that the server correctly passes the POST request payload using
 // example PostHandler.
 TEST_F(HttpServerApplicationTest, PostData) {
-  http_server::HttpServerPtr http_server(CreateHttpServer());
-  uint16_t assigned_port;
-  http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; });
-  http_server.WaitForIncomingResponse();
+  auto http_server = CreateHttpServer();
+  uint16_t assigned_port = 0;
+  EXPECT_TRUE(http_server->GetPort(&assigned_port));
+  EXPECT_NE(assigned_port, 0u);
 
   HttpHandlerPtr http_handler_ptr;
   PostHandler handler(GetProxy(&http_handler_ptr).Pass());
 
   // Set the test handler and wait for confirmation.
-  http_server->SetHandler("/post", http_handler_ptr.Pass(),
-                          [](bool result) { EXPECT_TRUE(result); });
-  http_server.WaitForIncomingResponse();
+  bool result = false;
+  EXPECT_TRUE(
+      http_server->SetHandler("/post", http_handler_ptr.Pass(), &result));
+  EXPECT_TRUE(result);
 
   mojo::URLLoaderPtr url_loader;
   network_service_->CreateURLLoader(GetProxy(&url_loader));
diff --git a/services/nacl/nonsfi/BUILD.gn b/services/nacl/nonsfi/BUILD.gn
index 791e5aa..6f9ac1f 100644
--- a/services/nacl/nonsfi/BUILD.gn
+++ b/services/nacl/nonsfi/BUILD.gn
@@ -35,7 +35,7 @@
       ]
 
       deps = [
-        ":pnacl_translator_bindings",
+        ":pnacl_translator_bindings_sync",
         "//base",
         "//mojo/application:application",
         "//mojo/application:content_handler",
diff --git a/services/nacl/nonsfi/content_handler_main_pexe.cc b/services/nacl/nonsfi/content_handler_main_pexe.cc
index d590b89..7fcc2a1 100644
--- a/services/nacl/nonsfi/content_handler_main_pexe.cc
+++ b/services/nacl/nonsfi/content_handler_main_pexe.cc
@@ -21,57 +21,11 @@
 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
 #include "mojo/services/files/interfaces/directory.mojom-sync.h"
 #include "mojo/services/files/interfaces/files.mojom.h"
-#include "services/nacl/nonsfi/pnacl_compile.mojom.h"
-#include "services/nacl/nonsfi/pnacl_link.mojom.h"
+#include "services/nacl/nonsfi/pnacl_compile.mojom-sync.h"
+#include "services/nacl/nonsfi/pnacl_link.mojom-sync.h"
 
 namespace nacl {
 namespace content_handler {
-namespace {
-
-class CompilerUI {
- public:
-  explicit CompilerUI(mojo::ScopedMessagePipeHandle handle) {
-    compiler_.Bind(
-        mojo::InterfaceHandle<mojo::nacl::PexeCompiler>(handle.Pass(), 0u));
-  }
-
-  // Synchronous method to compile pexe into object file.
-  mojo::Array<mojo::String> CompilePexe(mojo::String pexe_file_path) {
-    mojo::Array<mojo::String> output;
-    compiler_->PexeCompile(
-        pexe_file_path,
-        [&output](mojo::Array<mojo::String> o) { output = o.Pass(); });
-    CHECK(compiler_.WaitForIncomingResponse())
-        << "Waiting for pexe compiler failed";
-    return output;
-  }
-
- private:
-  mojo::nacl::PexeCompilerPtr compiler_;
-};
-
-class LinkerUI {
- public:
-  explicit LinkerUI(mojo::ScopedMessagePipeHandle handle) {
-    linker_.Bind(
-        mojo::InterfaceHandle<mojo::nacl::PexeLinker>(handle.Pass(), 0u));
-  }
-
-  // Synchronous method to link object file into nexe.
-  mojo::String LinkPexe(mojo::Array<mojo::String> object_file_paths) {
-    mojo::String output;
-    linker_->PexeLink(std::move(object_file_paths),
-                      [&output](mojo::String o) { output = o; });
-    CHECK(linker_.WaitForIncomingResponse())
-        << "Waiting for pexe linker failed";
-    return output;
-  }
-
- private:
-  mojo::nacl::PexeLinkerPtr linker_;
-};
-
-}  // namespace anonymous
 
 class PexeContentHandler : public mojo::ApplicationDelegate,
                            public mojo::ContentHandlerFactory::Delegate {
@@ -82,9 +36,9 @@
   // Overridden from ApplicationDelegate:
   void Initialize(mojo::ApplicationImpl* app) override {
     mojo::ConnectToService(app->shell(), "mojo:pnacl_compile",
-                           GetProxy(&compiler_init_));
+                           GetSynchronousProxy(&compiler_init_));
     mojo::ConnectToService(app->shell(), "mojo:pnacl_link",
-                           GetProxy(&linker_init_));
+                           GetSynchronousProxy(&linker_init_));
     mojo::ConnectToService(app->shell(), "mojo:files", GetProxy(&files_));
     mojo::files::Error error = mojo::files::Error::INTERNAL;
     files_->OpenFileSystem("app_persistent_cache",
@@ -136,28 +90,20 @@
 
   int DoPexeTranslation(base::FilePath& pexe_file_path) {
     // Compile the pexe into an object file
-    mojo::ScopedMessagePipeHandle parent_compile_pipe;
-    mojo::ScopedMessagePipeHandle child_compile_pipe;
-    CHECK_EQ(CreateMessagePipe(nullptr, &parent_compile_pipe,
-                               &child_compile_pipe), MOJO_RESULT_OK)
-        << "Could not create message pipe to compiler";
-    compiler_init_->PexeCompilerStart(child_compile_pipe.Pass());
+    mojo::SynchronousInterfacePtr<mojo::nacl::PexeCompiler> compiler;
+    CHECK(compiler_init_->PexeCompilerStart(GetSynchronousProxy(&compiler)));
 
     // Communicate with the compiler using a mojom interface.
-    CompilerUI compiler_ui(parent_compile_pipe.Pass());
-    mojo::Array<mojo::String> object_files =
-        compiler_ui.CompilePexe(pexe_file_path.value());
+    mojo::Array<mojo::String> object_files;
+    CHECK(compiler->PexeCompile(pexe_file_path.value(), &object_files));
 
     // Link the object file into a nexe
-    mojo::ScopedMessagePipeHandle parent_link_pipe;
-    mojo::ScopedMessagePipeHandle child_link_pipe;
-    CHECK_EQ(CreateMessagePipe(nullptr, &parent_link_pipe, &child_link_pipe),
-             MOJO_RESULT_OK) << "Could not create message pipe to linker";
-    linker_init_->PexeLinkerStart(child_link_pipe.Pass());
+    mojo::SynchronousInterfacePtr<mojo::nacl::PexeLinker> linker;
+    CHECK(linker_init_->PexeLinkerStart(GetSynchronousProxy(&linker)));
 
     // Communicate with the linker using a mojom interface.
-    LinkerUI linker_ui(parent_link_pipe.Pass());
-    mojo::String nexe_file = linker_ui.LinkPexe(std::move(object_files));
+    mojo::String nexe_file;
+    CHECK(linker->PexeLink(std::move(object_files), &nexe_file));
 
     // Open the nexe file and launch it (with our mojo handle)
     int nexe_fd = open(nexe_file.get().c_str(), O_RDONLY);
@@ -205,8 +151,8 @@
   mojo::SynchronousInterfacePtr<mojo::files::Directory> nexe_cache_directory_;
   mojo::files::FilesPtr files_;
   mojo::ContentHandlerFactory content_handler_factory_;
-  mojo::nacl::PexeCompilerInitPtr compiler_init_;
-  mojo::nacl::PexeLinkerInitPtr linker_init_;
+  mojo::SynchronousInterfacePtr<mojo::nacl::PexeCompilerInit> compiler_init_;
+  mojo::SynchronousInterfacePtr<mojo::nacl::PexeLinkerInit> linker_init_;
 
   DISALLOW_COPY_AND_ASSIGN(PexeContentHandler);
 };
diff --git a/services/nacl/nonsfi/pnacl_compile.cc b/services/nacl/nonsfi/pnacl_compile.cc
index 969925e..38d5ef4 100644
--- a/services/nacl/nonsfi/pnacl_compile.cc
+++ b/services/nacl/nonsfi/pnacl_compile.cc
@@ -21,13 +21,14 @@
 
 class PexeCompilerImpl : public PexeCompilerInit {
  public:
-  void PexeCompilerStart(ScopedMessagePipeHandle handle) override {
+  void PexeCompilerStart(
+      InterfaceRequest<PexeCompiler> compiler_request) override {
     int nexe_fd =
         ::nacl::DataToTempFileDescriptor(::nacl::kPnaclTranslatorCompile);
     CHECK(nexe_fd >= 0) << "Could not open compiler nexe";
-    ::nacl::MojoLaunchNexeNonsfi(nexe_fd,
-                                 handle.release().value(),
-                                 true /* enable_translate_irt */);
+    ::nacl::MojoLaunchNexeNonsfi(
+        nexe_fd, compiler_request.PassMessagePipe().release().value(),
+        true /* enable_translate_irt */);
   }
 };
 
diff --git a/services/nacl/nonsfi/pnacl_compile.mojom b/services/nacl/nonsfi/pnacl_compile.mojom
index 9e60e79..04e2750 100644
--- a/services/nacl/nonsfi/pnacl_compile.mojom
+++ b/services/nacl/nonsfi/pnacl_compile.mojom
@@ -6,7 +6,7 @@
 
 [ServiceName="mojo::nacl::PexeCompilerInit"]
 interface PexeCompilerInit {
-  PexeCompilerStart(handle<message_pipe> message_handle);
+  PexeCompilerStart(PexeCompiler& compiler_request);
 };
 
 interface PexeCompiler {
diff --git a/services/nacl/nonsfi/pnacl_link.cc b/services/nacl/nonsfi/pnacl_link.cc
index 509495c..1e5e493 100644
--- a/services/nacl/nonsfi/pnacl_link.cc
+++ b/services/nacl/nonsfi/pnacl_link.cc
@@ -21,12 +21,12 @@
 
 class PexeLinkerImpl : public PexeLinkerInit {
  public:
-  void PexeLinkerStart(ScopedMessagePipeHandle handle) override {
+  void PexeLinkerStart(InterfaceRequest<PexeLinker> linker_request) override {
     int nexe_fd = ::nacl::DataToTempFileDescriptor(::nacl::kLdNexe);
     CHECK(nexe_fd >= 0) << "Could not open linker nexe";
-    ::nacl::MojoLaunchNexeNonsfi(nexe_fd,
-                                 handle.release().value(),
-                                 true /* enable_translate_irt */);
+    ::nacl::MojoLaunchNexeNonsfi(
+        nexe_fd, linker_request.PassMessagePipe().release().value(),
+        true /* enable_translate_irt */);
   }
 };
 
diff --git a/services/nacl/nonsfi/pnacl_link.mojom b/services/nacl/nonsfi/pnacl_link.mojom
index ae892ea..30f29e1 100644
--- a/services/nacl/nonsfi/pnacl_link.mojom
+++ b/services/nacl/nonsfi/pnacl_link.mojom
@@ -6,7 +6,7 @@
 
 [ServiceName="mojo::nacl::PexeLinkerInit"]
 interface PexeLinkerInit {
-  PexeLinkerStart(handle<message_pipe> message_handle);
+  PexeLinkerStart(PexeLinker& linker_request);
 };
 
 interface PexeLinker {
diff --git a/shell/BUILD.gn b/shell/BUILD.gn
index 23b7dc6..028ab49 100644
--- a/shell/BUILD.gn
+++ b/shell/BUILD.gn
@@ -673,6 +673,7 @@
     "//mojo/public/interfaces/application",
     "//mojo/services/http_server/cpp",
     "//mojo/services/http_server/interfaces",
+    "//mojo/services/http_server/interfaces:interfaces_sync",
     "//mojo/services/network/interfaces",
     "//shell/test:bindings",
   ]
diff --git a/shell/shell_apptest.cc b/shell/shell_apptest.cc
index f4b8154..7f64883 100644
--- a/shell/shell_apptest.cc
+++ b/shell/shell_apptest.cc
@@ -15,10 +15,11 @@
 #include "mojo/public/cpp/application/application_impl.h"
 #include "mojo/public/cpp/application/application_test_base.h"
 #include "mojo/public/cpp/application/connect.h"
+#include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "mojo/public/interfaces/application/application_connector.mojom.h"
 #include "mojo/services/http_server/cpp/http_server_util.h"
-#include "mojo/services/http_server/interfaces/http_server.mojom.h"
+#include "mojo/services/http_server/interfaces/http_server.mojom-sync.h"
 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h"
 #include "mojo/services/network/interfaces/net_address.mojom.h"
 #include "shell/kPingable.h"
@@ -92,23 +93,22 @@
     local_address->ipv4->addr[2] = 0;
     local_address->ipv4->addr[3] = 1;
     local_address->ipv4->port = 0;
-    http_server_factory_->CreateHttpServer(GetProxy(&http_server_),
+    http_server_factory_->CreateHttpServer(GetSynchronousProxy(&http_server_),
                                            local_address.Pass());
 
-    http_server_->GetPort([this](uint16_t p) { port_ = p; });
-    EXPECT_TRUE(http_server_.WaitForIncomingResponse());
+    EXPECT_TRUE(http_server_->GetPort(&port_));
 
     http_server::HttpHandlerPtr http_handler;
     handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_));
-    http_server_->SetHandler(".*", http_handler.Pass(),
-                             [](bool result) { EXPECT_TRUE(result); });
-    EXPECT_TRUE(http_server_.WaitForIncomingResponse());
+    bool result = false;
+    EXPECT_TRUE(http_server_->SetHandler(".*", http_handler.Pass(), &result));
+    EXPECT_TRUE(result);
   }
 
   std::string GetURL(const std::string& path) { return ::GetURL(port_, path); }
 
   http_server::HttpServerFactoryPtr http_server_factory_;
-  http_server::HttpServerPtr http_server_;
+  mojo::SynchronousInterfacePtr<http_server::HttpServer> http_server_;
   scoped_ptr<GetHandler> handler_;
   uint16_t port_;