Revert of [ios] Implemented LoadHtml w/o using data:// URLs. (patchset #4 id:60001 of https://codereview.chromium.org/2295053003/ )
Reason for revert:
Breaking iOS build.
Original issue's description:
> [ios] Implemented LoadHtml w/o using data:// URLs.
>
> This allows loading HTML and setting a specific URL (last part is needed
> by suggestions tests, which currently use private API to set the URL).
>
> BUG=619687
>
> Committed: https://crrev.com/7d93683d73e6bb131651275c752a0792f2733410
> Cr-Commit-Position: refs/heads/master@{#416151}
TBR=jyquinn@chromium.org,eugenebut@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=619687
Review-Url: https://codereview.chromium.org/2306053002
Cr-Commit-Position: refs/heads/master@{#416246}
diff --git a/ios/web/public/test/web_test_with_web_state.h b/ios/web/public/test/web_test_with_web_state.h
index 1f01f18..8c26f2b 100644
--- a/ios/web/public/test/web_test_with_web_state.h
+++ b/ios/web/public/test/web_test_with_web_state.h
@@ -30,12 +30,15 @@
void WillProcessTask(const base::PendingTask& pending_task) override;
void DidProcessTask(const base::PendingTask& pending_task) override;
- // Loads the specified HTML content with URL into the WebState.
- void LoadHtml(NSString* html, const GURL& url);
- // Loads the specified HTML content into the WebState, using test url name.
+ // Loads the specified HTML content into the WebController via public APIs.
void LoadHtml(NSString* html);
- // Loads the specified HTML content into the WebState, using test url name.
+ // Loads the specified HTML content into the WebController via public APIs.
void LoadHtml(const std::string& html);
+ // Loads |url| into the WebController via public APIs.
+ // Note if anyone uses this to load web pages from the live internet, the
+ // tests can be flaky / dependent on content and behavior beyond our control.
+ // Use this only when it's impossible to test with static HTML using LoadHtml.
+ void LoadURL(const GURL& url);
// Blocks until both known NSRunLoop-based and known message-loop-based
// background tasks have completed
void WaitForBackgroundTasks();
diff --git a/ios/web/public/test/web_test_with_web_state.mm b/ios/web/public/test/web_test_with_web_state.mm
index c5e9aae..d16f409 100644
--- a/ios/web/public/test/web_test_with_web_state.mm
+++ b/ios/web/public/test/web_test_with_web_state.mm
@@ -73,43 +73,48 @@
processed_a_task_ = true;
}
-void WebTestWithWebState::LoadHtml(NSString* html, const GURL& url) {
- ASSERT_FALSE(web_state()->IsLoading());
-
- CRWWebController* web_controller = GetWebController(web_state());
- [web_controller loadHTML:html forURL:url];
-
- // Wait until the navigation is committed to update MIME type.
- ASSERT_EQ(LOAD_REQUESTED, web_controller.loadPhase);
- base::TimeDelta spin_delay = base::TimeDelta::FromMilliseconds(10);
- while (web_controller.loadPhase != PAGE_LOADING)
- base::test::ios::SpinRunLoopWithMaxDelay(spin_delay);
-
- // loadHTML:forURL: does not notify web view delegate about received response,
- // so web controller does not get a chance to properly update MIME type and it
- // should be set manually after navigation is committed but before WebState
- // signal load completion and clients will start checking if MIME type is in
- // fact HTML.
- [web_controller webStateImpl]->SetContentsMimeType("text/html");
-
- // Wait until the page is loaded.
- ASSERT_EQ(PAGE_LOADING, web_controller.loadPhase);
- while (web_controller.loadPhase != PAGE_LOADED)
- base::test::ios::SpinRunLoopWithMaxDelay(spin_delay);
-
- // Wait until scripts execution becomes possible.
- base::test::ios::WaitUntilCondition(^bool {
- return [ExecuteJavaScript(@"0;") isEqual:@0];
- });
-}
-
void WebTestWithWebState::LoadHtml(NSString* html) {
- GURL url("https://chromium.test/");
- LoadHtml(html, url);
+ LoadHtml([html UTF8String]);
}
void WebTestWithWebState::LoadHtml(const std::string& html) {
- LoadHtml(base::SysUTF8ToNSString(html));
+ NSString* load_check = CreateLoadCheck();
+ std::string marked_html = html + [load_check UTF8String];
+ std::string encoded_html;
+ base::Base64Encode(marked_html, &encoded_html);
+ GURL url("data:text/html;charset=utf8;base64," + encoded_html);
+ LoadURL(url);
+
+ if (ResetPageIfNavigationStalled(load_check)) {
+ LoadHtml(html);
+ }
+}
+
+void WebTestWithWebState::LoadURL(const GURL& url) {
+ // First step is to ensure that the web controller has finished any previous
+ // page loads so the new load is not confused.
+ while ([GetWebController(web_state()) loadPhase] != PAGE_LOADED)
+ WaitForBackgroundTasks();
+ id originalMockDelegate =
+ [OCMockObject niceMockForProtocol:@protocol(CRWWebDelegate)];
+ id mockDelegate =
+ [[WebDelegateMock alloc] initWithRepresentedObject:originalMockDelegate];
+ id existingDelegate = GetWebController(web_state()).delegate;
+ GetWebController(web_state()).delegate = mockDelegate;
+
+ web::NavigationManagerImpl& navManager =
+ [GetWebController(web_state()) webStateImpl]->GetNavigationManagerImpl();
+ navManager.InitializeSession(@"name", nil, NO, 0);
+ [navManager.GetSessionController() addPendingEntry:url
+ referrer:web::Referrer()
+ transition:ui::PAGE_TRANSITION_TYPED
+ rendererInitiated:NO];
+
+ [GetWebController(web_state()) loadCurrentURL];
+ while ([GetWebController(web_state()) loadPhase] != PAGE_LOADED)
+ WaitForBackgroundTasks();
+ GetWebController(web_state()).delegate = existingDelegate;
+ [web_state()->GetView() layoutIfNeeded];
}
void WebTestWithWebState::WaitForBackgroundTasks() {
@@ -172,6 +177,18 @@
return web_state_.get();
}
+bool WebTestWithWebState::ResetPageIfNavigationStalled(NSString* load_check) {
+ id inner_html = ExecuteJavaScript(
+ @"(document && document.body && document.body.innerHTML) || 'undefined'");
+ if (![inner_html rangeOfString:load_check].length) {
+ web_state_->SetWebUsageEnabled(false);
+ web_state_->SetWebUsageEnabled(true);
+ [GetWebController(web_state()) triggerPendingLoad];
+ return true;
+ }
+ return false;
+}
+
NSString* WebTestWithWebState::CreateLoadCheck() {
return [NSString stringWithFormat:@"<p style=\"display: none;\">%d</p>",
s_html_load_count++];
diff --git a/ios/web/web_state/js/core_js_unittest.mm b/ios/web/web_state/js/core_js_unittest.mm
index 7f4d7c7..c657e916 100644
--- a/ios/web/web_state/js/core_js_unittest.mm
+++ b/ios/web/web_state/js/core_js_unittest.mm
@@ -240,7 +240,7 @@
LoadHtml(base::StringPrintf(image, "http://destination"));
id result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)");
NSDictionary* expected_result = @{
- @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()],
+ @"src" : @"foo",
@"referrerPolicy" : @"default",
@"href" : @"http://destination/",
};
@@ -250,7 +250,7 @@
LoadHtml(base::StringPrintf(image, "javascript:console.log('whatever')"));
result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)");
expected_result = @{
- @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()],
+ @"src" : @"foo",
@"referrerPolicy" : @"default",
@"href" : @"javascript:console.log(",
};
@@ -268,7 +268,7 @@
LoadHtml(base::StringPrintf(image, javascript.c_str()));
result = ExecuteJavaScript(@"__gCrWeb['getElementFromPoint'](200, 200)");
expected_result = @{
- @"src" : [NSString stringWithFormat:@"%sfoo", BaseUrl().c_str()],
+ @"src" : @"foo",
@"referrerPolicy" : @"default",
};
// Make sure the returned JSON does not have an 'href' key.
diff --git a/ios/web/web_state/ui/crw_web_controller.h b/ios/web/web_state/ui/crw_web_controller.h
index a926691..46b9aef0 100644
--- a/ios/web/web_state/ui/crw_web_controller.h
+++ b/ios/web/web_state/ui/crw_web_controller.h
@@ -314,9 +314,6 @@
// Returns the header height.
- (CGFloat)headerHeight;
-// Loads the HTML into the page at the given URL.
-- (void)loadHTML:(NSString*)HTML forURL:(const GURL&)URL;
-
// Caches request POST data in the given session entry. Exposed for testing.
- (void)cachePOSTDataForRequest:(NSURLRequest*)request
inSessionEntry:(CRWSessionEntry*)currentSessionEntry;
diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm
index e7fe4f3c..09602242 100644
--- a/ios/web/web_state/ui/crw_web_controller.mm
+++ b/ios/web/web_state/ui/crw_web_controller.mm
@@ -4767,9 +4767,8 @@
// Remove the transient content view.
[self clearTransientContentView];
+ DLOG_IF(WARNING, !_webView) << "_webView null while trying to load HTML";
_loadPhase = web::LOAD_REQUESTED;
- [self ensureWebViewCreated];
- DCHECK(_webView) << "_webView null while trying to load HTML";
[_webView loadHTMLString:HTML baseURL:net::NSURLWithGURL(URL)];
}
@@ -4976,6 +4975,13 @@
}
}
+ if (!allowLoad && action.targetFrame.isMainFrame) {
+ // WKWebView will stop this navigation without calling any further
+ // callbacks, so change load phase to loaded now.
+ _loadPhase = web::PAGE_LOADED;
+ _webStateImpl->SetIsLoading(false);
+ }
+
decisionHandler(allowLoad ? WKNavigationActionPolicyAllow
: WKNavigationActionPolicyCancel);
}
diff --git a/ios/web/web_state/ui/crw_web_controller_unittest.mm b/ios/web/web_state/ui/crw_web_controller_unittest.mm
index e3f2780..50832d9 100644
--- a/ios/web/web_state/ui/crw_web_controller_unittest.mm
+++ b/ios/web/web_state/ui/crw_web_controller_unittest.mm
@@ -196,12 +196,7 @@
- (web::BlockedPopupInfo*)blockedPopupInfo {
return _blockedPopupInfo.get();
}
-- (BOOL)webController:(CRWWebController*)webController
- shouldOpenURL:(const GURL&)URL
- mainDocumentURL:(const GURL&)mainDocumentURL
- linkClicked:(BOOL)linkClicked {
- return YES;
-}
+
@end
@interface CountingObserver : NSObject<CRWWebControllerObserver>