Commit c826e1a6 authored by Sebastien Lalancette's avatar Sebastien Lalancette Committed by Commit Bot

[iOS] Prevent PDF Generation of Non-HTML Web States.

WKWebView was returning a blank PDF when requesting a PDF to get
generated for a loaded PDF (during full page screenshot scenario).

Fix: Disable PDF generation on non-HTML Web states.

Bug: 1131566
Change-Id: Ia5ac045b8ec1335266e98ef8cf7eb00a8c62f0d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2431553
Commit-Queue: Sebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810814}
parent 09d835d0
......@@ -877,7 +877,8 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
// Invoke the |completionBlock| with nil rather than a blank PDF for certain
// URLs.
const GURL& URL = self.webState->GetLastCommittedURL();
if (!URL.is_valid() || web::GetWebClient()->IsAppSpecificURL(URL)) {
if (![self contentIsHTML] || !URL.is_valid() ||
web::GetWebClient()->IsAppSpecificURL(URL)) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(nil);
});
......
......@@ -299,6 +299,51 @@ TEST_F(WebStateTest, CreateFullPagePdf_InvalidURLs) {
}
}
// Tests that CreateFullPagePdf invokes completion callback nil when the
// WebState content is not HTML (e.g. a PDF file).
TEST_F(WebStateTest, CreateFullPagePdfWebStatePdfContent) {
CGRect fake_bounds = CGRectMake(0, 0, 100, 100);
UIGraphicsPDFRenderer* pdf_renderer =
[[UIGraphicsPDFRenderer alloc] initWithBounds:fake_bounds];
NSData* pdf_data = [pdf_renderer
PDFDataWithActions:^(UIGraphicsPDFRendererContext* context) {
[context beginPage];
[[UIColor blueColor] setFill];
[context fillRect:fake_bounds];
}];
GURL test_url("https://www.chromium.org/somePDF.pdf");
NavigationManager::WebLoadParams load_params(test_url);
web_state()->GetNavigationManager()->LoadURLWithParams(load_params);
ASSERT_TRUE(base::test::ios::WaitUntilConditionOrTimeout(
base::test::ios::kWaitForPageLoadTimeout, ^bool {
return web_state()->GetLastCommittedURL() == test_url &&
!web_state()->IsLoading();
}));
std::string mime_type = "application/pdf";
web_state()->LoadData(
pdf_data, [NSString stringWithUTF8String:mime_type.c_str()], test_url);
ASSERT_TRUE(base::test::ios::WaitUntilConditionOrTimeout(
base::test::ios::kWaitForPageLoadTimeout, ^bool {
return !web_state()->IsLoading();
}));
__block NSData* callback_data = nil;
__block bool callback_called = false;
web_state()->CreateFullPagePdf(base::BindOnce(^(NSData* pdf_document_data) {
callback_data = [pdf_document_data copy];
callback_called = true;
}));
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^bool {
return callback_called;
}));
ASSERT_FALSE(callback_data);
}
// Tests that message sent from main frame triggers the ScriptCommandCallback
// with |is_main_frame| = true.
TEST_F(WebStateTest, MessageFromMainFrame) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment