Migrate dom distiller to SimpleURLLoader

BUG=838955

TBR=noyau@chromium.org (one line in ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc)

Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I050dbe74fc7ccd7b7811a5532abc931fda189177
Reviewed-on: https://chromium-review.googlesource.com/1140973
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576047}
diff --git a/chrome/browser/dom_distiller/dom_distiller_service_factory.cc b/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
index 4bf8a6c..7c13015 100644
--- a/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
+++ b/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
@@ -19,6 +19,7 @@
 #include "components/leveldb_proto/proto_database_impl.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/storage_partition.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
 
 namespace dom_distiller {
 
@@ -73,7 +74,7 @@
   std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
       new DistillerURLFetcherFactory(
           content::BrowserContext::GetDefaultStoragePartition(profile)
-              ->GetURLRequestContext()));
+              ->GetURLLoaderFactoryForBrowserProcess()));
 
   dom_distiller::proto::DomDistillerOptions options;
   if (VLOG_IS_ON(1)) {
diff --git a/components/dom_distiller/DEPS b/components/dom_distiller/DEPS
index 35ae9ad..d34ee2c 100644
--- a/components/dom_distiller/DEPS
+++ b/components/dom_distiller/DEPS
@@ -17,6 +17,8 @@
   "+net/http",
   "+net/traffic_annotation",
   "+net/url_request",
+  "+services/network/public/cpp",
+  "+services/network/test",
   "+ui/base/l10n",
   "+ui/base/resource",
   "+ui/gfx",
diff --git a/components/dom_distiller/core/BUILD.gn b/components/dom_distiller/core/BUILD.gn
index 63fe88e..ecd5d50 100644
--- a/components/dom_distiller/core/BUILD.gn
+++ b/components/dom_distiller/core/BUILD.gn
@@ -71,6 +71,7 @@
     "//components/strings",
     "//components/sync",
     "//components/variations",
+    "//services/network/public/cpp:cpp",
     "//skia",
     "//third_party/re2",
     "//ui/base",
@@ -152,6 +153,8 @@
     "//components/sync",
     "//components/sync_preferences:test_support",
     "//net:test_support",
+    "//services/network:test_support",
+    "//services/network/public/cpp:cpp",
     "//testing/gmock",
     "//testing/gtest",
     "//ui/base",
diff --git a/components/dom_distiller/core/distiller_unittest.cc b/components/dom_distiller/core/distiller_unittest.cc
index 3420e54..b27dd92 100644
--- a/components/dom_distiller/core/distiller_unittest.cc
+++ b/components/dom_distiller/core/distiller_unittest.cc
@@ -30,6 +30,7 @@
 #include "components/dom_distiller/core/proto/distilled_article.pb.h"
 #include "components/dom_distiller/core/proto/distilled_page.pb.h"
 #include "net/url_request/url_request_context_getter.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "third_party/dom_distiller_js/dom_distiller.pb.h"
diff --git a/components/dom_distiller/core/distiller_url_fetcher.cc b/components/dom_distiller/core/distiller_url_fetcher.cc
index 2cf48ec..035d40c 100644
--- a/components/dom_distiller/core/distiller_url_fetcher.cc
+++ b/components/dom_distiller/core/distiller_url_fetcher.cc
@@ -4,34 +4,27 @@
 
 #include "components/dom_distiller/core/distiller_url_fetcher.h"
 
-#include "components/data_use_measurement/core/data_use_user_data.h"
-#include "net/http/http_status_code.h"
 #include "net/traffic_annotation/network_traffic_annotation.h"
-#include "net/url_request/url_fetcher.h"
-#include "net/url_request/url_fetcher_delegate.h"
-#include "net/url_request/url_request_context_getter.h"
-#include "net/url_request/url_request_status.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
+#include "services/network/public/cpp/simple_url_loader.h"
 #include "url/gurl.h"
 
-using net::URLFetcher;
-
 namespace dom_distiller {
 
 DistillerURLFetcherFactory::DistillerURLFetcherFactory(
-    net::URLRequestContextGetter* context_getter)
-  : context_getter_(context_getter) {
-}
+    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
+    : url_loader_factory_(url_loader_factory) {}
+
+DistillerURLFetcherFactory::~DistillerURLFetcherFactory() {}
 
 DistillerURLFetcher*
 DistillerURLFetcherFactory::CreateDistillerURLFetcher() const {
-  return new DistillerURLFetcher(context_getter_);
+  return new DistillerURLFetcher(url_loader_factory_);
 }
 
-
 DistillerURLFetcher::DistillerURLFetcher(
-    net::URLRequestContextGetter* context_getter)
-  : context_getter_(context_getter) {
-}
+    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
+    : url_loader_factory_(url_loader_factory) {}
 
 DistillerURLFetcher::~DistillerURLFetcher() {
 }
@@ -39,14 +32,16 @@
 void DistillerURLFetcher::FetchURL(const std::string& url,
                                    const URLFetcherCallback& callback) {
   // Don't allow a fetch if one is pending.
-  DCHECK(!url_fetcher_ || !url_fetcher_->GetStatus().is_io_pending());
+  DCHECK(!url_loader_);
   callback_ = callback;
-  url_fetcher_ = CreateURLFetcher(context_getter_, url);
-  url_fetcher_->Start();
+  url_loader_ = CreateURLFetcher(url);
+  url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
+      url_loader_factory_.get(),
+      base::BindOnce(&DistillerURLFetcher::OnURLLoadComplete,
+                     base::Unretained(this)));
 }
 
-std::unique_ptr<URLFetcher> DistillerURLFetcher::CreateURLFetcher(
-    net::URLRequestContextGetter* context_getter,
+std::unique_ptr<network::SimpleURLLoader> DistillerURLFetcher::CreateURLFetcher(
     const std::string& url) {
   net::NetworkTrafficAnnotationTag traffic_annotation =
       net::DefineNetworkTrafficAnnotation("dom_distiller", R"(
@@ -79,24 +74,30 @@
             "Not implemented, considered not useful as no content is being "
             "uploaded; this request merely downloads the resources on the web."
         })");
-  std::unique_ptr<net::URLFetcher> fetcher =
-      URLFetcher::Create(GURL(url), URLFetcher::GET, this, traffic_annotation);
-  data_use_measurement::DataUseUserData::AttachToFetcher(
-      fetcher.get(), data_use_measurement::DataUseUserData::DOM_DISTILLER);
-  fetcher->SetRequestContext(context_getter);
+  auto resource_request = std::make_unique<network::ResourceRequest>();
+  resource_request->url = GURL(url);
+  resource_request->method = "GET";
+
+  // TODO(crbug.com/808498): Restore the data use measurement when bug is fixed.
+  auto url_loader = network::SimpleURLLoader::Create(
+      std::move(resource_request), traffic_annotation);
   static const int kMaxRetries = 5;
-  fetcher->SetMaxRetriesOn5xx(kMaxRetries);
-  return fetcher;
+  url_loader->SetRetryOptions(kMaxRetries,
+                              network::SimpleURLLoader::RETRY_ON_5XX);
+
+  return url_loader;
 }
 
-void DistillerURLFetcher::OnURLFetchComplete(
-    const URLFetcher* source) {
+void DistillerURLFetcher::OnURLLoadComplete(
+    std::unique_ptr<std::string> response_body) {
+  // The loader is not needed at this point anymore.
+  url_loader_.reset();
+
   std::string response;
-  if (source && source->GetStatus().is_success() &&
-      source->GetResponseCode() == net::HTTP_OK) {
+  if (response_body) {
     // Only copy over the data if the request was successful. Insert
     // an empty string into the proto otherwise.
-    source->GetResponseAsString(&response);
+    response = std::move(*response_body);
   }
   callback_.Run(response);
 }
diff --git a/components/dom_distiller/core/distiller_url_fetcher.h b/components/dom_distiller/core/distiller_url_fetcher.h
index d0178e0..c883dca 100644
--- a/components/dom_distiller/core/distiller_url_fetcher.h
+++ b/components/dom_distiller/core/distiller_url_fetcher.h
@@ -9,9 +9,11 @@
 
 #include "base/callback.h"
 #include "base/macros.h"
-#include "net/url_request/url_fetcher.h"
-#include "net/url_request/url_fetcher_delegate.h"
-#include "net/url_request/url_request_context_getter.h"
+
+namespace network {
+class SharedURLLoaderFactory;
+class SimpleURLLoader;
+}  // namespace network
 
 namespace dom_distiller {
 
@@ -20,21 +22,23 @@
 // Class for creating a DistillerURLFetcher.
 class DistillerURLFetcherFactory {
  public:
-  explicit DistillerURLFetcherFactory(
-      net::URLRequestContextGetter* context_getter);
-  virtual ~DistillerURLFetcherFactory() {}
+  DistillerURLFetcherFactory(
+      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
+  virtual ~DistillerURLFetcherFactory();
   virtual DistillerURLFetcher* CreateDistillerURLFetcher() const;
 
  private:
-  net::URLRequestContextGetter* context_getter_;
+  friend class TestDistillerURLFetcherFactory;
+  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
 };
 
-// This class fetches a URL, and notifies the caller when the operation
+// This class loads a URL, and notifies the caller when the operation
 // completes or fails. If the request fails, an empty string will be returned.
-class DistillerURLFetcher : public net::URLFetcherDelegate {
+class DistillerURLFetcher {
  public:
-  explicit DistillerURLFetcher(net::URLRequestContextGetter* context_getter);
-  ~DistillerURLFetcher() override;
+  explicit DistillerURLFetcher(
+      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
+  virtual ~DistillerURLFetcher();
 
   // Indicates when a fetch is done.
   typedef base::Callback<void(const std::string& data)> URLFetcherCallback;
@@ -44,17 +48,15 @@
                         const URLFetcherCallback& callback);
 
  protected:
-  virtual std::unique_ptr<net::URLFetcher> CreateURLFetcher(
-      net::URLRequestContextGetter* context_getter,
+  virtual std::unique_ptr<network::SimpleURLLoader> CreateURLFetcher(
       const std::string& url);
 
  private:
-  // net::URLFetcherDelegate:
-  void OnURLFetchComplete(const net::URLFetcher* source) override;
+  void OnURLLoadComplete(std::unique_ptr<std::string> response_body);
 
-  std::unique_ptr<net::URLFetcher> url_fetcher_;
+  std::unique_ptr<network::SimpleURLLoader> url_loader_;
   URLFetcherCallback callback_;
-  net::URLRequestContextGetter* context_getter_;
+  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
   DISALLOW_COPY_AND_ASSIGN(DistillerURLFetcher);
 };
 
diff --git a/components/dom_distiller/core/distiller_url_fetcher_unittest.cc b/components/dom_distiller/core/distiller_url_fetcher_unittest.cc
index 7454119..a19e6a0 100644
--- a/components/dom_distiller/core/distiller_url_fetcher_unittest.cc
+++ b/components/dom_distiller/core/distiller_url_fetcher_unittest.cc
@@ -2,16 +2,14 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "components/dom_distiller/core/distiller_url_fetcher.h"
 #include "base/bind.h"
 #include "base/bind_helpers.h"
 #include "base/message_loop/message_loop.h"
 #include "base/run_loop.h"
-#include "components/dom_distiller/core/distiller_url_fetcher.h"
-#include "net/http/http_status_code.h"
-#include "net/url_request/test_url_fetcher_factory.h"
-#include "net/url_request/url_fetcher.h"
-#include "net/url_request/url_request_context_getter.h"
-#include "net/url_request/url_request_status.h"
+#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
+#include "services/network/test/test_url_loader_factory.h"
+#include "services/network/test/test_utils.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "url/gurl.h"
 
@@ -23,6 +21,11 @@
 
 class DistillerURLFetcherTest : public testing::Test {
  public:
+  DistillerURLFetcherTest()
+      : test_shared_url_loader_factory_(
+            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
+                &test_url_loader_factory_)) {}
+
   void FetcherCallback(const std::string& response) {
      response_ = response;
   }
@@ -30,18 +33,16 @@
  protected:
   // testing::Test implementation:
   void SetUp() override {
-    url_fetcher_.reset(new dom_distiller::DistillerURLFetcher(nullptr));
-    factory_.reset(new net::FakeURLFetcherFactory(nullptr));
-    factory_->SetFakeResponse(
-        GURL(kTestPageA),
-        std::string(kTestPageAResponse, sizeof(kTestPageAResponse)),
-        net::HTTP_OK,
-        net::URLRequestStatus::SUCCESS);
-    factory_->SetFakeResponse(
+    url_fetcher_.reset(new dom_distiller::DistillerURLFetcher(
+        test_shared_url_loader_factory_));
+    test_url_loader_factory_.AddResponse(
+        kTestPageA,
+        std::string(kTestPageAResponse, sizeof(kTestPageAResponse)));
+    test_url_loader_factory_.AddResponse(
         GURL(kTestPageB),
+        network::CreateResourceResponseHead(net::HTTP_INTERNAL_SERVER_ERROR),
         std::string(kTestPageBResponse, sizeof(kTestPageBResponse)),
-        net::HTTP_INTERNAL_SERVER_ERROR,
-        net::URLRequestStatus::SUCCESS);
+        network::URLLoaderCompletionStatus(net::OK));
   }
 
   void Fetch(const std::string& url,
@@ -56,7 +57,9 @@
   }
 
   std::unique_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_;
-  std::unique_ptr<net::FakeURLFetcherFactory> factory_;
+  network::TestURLLoaderFactory test_url_loader_factory_;
+  scoped_refptr<network::SharedURLLoaderFactory>
+      test_shared_url_loader_factory_;
   std::string response_;
 };
 
diff --git a/components/dom_distiller/standalone/content_extractor_browsertest.cc b/components/dom_distiller/standalone/content_extractor_browsertest.cc
index 5acc5f3..1286ea0 100644
--- a/components/dom_distiller/standalone/content_extractor_browsertest.cc
+++ b/components/dom_distiller/standalone/content_extractor_browsertest.cc
@@ -142,7 +142,7 @@
   std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
       new DistillerURLFetcherFactory(
           content::BrowserContext::GetDefaultStoragePartition(context)
-              ->GetURLRequestContext()));
+              ->GetURLLoaderFactoryForBrowserProcess()));
 
   dom_distiller::proto::DomDistillerOptions options;
   if (base::CommandLine::ForCurrentProcess()->HasSwitch(kExtractTextOnly)) {
diff --git a/ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc b/ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
index 8d94469..65492d2 100644
--- a/ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
+++ b/ios/chrome/browser/dom_distiller/dom_distiller_service_factory.cc
@@ -22,6 +22,7 @@
 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
 #include "ios/web/public/browser_state.h"
 #include "ios/web/public/web_thread.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
 
 namespace {
 // A simple wrapper for DomDistillerService to expose it as a
@@ -77,7 +78,7 @@
 
   std::unique_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory =
       std::make_unique<DistillerURLFetcherFactory>(
-          context->GetRequestContext());
+          context->GetSharedURLLoaderFactory());
 
   dom_distiller::proto::DomDistillerOptions options;
   std::unique_ptr<DistillerFactory> distiller_factory =
diff --git a/ios/chrome/browser/reading_list/reading_list_download_service_factory.cc b/ios/chrome/browser/reading_list/reading_list_download_service_factory.cc
index fd70a2f..d7f2c3a 100644
--- a/ios/chrome/browser/reading_list/reading_list_download_service_factory.cc
+++ b/ios/chrome/browser/reading_list/reading_list_download_service_factory.cc
@@ -16,6 +16,7 @@
 #include "ios/chrome/browser/reading_list/reading_list_distiller_page_factory.h"
 #include "ios/chrome/browser/reading_list/reading_list_download_service.h"
 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
 
 // static
 ReadingListDownloadService*
@@ -55,7 +56,7 @@
 
   auto distiller_url_fetcher_factory =
       std::make_unique<dom_distiller::DistillerURLFetcherFactory>(
-          context->GetRequestContext());
+          context->GetSharedURLLoaderFactory());
 
   dom_distiller::proto::DomDistillerOptions options;
   auto distiller_factory =
diff --git a/ios/chrome/browser/reading_list/url_downloader.cc b/ios/chrome/browser/reading_list/url_downloader.cc
index 3602c8a..0fec280 100644
--- a/ios/chrome/browser/reading_list/url_downloader.cc
+++ b/ios/chrome/browser/reading_list/url_downloader.cc
@@ -23,7 +23,6 @@
 #include "net/base/load_flags.h"
 #include "net/http/http_response_headers.h"
 #include "net/url_request/url_fetcher.h"
-#include "net/url_request/url_fetcher_delegate.h"
 #include "net/url_request/url_request_context_getter.h"
 #include "net/url_request/url_request_status.h"
 #include "url/gurl.h"
diff --git a/ios/chrome/browser/reading_list/url_downloader.h b/ios/chrome/browser/reading_list/url_downloader.h
index 28d7cbc..ed283ad 100644
--- a/ios/chrome/browser/reading_list/url_downloader.h
+++ b/ios/chrome/browser/reading_list/url_downloader.h
@@ -7,9 +7,11 @@
 
 #include "base/callback.h"
 #include "base/containers/circular_deque.h"
+#include "base/files/file_path.h"
 #include "base/task/cancelable_task_tracker.h"
 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h"
 #include "ios/chrome/browser/reading_list/reading_list_distiller_page.h"
+#include "net/url_request/url_fetcher_delegate.h"
 
 class PrefService;
 class GURL;