[Cronet] Don't copy UrlResponseInfo or Error.

Currently, RequestFinishedInfo contains UrlResponseInfo and Error
members. Because struct-in-struct is implemented to use
base::Optional<T>, it therefore contains inline copies of these members.

These structures can be large -- the UrlResponseInfo can contain
headers, and the Error contains error strings.

Therefore, instead of copying these members into the UrlResponseInfo,
separate pointers should be passed to OnRequestFinished().

Ownership of those pointers should be shared in a similar manner to
|request_info| for consistency and to avoid confusion.

Bug: 879208
Change-Id: I9b2ff91706819b8d10e7093e33002d6868dfb735
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1610591
Reviewed-by: Paul Jensen <pauljensen@chromium.org>
Reviewed-by: Misha Efimov <mef@chromium.org>
Commit-Queue: Caleb Raitto <caraitto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661362}
diff --git a/components/cronet/native/cronet.idl b/components/cronet/native/cronet.idl
index 95d5883..b28c782 100644
--- a/components/cronet/native/cronet.idl
+++ b/components/cronet/native/cronet.idl
@@ -1440,17 +1440,6 @@
    * Returns the reason why the request finished.
    */
   FINISHED_REASON finished_reason = SUCCEEDED;
-
-  /**
-   * {@link UrlResponseInfo} for the request, if its response had started.
-   */
-  UrlResponseInfo? response_info;
-
-  /**
-   * If the request failed, will be equal to the same {@link Error} provided to
-   * {@link UrlRequestCallback.OnFailed}.
-   */
-  Error? error;
 };
 
 /**
@@ -1479,6 +1468,19 @@
    *
    *     This is accomplished by ownership being shared between the {@code
    *     UrlRequest} and the code that calls this listener.
+   *
+   * @param response_info A pointer to the same UrlResponseInfo passed to
+   *     {@link UrlRequestCallback.OnCanceled()}, {@link
+   *     UrlRequestCallback.OnFailed()} or {@link
+   *     UrlRequestCallback.OnSucceeded()}. The lifetime and ownership of
+   *     {@code response_info} works the same as for {@code request_info}.
+   *
+   * @param error A pointer to the same Error passed to
+   *     {@code UrlRequestCallback.OnFailed()}, or null if there was no error.
+   *     The lifetime and ownership of {@code error} works the same as for
+   *     {@code request_info}.
    */
-  OnRequestFinished(RequestFinishedInfo request_info);
+  OnRequestFinished(RequestFinishedInfo request_info,
+                    UrlResponseInfo response_info,
+                    Error error);
 };
diff --git a/components/cronet/native/engine.cc b/components/cronet/native/engine.cc
index 59698194..dcda7e0 100644
--- a/components/cronet/native/engine.cc
+++ b/components/cronet/native/engine.cc
@@ -312,7 +312,10 @@
             [](scoped_refptr<RequestInfo> request_info,
                Cronet_RequestFinishedInfoListenerPtr
                    request_finished_listener) {
-              request_finished_listener->OnRequestFinished(&request_info->data);
+              // TODO(crbug.com/879208): Pass-though UrlResponseInfo and Error
+              // too.
+              request_finished_listener->OnRequestFinished(&request_info->data,
+                                                           nullptr, nullptr);
             },
             request_info, request_finished_listener)));
   }
diff --git a/components/cronet/native/engine_unittest.cc b/components/cronet/native/engine_unittest.cc
index 8653293..b3ad17bc 100644
--- a/components/cronet/native/engine_unittest.cc
+++ b/components/cronet/native/engine_unittest.cc
@@ -32,7 +32,9 @@
 // increment this value.
 void TestRequestInfoListener_OnRequestFinished(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info) {
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr,
+    Cronet_ErrorPtr) {
   CHECK(self);
   Cronet_ClientContext context =
       Cronet_RequestFinishedInfoListener_GetClientContext(self);
diff --git a/components/cronet/native/generated/cronet.idl_c.h b/components/cronet/native/generated/cronet.idl_c.h
index d59ed25..cf74264 100644
--- a/components/cronet/native/generated/cronet.idl_c.h
+++ b/components/cronet/native/generated/cronet.idl_c.h
@@ -669,12 +669,16 @@
 CRONET_EXPORT
 void Cronet_RequestFinishedInfoListener_OnRequestFinished(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info);
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr response_info,
+    Cronet_ErrorPtr error);
 // The app implements abstract interface Cronet_RequestFinishedInfoListener by
 // defining custom functions for each method.
 typedef void (*Cronet_RequestFinishedInfoListener_OnRequestFinishedFunc)(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info);
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr response_info,
+    Cronet_ErrorPtr error);
 // The app creates an instance of Cronet_RequestFinishedInfoListener by
 // providing custom functions for each method.
 CRONET_EXPORT Cronet_RequestFinishedInfoListenerPtr
