Title: [171057] trunk/Source/WebKit2
Revision
171057
Author
m...@apple.com
Date
2014-07-13 12:39:45 -0700 (Sun, 13 Jul 2014)

Log Message

<rdar://problem/17295636> [Cocoa] Include element snapshot in _WKActivatedElementInfo
https://bugs.webkit.org/show_bug.cgi?id=134872

Reviewed by Sam Weinig.

* Shared/InteractionInformationAtPosition.cpp:
(WebKit::InteractionInformationAtPosition::encode): Encode the image if there is one.
(WebKit::InteractionInformationAtPosition::decode): Decode the image if there is one.
* Shared/InteractionInformationAtPosition.h: Added an image member to the struct.

* UIProcess/API/Cocoa/_WKActivatedElementInfo.h: Exposed the boundingRect property and added
an image property.
* UIProcess/API/Cocoa/_WKActivatedElementInfo.mm:
(-[_WKActivatedElementInfo _initWithType:URL:location:title:rect:image:]): Added an image
parameter, which is stored in a new ivar.
(-[_WKActivatedElementInfo image]): Added this getter, which converts the ShareableBitmap
into a cached Cocoa image and returns it.
* UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h: Added image parameter to the
initializer, removed _boundingRect property declaration from here.

* UIProcess/ios/WKActionSheetAssistant.mm:
(-[WKActionSheetAssistant showImageSheet]): Pass the image from the position information
into the _WKActivatedElementInfo initializer.
(-[WKActionSheetAssistant showLinkSheet]): Ditto.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::snapshotNode): Added.
* WebProcess/WebPage/WebPage.h:

* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::getPositionInformation): If the element is a link or an image, store a
snapshot of it in the image member of the InteractionInformationAtPosition.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (171056 => 171057)


--- trunk/Source/WebKit2/ChangeLog	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/ChangeLog	2014-07-13 19:39:45 UTC (rev 171057)
@@ -1,5 +1,40 @@
 2014-07-13  Dan Bernstein  <m...@apple.com>
 
+        <rdar://problem/17295636> [Cocoa] Include element snapshot in _WKActivatedElementInfo
+        https://bugs.webkit.org/show_bug.cgi?id=134872
+
+        Reviewed by Sam Weinig.
+
+        * Shared/InteractionInformationAtPosition.cpp:
+        (WebKit::InteractionInformationAtPosition::encode): Encode the image if there is one.
+        (WebKit::InteractionInformationAtPosition::decode): Decode the image if there is one.
+        * Shared/InteractionInformationAtPosition.h: Added an image member to the struct.
+
+        * UIProcess/API/Cocoa/_WKActivatedElementInfo.h: Exposed the boundingRect property and added
+        an image property.
+        * UIProcess/API/Cocoa/_WKActivatedElementInfo.mm:
+        (-[_WKActivatedElementInfo _initWithType:URL:location:title:rect:image:]): Added an image
+        parameter, which is stored in a new ivar.
+        (-[_WKActivatedElementInfo image]): Added this getter, which converts the ShareableBitmap
+        into a cached Cocoa image and returns it.
+        * UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h: Added image parameter to the
+        initializer, removed _boundingRect property declaration from here.
+
+        * UIProcess/ios/WKActionSheetAssistant.mm:
+        (-[WKActionSheetAssistant showImageSheet]): Pass the image from the position information
+        into the _WKActivatedElementInfo initializer.
+        (-[WKActionSheetAssistant showLinkSheet]): Ditto.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::snapshotNode): Added.
+        * WebProcess/WebPage/WebPage.h:
+
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::getPositionInformation): If the element is a link or an image, store a
+        snapshot of it in the image member of the InteractionInformationAtPosition.
+
+2014-07-13  Dan Bernstein  <m...@apple.com>
+
         [Cocoa] Clean up session state API a little
         https://bugs.webkit.org/show_bug.cgi?id=134871
 

