ApplicationManager: Use callback to get notified on application shutdown.

Until now, the Delegate had a OnApplicationError that was called
whenever an application ended. Because it was used to keep track of when
a given started application ended, it needed to send back the URL that
was used to start an application. This was cumbersome as mapping and
rediect change the url during the loading process.

Instead this change allows a caller fo ApplicationManager to register a
closure that will be called when the application ends, if the call
started a new application.

R=davemoore@chromium.org

Review URL: https://codereview.chromium.org/983113002
diff --git a/shell/shell_test_base.cc b/shell/shell_test_base.cc
index 20e6b35..6546d75 100644
--- a/shell/shell_test_base.cc
+++ b/shell/shell_test_base.cc
@@ -4,6 +4,7 @@
 
 #include "shell/shell_test_base.h"
 
+#include "base/bind.h"
 #include "base/command_line.h"
 #include "base/files/file_path.h"
 #include "base/files/file_util.h"
@@ -17,6 +18,17 @@
 namespace shell {
 namespace test {
 
+namespace {
+
+void QuitIfRunning() {
+  if (base::MessageLoop::current() &&
+      base::MessageLoop::current()->is_running()) {
+    base::MessageLoop::current()->QuitWhenIdle();
+  }
+}
+
+}  // namespace
+
 ShellTestBase::ShellTestBase() {
 }
 
@@ -35,8 +47,13 @@
 ScopedMessagePipeHandle ShellTestBase::ConnectToService(
     const GURL& application_url,
     const std::string& service_name) {
-  return shell_context_.ConnectToServiceByName(application_url, service_name)
-      .Pass();
+  ServiceProviderPtr services;
+  shell_context_.application_manager()->ConnectToApplication(
+      application_url, GURL(), GetProxy(&services), nullptr,
+      base::Bind(&QuitIfRunning));
+  MessagePipe pipe;
+  services->ConnectToService(service_name, pipe.handle1.Pass());
+  return pipe.handle0.Pass();
 }
 
 #if !defined(OS_ANDROID)