Skip to content

Commit

Permalink
[ios] Move loadData and loadHTML out of CRWWebController
Browse files Browse the repository at this point in the history
Bug: 956512
Change-Id: I796f2dd881bda3d67c726adee407dddfde03cf7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1643193
Reviewed-by: Yi Su <mrsuyi@chromium.org>
Reviewed-by: Mark Cogan <marq@chromium.org>
Commit-Queue: David Jean <djean@chromium.org>
Cr-Commit-Position: refs/heads/master@{#670838}
  • Loading branch information
David Jean authored and Commit Bot committed Jun 20, 2019
1 parent 2b6bc5a commit 1a5be02
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 81 deletions.
87 changes: 6 additions & 81 deletions ios/web/web_state/ui/crw_web_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -959,96 +959,21 @@ - (void)loadCurrentURLIfNecessary {
- (void)loadData:(NSData*)data
MIMEType:(NSString*)MIMEType
forURL:(const GURL&)URL {
[self stopLoading];
web::NavigationItemImpl* item =
self.navigationManagerImpl->GetLastCommittedItemImpl();
auto navigationContext = web::NavigationContextImpl::CreateNavigationContext(
self.webStateImpl, URL,
/*has_user_gesture=*/true, item->GetTransitionType(),
/*is_renderer_initiated=*/false);
self.navigationHandler.navigationState = web::WKNavigationState::REQUESTED;
navigationContext->SetNavigationItemUniqueID(item->GetUniqueID());

item->SetNavigationInitiationType(
web::NavigationInitiationType::BROWSER_INITIATED);
// The error_retry_state_machine may still be in the
// |kDisplayingWebErrorForFailedNavigation| from the navigation that is being
// replaced. As the navigation is now successful, the error can be cleared.
item->error_retry_state_machine().SetNoNavigationError();
// The load data call will replace the current navigation and the webView URL
// of the navigation will be replaced by |URL|. Set the URL of the
// navigationItem to keep them synced.
// Note: it is possible that the URL in item already match |url|. But item can
// also contain a placeholder URL intended to be replaced.
item->SetURL(URL);
navigationContext->SetMimeType(MIMEType);
if (item->GetUserAgentType() == web::UserAgentType::NONE &&
URLNeedsUserAgentType(URL)) {
item->SetUserAgentType(web::UserAgentType::MOBILE);
}

WKNavigation* navigation =
[self.webView loadData:data
MIMEType:MIMEType
characterEncodingName:base::SysUTF8ToNSString(base::kCodepageUTF8)
baseURL:net::NSURLWithGURL(URL)];

[self.navigationHandler.navigationStates
setContext:std::move(navigationContext)
forNavigation:navigation];
[self.navigationHandler.navigationStates
setState:web::WKNavigationState::REQUESTED
forNavigation:navigation];
[_requestController loadData:data
webView:self.webView
MIMEType:MIMEType
forURL:URL];
}

// Loads the HTML into the page at the given URL. Only for testing purpose.
- (void)loadHTML:(NSString*)HTML forURL:(const GURL&)URL {
DCHECK(HTML.length);
// Remove the transient content view.
self.webStateImpl->ClearTransientContent();

self.navigationHandler.navigationState = web::WKNavigationState::REQUESTED;

// Web View should not be created for App Specific URLs.
if (!web::GetWebClient()->IsAppSpecificURL(URL)) {
[self ensureWebViewCreated];
DCHECK(self.webView) << "self.webView null while trying to load HTML";
}
WKNavigation* navigation =
[self.webView loadHTMLString:HTML baseURL:net::NSURLWithGURL(URL)];
[self.navigationHandler.navigationStates
setState:web::WKNavigationState::REQUESTED
forNavigation:navigation];
std::unique_ptr<web::NavigationContextImpl> context;
const ui::PageTransition loadHTMLTransition =
ui::PageTransition::PAGE_TRANSITION_TYPED;
if (self.webStateImpl->HasWebUI()) {
// WebUI uses |loadHTML:forURL:| to feed the content to web view. This
// should not be treated as a navigation, but WKNavigationDelegate callbacks
// still expect a valid context.
context = web::NavigationContextImpl::CreateNavigationContext(
self.webStateImpl, URL, /*has_user_gesture=*/true, loadHTMLTransition,
/*is_renderer_initiated=*/false);
context->SetNavigationItemUniqueID(self.currentNavItem->GetUniqueID());
if (web::features::StorePendingItemInContext()) {
// Transfer pending item ownership to NavigationContext.
// NavigationManager owns pending item after navigation is requested and
// until navigation context is created.
context->SetItem(self.navigationManagerImpl->ReleasePendingItem());
}
} else {
context = [self registerLoadRequestForURL:URL
referrer:web::Referrer()
transition:loadHTMLTransition
sameDocumentNavigation:NO
hasUserGesture:YES
rendererInitiated:NO
placeholderNavigation:NO];
}
context->SetLoadingHtmlString(true);
context->SetMimeType(@"text/html");
[self.navigationHandler.navigationStates setContext:std::move(context)
forNavigation:navigation];

[_requestController loadHTML:HTML webView:self.webView forURL:URL];
}

- (void)requirePageReconstruction {
Expand Down
13 changes: 13 additions & 0 deletions ios/web/web_state/ui/crw_web_request_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ class WKBackForwardListItemHolder;
(web::WKBackForwardListItemHolder*)
holder;

// Loads |data| of type |MIMEType| and replaces last committed URL with the
// given |URL|.
- (void)loadData:(NSData*)data
webView:(WKWebView*)webView
MIMEType:(NSString*)MIMEType
forURL:(const GURL&)URL;

// Loads |HTML| into the page and use |URL| to resolve relative URLs within the
// document.
- (void)loadHTML:(NSString*)HTML
webView:(WKWebView*)webView
forURL:(const GURL&)URL;

@end

#endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_REQUEST_CONTROLLER_H_
95 changes: 95 additions & 0 deletions ios/web/web_state/ui/crw_web_request_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <WebKit/WebKit.h>

#include "base/feature_list.h"
#include "base/i18n/i18n_constants.h"
#import "base/ios/block_types.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
Expand All @@ -32,6 +33,7 @@
using web::wk_navigation_util::ExtractUrlFromPlaceholderUrl;
using web::wk_navigation_util::IsPlaceholderUrl;
using web::wk_navigation_util::kReferrerHeaderName;
using web::wk_navigation_util::URLNeedsUserAgentType;

namespace {
// Values for the histogram that counts slow/fast back/forward navigations.
Expand Down Expand Up @@ -256,6 +258,99 @@ - (instancetype)initWithWebState:(web::WebStateImpl*)webState {
}));
}

- (void)loadData:(NSData*)data
webView:(WKWebView*)webView
MIMEType:(NSString*)MIMEType
forURL:(const GURL&)URL {
[_delegate webRequestControllerStopLoading:self];
web::NavigationItemImpl* item =
self.navigationManagerImpl->GetLastCommittedItemImpl();
auto navigationContext = web::NavigationContextImpl::CreateNavigationContext(
self.webState, URL,
/*has_user_gesture=*/true, item->GetTransitionType(),
/*is_renderer_initiated=*/false);
self.navigationHandler.navigationState = web::WKNavigationState::REQUESTED;
navigationContext->SetNavigationItemUniqueID(item->GetUniqueID());

item->SetNavigationInitiationType(
web::NavigationInitiationType::BROWSER_INITIATED);
// The error_retry_state_machine may still be in the
// |kDisplayingWebErrorForFailedNavigation| from the navigation that is being
// replaced. As the navigation is now successful, the error can be cleared.
item->error_retry_state_machine().SetNoNavigationError();
// The load data call will replace the current navigation and the webView URL
// of the navigation will be replaced by |URL|. Set the URL of the
// navigationItem to keep them synced.
// Note: it is possible that the URL in item already match |url|. But item can
// also contain a placeholder URL intended to be replaced.
item->SetURL(URL);
navigationContext->SetMimeType(MIMEType);
if (item->GetUserAgentType() == web::UserAgentType::NONE &&
URLNeedsUserAgentType(URL)) {
item->SetUserAgentType(web::UserAgentType::MOBILE);
}

WKNavigation* navigation =
[webView loadData:data
MIMEType:MIMEType
characterEncodingName:base::SysUTF8ToNSString(base::kCodepageUTF8)
baseURL:net::NSURLWithGURL(URL)];

[self.navigationHandler.navigationStates
setContext:std::move(navigationContext)
forNavigation:navigation];
[self.navigationHandler.navigationStates
setState:web::WKNavigationState::REQUESTED
forNavigation:navigation];
}

- (void)loadHTML:(NSString*)HTML
webView:(WKWebView*)webView
forURL:(const GURL&)URL {
DCHECK(HTML.length);
// Remove the transient content view.
self.webState->ClearTransientContent();

self.navigationHandler.navigationState = web::WKNavigationState::REQUESTED;

WKNavigation* navigation = [webView loadHTMLString:HTML
baseURL:net::NSURLWithGURL(URL)];
[self.navigationHandler.navigationStates
setState:web::WKNavigationState::REQUESTED
forNavigation:navigation];
std::unique_ptr<web::NavigationContextImpl> context;
const ui::PageTransition loadHTMLTransition =
ui::PageTransition::PAGE_TRANSITION_TYPED;
if (self.webState->HasWebUI()) {
// WebUI uses |loadHTML:forURL:| to feed the content to web view. This
// should not be treated as a navigation, but WKNavigationDelegate callbacks
// still expect a valid context.
context = web::NavigationContextImpl::CreateNavigationContext(
self.webState, URL, /*has_user_gesture=*/true, loadHTMLTransition,
/*is_renderer_initiated=*/false);
context->SetNavigationItemUniqueID(self.currentNavItem->GetUniqueID());
if (web::features::StorePendingItemInContext()) {
// Transfer pending item ownership to NavigationContext.
// NavigationManager owns pending item after navigation is requested and
// until navigation context is created.
context->SetItem(self.navigationManagerImpl->ReleasePendingItem());
}
} else {
context = [_delegate webRequestController:self
registerLoadRequestForURL:URL
referrer:web::Referrer()
transition:loadHTMLTransition
sameDocumentNavigation:NO
hasUserGesture:YES
rendererInitiated:NO
placeholderNavigation:NO];
}
context->SetLoadingHtmlString(true);
context->SetMimeType(@"text/html");
[self.navigationHandler.navigationStates setContext:std::move(context)
forNavigation:navigation];
}

// Reports Navigation.IOSWKWebViewSlowFastBackForward UMA. No-op if pending
// navigation is not back forward navigation.
- (void)reportBackForwardNavigationTypeForFastNavigation:(BOOL)isFast {
Expand Down

0 comments on commit 1a5be02

Please sign in to comment.