Reland: Do not call 'navigation committed' callback if there was no pending item to commit.

GetPendingItem can return null inside webView:didCommitNavigation:
This is due to crbug.com/925304, which allows to discard pending items
by another navigations prematurely.

If pending item is null, it means that Navigation Manager will not commit
that item and WebStateObservers can crash because they expect to have at
least one committed item.

This CL leaves navigation as "uncommitted" if there was no pending item
inside webView:didCommitNavigation:

Original CL: https://chromium-review.googlesource.com/c/1448683

TBR=eugenebut@google.com

(cherry picked from commit 621ddc377ebc96f7ecd524a39956b911fd70d6f1)

Bug: 925304
Change-Id: I7852da807d183e87d1fd029d75ed63cde2287d7b
Reviewed-on: https://chromium-review.googlesource.com/c/1450300
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: Justin Cohen <justincohen@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#628537}
Reviewed-on: https://chromium-review.googlesource.com/c/1451219
Reviewed-by: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#148}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm
index 7a816e5..490456f 100644
--- a/ios/web/web_state/ui/crw_web_controller.mm
+++ b/ios/web/web_state/ui/crw_web_controller.mm
@@ -4944,7 +4944,24 @@
   // |context| will be nil if this navigation has been already committed and
   // finished.
   if (context) {
-    context->SetHasCommitted(true);
+    web::NavigationManager* navigationManager =
+        _webStateImpl->GetNavigationManager();
+    if ((navigationManager->GetPendingItem()) ||
+        (context->IsLoadingHtmlString()) ||
+        (!web::GetWebClient()->IsSlimNavigationManagerEnabled() &&
+         ui::PageTransitionCoreTypeIs(context->GetPageTransition(),
+                                      ui::PAGE_TRANSITION_RELOAD) &&
+         navigationManager->GetLastCommittedItem())) {
+      // Commit navigation if at least one of these is true:
+      //  - Navigation has pending item (this should always be true, but
+      //    pending item may not exist due to crbug.com/925304).
+      //  - Navigation is loadHTMLString:baseURL: navigation, which does not
+      //    create a pending item, but modifies committed item instead.
+      //  - Transition type is reload with Legacy Navigation Manager (Legacy
+      //    Navigation Manager does not create pending item for reload due to
+      //    crbug.com/676129)
+      context->SetHasCommitted(true);
+    }
     context->SetResponseHeaders(_webStateImpl->GetHttpResponseHeaders());
   }