Add RunMainApplication()/TerminateMainApplication() for use in MojoMain().

Move run_application.h to //mojo/public/cpp/application:application.

To do (separately): Provide an implementation of run_application.h in
the "chromium" implementation. (Now that there are "main" versions of
these, we can.) This will allow more code to be converted to use
ApplicationImplBase, etc. (instead of ApplicationDelegate and
ApplicationRunner[Chromium]).

R=vardhan@google.com

Review URL: https://codereview.chromium.org/2008543004 .
diff --git a/examples/spinning_cube/spinning_cube_app.cc b/examples/spinning_cube/spinning_cube_app.cc
index eac07b2..a1fc4a7 100644
--- a/examples/spinning_cube/spinning_cube_app.cc
+++ b/examples/spinning_cube/spinning_cube_app.cc
@@ -95,5 +95,5 @@
 
 MojoResult MojoMain(MojoHandle application_request) {
   examples::SpinningCubeApp spinning_cube_app;
-  return mojo::RunApplication(application_request, &spinning_cube_app);
+  return mojo::RunMainApplication(application_request, &spinning_cube_app);
 }
diff --git a/mojo/public/cpp/application/BUILD.gn b/mojo/public/cpp/application/BUILD.gn
index d46f34a..dccdcd8 100644
--- a/mojo/public/cpp/application/BUILD.gn
+++ b/mojo/public/cpp/application/BUILD.gn
@@ -17,6 +17,7 @@
     "lib/application_impl_base.cc",
     "lib/connect.cc",
     "lib/service_provider_impl.cc",
+    "run_application.h",
     "service_connector.h",
     "service_provider_impl.h",
   ]
@@ -35,7 +36,6 @@
     "application_runner.h",
     "lib/application_runner.cc",
     "lib/run_application.cc",
-    "run_application.h",
   ]
 
   public_deps = [
diff --git a/mojo/public/cpp/application/lib/run_application.cc b/mojo/public/cpp/application/lib/run_application.cc
index 03373ef..4ca4a1a 100644
--- a/mojo/public/cpp/application/lib/run_application.cc
+++ b/mojo/public/cpp/application/lib/run_application.cc
@@ -58,6 +58,11 @@
 
 }  // namespace
 
+MojoResult RunMainApplication(MojoHandle application_request_handle,
+                              ApplicationImplBase* application_impl) {
+  return RunApplication(application_request_handle, application_impl);
+}
+
 MojoResult RunApplication(MojoHandle application_request_handle,
                           ApplicationImplBase* application_impl) {
   assert(!GetCurrentResultHolder());
@@ -82,6 +87,10 @@
   return result_holder.result;
 }
 
+void TerminateMainApplication(MojoResult result) {
+  TerminateApplication(result);
+}
+
 void TerminateApplication(MojoResult result) {
   RunLoop::current()->Quit();
 
diff --git a/mojo/public/cpp/application/run_application.h b/mojo/public/cpp/application/run_application.h
index 5bfbbd5..3f323d9 100644
--- a/mojo/public/cpp/application/run_application.h
+++ b/mojo/public/cpp/application/run_application.h
@@ -12,38 +12,40 @@
 
 class ApplicationImplBase;
 
-// Sets up a message (run) loop and runs the provided application
-// implementation. The return value will be the one provided to
-// |TerminateApplication()|.
+// |RunMainApplication()| and |RunApplication()| set up a message (run) loop and
+// run the provided application implementation. |RunMainApplication()| should be
+// used on the application's main thread (e.g., from |MojoMain()|) and may do
+// additional initialization. (In the case of the "standalone" implementation,
+// the two are equivalent.) The return value will be the one provided to
+// |TerminateMainApplication()| or |TerminateApplication()|, respectively.
 //
 // Typical use (where |MyApplicationImpl| is an implementation of
 // |ApplicationImplBase|):
 //
 //   MojoResult MojoMain(MojoHandle application_request_handle) {
 //     MyApplicationImpl my_app;
-//     return mojo::RunApplication(application_request_handle, &my_app);
+//     return mojo::RunMainApplication(application_request_handle, &my_app);
 //   }
 //
-// Note that this may actually be used on any thread (that is not already
+// |RunApplication()| may be used on secondary threads (that are 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): 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 "RunMainApplication()" and
-// "TerminateMainApplication()" functions.
+MojoResult RunMainApplication(MojoHandle application_request_handle,
+                              ApplicationImplBase* application_impl);
 MojoResult 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"
+// |TerminateMainApplication()| and |TerminateApplication()| terminate the
+// application that is running on the current thread. They may only be called
+// from "inside" |RunMainApplication()| and |RunApplication()| respectively
+// (i.e., |Run[Main]Application()| is on the stack, which means that the message
+// loop is running). They will cause |Run[Main]Application()| to return "soon"
 // (assuming the message loop is not blocked processing some task), with return
-// value |result|. This may cause queued work to *not* be executed. This should
-// be executed at most once (per |RunApplication()|).
+// value |result|. They may cause queued work to *not* be executed. They should
+// be executed at most once (per |Run[Main]Application()|).
+void TerminateMainApplication(MojoResult result);
 void TerminateApplication(MojoResult result);
 
 }  // namespace mojo
diff --git a/mojo/public/cpp/bindings/tests/versioning_test_service.cc b/mojo/public/cpp/bindings/tests/versioning_test_service.cc
index 7c59787..b7298fe 100644
--- a/mojo/public/cpp/bindings/tests/versioning_test_service.cc
+++ b/mojo/public/cpp/bindings/tests/versioning_test_service.cc
@@ -121,5 +121,5 @@
 
 MojoResult MojoMain(MojoHandle application_request) {
   mojo::test::versioning::HumanResourceSystemServer hr_system_server;
-  return mojo::RunApplication(application_request, &hr_system_server);
+  return mojo::RunMainApplication(application_request, &hr_system_server);
 }
diff --git a/mojo/services/log/cpp/log_client.h b/mojo/services/log/cpp/log_client.h
index 4c10c5c..2f99013 100644
--- a/mojo/services/log/cpp/log_client.h
+++ b/mojo/services/log/cpp/log_client.h
@@ -25,7 +25,7 @@
 //
 //  MojoResult MojoMain(MojoHandle app_request) {
 //    MyApp app;
-//    return mojo::RunApplication(&app);
+//    return mojo::RunMainApplication(&app);
 //  }
 
 #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 9a0f612..856142f 100644
--- a/services/test_service/test_request_tracker_application.cc
+++ b/services/test_service/test_request_tracker_application.cc
@@ -43,5 +43,5 @@
 
 MojoResult MojoMain(MojoHandle application_request) {
   mojo::test::TestRequestTrackerApplication app;
-  return mojo::RunApplication(application_request, &app);
+  return mojo::RunMainApplication(application_request, &app);
 }
diff --git a/services/test_service/test_service_application.cc b/services/test_service/test_service_application.cc
index af116a3..5e623b6 100644
--- a/services/test_service/test_service_application.cc
+++ b/services/test_service/test_service_application.cc
@@ -52,5 +52,5 @@
 
 MojoResult MojoMain(MojoHandle application_request) {
   mojo::test::TestServiceApplication app;
-  return mojo::RunApplication(application_request, &app);
+  return mojo::RunMainApplication(application_request, &app);
 }