Modified: trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.cpp (171056 => 171057)


--- trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.cpp	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.cpp	2014-07-13 19:39:45 UTC (rev 171057)
@@ -42,6 +42,11 @@
     encoder << url;
     encoder << title;
     encoder << bounds;
+
+    ShareableBitmap::Handle handle;
+    if (image)
+        image->createHandle(handle, SharedMemory::ReadOnly);
+    encoder << handle;
 }
 
 bool InteractionInformationAtPosition::decode(IPC::ArgumentDecoder& decoder, InteractionInformationAtPosition& result)
@@ -70,6 +75,13 @@
     if (!decoder.decode(result.bounds))
         return false;
 
+    ShareableBitmap::Handle handle;
+    if (!decoder.decode(handle))
+        return false;
+
+    if (!handle.isNull())
+        result.image = ShareableBitmap::create(handle, SharedMemory::ReadOnly);
+
     return true;
 }
 #endif

Modified: trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.h (171056 => 171057)


--- trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.h	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/Shared/InteractionInformationAtPosition.h	2014-07-13 19:39:45 UTC (rev 171057)
@@ -27,6 +27,7 @@
 #define InteractionInformationAtPosition_h
 
 #include "ArgumentCoders.h"
+#include "ShareableBitmap.h"
 #include <WebCore/IntPoint.h>
 #include <WebCore/SelectionRect.h>
 #include <wtf/text/WTFString.h>
@@ -50,6 +51,7 @@
     String url;
     String title;
     WebCore::IntRect bounds;
+    RefPtr<ShareableBitmap> image;
 
     void encode(IPC::ArgumentEncoder&) const;
     static bool decode(IPC::ArgumentDecoder&, InteractionInformationAtPosition&);

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h (171056 => 171057)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.h	2014-07-13 19:39:45 UTC (rev 171057)
@@ -27,7 +27,11 @@
 
 #if WK_API_ENABLED
 
-#import <Foundation/Foundation.h>
+#if TARGET_OS_IPHONE
+@class UIImage;
+#else
+@class NSImage;
+#endif
 
 typedef NS_ENUM(NSInteger, _WKActivatedElementType) {
     _WKActivatedElementTypeLink,
@@ -40,6 +44,12 @@
 @property (nonatomic, readonly) NSURL *URL;
 @property (nonatomic, readonly) NSString *title;
 @property (nonatomic, readonly) _WKActivatedElementType type;
+@property (nonatomic, readonly) CGRect boundingRect;
+#if TARGET_OS_IPHONE
+@property (nonatomic, readonly, copy) UIImage *image;
+#else
+@property (nonatomic, readonly, copy) NSImage *image;
+#endif
 
 @end
 

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm (171056 => 171057)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfo.mm	2014-07-13 19:39:45 UTC (rev 171057)
@@ -28,16 +28,31 @@
 
 #if WK_API_ENABLED
 
+#import "ShareableBitmap.h"
 #import <wtf/RetainPtr.h>
 
+#if PLATFORM(IOS)
+#import <UIKit/UIImage.h>
+#endif
+
+#if PLATFORM(MAC)
+#import <AppKit/NSImage.h>
+#endif
+
 @implementation _WKActivatedElementInfo  {
     RetainPtr<NSURL> _URL;
     RetainPtr<NSString> _title;
     CGPoint _interactionLocation;
-    CGRect _boundingRect;
+    RefPtr<WebKit::ShareableBitmap> _image;
+#if PLATFORM(IOS)
+    RetainPtr<UIImage> _uiImage;
+#endif
+#if PLATFORM(MAC)
+    RetainPtr<NSImage> _nsImage;
+#endif
 }
 
-- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title rect:(CGRect)rect
+- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image
 {
     if (!(self = [super init]))
         return nil;
@@ -47,6 +62,7 @@
     _title = adoptNS([title copy]);
     _boundingRect = rect;
     _type = type;
+    _image = image;
 
     return self;
 }
@@ -61,16 +77,43 @@
     return _title.get();
 }
 
