Web Automation: fail gracefully when a screenshot cannot be encoded as base64
authorbburg@apple.com <bburg@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 10 Feb 2017 17:25:36 +0000 (17:25 +0000)
committerbburg@apple.com <bburg@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 10 Feb 2017 17:25:36 +0000 (17:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=168095
<rdar://problem/30297427>

Reviewed by Joseph Pecoraro.

Convert platformGetBase64EncodedPNGData to return a std::optional<String>.
Return nullopt if we can't create a screenshot or convert it to base64.

* UIProcess/Automation/WebAutomationSession.cpp:
(WebKit::WebAutomationSession::didTakeScreenshot):
(WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):
* UIProcess/Automation/WebAutomationSession.h:
* UIProcess/Automation/cocoa/WebAutomationSessionCocoa.mm:
(WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@212115 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/Automation/WebAutomationSession.cpp
Source/WebKit2/UIProcess/Automation/WebAutomationSession.h
Source/WebKit2/UIProcess/Automation/cocoa/WebAutomationSessionCocoa.mm

index 0946f1c24b7f89969a6ec0158b6b69229706a757..faecde599118c12168c396e9260292005fef8738 100644 (file)
@@ -1,3 +1,21 @@
+2017-02-10  Brian Burg  <bburg@apple.com>
+
+        Web Automation: fail gracefully when a screenshot cannot be encoded as base64
+        https://bugs.webkit.org/show_bug.cgi?id=168095
+        <rdar://problem/30297427>
+
+        Reviewed by Joseph Pecoraro.
+
+        Convert platformGetBase64EncodedPNGData to return a std::optional<String>.
+        Return nullopt if we can't create a screenshot or convert it to base64.
+
+        * UIProcess/Automation/WebAutomationSession.cpp:
+        (WebKit::WebAutomationSession::didTakeScreenshot):
+        (WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):
+        * UIProcess/Automation/WebAutomationSession.h:
+        * UIProcess/Automation/cocoa/WebAutomationSessionCocoa.mm:
+        (WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):
+
 2017-02-09  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         Unreviewed. Fix GTK+ build with threaded compositor disabled.
 2017-02-09  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         Unreviewed. Fix GTK+ build with threaded compositor disabled.
index 5f0314650df5333224c1e601a1da6120bb2facaa..228ea533c2984e24a32c748ddbf0b55b6a72f77a 100644 (file)
@@ -916,13 +916,13 @@ void WebAutomationSession::didTakeScreenshot(uint64_t callbackID, const Shareabl
         return;
     }
 
         return;
     }
 
-    String base64EncodedData = platformGetBase64EncodedPNGData(imageDataHandle);
-    if (base64EncodedData.isEmpty()) {
+    std::optional<String> base64EncodedData = platformGetBase64EncodedPNGData(imageDataHandle);
+    if (!base64EncodedData) {
         callback->sendFailure(STRING_FOR_PREDEFINED_ERROR_NAME(InternalError));
         return;
     }
 
         callback->sendFailure(STRING_FOR_PREDEFINED_ERROR_NAME(InternalError));
         return;
     }
 
-    callback->sendSuccess(base64EncodedData);
+    callback->sendSuccess(base64EncodedData.value());
 }
 
 // Platform-dependent Implementation Stubs.
 }
 
 // Platform-dependent Implementation Stubs.
@@ -942,7 +942,7 @@ void WebAutomationSession::platformSimulateKeySequence(WebPageProxy&, const Stri
 #endif // !PLATFORM(MAC)
 
 #if !PLATFORM(COCOA)
 #endif // !PLATFORM(MAC)
 
 #if !PLATFORM(COCOA)
-String WebAutomationSession::platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&)
+std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&)
 {
     return String();
 }
 {
     return String();
 }
index b31c55bdae96c54195a62151ac79adc046785d66..cd2d0b8af3dad1472b378ee3a3c3223f804f9637 100644 (file)
@@ -167,7 +167,7 @@ private:
     // Simulates key presses to produce the codepoints in a string. One or more code points are delivered atomically at grapheme cluster boundaries.
     void platformSimulateKeySequence(WebPageProxy&, const String&);
     // Get base64 encoded PNG data from a bitmap.
     // Simulates key presses to produce the codepoints in a string. One or more code points are delivered atomically at grapheme cluster boundaries.
     void platformSimulateKeySequence(WebPageProxy&, const String&);
     // Get base64 encoded PNG data from a bitmap.
-    String platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&);
+    std::optional<String> platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&);
 
 #if PLATFORM(MAC)
     void sendSynthesizedEventsToPage(WebPageProxy&, NSArray *eventsToSend);
 
 #if PLATFORM(MAC)
     void sendSynthesizedEventsToPage(WebPageProxy&, NSArray *eventsToSend);
index 3687d28242025d9728bb00094b0f7003a3a627c4..beadecb574526a0bfaffefedba15f884e12b5f44 100644 (file)
@@ -37,19 +37,22 @@ using namespace WebCore;
 
 namespace WebKit {
 
 
 namespace WebKit {
 
-String WebAutomationSession::platformGetBase64EncodedPNGData(const ShareableBitmap::Handle& imageDataHandle)
+std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ShareableBitmap::Handle& imageDataHandle)
 {
     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(imageDataHandle, SharedMemory::Protection::ReadOnly);
 {
     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(imageDataHandle, SharedMemory::Protection::ReadOnly);
+    if (!bitmap)
+        return std::nullopt;
+
     RetainPtr<CGImageRef> cgImage = bitmap->makeCGImage();
     RetainPtr<NSMutableData> imageData = adoptNS([[NSMutableData alloc] init]);
     RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData((CFMutableDataRef)imageData.get(), kUTTypePNG, 1, 0));
     if (!destination)
     RetainPtr<CGImageRef> cgImage = bitmap->makeCGImage();
     RetainPtr<NSMutableData> imageData = adoptNS([[NSMutableData alloc] init]);
     RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData((CFMutableDataRef)imageData.get(), kUTTypePNG, 1, 0));
     if (!destination)
-        return String();
+        return std::nullopt;
 
     CGImageDestinationAddImage(destination.get(), cgImage.get(), 0);
     CGImageDestinationFinalize(destination.get());
 
 
     CGImageDestinationAddImage(destination.get(), cgImage.get(), 0);
     CGImageDestinationFinalize(destination.get());
 
-    return [imageData base64EncodedStringWithOptions:0];
+    return String([imageData base64EncodedStringWithOptions:0]);
 }
 
 } // namespace WebKit
 }
 
 } // namespace WebKit