Handle URLLoaderFactory error in StoragePartitionImpl and SystemNetworkContextManager

This CL:
 1.Adds reconnect logic to |StoragePartitionImpl::GetURLLoaderFactoryForBrowserProcess()|
   and |SystemNetworkContextManager::GetURLLoaderFactory()| to makes sure they return
   valid interface after crash.
 2.Moved two common test APIs into "browser_test_utils.h".
 3.Updated browser tests to run against restarted NetworkContext/URLLoaderFactory.

Note:
 * More tests will be added for other NetworkContext providers (e.g. IOThread),
   |URLLoaderFactoryGetter|, as well as Renderer related stuff.

Bug: 780956
Change-Id: I78cd400263e79f2ba2b6088e108a499ee87ae1c3
Reviewed-on: https://chromium-review.googlesource.com/769855
Commit-Queue: Chong Zhang <[email protected]>
Reviewed-by: John Abd-El-Malek <[email protected]>
Reviewed-by: Matt Menke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#523017}
diff --git a/content/browser/network_service_restart_browsertest.cc b/content/browser/network_service_restart_browsertest.cc
index 829ba96e..44e74b42 100644
--- a/content/browser/network_service_restart_browsertest.cc
+++ b/content/browser/network_service_restart_browsertest.cc
@@ -10,15 +10,10 @@
 #include "content/public/browser/web_contents.h"
 #include "content/public/common/content_features.h"
 #include "content/public/common/network_service.mojom.h"
-#include "content/public/common/network_service_test.mojom.h"
-#include "content/public/common/service_manager_connection.h"
-#include "content/public/common/service_names.mojom.h"
+#include "content/public/test/browser_test_utils.h"
 #include "content/public/test/content_browser_test.h"
 #include "content/public/test/content_browser_test_utils.h"
-#include "content/public/test/simple_url_loader_test_helper.h"
 #include "content/shell/browser/shell.h"
-#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
-#include "services/service_manager/public/cpp/connector.h"
 
 namespace content {
 
@@ -44,45 +39,10 @@
     EXPECT_TRUE(embedded_test_server()->Start());
   }
 
-  void SimulateNetworkServiceCrash() {
-    mojom::NetworkServiceTestPtr network_service_test;
-    ServiceManagerConnection::GetForProcess()->GetConnector()->BindInterface(
-        mojom::kNetworkServiceName, &network_service_test);
-
-    base::RunLoop run_loop;
-    network_service_test.set_connection_error_handler(run_loop.QuitClosure());
-
-    network_service_test->SimulateCrash();
-    run_loop.Run();
-
-    // Make sure the cached NetworkServicePtr receives error notification.
-    FlushNetworkServiceInstanceForTesting();
-  }
-
-  int LoadBasicRequest(mojom::NetworkContext* network_context) {
-    mojom::URLLoaderFactoryPtr url_loader_factory;
-    network_context->CreateURLLoaderFactory(MakeRequest(&url_loader_factory),
-                                            0);
-    // |url_loader_factory| will receive error notification asynchronously if
-    // |network_context| has already encountered error. However it's still false
-    // at this point.
-    EXPECT_FALSE(url_loader_factory.encountered_error());
-
-    std::unique_ptr<ResourceRequest> request =
-        std::make_unique<ResourceRequest>();
+  GURL GetTestURL() const {
     // Use '/echoheader' instead of '/echo' to avoid a disk_cache bug.
     // See https://crbug.com/792255.
-    request->url = embedded_test_server()->GetURL("/echoheader");
-
-    content::SimpleURLLoaderTestHelper simple_loader_helper;
-    std::unique_ptr<content::SimpleURLLoader> simple_loader =
-        content::SimpleURLLoader::Create(std::move(request),
-                                         TRAFFIC_ANNOTATION_FOR_TESTS);
-    simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
-        url_loader_factory.get(), simple_loader_helper.GetCallback());
-    simple_loader_helper.WaitForCallback();
-
-    return simple_loader->NetError();
+    return embedded_test_server()->GetURL("/echoheader");
   }
 
  private:
@@ -94,7 +54,7 @@
 IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest,
                        NetworkServiceProcessRecovery) {
   mojom::NetworkContextPtr network_context = CreateNetworkContext();
-  EXPECT_EQ(net::OK, LoadBasicRequest(network_context.get()));
+  EXPECT_EQ(net::OK, LoadBasicRequest(network_context.get(), GetTestURL()));
   EXPECT_TRUE(network_context.is_bound());
   EXPECT_FALSE(network_context.encountered_error());
 
@@ -108,11 +68,12 @@
   EXPECT_TRUE(network_context.is_bound());
   EXPECT_TRUE(network_context.encountered_error());
   // Make sure we could get |net::ERR_FAILED| with an invalid |network_context|.
-  EXPECT_EQ(net::ERR_FAILED, LoadBasicRequest(network_context.get()));
+  EXPECT_EQ(net::ERR_FAILED,
+            LoadBasicRequest(network_context.get(), GetTestURL()));
 
   // NetworkService should restart automatically and return valid interface.
   mojom::NetworkContextPtr network_context2 = CreateNetworkContext();
-  EXPECT_EQ(net::OK, LoadBasicRequest(network_context2.get()));
+  EXPECT_EQ(net::OK, LoadBasicRequest(network_context2.get(), GetTestURL()));
   EXPECT_TRUE(network_context2.is_bound());
   EXPECT_FALSE(network_context2.encountered_error());
 }
@@ -126,7 +87,7 @@
           shell()->web_contents()->GetBrowserContext()));
 
   mojom::NetworkContext* old_network_context = partition->GetNetworkContext();
-  EXPECT_EQ(net::OK, LoadBasicRequest(old_network_context));
+  EXPECT_EQ(net::OK, LoadBasicRequest(old_network_context, GetTestURL()));
 
   // Crash the NetworkService process. Existing interfaces should receive error
   // notifications at some point.
@@ -137,7 +98,8 @@
   // |partition->GetNetworkContext()| should return a valid new pointer after
   // crash.
   EXPECT_NE(old_network_context, partition->GetNetworkContext());
-  EXPECT_EQ(net::OK, LoadBasicRequest(partition->GetNetworkContext()));
+  EXPECT_EQ(net::OK,
+            LoadBasicRequest(partition->GetNetworkContext(), GetTestURL()));
 }
 
 }  // namespace content