Index: ios/web/web_state/ui/crw_web_controller.mm |
diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm |
index 5dde5d99291db2cfe51bb4cfe8c6cf642a1b5888..e219a8603c69f06d6e5c7c2220508fb9c866479a 100644 |
--- a/ios/web/web_state/ui/crw_web_controller.mm |
+++ b/ios/web/web_state/ui/crw_web_controller.mm |
@@ -718,6 +718,8 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*); |
stateObject:(NSString*)stateObject; |
// Sets _documentURL to newURL, and updates any relevant state information. |
- (void)setDocumentURL:(const GURL&)newURL; |
+// Sets WKWebView's title to the last committed navigation item. |
+- (void)updateNavigationItemTitle; |
// Returns YES if the current navigation item corresponds to a web page |
// loaded by a POST request. |
- (BOOL)isCurrentNavigationItemPOST; |
@@ -1379,6 +1381,29 @@ const NSTimeInterval kSnapshotOverlayTransition = 0.5; |
} |
} |
+- (void)updateNavigationItemTitle { |
+ NSString* webViewTitle = [_webView title]; |
+ DCHECK(webViewTitle); |
+ auto& navigationManager = _webStateImpl->GetNavigationManagerImpl(); |
+ web::NavigationItem* item = navigationManager.GetLastCommittedItem(); |
+ if (!item) |
+ return; |
+ |
+ base::string16 newTitle = base::SysNSStringToUTF16(webViewTitle); |
+ if (item->GetTitle() == newTitle) |
+ return; |
+ |
+ item->SetTitle(newTitle); |
+ // TODO(crbug.com/546218): See if this can be removed; it's not clear that |
+ // other platforms send this (tab sync triggers need to be compared against |
+ // upstream). |
+ navigationManager.OnNavigationItemChanged(); |
+ |
+ if ([_delegate respondsToSelector:@selector(webController:titleDidChange:)]) { |
+ [_delegate webController:self titleDidChange:webViewTitle]; |
+ } |
+} |
+ |
- (BOOL)isCurrentNavigationItemPOST { |
// |_pendingNavigationInfo| will be nil if the decidePolicy* delegate methods |
// were not called. |
@@ -4754,9 +4779,10 @@ const NSTimeInterval kSnapshotOverlayTransition = 0.5; |
DCHECK_EQ(_webView, webView); |
_certVerificationErrors->Clear(); |
- // This is the point where the document's URL has actually changed, and |
- // pending navigation information should be applied to state information. |
+ // This is the point where the document's URL and title have actually changed, |
+ // and pending navigation information should be applied to state information. |
[self setDocumentURL:net::GURLWithNSURL([_webView URL])]; |
+ [self updateNavigationItemTitle]; |
if (!_lastRegisteredRequestURL.is_valid() && |
_documentURL != _lastRegisteredRequestURL) { |
@@ -4994,10 +5020,12 @@ const NSTimeInterval kSnapshotOverlayTransition = 0.5; |
return; |
} |
- if ([self.delegate |
- respondsToSelector:@selector(webController:titleDidChange:)]) { |
- DCHECK([_webView title]); |
- [self.delegate webController:self titleDidChange:[_webView title]]; |
+ bool hasPendingNavigation = web::WKNavigationState::COMMITTED <= |
+ [_navigationStates lastAddedNavigationState]; |
+ if (hasPendingNavigation) { |
+ // Do not update the title if there is a navigation in progress because |
+ // there is no way to tell if KVO change fired for new or previous page. |
+ [self updateNavigationItemTitle]; |
} |
} |