Change AuthenticatingURLLoader to be a URLLoaderInterceptor

This CL changes AuthenticatingURLLoader to be a URLLoaderInterceptor rather
than a one-off interface whose implementation wrapped URLLoaders. Renaming of
various classes/files to more appropriate names will happen in a follow-up CL
to minimize churn.

It also removes the background loader used to load the
authenticating_url_loader app; it is no longer needed as the NetworkService's
URLLoaders are now being used directly again and can fetch the
authenticating_url_loader app.

R=qsr@chromium.org

Review URL: https://codereview.chromium.org/1155283003
diff --git a/shell/BUILD.gn b/shell/BUILD.gn
index 3a9b8fc..273b186 100644
--- a/shell/BUILD.gn
+++ b/shell/BUILD.gn
@@ -122,8 +122,6 @@
 
 source_set("parent_lib") {
   sources = [
-    "authenticating_url_loader_loader.cc",
-    "authenticating_url_loader_loader.h",
     "background_application_loader.cc",
     "background_application_loader.h",
     "child_process_host.cc",
@@ -163,7 +161,6 @@
     "//mojo/public/interfaces/application",
     "//mojo/services/network/public/interfaces",
     "//shell/application_manager",
-    "//services/authenticating_url_loader",
     "//services/url_response_disk_cache",
     "//services/tracing:bindings",
     "//url",
diff --git a/shell/application_manager/application_manager.cc b/shell/application_manager/application_manager.cc
index fb9d467..dbb1422 100644
--- a/shell/application_manager/application_manager.cc
+++ b/shell/application_manager/application_manager.cc
@@ -12,6 +12,7 @@
 #include "base/trace_event/trace_event.h"
 #include "mojo/public/cpp/bindings/binding.h"
 #include "mojo/public/cpp/bindings/error_handler.h"
+#include "mojo/services/authenticating_url_loader/public/interfaces/authenticating_url_loader_factory.mojom.h"
 #include "mojo/services/authentication/public/interfaces/authentication.mojom.h"
 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.h"
 #include "shell/application_manager/fetcher.h"
@@ -100,7 +101,7 @@
     : options_(options),
       delegate_(delegate),
       blocking_pool_(nullptr),
-      initialized_authentication_service_(false),
+      initialized_authentication_interceptor_(false),
       weak_ptr_factory_(this) {
 }
 
@@ -188,29 +189,32 @@
                      &url_response_disk_cache_);
   }
 
