Add a mojo::RunApplication() for running implementations of ApplicationImplBase.

Also add a mojo::TerminateApplication() that works with it. And convert
some low-hanging fruit.

The API probably isn't final yet:
* As indicated by the TODO, RunApplication() should probably return a
  MojoResult.
* Conversely, TerminateApplication() should probably take a MojoResult.
* I should probably have an additional "RunMainApplication()"
  (MainRunApplication()?) for the main thread/MojoMain() case.
* Then I can separate out the APIs from the implementation, and the
  "chromium" versions can implement the same API.
* (The main/non-main cases are the same in the "standalone" case, but in
  the "chromium" version, the main case has to do more stuff.)

R=vardhan@google.com

Review URL: https://codereview.chromium.org/2004493002 .
diff --git a/examples/echo/echo_benchmark.cc b/examples/echo/echo_benchmark.cc
index 2a48404..e8da193 100644
--- a/examples/echo/echo_benchmark.cc
+++ b/examples/echo/echo_benchmark.cc
@@ -13,7 +13,6 @@
 #include "mojo/public/c/system/main.h"
 #include "mojo/public/cpp/application/application_delegate.h"
 #include "mojo/public/cpp/application/application_impl.h"
-#include "mojo/public/cpp/application/application_runner.h"
 #include "mojo/public/cpp/application/connect.h"
 #include "mojo/public/cpp/utility/run_loop.h"
 
diff --git a/examples/spinning_cube/spinning_cube_app.cc b/examples/spinning_cube/spinning_cube_app.cc
index 694c66a..adcef63 100644
--- a/examples/spinning_cube/spinning_cube_app.cc
+++ b/examples/spinning_cube/spinning_cube_app.cc
@@ -10,10 +10,9 @@
 #include "base/bind.h"
 #include "examples/spinning_cube/gles2_client_impl.h"
 #include "mojo/public/c/system/main.h"
-#include "mojo/public/cpp/application/application_delegate.h"
-#include "mojo/public/cpp/application/application_impl.h"
-#include "mojo/public/cpp/application/application_runner.h"
+#include "mojo/public/cpp/application/application_impl_base.h"
 #include "mojo/public/cpp/application/connect.h"
+#include "mojo/public/cpp/application/run_application.h"
 #include "mojo/public/cpp/application/service_provider_impl.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "mojo/public/cpp/utility/run_loop.h"
