diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm index 1f20df56c0032..3e85b26656865 100644 --- a/ios/web/web_state/ui/crw_web_controller.mm +++ b/ios/web/web_state/ui/crw_web_controller.mm @@ -739,8 +739,9 @@ - (void)replaceStateWithPageURL:(const GURL&)pageUrl 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; +// Sets last committed NavigationItem's title to the given |title|, which can +// not be nil. +- (void)setNavigationItemTitle:(NSString*)title; // Returns YES if the current navigation item corresponds to a web page // loaded by a POST request. - (BOOL)isCurrentNavigationItemPOST; @@ -1423,15 +1424,14 @@ - (void)setDocumentURL:(const GURL&)newURL { } } -- (void)updateNavigationItemTitle { - NSString* webViewTitle = [_webView title]; - DCHECK(webViewTitle); +- (void)setNavigationItemTitle:(NSString*)title { + DCHECK(title); auto& navigationManager = _webStateImpl->GetNavigationManagerImpl(); web::NavigationItem* item = navigationManager.GetLastCommittedItem(); if (!item) return; - base::string16 newTitle = base::SysNSStringToUTF16(webViewTitle); + base::string16 newTitle = base::SysNSStringToUTF16(title); if (item->GetTitle() == newTitle) return; @@ -1442,7 +1442,7 @@ - (void)updateNavigationItemTitle { navigationManager.OnNavigationItemChanged(); if ([_delegate respondsToSelector:@selector(webController:titleDidChange:)]) { - [_delegate webController:self titleDidChange:webViewTitle]; + [_delegate webController:self titleDidChange:title]; } } @@ -1870,14 +1870,13 @@ - (void)loadNativeViewWithSuccess:(BOOL)loadSuccess { // Perform post-load-finished updates. [self didFinishWithURL:currentURL loadSuccess:loadSuccess]; - // Inform the embedder the title changed. + NSString* title = [self.nativeController title]; + if (title) + [self setNavigationItemTitle:title]; + + // If the controller handles title change notification, route those to the + // delegate. if ([_delegate respondsToSelector:@selector(webController:titleDidChange:)]) { - NSString* title = [self.nativeController title]; - // If a title is present, notify the delegate. - if (title) - [_delegate webController:self titleDidChange:title]; - // If the controller handles title change notification, route those to the - // delegate. if ([self.nativeController respondsToSelector:@selector(setDelegate:)]) { [self.nativeController setDelegate:self]; } @@ -4982,7 +4981,6 @@ - (void)webView:(WKWebView*)webView // 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) { @@ -5025,6 +5023,10 @@ - (void)webView:(WKWebView*)webView // Attempt to update the HTML5 history state. [self updateHTML5HistoryState]; + // This is the point where pending entry has been committed, and navigation + // item title should be updated. + [self setNavigationItemTitle:[_webView title]]; + // Report cases where SSL cert is missing for a secure connection. if (_documentURL.SchemeIsCryptographic()) { scoped_refptr cert = @@ -5225,7 +5227,7 @@ - (void)webViewTitleDidChange { 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]; + [self setNavigationItemTitle:[_webView title]]; } }