Commit 1ffb820e authored by edchin's avatar edchin Committed by Commit Bot

[ios] Fix snapshot frame

When using the WKWebView snapshot API, the specified frame should be in
the coordinate space of the web view rather than the BVC.

Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: Idc5f28745085e36891d77a4005e10f4d3bfd66c5
Reviewed-on: https://chromium-review.googlesource.com/c/1257828Reviewed-by: default avataredchin <edchin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: edchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596248}
parent b33058c7
...@@ -95,7 +95,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -95,7 +95,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
@interface SnapshotGenerator ()<CRWWebStateObserver> @interface SnapshotGenerator ()<CRWWebStateObserver>
// Returns the bounds of the snapshot. Will return an empty rectangle if the // Returns the frame of the snapshot. Will return an empty rectangle if the
// WebState is not ready to capture a snapshot. // WebState is not ready to capture a snapshot.
- (CGRect)snapshotFrameVisibleFrameOnly:(BOOL)visibleFrameOnly; - (CGRect)snapshotFrameVisibleFrameOnly:(BOOL)visibleFrameOnly;
...@@ -219,8 +219,13 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -219,8 +219,13 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
- (void)updateWebViewSnapshotWithCompletion:(void (^)(UIImage*))completion { - (void)updateWebViewSnapshotWithCompletion:(void (^)(UIImage*))completion {
DCHECK(_webState); DCHECK(_webState);
CGRect frame = [self snapshotFrameVisibleFrameOnly:YES]; CGRect snapshotFrame = [self snapshotFrameVisibleFrameOnly:YES];
if (CGRectIsEmpty(frame)) { // WebState's |TakeSnapshot()| accepts a |rect| in the web view's coordinate
// space, but |-snapshotFrameVisibleFrameOnly:| returns a frame in the BVC's
// coordinate space.
CGRect webViewSnapshotFrame =
CGRectMake(0, 0, snapshotFrame.size.width, snapshotFrame.size.height);
if (CGRectIsEmpty(webViewSnapshotFrame)) {
if (completion) { if (completion) {
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::UI}, base::PostTaskWithTraits(FROM_HERE, {web::WebThread::UI},
base::BindOnce(^{ base::BindOnce(^{
...@@ -229,11 +234,11 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -229,11 +234,11 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
} }
return; return;
} }
DCHECK(std::isnormal(frame.size.width) && (frame.size.width > 0)) CGSize size = webViewSnapshotFrame.size;
<< ": frame.size.width=" << frame.size.width; DCHECK(std::isnormal(size.width) && (size.width > 0))
DCHECK(std::isnormal(frame.size.height) && (frame.size.height > 0)) << ": webViewSnapshotFrame.size.width=" << size.width;
<< ": frame.size.height=" << frame.size.height; DCHECK(std::isnormal(size.height) && (size.height > 0))
<< ": webViewSnapshotFrame.size.height=" << size.height;
NSArray<SnapshotOverlay*>* overlays = NSArray<SnapshotOverlay*>* overlays =
[_delegate snapshotOverlaysForWebState:_webState]; [_delegate snapshotOverlaysForWebState:_webState];
UIImage* snapshot = UIImage* snapshot =
...@@ -252,7 +257,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -252,7 +257,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
[_delegate willUpdateSnapshotForWebState:_webState]; [_delegate willUpdateSnapshotForWebState:_webState];
__weak SnapshotGenerator* weakSelf = self; __weak SnapshotGenerator* weakSelf = self;
_webState->TakeSnapshot( _webState->TakeSnapshot(
frame, base::BindOnce(^(gfx::Image image) { webViewSnapshotFrame, base::BindOnce(^(gfx::Image image) {
SnapshotGenerator* strongSelf = weakSelf; SnapshotGenerator* strongSelf = weakSelf;
if (!strongSelf) if (!strongSelf)
return; return;
...@@ -260,7 +265,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -260,7 +265,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
if (overlays.count > 0) { if (overlays.count > 0) {
snapshot = [strongSelf snapshotWithOverlays:overlays snapshot = [strongSelf snapshotWithOverlays:overlays
snapshot:snapshot snapshot:snapshot
frame:frame]; frame:snapshotFrame];
} }
[strongSelf.snapshotCache setImage:snapshot [strongSelf.snapshotCache setImage:snapshot
withSessionID:_snapshotSessionId]; withSessionID:_snapshotSessionId];
......
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