@@ -22,7 +21,7 @@
 
 namespace examples {
 
-class SpinningCubeApp : public mojo::ApplicationDelegate,
+class SpinningCubeApp : public mojo::ApplicationImplBase,
                         public mojo::NativeViewportEventDispatcher {
  public:
   SpinningCubeApp() : dispatcher_binding_(this) {}
@@ -32,8 +31,9 @@
     mojo::ignore_result(gles2_client_.release());
   }
 
-  void Initialize(mojo::ApplicationImpl* app) override {
-    mojo::ConnectToService(app->shell(), "mojo:native_viewport_service",
+  // |ApplicationImplBase| overrides:
+  void OnInitialize() override {
+    mojo::ConnectToService(shell(), "mojo:native_viewport_service",
                            GetProxy(&viewport_));
     viewport_.set_connection_error_handler(
         [this]() { OnViewportConnectionError(); });
@@ -94,7 +94,7 @@
 }  // namespace examples
 
 MojoResult MojoMain(MojoHandle application_request) {
-  mojo::ApplicationRunner runner(std::unique_ptr<examples::SpinningCubeApp>(
-      new examples::SpinningCubeApp()));
-  return runner.Run(application_request);
+  examples::SpinningCubeApp spinning_cube_app;
+  mojo::RunApplication(application_request, &spinning_cube_app);
+  return MOJO_RESULT_OK;
 }
diff --git a/examples/trace_me/trace_me_app.cc b/examples/trace_me/trace_me_app.cc
index b33ccc5..b26b004 100644
--- a/examples/trace_me/trace_me_app.cc
+++ b/examples/trace_me/trace_me_app.cc
@@ -13,7 +13,6 @@
 #include "mojo/public/c/system/main.h"
 #include "mojo/public/cpp/application/application_delegate.h"
 #include "mojo/public/cpp/application/application_impl.h"
-#include "mojo/public/cpp/application/application_runner.h"
 #include "mojo/public/cpp/application/service_provider_impl.h"
 
 // This is an example app that uses implementation of tracing from mojo/common
diff --git a/mojo/public/cpp/application/BUILD.gn b/mojo/public/cpp/application/BUILD.gn
index ec0deae..d46f34a 100644
--- a/mojo/public/cpp/application/BUILD.gn
+++ b/mojo/public/cpp/application/BUILD.gn
@@ -32,7 +32,10 @@
 # GYP version: mojo/public/mojo_public.gyp:mojo_application_standalone
 mojo_sdk_source_set("standalone") {
   sources = [
+    "application_runner.h",
     "lib/application_runner.cc",
+    "lib/run_application.cc",
+    "run_application.h",
   ]
 
   public_deps = [
@@ -41,7 +44,9 @@
 
   mojo_sdk_deps = [
     "mojo/public/cpp/environment:standalone",
+    "mojo/public/cpp/system",
     "mojo/public/cpp/utility",
+    "mojo/public/interfaces/application",
   ]
 }
 
diff --git a/mojo/public/cpp/application/application_impl_base.h b/mojo/public/cpp/application/application_impl_base.h
index 8d3cbf6..1553e5d 100644
--- a/mojo/public/cpp/application/application_impl_base.h
+++ b/mojo/public/cpp/application/application_impl_base.h
@@ -26,12 +26,16 @@
 // To use this class, subclass it and implement/override the required methods
 // (see below).
 //
+// Note that by default |ApplicationImplBase|s are not "strongly bound" to their
+// |Application| requests (so, e.g., they can thus be constructed on the stack).
+// A subclass may make itself strongly bound by setting a suitable connection
+// error handler on the binding (available via |application_binding()|).
+//
 // TODO(vtl): ApplicationRunners should take this instead of an
 // ApplicationDelegate. Write more here when that's true (it's pretty hard to
 // use this class in the current setup).
 class ApplicationImplBase : public Application {
  public:
-  ApplicationImplBase();
   ~ApplicationImplBase() override;
 
   // Binds the given |Application| request to this object. This must be done
@@ -48,30 +52,41 @@
   // until this object is destroyed.
   Shell* shell() const { return shell_.get(); }
 
+  // Returns this object's |Application| binding (set up by |Bind()|; of course,
+  // you can always manipulate it directly).
+  const Binding<Application>& application_binding() const {
+    return application_binding_;
+  }
+  Binding<Application>& application_binding() { return application_binding_; }
+
   // Returns any initial configuration arguments, passed by the shell.
   const std::vector<std::string>& args() const { return args_; }
   bool HasArg(const std::string& arg) const;
 
   const std::string& url() const { return url_; }
 
-  // Methods to be implemented/overridden by subclasses:
+  // Methods to be overridden (if desired) by subclasses:
 
   // Called after |Initialize()| has been received (|shell()|, |args()|, and
   // |url()| will be valid when this is called. The default implementation does
   // nothing.
-  virtual void OnInitialize() {}
+  virtual void OnInitialize();
 
   // Called when another application connects to this application (i.e., we
   // receive |AcceptConnection()|). This should either configure what services
   // are "provided" (made available via a |ServiceProvider|) to that application
   // and return true, or this may return false to reject the connection
-  // entirely.
-  virtual bool OnAcceptConnection(
-      ServiceProviderImpl* service_provider_impl) = 0;
+  // entirely. The default implementation rejects all connections (by just
+  // returning false).
+  virtual bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl);
 
   // Called before quitting the main message (run) loop, i.e., before
   // |Terminate()|. The default implementation does nothing.
-  virtual void OnQuit() {}
+  virtual void OnQuit();
+
+ protected:
+  // This class is meant to be subclassed.
+  ApplicationImplBase();
 
  private:
   // |Application| implementation. In general, you probably shouldn't call these
diff --git a/mojo/public/cpp/application/lib/application_impl_base.cc b/mojo/public/cpp/application/lib/application_impl_base.cc
index 544c262..085e8c8 100644
--- a/mojo/public/cpp/application/lib/application_impl_base.cc
+++ b/mojo/public/cpp/application/lib/application_impl_base.cc
@@ -13,8 +13,6 @@
 
 namespace mojo {
 
-ApplicationImplBase::ApplicationImplBase() : application_binding_(this) {}
-
 ApplicationImplBase::~ApplicationImplBase() {}
 
 void ApplicationImplBase::Bind(
@@ -26,6 +24,17 @@
   return std::find(args_.begin(), args_.end(), arg) != args_.end();
 }
 
+void ApplicationImplBase::OnInitialize() {}
+
+bool ApplicationImplBase::OnAcceptConnection(
+    ServiceProviderImpl* service_provider_impl) {
+  return false;
+}
+
+void ApplicationImplBase::OnQuit() {}
+
+ApplicationImplBase::ApplicationImplBase() : application_binding_(this) {}
+
 void ApplicationImplBase::Initialize(InterfaceHandle<Shell> shell,
                                      Array<String> args,
                                      const mojo::String& url) {
diff --git a/mojo/public/cpp/application/lib/run_application.cc b/mojo/public/cpp/application/lib/run_application.cc
new file mode 100644
index 0000000..82d3089
--- /dev/null
+++ b/mojo/public/cpp/application/lib/run_application.cc
@@ -0,0 +1,32 @@
+// Copyright 2016 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 "mojo/public/cpp/application/run_application.h"
+
+#include "mojo/public/cpp/application/application_impl_base.h"
+#include "mojo/public/cpp/system/message_pipe.h"
+#include "mojo/public/cpp/utility/run_loop.h"
+#include "mojo/public/interfaces/application/application.mojom.h"
+
+namespace mojo {
+
+void RunApplication(MojoHandle application_request_handle,
+                    ApplicationImplBase* application_impl) {
+  // TODO(vtl): Possibly we should have an assertion that we're not running, but
+  // that requires TLS.
+
+  RunLoop loop;
+  application_impl->Bind(InterfaceRequest<Application>(
+      MakeScopedHandle(MessagePipeHandle(application_request_handle))));
+  loop.Run();
+
+  // TODO(vtl): Should we unbind stuff here? (Should there be "will start"/"did
+  // stop" notifications to the |ApplicationImplBase|?)
+}
+
+void TerminateApplication() {
+  RunLoop::current()->Quit();
+}
+
+}  // namespace mojo
diff --git a/mojo/public/cpp/application/run_application.h b/mojo/public/cpp/application/run_application.h
new file mode 100644
index 0000000..98f0ef1
--- /dev/null
+++ b/mojo/public/cpp/application/run_application.h
@@ -0,0 +1,53 @@
+// Copyright 2016 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.
+
+#ifndef MOJO_PUBLIC_CPP_APPLICATION_RUN_APPLICATION_H_
+#define MOJO_PUBLIC_CPP_APPLICATION_RUN_APPLICATION_H_
+
+#include "mojo/public/c/system/handle.h"
+#include "mojo/public/c/system/result.h"
+
+namespace mojo {
+
+class ApplicationImplBase;
+
+// Sets up a message (run) loop and runs the provided application
+// implementation.
+//
+// Typical use (where |MyApplicationImpl| is an implementation of
+// |ApplicationImplBase|):
+//
+//   MojoResult MojoMain(MojoHandle application_request_handle) {
+//     MyApplicationImpl my_app;
+//     mojo::RunApplication(application_request_handle, &my_app);
+//     return MOJO_RESULT_OK;
+//   }
+//
+// Note that this may actually be used on any thread (that is not already
+// running a message loop of some sort).
+//
+// |*application_impl| must remain alive until the message loop starts running
+// (thus it may own itself).
+//
+// TODO(vtl): Possibly this should return a |MojoResult| and
+// |TerminateApplication()| should take a |MojoResult| argument (which this
+// returns), but to communicate that value requires TLS.
+// TODO(vtl): Maybe this should be like |Environment|, with different possible
+// implementations (e.g., "standalone" vs "chromium"). However,
+// |ApplicationRunnerChromium| currently has additional goop specific to the
+// main thread. Probably we should have additional "MainRunApplication()" and
+// "MainTerminateApplication()" functions.
+void RunApplication(MojoHandle application_request_handle,
+                    ApplicationImplBase* application_impl);
+
+// Terminates the currently-running application. This may only be called from
+// "inside" |RunApplication()| (i.e., it is on the stack, which means that the
+// message loop is running). This will cause |RunApplication()| to return "soon"
+// (assuming the message loop is not blocked processing some task). This may
+// cause queued work to *not* be executed.
+void TerminateApplication();
+
+}  // namespace mojo
+
+#endif  // MOJO_PUBLIC_CPP_APPLICATION_RUN_APPLICATION_H_
diff --git a/mojo/public/cpp/bindings/tests/versioning_test_service.cc b/mojo/public/cpp/bindings/tests/versioning_test_service.cc
index cba93f9..3f7d503 100644
--- a/mojo/public/cpp/bindings/tests/versioning_test_service.cc
+++ b/mojo/public/cpp/bindings/tests/versioning_test_service.cc
@@ -6,8 +6,8 @@
 #include <memory>
 
 #include "mojo/public/c/system/main.h"
-#include "mojo/public/cpp/application/application_delegate.h"
-#include "mojo/public/cpp/application/application_runner.h"
+#include "mojo/public/cpp/application/application_impl_base.h"
+#include "mojo/public/cpp/application/run_application.h"
 #include "mojo/public/cpp/application/service_provider_impl.h"
 #include "mojo/public/cpp/bindings/strong_binding.h"
 #include "mojo/public/cpp/system/macros.h"
@@ -90,15 +90,17 @@
   std::map<uint64_t, EmployeeInfo*> employees_;
 
   StrongBinding<HumanResourceDatabase> strong_binding_;
+
+  MOJO_DISALLOW_COPY_AND_ASSIGN(HumanResourceDatabaseImpl);
 };
 
-class HumanResourceSystemServer : public ApplicationDelegate {
+class HumanResourceSystemServer : public ApplicationImplBase {
  public:
   HumanResourceSystemServer() {}
+  ~HumanResourceSystemServer() override {}
 
-  // ApplicationDelegate implementation.
-  bool ConfigureIncomingConnection(
-      ServiceProviderImpl* service_provider_impl) override {
+  // |ApplicationImplBase| overrides:
+  bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override {
     service_provider_impl->AddService<HumanResourceDatabase>(
         [](const ConnectionContext& connection_context,
            InterfaceRequest<HumanResourceDatabase> hr_db_request) {
@@ -108,6 +110,9 @@
         });
     return true;
   }
+
+ private:
+  MOJO_DISALLOW_COPY_AND_ASSIGN(HumanResourceSystemServer);
 };
 
 }  // namespace versioning
@@ -115,9 +120,7 @@
 }  // namespace mojo
 
 MojoResult MojoMain(MojoHandle application_request) {
-  mojo::ApplicationRunner runner(
-      std::unique_ptr<mojo::test::versioning::HumanResourceSystemServer>(
-          new mojo::test::versioning::HumanResourceSystemServer()));
-
-  return runner.Run(application_request);
+  mojo::test::versioning::HumanResourceSystemServer hr_system_server;
+  mojo::RunApplication(application_request, &hr_system_server);
+  return MOJO_RESULT_OK;
 }
diff --git a/mojo/services/log/cpp/log_client.h b/mojo/services/log/cpp/log_client.h
index 3639307..049ed16 100644
--- a/mojo/services/log/cpp/log_client.h
+++ b/mojo/services/log/cpp/log_client.h
@@ -8,24 +8,25 @@
 //
 // Example application that uses this log client to talk to the log service:
 //
-//  class MyDelegate : public mojo::ApplicationDelegate {
+//  class MyApp : public mojo::ApplicationImplBase {
 //   public:
-//    void Initialize(mojo::ApplicationImpl* app) override {
+//    void OnInitialize() override {
 //      LogPtr log;
-//      app->ConnectToService("mojo:log", &log);
+//      mojo::ConnectToService(shell(), "mojo:log", &log);
 //      mojo::log::InitializeLogger(std::move(log),
 //                                  mojo::Environment::GetDefaultLogger());
 //      mojo::Environment::SetDefaultLogger(mojo::log::GetLogger());
 //    }
 //
-//    void Quit() {
+//    void OnQuit() override {
 //      mojo::log::DestroyLogger();
 //    }
 //  };
 //
 //  MojoResult MojoMain(MojoHandle app_request) {
-//    mojo::ApplicationRunner runner(new MyDelegate);
-//    return runner.Run(app_request);
+//    MyApp app;
+//    mojo::RunApplication(&app);
+//    return MOJO_RESULT_OK;
 //  }
 
 #ifndef MOJO_SERVICES_LOG_CPP_LOG_CLIENT_H_
diff --git a/services/test_service/test_request_tracker_application.cc b/services/test_service/test_request_tracker_application.cc
index 1877708..2c41f76 100644
--- a/services/test_service/test_request_tracker_application.cc
+++ b/services/test_service/test_request_tracker_application.cc
@@ -4,35 +4,26 @@
 
 #include "services/test_service/test_request_tracker_application.h"
 
-#include <memory>
-
 #include "mojo/public/c/system/main.h"
-#include "mojo/public/cpp/application/application_runner.h"
+#include "mojo/public/cpp/application/run_application.h"
 #include "mojo/public/cpp/application/service_provider_impl.h"
 #include "services/test_service/test_time_service_impl.h"
 
 namespace mojo {
 namespace test {
 
-TestRequestTrackerApplication::TestRequestTrackerApplication()
-    : app_impl_(nullptr) {
-}
+TestRequestTrackerApplication::TestRequestTrackerApplication() {}
 
-TestRequestTrackerApplication::~TestRequestTrackerApplication() {
-}
+TestRequestTrackerApplication::~TestRequestTrackerApplication() {}
 
-void TestRequestTrackerApplication::Initialize(ApplicationImpl* app) {
-  app_impl_ = app;
-}
-
-bool TestRequestTrackerApplication::ConfigureIncomingConnection(
+bool TestRequestTrackerApplication::OnAcceptConnection(
     ServiceProviderImpl* service_provider_impl) {
   // Every instance of the service and recorder shares the context.
   // Note, this app is single-threaded, so this is thread safe.
   service_provider_impl->AddService<TestTimeService>(
       [this](const ConnectionContext& connection_context,
              InterfaceRequest<TestTimeService> request) {
-        new TestTimeServiceImpl(app_impl_, request.Pass());
+        new TestTimeServiceImpl(this, request.Pass());
       });
   service_provider_impl->AddService<TestRequestTracker>(
       [this](const ConnectionContext& connection_context,
@@ -51,8 +42,7 @@
 }  // namespace mojo
 
 MojoResult MojoMain(MojoHandle application_request) {
-  mojo::ApplicationRunner runner(
-      std::unique_ptr<mojo::test::TestRequestTrackerApplication>(
-          new mojo::test::TestRequestTrackerApplication()));
-  return runner.Run(application_request);
+  mojo::test::TestRequestTrackerApplication app;
+  mojo::RunApplication(application_request, &app);
+  return MOJO_RESULT_OK;
 }
diff --git a/services/test_service/test_request_tracker_application.h b/services/test_service/test_request_tracker_application.h
index 53a01bf..8ffa83e 100644
--- a/services/test_service/test_request_tracker_application.h
+++ b/services/test_service/test_request_tracker_application.h
@@ -5,29 +5,25 @@
 #ifndef SERVICES_TEST_SERVICE_TEST_REQUEST_TRACKER_APPLICATION_H_
 #define SERVICES_TEST_SERVICE_TEST_REQUEST_TRACKER_APPLICATION_H_
 
-#include "mojo/public/cpp/application/application_delegate.h"
+#include "mojo/public/cpp/application/application_impl_base.h"
 #include "mojo/public/cpp/system/macros.h"
 #include "services/test_service/test_request_tracker_impl.h"
 
 namespace mojo {
-class ApplicationImpl;
 namespace test {
+
 class TestTimeService;
 
 // Embeds TestRequestTracker mojo services into an application.
-class TestRequestTrackerApplication : public ApplicationDelegate {
+class TestRequestTrackerApplication : public ApplicationImplBase {
  public:
   TestRequestTrackerApplication();
   ~TestRequestTrackerApplication() override;
 
-  void Initialize(ApplicationImpl* app) override;
-
-  // ApplicationDelegate methods:
-  bool ConfigureIncomingConnection(
-      ServiceProviderImpl* service_provider_impl) override;
+  // |ApplicationImplBase| method:
+  bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override;
 
  private:
-  ApplicationImpl* app_impl_;
   TrackingContext context_;
   MOJO_DISALLOW_COPY_AND_ASSIGN(TestRequestTrackerApplication);
 };
diff --git a/services/test_service/test_service_application.cc b/services/test_service/test_service_application.cc
index d458d09..c883cef 100644
--- a/services/test_service/test_service_application.cc
+++ b/services/test_service/test_service_application.cc
@@ -6,39 +6,31 @@
 
 #include <assert.h>
 
-#include <memory>
-
 #include "mojo/public/c/system/main.h"
-#include "mojo/public/cpp/application/application_runner.h"
+#include "mojo/public/cpp/application/run_application.h"
 #include "mojo/public/cpp/application/service_provider_impl.h"
-#include "mojo/public/cpp/utility/run_loop.h"
 #include "services/test_service/test_service_impl.h"
 #include "services/test_service/test_time_service_impl.h"
 
 namespace mojo {
 namespace test {
 
-TestServiceApplication::TestServiceApplication()
-    : ref_count_(0), app_impl_(nullptr) {}
+TestServiceApplication::TestServiceApplication() : ref_count_(0) {}
 
 TestServiceApplication::~TestServiceApplication() {}
 
-void TestServiceApplication::Initialize(ApplicationImpl* app) {
-  app_impl_ = app;
-}
-
-bool TestServiceApplication::ConfigureIncomingConnection(
+bool TestServiceApplication::OnAcceptConnection(
     ServiceProviderImpl* service_provider_impl) {
   service_provider_impl->AddService<TestService>(
       [this](const ConnectionContext& connection_context,
              InterfaceRequest<TestService> request) {
-        new TestServiceImpl(app_impl_, this, request.Pass());
+        new TestServiceImpl(this, request.Pass());
         AddRef();
       });
   service_provider_impl->AddService<TestTimeService>(
       [this](const ConnectionContext& connection_context,
              InterfaceRequest<TestTimeService> request) {
-        new TestTimeServiceImpl(app_impl_, request.Pass());
+        new TestTimeServiceImpl(this, request.Pass());
       });
   return true;
 }
@@ -52,15 +44,14 @@
   assert(ref_count_ > 0);
   ref_count_--;
   if (ref_count_ <= 0)
-    RunLoop::current()->Quit();
+    TerminateApplication();
 }
 
 }  // namespace test
 }  // namespace mojo
 
 MojoResult MojoMain(MojoHandle application_request) {
-  mojo::ApplicationRunner runner(
-      std::unique_ptr<mojo::test::TestServiceApplication>(
-          new mojo::test::TestServiceApplication()));
-  return runner.Run(application_request);
+  mojo::test::TestServiceApplication app;
+  mojo::RunApplication(application_request, &app);
+  return MOJO_RESULT_OK;
 }
diff --git a/services/test_service/test_service_application.h b/services/test_service/test_service_application.h
index 9defb8a..27d1451 100644
--- a/services/test_service/test_service_application.h
+++ b/services/test_service/test_service_application.h
@@ -5,32 +5,25 @@
 #ifndef SERVICES_TEST_SERVICE_TEST_SERVICE_APPLICATION_H_
 #define SERVICES_TEST_SERVICE_TEST_SERVICE_APPLICATION_H_
 
-#include "mojo/public/cpp/application/application_delegate.h"
+#include "mojo/public/cpp/application/application_impl_base.h"
 #include "mojo/public/cpp/system/macros.h"
 
 namespace mojo {
 namespace test {
 
-class TestService;
-class TestTimeService;
-
-class TestServiceApplication : public ApplicationDelegate {
+class TestServiceApplication : public ApplicationImplBase {
  public:
   TestServiceApplication();
   ~TestServiceApplication() override;
 
-  void Initialize(ApplicationImpl* app) override;
-
-  // ApplicationDelegate implementation.
-  bool ConfigureIncomingConnection(
-      ServiceProviderImpl* service_provider_impl) override;
+  // |ApplicationImplBase| method:
+  bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override;
 
   void AddRef();
   void ReleaseRef();
 
  private:
   int ref_count_;
-  ApplicationImpl* app_impl_;
 
   MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceApplication);
 };
diff --git a/services/test_service/test_service_impl.cc b/services/test_service/test_service_impl.cc
index 27487c8..bbd6a27 100644
--- a/services/test_service/test_service_impl.cc
+++ b/services/test_service/test_service_impl.cc
@@ -7,7 +7,6 @@
 #include "base/bind.h"
 #include "base/i18n/time_formatting.h"
 #include "base/strings/utf_string_conversions.h"
-#include "mojo/public/cpp/application/application_impl.h"
 #include "mojo/public/cpp/application/connect.h"
 #include "services/test_service/test_service_application.h"
 #include "services/test_service/test_time_service_impl.h"
@@ -16,18 +15,14 @@
 namespace mojo {
 namespace test {
 
-TestServiceImpl::TestServiceImpl(ApplicationImpl* app_impl,
-                                 TestServiceApplication* application,
+TestServiceImpl::TestServiceImpl(TestServiceApplication* application,
                                  InterfaceRequest<TestService> request)
-    : application_(application),
-      app_impl_(app_impl),
-      binding_(this, request.Pass()) {
+    : application_(application), binding_(this, request.Pass()) {
   binding_.set_connection_error_handler(
       [this]() { application_->ReleaseRef(); });
 }
 
-TestServiceImpl::~TestServiceImpl() {
-}
+TestServiceImpl::~TestServiceImpl() {}
 
 void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) {
   if (tracking_)
@@ -44,7 +39,7 @@
 void TestServiceImpl::ConnectToAppAndGetTime(
     const mojo::String& app_url,
     const mojo::Callback<void(int64_t)>& callback) {
-  ConnectToService(app_impl_->shell(), app_url, GetProxy(&time_service_));
+  ConnectToService(application_->shell(), app_url, GetProxy(&time_service_));
   if (tracking_) {
     tracking_->RecordNewRequest();
     time_service_->StartTrackingRequests(mojo::Callback<void()>());
@@ -55,7 +50,7 @@
 void TestServiceImpl::StartTrackingRequests(
     const mojo::Callback<void()>& callback) {
   TestRequestTrackerPtr tracker;
-  ConnectToService(app_impl_->shell(), "mojo:test_request_tracker_app",
+  ConnectToService(application_->shell(), "mojo:test_request_tracker_app",
                    GetProxy(&tracker));
   tracking_.reset(new TrackedService(tracker.Pass(), Name_, callback));
 }
diff --git a/services/test_service/test_service_impl.h b/services/test_service/test_service_impl.h
index df5803c..92f0b91 100644
--- a/services/test_service/test_service_impl.h
+++ b/services/test_service/test_service_impl.h
@@ -11,7 +11,6 @@
 #include "services/test_service/test_service.mojom.h"
 
 namespace mojo {
-class ApplicationImpl;
 namespace test {
 
 class TrackedService;
@@ -19,8 +18,7 @@
 
 class TestServiceImpl : public TestService {
  public:
-  TestServiceImpl(ApplicationImpl* app_impl,
-                  TestServiceApplication* application,
+  TestServiceImpl(TestServiceApplication* application,
                   InterfaceRequest<TestService> request);
   ~TestServiceImpl() override;
 
@@ -33,7 +31,6 @@
 
  private:
   TestServiceApplication* const application_;
-  ApplicationImpl* const app_impl_;
   TestTimeServicePtr time_service_;
   scoped_ptr<TrackedService> tracking_;
   StrongBinding<TestService> binding_;
diff --git a/services/test_service/test_time_service_impl.cc b/services/test_service/test_time_service_impl.cc
index 0f61f06..3bb0bde 100644
--- a/services/test_service/test_time_service_impl.cc
+++ b/services/test_service/test_time_service_impl.cc
@@ -3,7 +3,7 @@
 // found in the LICENSE file.
 
 #include "base/time/time.h"
-#include "mojo/public/cpp/application/application_impl.h"
+#include "mojo/public/cpp/application/application_impl_base.h"
 #include "mojo/public/cpp/application/connect.h"
 #include "services/test_service/test_request_tracker.mojom.h"
 #include "services/test_service/test_time_service_impl.h"
@@ -13,18 +13,16 @@
 namespace test {
 
 TestTimeServiceImpl::TestTimeServiceImpl(
-    ApplicationImpl* app_impl,
+    ApplicationImplBase* application,
     InterfaceRequest<TestTimeService> request)
-    : app_impl_(app_impl), binding_(this, request.Pass()) {
-}
+    : application_(application), binding_(this, request.Pass()) {}
 
-TestTimeServiceImpl::~TestTimeServiceImpl() {
-}
+TestTimeServiceImpl::~TestTimeServiceImpl() {}
 
 void TestTimeServiceImpl::StartTrackingRequests(
     const mojo::Callback<void()>& callback) {
   TestRequestTrackerPtr tracker;
-  ConnectToService(app_impl_->shell(), "mojo:test_request_tracker_app",
+  ConnectToService(application_->shell(), "mojo:test_request_tracker_app",
                    GetProxy(&tracker));
   tracking_.reset(new TrackedService(tracker.Pass(), Name_, callback));
 }
diff --git a/services/test_service/test_time_service_impl.h b/services/test_service/test_time_service_impl.h
index 5ca569b..27a028f 100644
--- a/services/test_service/test_time_service_impl.h
+++ b/services/test_service/test_time_service_impl.h
@@ -12,13 +12,16 @@
 #include "services/test_service/test_service.mojom.h"
 
 namespace mojo {
+
+class ApplicationImplBase;
+
 namespace test {
 
 class TrackedService;
 
 class TestTimeServiceImpl : public TestTimeService {
  public:
-  TestTimeServiceImpl(ApplicationImpl* app_impl,
+  TestTimeServiceImpl(ApplicationImplBase* application,
                       InterfaceRequest<TestTimeService> request);
   ~TestTimeServiceImpl() override;
 
@@ -28,7 +31,7 @@
   void StartTrackingRequests(const mojo::Callback<void()>& callback) override;
 
  private:
-  ApplicationImpl* app_impl_;
+  ApplicationImplBase* const application_;
   scoped_ptr<TrackedService> tracking_;
   StrongBinding<TestTimeService> binding_;
   MOJO_DISALLOW_COPY_AND_ASSIGN(TestTimeServiceImpl);