@@ -1240,22 +1244,6 @@
 void Cronet_RequestFinishedInfo_finished_reason_set(
     Cronet_RequestFinishedInfoPtr self,
     const Cronet_RequestFinishedInfo_FINISHED_REASON finished_reason);
-CRONET_EXPORT
-void Cronet_RequestFinishedInfo_response_info_set(
-    Cronet_RequestFinishedInfoPtr self,
-    const Cronet_UrlResponseInfoPtr response_info);
-// Move data from |response_info|. The caller retains ownership of
-// |response_info| and must destroy it.
-void Cronet_RequestFinishedInfo_response_info_move(
-    Cronet_RequestFinishedInfoPtr self,
-    Cronet_UrlResponseInfoPtr response_info);
-CRONET_EXPORT
-void Cronet_RequestFinishedInfo_error_set(Cronet_RequestFinishedInfoPtr self,
-                                          const Cronet_ErrorPtr error);
-// Move data from |error|. The caller retains ownership of |error| and must
-// destroy it.
-void Cronet_RequestFinishedInfo_error_move(Cronet_RequestFinishedInfoPtr self,
-                                           Cronet_ErrorPtr error);
 // Cronet_RequestFinishedInfo getters.
 CRONET_EXPORT
 Cronet_MetricsPtr Cronet_RequestFinishedInfo_metrics_get(
@@ -1274,12 +1262,6 @@
 Cronet_RequestFinishedInfo_FINISHED_REASON
 Cronet_RequestFinishedInfo_finished_reason_get(
     const Cronet_RequestFinishedInfoPtr self);
-CRONET_EXPORT
-Cronet_UrlResponseInfoPtr Cronet_RequestFinishedInfo_response_info_get(
-    const Cronet_RequestFinishedInfoPtr self);
-CRONET_EXPORT
-Cronet_ErrorPtr Cronet_RequestFinishedInfo_error_get(
-    const Cronet_RequestFinishedInfoPtr self);
 
 #ifdef __cplusplus
 }
diff --git a/components/cronet/native/generated/cronet.idl_impl_interface.cc b/components/cronet/native/generated/cronet.idl_impl_interface.cc
index d6dcc5b..e7229f0 100644
--- a/components/cronet/native/generated/cronet.idl_impl_interface.cc
+++ b/components/cronet/native/generated/cronet.idl_impl_interface.cc
@@ -937,9 +937,11 @@
 
 void Cronet_RequestFinishedInfoListener_OnRequestFinished(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info) {
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr response_info,
+    Cronet_ErrorPtr error) {
   DCHECK(self);
-  self->OnRequestFinished(request_info);
+  self->OnRequestFinished(request_info, response_info, error);
 }
 
 // Implementation of Cronet_RequestFinishedInfoListener that forwards calls to C
@@ -955,8 +957,10 @@
   ~Cronet_RequestFinishedInfoListenerStub() override {}
 
  protected:
-  void OnRequestFinished(Cronet_RequestFinishedInfoPtr request_info) override {
-    OnRequestFinishedFunc_(this, request_info);
+  void OnRequestFinished(Cronet_RequestFinishedInfoPtr request_info,
+                         Cronet_UrlResponseInfoPtr response_info,
+                         Cronet_ErrorPtr error) override {
+    OnRequestFinishedFunc_(this, request_info, response_info, error);
   }
 
  private:
diff --git a/components/cronet/native/generated/cronet.idl_impl_interface.h b/components/cronet/native/generated/cronet.idl_impl_interface.h
index 6a0c562..9021809 100644
--- a/components/cronet/native/generated/cronet.idl_impl_interface.h
+++ b/components/cronet/native/generated/cronet.idl_impl_interface.h
@@ -239,8 +239,9 @@
   }
   Cronet_ClientContext client_context() const { return client_context_; }
 