-  if (!url_loader_factory_) {
-    ConnectToService(GURL("mojo:authenticating_url_loader"),
-                     &url_loader_factory_);
+  if (!network_service_) {
+    ConnectToService(GURL("mojo:network_service"), &network_service_);
   }
 
-  // NOTE: Attempting to initialize the AuthenticationService while connecting
-  // to the AuthenticationService would result in a recursive loop, so it has
-  // to be explicitly avoided here. AuthenticatingURLLoaders work fine without
-  // the AuthenticationService as long as authentication is not needed, so what
-  // this means in practice is that the AuthenticationService cannot itself
-  // require authentication to obtain.
-  if (!initialized_authentication_service_ &&
-      !EndsWith(resolved_url.path(), "/authentication.mojo", true)) {
+  // NOTE: Attempting to initialize the apps used authentication for while
+  // connecting to those apps would result in a recursive loop, so it has to be
+  // explicitly avoided here. What this means in practice is that these apps
+  // cannot themselves require authentication to obtain.
+  if (!initialized_authentication_interceptor_ &&
+      !EndsWith(resolved_url.path(), "/authentication.mojo", true) &&
+      !EndsWith(resolved_url.path(), "/authenticating_url_loader.mojo", true)) {
     authentication::AuthenticationServicePtr authentication_service;
     ConnectToService(GURL("mojo:authentication"), &authentication_service);
-    url_loader_factory_->SetAuthenticationService(
-        authentication_service.Pass());
-    initialized_authentication_service_ = true;
+    mojo::AuthenticatingURLLoaderFactoryPtr url_loader_factory;
+    ConnectToService(GURL("mojo:authenticating_url_loader"),
+                     &url_loader_factory);
+    mojo::URLLoaderInterceptorFactoryPtr interceptor_factory;
+    url_loader_factory->CreateURLLoaderInterceptorFactory(
+        GetProxy(&interceptor_factory), authentication_service.Pass());
+    network_service_->RegisterURLLoaderInterceptor(interceptor_factory.Pass());
+    initialized_authentication_interceptor_ = true;
   }
 
   new NetworkFetcher(options_.disable_cache, options_.predictable_app_filenames,
                      resolved_url, url_response_disk_cache_.get(),
-                     url_loader_factory_.get(), callback);
+                     network_service_.get(), callback);
 }
 
 bool ApplicationManager::ConnectToRunningApplication(
diff --git a/shell/application_manager/application_manager.h b/shell/application_manager/application_manager.h
index 23cde5e..12a0c7b 100644
--- a/shell/application_manager/application_manager.h
+++ b/shell/application_manager/application_manager.h
@@ -14,7 +14,7 @@
 #include "mojo/public/cpp/bindings/interface_request.h"
 #include "mojo/public/interfaces/application/application.mojom.h"
 #include "mojo/public/interfaces/application/service_provider.mojom.h"
-#include "mojo/services/authenticating_url_loader/public/interfaces/authenticating_url_loader_factory.mojom.h"
+#include "mojo/services/network/public/interfaces/network_service.mojom.h"
 #include "mojo/services/url_response_disk_cache/public/interfaces/url_response_disk_cache.mojom.h"
 #include "shell/application_manager/application_loader.h"
 #include "shell/application_manager/identity.h"
@@ -240,10 +240,10 @@
 
   base::SequencedWorkerPool* blocking_pool_;
   mojo::URLResponseDiskCachePtr url_response_disk_cache_;
-  mojo::AuthenticatingURLLoaderFactoryPtr url_loader_factory_;
+  mojo::NetworkServicePtr network_service_;
   MimeTypeToURLMap mime_type_to_url_;
   ScopedVector<NativeRunner> native_runners_;
-  bool initialized_authentication_service_;
+  bool initialized_authentication_interceptor_;
   base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
diff --git a/shell/application_manager/network_fetcher.cc b/shell/application_manager/network_fetcher.cc
index acfce9e..a08b831 100644
--- a/shell/application_manager/network_fetcher.cc
+++ b/shell/application_manager/network_fetcher.cc
@@ -39,7 +39,7 @@
     bool predictable_app_filenames,
     const GURL& url,
     mojo::URLResponseDiskCache* url_response_disk_cache,
-    mojo::AuthenticatingURLLoaderFactory* url_loader_factory,
+    mojo::NetworkService* network_service,
     const FetchCallback& loader_callback)
     : Fetcher(loader_callback),
       disable_cache_(disable_cache),
