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

[iOS] Maintain User's Scroll Position in Full Page Screenshots

Only doable on iOS 14+ since on iOS 13 we're using a different "page
size", resulting in a page with different width (and height).

Bug: 1131558
Change-Id: Idf3f10d77787f2513ed37b258a69901e6891cb30
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2426963
Commit-Queue: Sebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810596}
parent 2c4511c9
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#import "ios/chrome/browser/main/browser.h" #import "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/ui/main/browser_interface_provider.h" #import "ios/chrome/browser/ui/main/browser_interface_provider.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h" #import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/web/public/ui/crw_web_view_proxy.h"
#import "ios/web/public/ui/crw_web_view_scroll_view_proxy.h"
#import "ios/web/public/web_state.h" #import "ios/web/public/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -52,9 +54,23 @@ ...@@ -52,9 +54,23 @@
return; return;
} }
CGRect webViewFrame = CGRectZero;
if (@available(iOS 14, *)) {
// We can determine the viewed frame for PDFs generated on iOS 14+. Use it
// to maintain scroll position in the screenshot editing tool.
id<CRWWebViewProxy> webProxy = webState->GetWebViewProxy();
CRWWebViewScrollViewProxy* scrollProxy = webProxy.scrollViewProxy;
CGPoint contentOffset = scrollProxy.contentOffset;
CGSize contentSize = scrollProxy.contentSize;
webViewFrame = scrollProxy.frame;
webViewFrame.origin.x = contentOffset.x;
webViewFrame.origin.y =
contentSize.height - webViewFrame.size.height - contentOffset.y;
}
base::OnceCallback<void(NSData*)> callback = base::OnceCallback<void(NSData*)> callback =
base::BindOnce(^(NSData* pdfDoumentData) { base::BindOnce(^(NSData* pdfDoumentData) {
completionHandler(pdfDoumentData, 0, CGRectZero); completionHandler(pdfDoumentData, 0, webViewFrame);
}); });
webState->CreateFullPagePdf(std::move(callback)); webState->CreateFullPagePdf(std::move(callback));
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#import "ios/chrome/browser/web_state_list/web_state_opener.h" #import "ios/chrome/browser/web_state_list/web_state_opener.h"
#import "ios/web/public/test/fakes/test_web_state.h" #import "ios/web/public/test/fakes/test_web_state.h"
#import "ios/web/public/test/web_task_environment.h" #import "ios/web/public/test/web_task_environment.h"
#import "ios/web/public/ui/crw_web_view_proxy.h"
#import "ios/web/public/ui/crw_web_view_scroll_view_proxy.h"
#import "testing/platform_test.h" #import "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h" #import "third_party/ocmock/OCMock/OCMock.h"
...@@ -51,6 +53,31 @@ TEST_F(ScreenshotDelegateTest, ScreenshotService) { ...@@ -51,6 +53,31 @@ TEST_F(ScreenshotDelegateTest, ScreenshotService) {
auto web_state = std::make_unique<web::TestWebState>(); auto web_state = std::make_unique<web::TestWebState>();
TestBrowser browser; TestBrowser browser;
CRWWebViewScrollViewProxy* scroll_view_proxy =
[[CRWWebViewScrollViewProxy alloc] init];
UIScrollView* scroll_view = [[UIScrollView alloc] init];
[scroll_view_proxy setScrollView:scroll_view];
id web_view_proxy_mock = OCMProtocolMock(@protocol(CRWWebViewProxy));
[[[web_view_proxy_mock stub] andReturn:scroll_view_proxy] scrollViewProxy];
web_state->SetWebViewProxy(web_view_proxy_mock);
// Fake scroll_view contentOffset, contentSize and frame.
CGPoint content_offset = CGPointMake(10.0, 15.0);
CGSize content_size = CGSizeMake(425, 4000);
CGRect frame = CGRectMake(0, 0, 375, 812);
scroll_view.contentOffset = content_offset;
scroll_view.contentSize = content_size;
scroll_view.frame = frame;
CGRect expected_rect_in_page = CGRectZero;
if (@available(iOS 14, *)) {
expected_rect_in_page =
CGRectMake(content_offset.x,
content_size.height - frame.size.height - content_offset.y,
frame.size.width, frame.size.height);
}
// Insert the web_state into the Browser. // Insert the web_state into the Browser.
int insertion_index = browser.GetWebStateList()->InsertWebState( int insertion_index = browser.GetWebStateList()->InsertWebState(
WebStateList::kInvalidIndex, std::move(web_state), WebStateList::kInvalidIndex, std::move(web_state),
...@@ -65,16 +92,18 @@ TEST_F(ScreenshotDelegateTest, ScreenshotService) { ...@@ -65,16 +92,18 @@ TEST_F(ScreenshotDelegateTest, ScreenshotService) {
createScreenshotDelegate(); createScreenshotDelegate();
__block bool callback_ran = false; __block int nbCalls = 0;
[screenshotDelegate_ screenshotService:screenshot_service_ [screenshotDelegate_ screenshotService:screenshot_service_
generatePDFRepresentationWithCompletion:^(NSData* PDFData, generatePDFRepresentationWithCompletion:^(NSData* PDFData,
NSInteger indexOfCurrentPage, NSInteger indexOfCurrentPage,
CGRect rectInCurrentPage) { CGRect rectInCurrentPage) {
EXPECT_TRUE(PDFData); EXPECT_TRUE(PDFData);
callback_ran = true; EXPECT_TRUE(
CGRectEqualToRect(expected_rect_in_page, rectInCurrentPage));
++nbCalls;
}]; }];
EXPECT_TRUE(callback_ran); EXPECT_EQ(1, nbCalls);
} }
} }
......
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