-  virtual void OnRequestFinished(
-      Cronet_RequestFinishedInfoPtr request_info) = 0;
+  virtual void OnRequestFinished(Cronet_RequestFinishedInfoPtr request_info,
+                                 Cronet_UrlResponseInfoPtr response_info,
+                                 Cronet_ErrorPtr error) = 0;
 
  private:
   Cronet_ClientContext client_context_ = nullptr;
diff --git a/components/cronet/native/generated/cronet.idl_impl_interface_unittest.cc b/components/cronet/native/generated/cronet.idl_impl_interface_unittest.cc
index 4950a45..9f92f38 100644
--- a/components/cronet/native/generated/cronet.idl_impl_interface_unittest.cc
+++ b/components/cronet/native/generated/cronet.idl_impl_interface_unittest.cc
@@ -804,7 +804,9 @@
 // Implementation of Cronet_RequestFinishedInfoListener methods for testing.
 void TestCronet_RequestFinishedInfoListener_OnRequestFinished(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info) {
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr response_info,
+    Cronet_ErrorPtr error) {
   CHECK(self);
   Cronet_ClientContext client_context =
       Cronet_RequestFinishedInfoListener_GetClientContext(self);
diff --git a/components/cronet/native/generated/cronet.idl_impl_struct.cc b/components/cronet/native/generated/cronet.idl_impl_struct.cc
index 253d1dc..735b65a 100644
--- a/components/cronet/native/generated/cronet.idl_impl_struct.cc
+++ b/components/cronet/native/generated/cronet.idl_impl_struct.cc
@@ -1225,38 +1225,6 @@
   self->finished_reason = finished_reason;
 }
 
-void Cronet_RequestFinishedInfo_response_info_set(
-    Cronet_RequestFinishedInfoPtr self,
-    const Cronet_UrlResponseInfoPtr response_info) {
-  DCHECK(self);
-  self->response_info.reset();
-  if (response_info != nullptr)
-    self->response_info.emplace(*response_info);
-}
-void Cronet_RequestFinishedInfo_response_info_move(
-    Cronet_RequestFinishedInfoPtr self,
-    Cronet_UrlResponseInfoPtr response_info) {
-  DCHECK(self);
-  self->response_info.reset();
-  if (response_info != nullptr)
-    self->response_info.emplace(std::move(*response_info));
-}
-
-void Cronet_RequestFinishedInfo_error_set(Cronet_RequestFinishedInfoPtr self,
-                                          const Cronet_ErrorPtr error) {
-  DCHECK(self);
-  self->error.reset();
-  if (error != nullptr)
-    self->error.emplace(*error);
-}
-void Cronet_RequestFinishedInfo_error_move(Cronet_RequestFinishedInfoPtr self,
-                                           Cronet_ErrorPtr error) {
-  DCHECK(self);
-  self->error.reset();
-  if (error != nullptr)
-    self->error.emplace(std::move(*error));
-}
-
 // Struct Cronet_RequestFinishedInfo getters.
 Cronet_MetricsPtr Cronet_RequestFinishedInfo_metrics_get(
     const Cronet_RequestFinishedInfoPtr self) {
@@ -1290,19 +1258,3 @@
   DCHECK(self);
   return self->finished_reason;
 }
-
-Cronet_UrlResponseInfoPtr Cronet_RequestFinishedInfo_response_info_get(
-    const Cronet_RequestFinishedInfoPtr self) {
-  DCHECK(self);
-  if (self->response_info == base::nullopt)
-    return nullptr;
-  return &self->response_info.value();
-}
-
-Cronet_ErrorPtr Cronet_RequestFinishedInfo_error_get(
-    const Cronet_RequestFinishedInfoPtr self) {
-  DCHECK(self);
-  if (self->error == base::nullopt)
-    return nullptr;
-  return &self->error.value();
-}
diff --git a/components/cronet/native/generated/cronet.idl_impl_struct.h b/components/cronet/native/generated/cronet.idl_impl_struct.h
index 09264b0..3034fdd 100644
--- a/components/cronet/native/generated/cronet.idl_impl_struct.h
+++ b/components/cronet/native/generated/cronet.idl_impl_struct.h
@@ -211,8 +211,6 @@
   std::vector<Cronet_RawDataPtr> annotations;
   Cronet_RequestFinishedInfo_FINISHED_REASON finished_reason =
       Cronet_RequestFinishedInfo_FINISHED_REASON_SUCCEEDED;