-- (CGRect)_boundingRect
+- (CGPoint)_interactionLocation
 {
-    return _boundingRect;
+    return _interactionLocation;
 }
 
-- (CGPoint)_interactionLocation
+#if PLATFORM(IOS)
+- (UIImage *)image
 {
-    return _interactionLocation;
+    if (_uiImage)
+        return [[_uiImage copy] autorelease];
+
+    if (!_image)
+        return nil;
+
+    _uiImage = adoptNS([[UIImage alloc] initWithCGImage:_image->makeCGImageCopy().get()]);
+    _image = nullptr;
+
+    return [[_uiImage copy] autorelease];
 }
+#endif
 
+#if PLATFORM(MAC)
+- (NSImage *)image
+{
+    if (_nsImage)
+        return [[_nsImage copy] autorelease];
+
+    if (!_image)
+        return nil;
+
+    _nsImage = adoptNS([[NSImage alloc] initWithCGImage:_image->makeCGImageCopy().get() size:NSSizeFromCGSize(_boundingRect.size)]);
+    _image = nullptr;
+
+    return [[_nsImage copy] autorelease];
+}
+#endif
+
 @end
 
 #endif // WK_API_ENABLED

Modified: trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h (171056 => 171057)


--- trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/UIProcess/API/Cocoa/_WKActivatedElementInfoInternal.h	2014-07-13 19:39:45 UTC (rev 171057)
@@ -27,12 +27,15 @@
 
 #if WK_API_ENABLED
 
+namespace WebKit {
+    class ShareableBitmap;
+}
+
 @interface _WKActivatedElementInfo ()
 
-- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title rect:(CGRect)rect;
+- (instancetype)_initWithType:(_WKActivatedElementType)type URL:(NSURL *)url location:(CGPoint)location title:(NSString *)title rect:(CGRect)rect image:(WebKit::ShareableBitmap*)image;
 
 @property (nonatomic, readonly) CGPoint _interactionLocation;
-@property (nonatomic, readonly) CGRect _boundingRect;
 
 @end
 

Modified: trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm (171056 => 171057)


--- trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/UIProcess/ios/WKActionSheetAssistant.mm	2014-07-13 19:39:45 UTC (rev 171057)
@@ -233,7 +233,7 @@
         [defaultActions addObject:[_WKElementAction elementActionWithType:_WKElementActionTypeCopy]];
 
     auto elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeImage
-        URL:targetURL location:positionInformation.point title:positionInformation.title rect:positionInformation.bounds]);
+        URL:targetURL location:positionInformation.point title:positionInformation.title rect:positionInformation.bounds image:positionInformation.image.get()]);
 
     RetainPtr<NSArray> actions = _view.page->uiClient().actionsForElement(elementInfo.get(), WTF::move(defaultActions));
 
@@ -269,7 +269,7 @@
         [defaultActions addObject:[_WKElementAction elementActionWithType:_WKElementActionTypeCopy]];
 
     RetainPtr<_WKActivatedElementInfo> elementInfo = adoptNS([[_WKActivatedElementInfo alloc] _initWithType:_WKActivatedElementTypeLink
-        URL:targetURL location:positionInformation.point title:positionInformation.title rect:positionInformation.bounds]);
+        URL:targetURL location:positionInformation.point title:positionInformation.title rect:positionInformation.bounds image:positionInformation.image.get()]);
 
     RetainPtr<NSArray> actions = _view.page->uiClient().actionsForElement(elementInfo.get(), WTF::move(defaultActions));
 

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (171056 => 171057)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2014-07-13 19:39:45 UTC (rev 171057)
@@ -1691,6 +1691,57 @@
     return snapshot.release();
 }
 
