| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "core/clipboard/DataTransfer.h" | 26 #include "core/clipboard/DataTransfer.h" |
| 27 | 27 |
| 28 #include <memory> |
| 28 #include "core/HTMLNames.h" | 29 #include "core/HTMLNames.h" |
| 29 #include "core/clipboard/DataObject.h" | 30 #include "core/clipboard/DataObject.h" |
| 30 #include "core/clipboard/DataTransferItem.h" | 31 #include "core/clipboard/DataTransferItem.h" |
| 31 #include "core/clipboard/DataTransferItemList.h" | 32 #include "core/clipboard/DataTransferItemList.h" |
| 32 #include "core/editing/EphemeralRange.h" | 33 #include "core/editing/EphemeralRange.h" |
| 33 #include "core/editing/FrameSelection.h" | 34 #include "core/editing/FrameSelection.h" |
| 34 #include "core/editing/serializers/Serialization.h" | 35 #include "core/editing/serializers/Serialization.h" |
| 35 #include "core/fileapi/FileList.h" | 36 #include "core/fileapi/FileList.h" |
| 36 #include "core/frame/LocalFrame.h" | 37 #include "core/frame/LocalFrame.h" |
| 38 #include "core/frame/VisualViewport.h" |
| 37 #include "core/html/HTMLImageElement.h" | 39 #include "core/html/HTMLImageElement.h" |
| 38 #include "core/html/TextControlElement.h" | 40 #include "core/html/TextControlElement.h" |
| 39 #include "core/layout/LayoutImage.h" | 41 #include "core/layout/LayoutImage.h" |
| 40 #include "core/layout/LayoutObject.h" | 42 #include "core/layout/LayoutObject.h" |
| 41 #include "core/loader/resource/ImageResourceContent.h" | 43 #include "core/loader/resource/ImageResourceContent.h" |
| 44 #include "core/page/ChromeClient.h" |
| 45 #include "core/page/Page.h" |
| 46 #include "core/paint/PaintInfo.h" |
| 47 #include "core/paint/PaintLayer.h" |
| 48 #include "core/paint/PaintLayerPainter.h" |
| 42 #include "platform/DragImage.h" | 49 #include "platform/DragImage.h" |
| 43 #include "platform/clipboard/ClipboardMimeTypes.h" | 50 #include "platform/clipboard/ClipboardMimeTypes.h" |
| 44 #include "platform/clipboard/ClipboardUtilities.h" | 51 #include "platform/clipboard/ClipboardUtilities.h" |
| 52 #include "platform/graphics/StaticBitmapImage.h" |
| 53 #include "platform/graphics/paint/PaintRecordBuilder.h" |
| 45 #include "platform/network/mime/MIMETypeRegistry.h" | 54 #include "platform/network/mime/MIMETypeRegistry.h" |
| 46 #include <memory> | 55 #include "public/platform/WebScreenInfo.h" |
| 56 #include "third_party/skia/include/core/SkSurface.h" |
| 47 | 57 |
| 48 namespace blink { | 58 namespace blink { |
| 49 | 59 |
| 60 namespace { |
| 61 |
| 62 class DraggedNodeImageBuilder { |
| 63 STACK_ALLOCATED(); |
| 64 |
| 65 public: |
| 66 DraggedNodeImageBuilder(const LocalFrame& local_frame, Node& node) |
| 67 : local_frame_(&local_frame), |
| 68 node_(&node) |
| 69 #if DCHECK_IS_ON() |
| 70 , |
| 71 dom_tree_version_(node.GetDocument().DomTreeVersion()) |
| 72 #endif |
| 73 { |
| 74 for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_)) |
| 75 descendant.SetDragged(true); |
| 76 } |
| 77 |
| 78 ~DraggedNodeImageBuilder() { |
| 79 #if DCHECK_IS_ON() |
| 80 DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion()); |
| 81 #endif |
| 82 for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_)) |
| 83 descendant.SetDragged(false); |
| 84 } |
| 85 |
| 86 std::unique_ptr<DragImage> CreateImage() { |
| 87 #if DCHECK_IS_ON() |
| 88 DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion()); |
| 89 #endif |
| 90 // Construct layout object for |m_node| with pseudo class "-webkit-drag" |
| 91 local_frame_->View()->UpdateAllLifecyclePhasesExceptPaint(); |
| 92 LayoutObject* const dragged_layout_object = node_->GetLayoutObject(); |
| 93 if (!dragged_layout_object) |
| 94 return nullptr; |
| 95 // Paint starting at the nearest stacking context, clipped to the object |
| 96 // itself. This will also paint the contents behind the object if the |
| 97 // object contains transparency and there are other elements in the same |
| 98 // stacking context which stacked below. |
| 99 PaintLayer* layer = dragged_layout_object->EnclosingLayer(); |
| 100 if (!layer->StackingNode()->IsStackingContext()) |
| 101 layer = layer->StackingNode()->AncestorStackingContextNode()->Layer(); |
| 102 IntRect absolute_bounding_box = |
| 103 dragged_layout_object->AbsoluteBoundingBoxRectIncludingDescendants(); |
| 104 FloatRect bounding_box = |
| 105 layer->GetLayoutObject() |
| 106 .AbsoluteToLocalQuad(FloatQuad(absolute_bounding_box), |
| 107 kUseTransforms) |
| 108 .BoundingBox(); |
| 109 PaintLayerPaintingInfo painting_info(layer, LayoutRect(bounding_box), |
| 110 kGlobalPaintFlattenCompositingLayers, |
| 111 LayoutSize()); |
| 112 PaintLayerFlags flags = kPaintLayerHaveTransparency | |
| 113 kPaintLayerAppliedTransform | |
| 114 kPaintLayerUncachedClipRects; |
| 115 PaintRecordBuilder builder( |
| 116 DataTransfer::DeviceSpaceBounds(bounding_box, *local_frame_)); |
| 117 PaintLayerPainter(*layer).Paint(builder.Context(), painting_info, flags); |
| 118 PropertyTreeState border_box_properties = PropertyTreeState::Root(); |
| 119 if (RuntimeEnabledFeatures::SlimmingPaintV2Enabled()) { |
| 120 border_box_properties = |
| 121 *layer->GetLayoutObject().LocalBorderBoxProperties(); |
| 122 } |
| 123 return DataTransfer::CreateDragImageForFrame( |
| 124 *local_frame_, 1.0f, |
| 125 LayoutObject::ShouldRespectImageOrientation(dragged_layout_object), |
| 126 bounding_box, builder, border_box_properties); |
| 127 } |
| 128 |
| 129 private: |
| 130 const Member<const LocalFrame> local_frame_; |
| 131 const Member<Node> node_; |
| 132 #if DCHECK_IS_ON() |
| 133 const uint64_t dom_tree_version_; |
| 134 #endif |
| 135 }; |
| 136 } |
| 137 |
| 50 static DragOperation ConvertEffectAllowedToDragOperation(const String& op) { | 138 static DragOperation ConvertEffectAllowedToDragOperation(const String& op) { |
| 51 // Values specified in | 139 // Values specified in |
| 52 // http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-da
tatransfer-effectallowed | 140 // http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-da
tatransfer-effectallowed |
| 53 if (op == "uninitialized") | 141 if (op == "uninitialized") |
| 54 return kDragOperationEvery; | 142 return kDragOperationEvery; |
| 55 if (op == "none") | 143 if (op == "none") |
| 56 return kDragOperationNone; | 144 return kDragOperationNone; |
| 57 if (op == "copy") | 145 if (op == "copy") |
| 58 return kDragOperationCopy; | 146 return kDragOperationCopy; |
| 59 if (op == "link") | 147 if (op == "link") |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 207 } |
| 120 | 208 |
| 121 DataTransfer* DataTransfer::Create(DataTransferType type, | 209 DataTransfer* DataTransfer::Create(DataTransferType type, |
| 122 DataTransferAccessPolicy policy, | 210 DataTransferAccessPolicy policy, |
| 123 DataObject* data_object) { | 211 DataObject* data_object) { |
| 124 return new DataTransfer(type, policy, data_object); | 212 return new DataTransfer(type, policy, data_object); |
| 125 } | 213 } |
| 126 | 214 |
| 127 DataTransfer::~DataTransfer() {} | 215 DataTransfer::~DataTransfer() {} |
| 128 | 216 |
| 217 // Converts from bounds in CSS space to device space based on the given |
| 218 // frame. |
| 219 FloatRect DataTransfer::DeviceSpaceBounds(const FloatRect css_bounds, |
| 220 const LocalFrame& frame) { |
| 221 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); |
| 222 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); |
| 223 FloatRect device_bounds(css_bounds); |
| 224 device_bounds.SetWidth(css_bounds.Width() * device_scale_factor * |
| 225 page_scale_factor); |
| 226 device_bounds.SetHeight(css_bounds.Height() * device_scale_factor * |
| 227 page_scale_factor); |
| 228 return device_bounds; |
| 229 } |
| 230 |
| 231 // Returns a DragImage whose bitmap contains |contents|, positioned and scaled |
| 232 // in device space. |
| 233 std::unique_ptr<DragImage> DataTransfer::CreateDragImageForFrame( |
| 234 const LocalFrame& frame, |
| 235 float opacity, |
| 236 RespectImageOrientationEnum image_orientation, |
| 237 const FloatRect& css_bounds, |
| 238 PaintRecordBuilder& builder, |
| 239 const PropertyTreeState& property_tree_state) { |
| 240 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); |
| 241 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); |
| 242 |
| 243 FloatRect device_bounds = DeviceSpaceBounds(css_bounds, frame); |
| 244 |
| 245 AffineTransform transform; |
| 246 transform.Scale(device_scale_factor * page_scale_factor); |
| 247 transform.Translate(-device_bounds.X(), -device_bounds.Y()); |
| 248 |
| 249 // Rasterize upfront, since DragImage::create() is going to do it anyway |
| 250 // (SkImage::asLegacyBitmap). |
| 251 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry); |
| 252 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
| 253 device_bounds.Width(), device_bounds.Height(), &surface_props); |
| 254 if (!surface) |
| 255 return nullptr; |
| 256 |
| 257 SkiaPaintCanvas skia_paint_canvas(surface->getCanvas()); |
| 258 skia_paint_canvas.concat(AffineTransformToSkMatrix(transform)); |
| 259 builder.EndRecording(skia_paint_canvas, property_tree_state); |
| 260 |
| 261 RefPtr<Image> image = StaticBitmapImage::Create(surface->makeImageSnapshot()); |
| 262 float screen_device_scale_factor = |
| 263 frame.GetPage()->GetChromeClient().GetScreenInfo().device_scale_factor; |
| 264 |
| 265 return DragImage::Create(image.Get(), image_orientation, |
| 266 screen_device_scale_factor, kInterpolationHigh, |
| 267 opacity); |
| 268 } |
| 269 |
| 129 void DataTransfer::setDropEffect(const String& effect) { | 270 void DataTransfer::setDropEffect(const String& effect) { |
| 130 if (!IsForDragAndDrop()) | 271 if (!IsForDragAndDrop()) |
| 131 return; | 272 return; |
| 132 | 273 |
| 133 // The attribute must ignore any attempts to set it to a value other than | 274 // The attribute must ignore any attempts to set it to a value other than |
| 134 // none, copy, link, and move. | 275 // none, copy, link, and move. |
| 135 if (effect != "none" && effect != "copy" && effect != "link" && | 276 if (effect != "none" && effect != "copy" && effect != "link" && |
| 136 effect != "move") | 277 effect != "move") |
| 137 return; | 278 return; |
| 138 | 279 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 389 |
| 249 void DataTransfer::SetDragImageResource(ImageResourceContent* img, | 390 void DataTransfer::SetDragImageResource(ImageResourceContent* img, |
| 250 const IntPoint& loc) { | 391 const IntPoint& loc) { |
| 251 setDragImage(img, 0, loc); | 392 setDragImage(img, 0, loc); |
| 252 } | 393 } |
| 253 | 394 |
| 254 void DataTransfer::SetDragImageElement(Node* node, const IntPoint& loc) { | 395 void DataTransfer::SetDragImageElement(Node* node, const IntPoint& loc) { |
| 255 setDragImage(0, node, loc); | 396 setDragImage(0, node, loc); |
| 256 } | 397 } |
| 257 | 398 |
| 399 std::unique_ptr<DragImage> DataTransfer::NodeImage(const LocalFrame& frame, |
| 400 Node& node) { |
| 401 DraggedNodeImageBuilder image_node(frame, node); |
| 402 return image_node.CreateImage(); |
| 403 } |
| 404 |
| 258 std::unique_ptr<DragImage> DataTransfer::CreateDragImage( | 405 std::unique_ptr<DragImage> DataTransfer::CreateDragImage( |
| 259 IntPoint& loc, | 406 IntPoint& loc, |
| 260 LocalFrame* frame) const { | 407 LocalFrame* frame) const { |
| 261 if (drag_image_element_) { | 408 if (drag_image_element_) { |
| 262 loc = drag_loc_; | 409 loc = drag_loc_; |
| 263 | 410 |
| 264 return frame->NodeImage(*drag_image_element_); | 411 return NodeImage(*frame, *drag_image_element_); |
| 265 } | 412 } |
| 266 if (drag_image_) { | 413 if (drag_image_) { |
| 267 loc = drag_loc_; | 414 loc = drag_loc_; |
| 268 return DragImage::Create(drag_image_->GetImage()); | 415 return DragImage::Create(drag_image_->GetImage()); |
| 269 } | 416 } |
| 270 return nullptr; | 417 return nullptr; |
| 271 } | 418 } |
| 272 | 419 |
| 273 static ImageResourceContent* GetImageResourceContent(Element* element) { | 420 static ImageResourceContent* GetImageResourceContent(Element* element) { |
| 274 // Attempt to pull ImageResourceContent from element | 421 // Attempt to pull ImageResourceContent from element |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 } | 645 } |
| 499 } | 646 } |
| 500 | 647 |
| 501 DEFINE_TRACE(DataTransfer) { | 648 DEFINE_TRACE(DataTransfer) { |
| 502 visitor->Trace(data_object_); | 649 visitor->Trace(data_object_); |
| 503 visitor->Trace(drag_image_); | 650 visitor->Trace(drag_image_); |
| 504 visitor->Trace(drag_image_element_); | 651 visitor->Trace(drag_image_element_); |
| 505 } | 652 } |
| 506 | 653 |
| 507 } // namespace blink | 654 } // namespace blink |
| OLD | NEW |