Store web_frame id in autofillDriver

Due to crbug.com/892612, the AutofillDriver can live
longer than the webFrame.
As a consequence, it is not possible to store the
frame pointer and the WebFrame must be retrieved using
the frame ID before each use.

Bug: 935003
Change-Id: I5462907be5d7d2f70900c135af16ac3e774fae16
Reviewed-on: https://chromium-review.googlesource.com/c/1488791
Reviewed-by: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: Javier Ernesto Flores Robles <javierrobles@chromium.org>
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636051}
diff --git a/components/autofill/ios/browser/autofill_agent_unittests.mm b/components/autofill/ios/browser/autofill_agent_unittests.mm
index e323c47..72283a3 100644
--- a/components/autofill/ios/browser/autofill_agent_unittests.mm
+++ b/components/autofill/ios/browser/autofill_agent_unittests.mm
@@ -713,9 +713,11 @@
   test_web_state_.RemoveWebFrame(fake_main_frame_->GetFrameId());
 
   // Both frames available, then page loaded.
+  test_web_state_.SetLoading(true);
   auto main_frame_unique =
       std::make_unique<web::FakeWebFrame>("main", true, GURL());
   web::FakeWebFrame* main_frame = main_frame_unique.get();
+  test_web_state_.AddWebFrame(std::move(main_frame_unique));
   autofill::AutofillDriverIOS* main_frame_driver =
       autofill::AutofillDriverIOS::FromWebStateAndWebFrame(&test_web_state_,
                                                            main_frame);
@@ -725,13 +727,11 @@
         EXPECT_TRUE(main_frame_driver->is_processed());
       });
   FakeWebFrameCallback* iframe = iframe_unique.get();
+  test_web_state_.AddWebFrame(std::move(iframe_unique));
   autofill::AutofillDriverIOS* iframe_driver =
       autofill::AutofillDriverIOS::FromWebStateAndWebFrame(&test_web_state_,
                                                            iframe);
   EXPECT_FALSE(iframe_driver->IsInMainFrame());
-  test_web_state_.SetLoading(true);
-  test_web_state_.AddWebFrame(std::move(main_frame_unique));
-  test_web_state_.AddWebFrame(std::move(iframe_unique));
   EXPECT_FALSE(main_frame_driver->is_processed());
   EXPECT_FALSE(iframe_driver->is_processed());
   test_web_state_.SetLoading(false);
diff --git a/components/autofill/ios/browser/autofill_driver_ios.h b/components/autofill/ios/browser/autofill_driver_ios.h
index 653a71b..5e9408d 100644
--- a/components/autofill/ios/browser/autofill_driver_ios.h
+++ b/components/autofill/ios/browser/autofill_driver_ios.h
@@ -80,9 +80,9 @@
   // The WebState with which this object is associated.
   web::WebState* web_state_ = nullptr;
 
-  // The WebState with which this object is associated.
-  // nullptr if frame messaging is disabled.
-  web::WebFrame* web_frame_ = nullptr;
+  // The id of the WebFrame with which this object is associated.
+  // "" if frame messaging is disabled.
+  std::string web_frame_id_;
 
   // AutofillDriverIOSBridge instance that is passed in.
   __unsafe_unretained id<AutofillDriverIOSBridge> bridge_;
diff --git a/components/autofill/ios/browser/autofill_driver_ios.mm b/components/autofill/ios/browser/autofill_driver_ios.mm
index edac340..214164a 100644
--- a/components/autofill/ios/browser/autofill_driver_ios.mm
+++ b/components/autofill/ios/browser/autofill_driver_ios.mm
@@ -12,6 +12,7 @@
 #include "components/autofill/ios/browser/autofill_switches.h"
 #include "ios/web/public/browser_state.h"
 #import "ios/web/public/origin_util.h"
+#import "ios/web/public/web_state/web_frame_util.h"
 #import "ios/web/public/web_state/web_state.h"
 #include "services/network/public/cpp/shared_url_loader_factory.h"
 #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
@@ -63,10 +64,10 @@
     const std::string& app_locale,
     AutofillManager::AutofillDownloadManagerState enable_download_manager)
     : web_state_(web_state),
-      web_frame_(web_frame),
       bridge_(bridge),
       autofill_manager_(this, client, app_locale, enable_download_manager),
       autofill_external_delegate_(&autofill_manager_, this) {
+  web_frame_id_ = web::GetWebFrameId(web_frame);
   autofill_manager_.SetExternalDelegate(&autofill_external_delegate_);
 }
 
@@ -77,7 +78,8 @@
 }
 
 bool AutofillDriverIOS::IsInMainFrame() const {
-  return web_frame_ ? web_frame_->IsMainFrame() : true;
+  web::WebFrame* web_frame = web::GetWebFrameWithId(web_state_, web_frame_id_);
+  return web_frame ? web_frame->IsMainFrame() : true;
 }
 
 net::URLRequestContextGetter* AutofillDriverIOS::GetURLRequestContext() {
@@ -98,7 +100,8 @@
     int query_id,
     RendererFormDataAction action,
     const FormData& data) {
-  [bridge_ fillFormData:data inFrame:web_frame_];
+  web::WebFrame* web_frame = web::GetWebFrameWithId(web_state_, web_frame_id_);
+  [bridge_ fillFormData:data inFrame:web_frame];
 }
 
 void AutofillDriverIOS::PropagateAutofillPredictions(
@@ -108,8 +111,9 @@
 
 void AutofillDriverIOS::SendAutofillTypePredictionsToRenderer(
     const std::vector<FormStructure*>& forms) {
+  web::WebFrame* web_frame = web::GetWebFrameWithId(web_state_, web_frame_id_);
   [bridge_ fillFormDataPredictions:FormStructure::GetFieldTypePredictions(forms)
-                           inFrame:web_frame_];
+                           inFrame:web_frame];
 }
 
 void AutofillDriverIOS::RendererShouldAcceptDataListSuggestion(