+PassRefPtr<WebImage> WebPage::snapshotNode(WebCore::Node& node, SnapshotOptions options, unsigned maximumPixelCount)
+{
+    Frame* coreFrame = m_mainFrame->coreFrame();
+    if (!coreFrame)
+        return nullptr;
+
+    FrameView* frameView = coreFrame->view();
+    if (!frameView)
+        return nullptr;
+
+    if (!node.renderer())
+        return nullptr;
+
+    LayoutRect topLevelRect;
+    IntRect snapshotRect = pixelSnappedIntRect(node.renderer()->paintingRootRect(topLevelRect));
+
+    double scaleFactor = 1;
+    IntSize snapshotSize = snapshotRect.size();
+    unsigned maximumHeight = maximumPixelCount / snapshotSize.width();
+    if (maximumHeight < static_cast<unsigned>(snapshotSize.height())) {
+        scaleFactor = static_cast<double>(maximumHeight) / snapshotSize.height();
+        snapshotSize = IntSize(snapshotSize.width() * scaleFactor, maximumHeight);
+    }
+
+    RefPtr<WebImage> snapshot = WebImage::create(snapshotSize, snapshotOptionsToImageOptions(options));
+    if (!snapshot->bitmap())
+        return nullptr;
+
+    auto graphicsContext = snapshot->bitmap()->createGraphicsContext();
+
+    if (!(options & SnapshotOptionsExcludeDeviceScaleFactor)) {
+        double deviceScaleFactor = corePage()->deviceScaleFactor();
+        graphicsContext->applyDeviceScaleFactor(deviceScaleFactor);
+        scaleFactor /= deviceScaleFactor;
+    }
+
+    graphicsContext->scale(FloatSize(scaleFactor, scaleFactor));
+    graphicsContext->translate(-snapshotRect.x(), -snapshotRect.y());
+
+    Color savedBackgroundColor = frameView->baseBackgroundColor();
+    frameView->setBaseBackgroundColor(Color::transparent);
+    frameView->setNodeToDraw(&node);
+
+    frameView->paintContentsForSnapshot(graphicsContext.get(), snapshotRect, FrameView::ExcludeSelection, FrameView::DocumentCoordinates);
+
+    frameView->setBaseBackgroundColor(savedBackgroundColor);
+    frameView->setNodeToDraw(nullptr);
+
+    return snapshot.release();
+}
+
 void WebPage::pageDidScroll()
 {
 #if PLATFORM(IOS)

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (171056 => 171057)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2014-07-13 19:39:45 UTC (rev 171057)
@@ -440,6 +440,7 @@
     
     PassRefPtr<WebImage> scaledSnapshotWithOptions(const WebCore::IntRect&, double additionalScaleFactor, SnapshotOptions);
     PassRefPtr<WebImage> snapshotAtSize(const WebCore::IntRect&, const WebCore::IntSize& bitmapSize, SnapshotOptions);
+    PassRefPtr<WebImage> snapshotNode(WebCore::Node&, SnapshotOptions, unsigned maximumPixelCount = std::numeric_limits<unsigned>::max());
 
     static const WebEvent* currentEvent();
 

Modified: trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm (171056 => 171057)


--- trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2014-07-13 18:55:43 UTC (rev 171056)
+++ trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm	2014-07-13 19:39:45 UTC (rev 171057)
@@ -1849,6 +1849,11 @@
                 linkElement = element;
                 elementIsLinkOrImage = true;
             }
+
+            if (elementIsLinkOrImage) {
+                // Ensure that the image contains at most 600K pixels, so that it is not too big.
+                info.image = snapshotNode(*element, SnapshotOptionsShareable, 600 * 1024)->bitmap();
+            }
             if (linkElement)
                 info.url = "" *)linkElement->document().completeURL(stripLeadingAndTrailingHTMLSpaces(linkElement->getAttribute(HTMLNames::hrefAttr))) absoluteString];
             info.title = element->fastGetAttribute(HTMLNames::titleAttr).string();
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to