Avoid connecting to the renderer's ImageDownloader Mojo service if the service registry doesn't exist.

The service registry will be reset if the renderer process dies for any
reason. It's not valid to use the service registry until the renderer is
recreated.

BUG=511382

Review URL: https://codereview.chromium.org/1244783007

Cr-Commit-Position: refs/heads/master@{#340838}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 1fe81ec8..4ff481b 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -2642,9 +2642,25 @@
     uint32_t max_bitmap_size,
     bool bypass_cache,
     const WebContents::ImageDownloadCallback& callback) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
   static int next_image_download_id = 0;
   const image_downloader::ImageDownloaderPtr& mojo_image_downloader =
       GetMainFrame()->GetMojoImageDownloader();
+  const int download_id = ++next_image_download_id;
+  if (!mojo_image_downloader) {
+    // If the renderer process is dead (i.e. crash, or memory pressure on
+    // Android), the downloader service will be invalid. Pre-Mojo, this would
+    // hang the callback indefinetly since the IPC would be dropped. Now,
+    // respond with a 400 HTTP error code to indicate that something went wrong.
+    BrowserThread::PostTask(
+        BrowserThread::UI, FROM_HERE,
+        base::Bind(&WebContents::ImageDownloadCallback::Run,
+                   base::Owned(new ImageDownloadCallback(callback)),
+                   download_id, 400, url, std::vector<SkBitmap>(),
+                   std::vector<gfx::Size>()));
+    return download_id;
+  }
+
   image_downloader::DownloadRequestPtr req =
       image_downloader::DownloadRequest::New();
 
@@ -2655,8 +2671,8 @@
 
   mojo_image_downloader->DownloadImage(
       req.Pass(),
-      base::Bind(&DidDownloadImage, callback, ++next_image_download_id, url));
-  return next_image_download_id;
+      base::Bind(&DidDownloadImage, callback, download_id, url));
+  return download_id;
 }
 
 bool WebContentsImpl::IsSubframe() const {