Commit f694b54e authored by Julian Mentasti-Meza's avatar Julian Mentasti-Meza Committed by Commit Bot

[iOS] Added CreateFullPagePDF to WKWebViewUtility.

CreateFullPagePDF generates a single page PDF of the WebView and invokes a callback with the data.

Bug: 1103777
Change-Id: I37683139e7b6bde34a86d96c9a6cce9c3db0f48a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2293159
Commit-Queue: Julian Mentasti-Meza <jmentasti@google.com>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarSebastien Lalancette <seblalancette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790097}
parent 6de16ab5
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#ifndef IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_ #ifndef IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_
#define IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_ #define IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h> #import <WebKit/WebKit.h>
#include "base/callback.h"
namespace web { namespace web {
// Returns true if a SafeBrowsing warning is currently displayed within // Returns true if a SafeBrowsing warning is currently displayed within
// |web_view|. // |web_view|.
...@@ -21,6 +24,11 @@ bool RequiresContentFilterBlockingWorkaround(); ...@@ -21,6 +24,11 @@ bool RequiresContentFilterBlockingWorkaround();
// https://bugs.webkit.org/show_bug.cgi?id=198794 WebKit bug. // https://bugs.webkit.org/show_bug.cgi?id=198794 WebKit bug.
// TODO(crbug.com/973653): Remove this workaround when WebKit bug is fixed. // TODO(crbug.com/973653): Remove this workaround when WebKit bug is fixed.
bool RequiresProvisionalNavigationFailureWorkaround(); bool RequiresProvisionalNavigationFailureWorkaround();
// Generates a PDF of the entire content of a |web_view| and invokes the
// |callback| with the NSData of the PDF.
void CreateFullPagePdf(WKWebView* web_view,
base::OnceCallback<void(NSData*)> callback);
} // namespace web } // namespace web
#endif // IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_ #endif // IOS_WEB_WEB_VIEW_WK_WEB_VIEW_UTIL_H_
...@@ -49,4 +49,39 @@ bool RequiresProvisionalNavigationFailureWorkaround() { ...@@ -49,4 +49,39 @@ bool RequiresProvisionalNavigationFailureWorkaround() {
return true; return true;
return false; return false;
} }
void CreateFullPagePdf(WKWebView* web_view,
base::OnceCallback<void(NSData*)> callback) {
// iOS14 createPDFWithConfiguration returns a PDF of the WebView
// Asynchronously though a |callback| thus this method's signature matches it
// for future insertion.
if (!web_view) {
std::move(callback).Run(nil);
return;
}
UIPrintPageRenderer* print_renderer = [[UIPrintPageRenderer alloc] init];
[print_renderer addPrintFormatter:[web_view viewPrintFormatter]
startingAtPageAtIndex:0];
// Set the size of a page to be the size of the WebPage.
CGRect entire_web_page =
CGRectMake(0, 0, web_view.scrollView.contentSize.width,
web_view.scrollView.contentSize.height);
[print_renderer setValue:[NSValue valueWithCGRect:entire_web_page]
forKey:@"paperRect"];
[print_renderer setValue:[NSValue valueWithCGRect:entire_web_page]
forKey:@"printableRect"];
UIGraphicsPDFRenderer* pdf_renderer =
[[UIGraphicsPDFRenderer alloc] initWithBounds:entire_web_page];
NSData* pdf_document_data = [pdf_renderer
PDFDataWithActions:^(UIGraphicsPDFRendererContext* context) {
[context beginPage];
[print_renderer drawPageAtIndex:0 inRect:entire_web_page];
}];
std::move(callback).Run(pdf_document_data);
}
} // namespace web } // namespace web
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#import "ios/web/web_view/wk_web_view_util.h" #import "ios/web/web_view/wk_web_view_util.h"
#include "base/bind.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -30,6 +31,8 @@ ...@@ -30,6 +31,8 @@
class WKWebViewUtilTest : public PlatformTest {}; class WKWebViewUtilTest : public PlatformTest {};
namespace web {
// Tests that IsSafeBrowsingWarningDisplayedInWebView returns true when safe // Tests that IsSafeBrowsingWarningDisplayedInWebView returns true when safe
// browsing warning is displayed in WKWebView. // browsing warning is displayed in WKWebView.
TEST_F(WKWebViewUtilTest, TestIsSafeBrowsingWarningDisplayedInWebView) { TEST_F(WKWebViewUtilTest, TestIsSafeBrowsingWarningDisplayedInWebView) {
...@@ -55,3 +58,37 @@ TEST_F(WKWebViewUtilTest, TestIsSafeBrowsingWarningDisplayedInWebView) { ...@@ -55,3 +58,37 @@ TEST_F(WKWebViewUtilTest, TestIsSafeBrowsingWarningDisplayedInWebView) {
EXPECT_TRUE(web::IsSafeBrowsingWarningDisplayedInWebView(web_view)); EXPECT_TRUE(web::IsSafeBrowsingWarningDisplayedInWebView(web_view));
} }
} }
// Tests that CreateFullPagePDF invokes the callback with NSData.
TEST_F(WKWebViewUtilTest, EnsureCallbackIsCalled) {
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
WKWebView* web_view = [[WKWebView alloc] initWithFrame:CGRectZero
configuration:config];
__block bool callback_called = false;
__block NSData* callback_data = nil;
CreateFullPagePdf(web_view, base::Bind(^(NSData* data) {
callback_called = true;
callback_data = [data copy];
}));
ASSERT_TRUE(callback_called);
EXPECT_TRUE(callback_data);
}
// Tests that CreateFullPagePDF invokes the callback with NULL if
// its given a NULL WKWebView.
TEST_F(WKWebViewUtilTest, NULLWebView) {
__block bool callback_called = false;
__block NSData* callback_data = nil;
CreateFullPagePdf(nil, base::Bind(^(NSData* data) {
callback_called = true;
callback_data = [data copy];
}));
ASSERT_TRUE(callback_called);
EXPECT_FALSE(callback_data);
}
}
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