@@ -47,7 +47,7 @@
       url_(url),
       url_response_disk_cache_(url_response_disk_cache),
       weak_ptr_factory_(this) {
-  StartNetworkRequest(url, url_loader_factory);
+  StartNetworkRequest(url, network_service);
 }
 
 NetworkFetcher::~NetworkFetcher() {
@@ -217,7 +217,7 @@
 
 void NetworkFetcher::StartNetworkRequest(
     const GURL& url,
-    mojo::AuthenticatingURLLoaderFactory* url_loader_factory) {
+    mojo::NetworkService* network_service) {
   TRACE_EVENT_ASYNC_BEGIN1("mojo_shell", "NetworkFetcher::NetworkRequest", this,
                            "url", url.spec());
   mojo::URLRequestPtr request(mojo::URLRequest::New());
@@ -231,7 +231,7 @@
   headers.push_back(header.Pass());
   request->headers = headers.Pass();
 
-  url_loader_factory->CreateAuthenticatingURLLoader(GetProxy(&url_loader_));
+  network_service->CreateURLLoader(GetProxy(&url_loader_));
   url_loader_->Start(request.Pass(),
                      base::Bind(&NetworkFetcher::OnLoadComplete,
                                 weak_ptr_factory_.GetWeakPtr()));
diff --git a/shell/application_manager/network_fetcher.h b/shell/application_manager/network_fetcher.h
index cf337d9..dc74610 100644
--- a/shell/application_manager/network_fetcher.h
+++ b/shell/application_manager/network_fetcher.h
@@ -10,7 +10,7 @@
 #include "base/files/file_path.h"
 #include "base/memory/weak_ptr.h"
 #include "mojo/services/authenticating_url_loader/public/interfaces/authenticating_url_loader.mojom.h"
-#include "mojo/services/authenticating_url_loader/public/interfaces/authenticating_url_loader_factory.mojom.h"
+#include "mojo/services/network/public/interfaces/network_service.mojom.h"
 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
 #include "mojo/services/url_response_disk_cache/public/interfaces/url_response_disk_cache.mojom.h"
 #include "url/gurl.h"
@@ -24,7 +24,7 @@
                  bool predictable_app_filenames,
                  const GURL& url,
                  mojo::URLResponseDiskCache* url_response_disk_cache,
-                 mojo::AuthenticatingURLLoaderFactory* url_loader_factory,
+                 mojo::NetworkService* network_service,
                  const FetchCallback& loader_callback);
 
   ~NetworkFetcher() override;
@@ -66,9 +66,8 @@
 
   bool PeekFirstLine(std::string* line) override;
 
-  void StartNetworkRequest(
-      const GURL& url,
-      mojo::AuthenticatingURLLoaderFactory* url_loader_factory);
+  void StartNetworkRequest(const GURL& url,
+                           mojo::NetworkService* network_service);
 
   void OnLoadComplete(mojo::URLResponsePtr response);
 
@@ -76,7 +75,7 @@
   const bool predictable_app_filenames_;
   const GURL url_;
   mojo::URLResponseDiskCache* url_response_disk_cache_;
-  mojo::AuthenticatingURLLoaderPtr url_loader_;
+  mojo::URLLoaderPtr url_loader_;
   mojo::URLResponsePtr response_;
   base::FilePath path_;
   base::WeakPtrFactory<NetworkFetcher> weak_ptr_factory_;
diff --git a/shell/authenticating_url_loader_loader.cc b/shell/authenticating_url_loader_loader.cc
deleted file mode 100644
index 5a2ae45..0000000
--- a/shell/authenticating_url_loader_loader.cc
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2015 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 "base/logging.h"
-#include "shell/authenticating_url_loader_loader.h"
-
-namespace shell {
-
-AuthenticatingURLLoaderLoader::AuthenticatingURLLoaderLoader() {
-}
-
-AuthenticatingURLLoaderLoader::~AuthenticatingURLLoaderLoader() {
-}
-
-void AuthenticatingURLLoaderLoader::Load(
-    const GURL& url,
-    mojo::InterfaceRequest<mojo::Application> application_request) {
-  DCHECK(application_request.is_pending());
-  application_.reset(new mojo::ApplicationImpl(&authenticating_url_loader_,
-                                               application_request.Pass()));
-}
-
-}  // namespace shell
diff --git a/shell/authenticating_url_loader_loader.h b/shell/authenticating_url_loader_loader.h
deleted file mode 100644
index 979805d..0000000
--- a/shell/authenticating_url_loader_loader.h
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2015 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 SHELL_AUTHENTICATING_URL_LOADER_LOADER_H_
-#define SHELL_AUTHENTICATING_URL_LOADER_LOADER_H_
-
-#include "base/macros.h"
-#include "base/memory/scoped_ptr.h"
-#include "mojo/public/cpp/application/application_impl.h"
-#include "services/authenticating_url_loader/authenticating_url_loader_app.h"
-#include "shell/application_manager/application_loader.h"
-
-namespace shell {
-
-class AuthenticatingURLLoaderLoader : public ApplicationLoader {
- public:
-  AuthenticatingURLLoaderLoader();
-  ~AuthenticatingURLLoaderLoader() override;
-
- private:
-  // ApplicationLoader overrides:
-  void Load(
-      const GURL& url,
-      mojo::InterfaceRequest<mojo::Application> application_request) override;
-
-  mojo::AuthenticatingURLLoaderApp authenticating_url_loader_;
-  scoped_ptr<mojo::ApplicationImpl> application_;
-
-  DISALLOW_COPY_AND_ASSIGN(AuthenticatingURLLoaderLoader);
-};
-
-}  // namespace shell
-
-#endif  // SHELL_AUTHENTICATING_URL_LOADER_LOADER_H_
diff --git a/shell/context.cc b/shell/context.cc
index a926919..40abac5 100644
--- a/shell/context.cc
+++ b/shell/context.cc
@@ -31,7 +31,6 @@
 #include "services/tracing/tracing.mojom.h"
 #include "shell/application_manager/application_loader.h"
 #include "shell/application_manager/application_manager.h"
-#include "shell/authenticating_url_loader_loader.h"
 #include "shell/background_application_loader.h"
 #include "shell/command_line_util.h"
 #include "shell/filename_util.h"
@@ -279,12 +278,6 @@
           "url_response_disk_cache", base::MessageLoop::TYPE_DEFAULT)),
       GURL("mojo:url_response_disk_cache"));
 
-  application_manager()->SetLoaderForURL(
-      make_scoped_ptr(new BackgroundApplicationLoader(
-          make_scoped_ptr(new AuthenticatingURLLoaderLoader()),
-          "authenticating_url_loader", base::MessageLoop::TYPE_DEFAULT)),
-      GURL("mojo:authenticating_url_loader"));
-
   EnsureEmbedderIsInitialized();
   task_runners_.reset(
       new TaskRunners(base::MessageLoop::current()->message_loop_proxy()));