[Reland-2] Merge-M63: Add a temporary fix for Blob lifetime with SW FetchEvent without MojoBlob

Add a new method to ServiceWorkerFetchResponseCallback for
legacy blob which calls back to the caller when it's done, and
hold a (copy) of WebServiceWorkerResponse until it's fired.

WebServiceWorkerResponse is a copyable wrapper object of
scoped_refptr<WebServiceWorkerResponsePrivate>, which has
RefPtr<BlobDataHandle> in its member.

(cherry picked from commit 7640944da7ef7b5fa2e61165b9f317b9764e1161)

Bug: 776642
Change-Id: I0d6fff4ca64236a3d6ed69e1790db2c41ccae226
Reviewed-on: https://chromium-review.googlesource.com/734135
Commit-Queue: Daniel Cheng <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Marijn Kruisselbrink <[email protected]>
Reviewed-by: Matt Falkenhagen <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#511284}
Reviewed-on: https://chromium-review.googlesource.com/750925
Reviewed-by: Kinuko Yasuda <[email protected]>
Cr-Commit-Position: refs/branch-heads/3239@{#345}
Cr-Branched-From: adb61db19020ed8ecee5e91b1a0ea4c924ae2988-refs/heads/master@{#508578}

(cherry picked from commit 415422e42b4395802fbf0ef93e4930ee7bf04f79)

Change-Id: I68fe200c3f45a91c4064cc4cd3a2dd25c1257aa2
Reviewed-on: https://chromium-review.googlesource.com/751222
Reviewed-by: Kinuko Yasuda <[email protected]>
Cr-Commit-Position: refs/branch-heads/3239@{#347}
Cr-Branched-From: adb61db19020ed8ecee5e91b1a0ea4c924ae2988-refs/heads/master@{#508578}

(cherry picked from commit 13802e31c540e80248e04e8f262091a0767ce988)

[email protected]

Change-Id: I5d269b558b9cf39d32bbc57c292ab26edfc2e1aa
Reviewed-on: https://chromium-review.googlesource.com/751602
Reviewed-by: Kinuko Yasuda <[email protected]>
Cr-Commit-Position: refs/branch-heads/3239@{#350}
Cr-Branched-From: adb61db19020ed8ecee5e91b1a0ea4c924ae2988-refs/heads/master@{#508578}
diff --git a/content/browser/service_worker/browser_side_controller_service_worker.cc b/content/browser/service_worker/browser_side_controller_service_worker.cc
index cdd82eeb..16bb35f 100644
--- a/content/browser/service_worker/browser_side_controller_service_worker.cc
+++ b/content/browser/service_worker/browser_side_controller_service_worker.cc
@@ -67,6 +67,11 @@
     callback->OnResponseBlob(response, std::move(body_as_blob),
                              dispatch_event_time);
   }
+  void OnResponseLegacyBlob(const ServiceWorkerResponse& response,
+                            base::Time dispatch_event_time,
+                            OnResponseLegacyBlobCallback callback) override {
+    NOTREACHED();
+  }
   void OnResponseStream(
       const ServiceWorkerResponse& response,
       blink::mojom::ServiceWorkerStreamHandlePtr body_as_stream,
diff --git a/content/browser/service_worker/service_worker_fetch_dispatcher.cc b/content/browser/service_worker/service_worker_fetch_dispatcher.cc
index f7712a5..ff802358 100644
--- a/content/browser/service_worker/service_worker_fetch_dispatcher.cc
+++ b/content/browser/service_worker/service_worker_fetch_dispatcher.cc
@@ -378,6 +378,15 @@
                    SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
                    dispatch_event_time);
   }
+  void OnResponseLegacyBlob(const ServiceWorkerResponse& response,
+                            base::Time dispatch_event_time,
+                            OnResponseLegacyBlobCallback callback) override {
+    HandleResponse(fetch_dispatcher_, version_, fetch_event_id_, response,
+                   nullptr /* body_as_stream */, nullptr /* body_as_blob */,
+                   SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
+                   dispatch_event_time);
+    std::move(callback).Run();
+  }
   void OnResponseStream(
       const ServiceWorkerResponse& response,
       blink::mojom::ServiceWorkerStreamHandlePtr body_as_stream,
diff --git a/content/child/service_worker/service_worker_subresource_loader.cc b/content/child/service_worker/service_worker_subresource_loader.cc
index 818c2da..c4b989c 100644
--- a/content/child/service_worker/service_worker_subresource_loader.cc
+++ b/content/child/service_worker/service_worker_subresource_loader.cc
@@ -234,6 +234,13 @@
                 nullptr /* body_as_stream */);
 }
 
+void ServiceWorkerSubresourceLoader::OnResponseLegacyBlob(
+    const ServiceWorkerResponse& response,
+    base::Time dispatch_event_time,
+    OnResponseLegacyBlobCallback callback) {
+  NOTREACHED();
+}
+
 void ServiceWorkerSubresourceLoader::OnResponseStream(
     const ServiceWorkerResponse& response,
     blink::mojom::ServiceWorkerStreamHandlePtr body_as_stream,
diff --git a/content/child/service_worker/service_worker_subresource_loader.h b/content/child/service_worker/service_worker_subresource_loader.h
index 4fd31be..18cfdc4c3 100644
--- a/content/child/service_worker/service_worker_subresource_loader.h
+++ b/content/child/service_worker/service_worker_subresource_loader.h
@@ -65,6 +65,9 @@
   void OnResponseBlob(const ServiceWorkerResponse& response,
                       storage::mojom::BlobPtr blob,
                       base::Time dispatch_event_time) override;
+  void OnResponseLegacyBlob(const ServiceWorkerResponse& response,
+                            base::Time dispatch_event_time,
+                            OnResponseLegacyBlobCallback callback) override;
   void OnResponseStream(
       const ServiceWorkerResponse& response,
       blink::mojom::ServiceWorkerStreamHandlePtr body_as_stream,
diff --git a/content/common/service_worker/service_worker_fetch_response_callback.mojom b/content/common/service_worker/service_worker_fetch_response_callback.mojom
index 567c6ba6..bf983b5 100644
--- a/content/common/service_worker/service_worker_fetch_response_callback.mojom
+++ b/content/common/service_worker/service_worker_fetch_response_callback.mojom
@@ -25,6 +25,12 @@
   OnResponseBlob(ServiceWorkerResponse response,
                  storage.mojom.Blob body_as_blob,
                  mojo.common.mojom.Time dispatch_event_time);
+   // Responds to the request with |response|. The body is provided as a
+   // non-Mojo Blob via |response.blob_uuid|. The callback is useful for Blob
+   // lifetime management purposes and is called once the caller is done with
+   // the Blob. TODO(kinuko): Remove this once MojoBlob is fully shipped.
+  OnResponseLegacyBlob(ServiceWorkerResponse response,
+                       mojo.common.mojom.Time dispatch_event_time) => ();
   // Responds to the request with |response|. The body is returned as a stream.
   OnResponseStream(ServiceWorkerResponse response,
                    blink.mojom.ServiceWorkerStreamHandle body_as_stream,
diff --git a/content/renderer/service_worker/service_worker_context_client.cc b/content/renderer/service_worker/service_worker_context_client.cc
index 9382d65..a1337d5 100644
--- a/content/renderer/service_worker/service_worker_context_client.cc
+++ b/content/renderer/service_worker/service_worker_context_client.cc
@@ -41,7 +41,6 @@
 #include "content/public/common/content_features.h"
 #include "content/public/common/push_event_payload.h"
 #include "content/public/common/referrer.h"
-#include "content/public/common/service_names.mojom.h"
 #include "content/public/renderer/content_renderer_client.h"
 #include "content/public/renderer/document_state.h"
 #include "content/renderer/devtools/devtools_agent.h"
@@ -56,8 +55,6 @@
 #include "ipc/ipc_message_macros.h"
 #include "net/base/net_errors.h"
 #include "net/http/http_response_headers.h"
-#include "services/service_manager/public/cpp/connector.h"
-#include "services/service_manager/public/cpp/interface_provider.h"
 #include "storage/common/blob_storage/blob_handle.h"
 #include "storage/public/interfaces/blobs.mojom.h"
 #include "third_party/WebKit/public/platform/InterfaceProvider.h"
@@ -284,9 +281,9 @@
   }
 }
 
-void GetBlobRegistry(storage::mojom::BlobRegistryRequest request) {
-  ChildThreadImpl::current()->GetConnector()->BindInterface(
-      mojom::kBrowserServiceName, std::move(request));
+void OnResponseBlobDispatchDone(
+    const blink::WebServiceWorkerResponse& response) {
+  // This frees the ref to the internal data of |response|.
 }
 
 }  // namespace
@@ -1021,26 +1018,19 @@
       context_->fetch_response_callbacks[fetch_event_id];
 
   if (response.blob_uuid.size()) {
-    // TODO(kinuko): Remove this hack once kMojoBlobs is enabled by default
-    // and crbug.com/755523 is resolved.
-    storage::mojom::BlobPtr blob_ptr;
+    blink::mojom::BlobPtr blob_ptr;
     if (response.blob) {
       blob_ptr = response.blob->TakeBlobPtr();
       response.blob = nullptr;
+      response_callback->OnResponseBlob(
+          response, std::move(blob_ptr),
+          base::Time::FromDoubleT(event_dispatch_time));
     } else {
-      if (!blob_registry_) {
-        // TODO(kinuko): We should use per-frame / per-worker InterfaceProvider
-        // instead (crbug.com/734210).
-        main_thread_task_runner_->PostTask(
-            FROM_HERE,
-            base::BindOnce(&GetBlobRegistry, MakeRequest(&blob_registry_)));
-      }
-      blob_registry_->GetBlobFromUUID(MakeRequest(&blob_ptr),
-                                      response.blob_uuid);
+      // TODO(kinuko): Remove this hack once kMojoBlobs is enabled by default.
+      response_callback->OnResponseLegacyBlob(
+          response, base::Time::FromDoubleT(event_dispatch_time),
+          base::BindOnce(&OnResponseBlobDispatchDone, web_response));
     }
-    response_callback->OnResponseBlob(
-        response, std::move(blob_ptr),
-        base::Time::FromDoubleT(event_dispatch_time));
   } else {
     response_callback->OnResponse(response,
                                   base::Time::FromDoubleT(event_dispatch_time));