From 77569373deed80a50d4787cd594f40953d1596a0 Mon Sep 17 00:00:00 2001 From: eugenebut Date: Thu, 23 Feb 2017 11:54:34 -0800 Subject: [PATCH] Cherry-picked remaining parts of 'Fixed title updating for back forward navigation.' Cherry-picked patchset #1 and #2 from cl/2691693002 BUG=688047, 677356 Review-Url: https://codereview.chromium.org/2685803002 NOTRY=true NOPRESUBMIT=true Review-Url: https://codereview.chromium.org/2711123004 Cr-Commit-Position: refs/branch-heads/2987@{#666} Cr-Branched-From: ad51088c0e8776e8dcd963dbe752c4035ba6dab6-refs/heads/master@{#444943} --- ios/web/web_state/ui/crw_web_controller.mm | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 deletions(-) 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]]; } }