-  base::Optional<Cronet_UrlResponseInfo> response_info;
-  base::Optional<Cronet_Error> error;
 
  private:
   DISALLOW_ASSIGN(Cronet_RequestFinishedInfo);
diff --git a/components/cronet/native/generated/cronet.idl_impl_struct_unittest.cc b/components/cronet/native/generated/cronet.idl_impl_struct_unittest.cc
index e2d6f1a..7b73c91 100644
--- a/components/cronet/native/generated/cronet.idl_impl_struct_unittest.cc
+++ b/components/cronet/native/generated/cronet.idl_impl_struct_unittest.cc
@@ -503,35 +503,6 @@
       second, Cronet_RequestFinishedInfo_finished_reason_get(first));
   EXPECT_EQ(Cronet_RequestFinishedInfo_finished_reason_get(first),
             Cronet_RequestFinishedInfo_finished_reason_get(second));
-  Cronet_UrlResponseInfoPtr test_response_info =
-      Cronet_UrlResponseInfo_Create();
-  EXPECT_EQ(Cronet_RequestFinishedInfo_response_info_get(first), nullptr);
-
-  Cronet_RequestFinishedInfo_response_info_set(first, test_response_info);
-  EXPECT_NE(Cronet_RequestFinishedInfo_response_info_get(first), nullptr);
-  Cronet_RequestFinishedInfo_response_info_set(first, nullptr);
-  EXPECT_EQ(Cronet_RequestFinishedInfo_response_info_get(first), nullptr);
-
-  Cronet_RequestFinishedInfo_response_info_move(first, test_response_info);
-  EXPECT_NE(Cronet_RequestFinishedInfo_response_info_get(first), nullptr);
-  Cronet_RequestFinishedInfo_response_info_move(first, nullptr);
-  EXPECT_EQ(Cronet_RequestFinishedInfo_response_info_get(first), nullptr);
-
-  Cronet_UrlResponseInfo_Destroy(test_response_info);
-  Cronet_ErrorPtr test_error = Cronet_Error_Create();
-  EXPECT_EQ(Cronet_RequestFinishedInfo_error_get(first), nullptr);
-
-  Cronet_RequestFinishedInfo_error_set(first, test_error);
-  EXPECT_NE(Cronet_RequestFinishedInfo_error_get(first), nullptr);
-  Cronet_RequestFinishedInfo_error_set(first, nullptr);
-  EXPECT_EQ(Cronet_RequestFinishedInfo_error_get(first), nullptr);
-
-  Cronet_RequestFinishedInfo_error_move(first, test_error);
-  EXPECT_NE(Cronet_RequestFinishedInfo_error_get(first), nullptr);
-  Cronet_RequestFinishedInfo_error_move(first, nullptr);
-  EXPECT_EQ(Cronet_RequestFinishedInfo_error_get(first), nullptr);
-
-  Cronet_Error_Destroy(test_error);
   Cronet_RequestFinishedInfo_Destroy(first);
   Cronet_RequestFinishedInfo_Destroy(second);
 }
diff --git a/components/cronet/native/test/url_request_test.cc b/components/cronet/native/test/url_request_test.cc
index ee4eccc..b0175cd 100644
--- a/components/cronet/native/test/url_request_test.cc
+++ b/components/cronet/native/test/url_request_test.cc
@@ -108,7 +108,9 @@
 // App implementation of Cronet_RequestFinishedInfoListener methods.
 static void TestRequestInfoListener_OnRequestFinished(
     Cronet_RequestFinishedInfoListenerPtr self,
-    Cronet_RequestFinishedInfoPtr request_info) {
+    Cronet_RequestFinishedInfoPtr request_info,
+    Cronet_UrlResponseInfoPtr,
+    Cronet_ErrorPtr) {
   CHECK(self);
 }