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 } // namespace |
50 static DragOperation ConvertEffectAllowedToDragOperation(const String& op) { | 137 static DragOperation ConvertEffectAllowedToDragOperation(const String& op) { |
51 // Values specified in | 138 // Values specified in |
52 // http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-da
tatransfer-effectallowed | 139 // http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-da
tatransfer-effectallowed |
53 if (op == "uninitialized") | 140 if (op == "uninitialized") |
54 return kDragOperationEvery; | 141 return kDragOperationEvery; |
55 if (op == "none") | 142 if (op == "none") |
56 return kDragOperationNone; | 143 return kDragOperationNone; |
57 if (op == "copy") | 144 if (op == "copy") |
58 return kDragOperationCopy; | 145 return kDragOperationCopy; |
59 if (op == "link") | 146 if (op == "link") |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 335 |
249 void DataTransfer::SetDragImageResource(ImageResourceContent* img, | 336 void DataTransfer::SetDragImageResource(ImageResourceContent* img, |
250 const IntPoint& loc) { | 337 const IntPoint& loc) { |
251 setDragImage(img, 0, loc); | 338 setDragImage(img, 0, loc); |
252 } | 339 } |
253 | 340 |
254 void DataTransfer::SetDragImageElement(Node* node, const IntPoint& loc) { | 341 void DataTransfer::SetDragImageElement(Node* node, const IntPoint& loc) { |
255 setDragImage(0, node, loc); | 342 setDragImage(0, node, loc); |
256 } | 343 } |
257 | 344 |
| 345 // static |
| 346 // Converts from bounds in CSS space to device space based on the given |
| 347 // frame. |
| 348 FloatRect DataTransfer::DeviceSpaceBounds(const FloatRect css_bounds, |
| 349 const LocalFrame& frame) { |
| 350 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); |
| 351 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); |
| 352 FloatRect device_bounds(css_bounds); |
| 353 device_bounds.SetWidth(css_bounds.Width() * device_scale_factor * |
| 354 page_scale_factor); |
| 355 device_bounds.SetHeight(css_bounds.Height() * device_scale_factor * |
| 356 page_scale_factor); |
| 357 return device_bounds; |
| 358 } |
| 359 |
| 360 // static |
| 361 // Returns a DragImage whose bitmap contains |contents|, positioned and scaled |
| 362 // in device space. |
| 363 std::unique_ptr<DragImage> DataTransfer::CreateDragImageForFrame( |
| 364 const LocalFrame& frame, |
| 365 float opacity, |
| 366 RespectImageOrientationEnum image_orientation, |
| 367 const FloatRect& css_bounds, |
| 368 PaintRecordBuilder& builder, |
| 369 const PropertyTreeState& property_tree_state) { |
| 370 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); |
| 371 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); |
| 372 |
| 373 FloatRect device_bounds = DeviceSpaceBounds(css_bounds, frame); |
| 374 |
| 375 AffineTransform transform; |
| 376 transform.Scale(device_scale_factor * page_scale_factor); |
| 377 transform.Translate(-device_bounds.X(), -device_bounds.Y()); |
| 378 |
| 379 // Rasterize upfront, since DragImage::create() is going to do it anyway |
| 380 // (SkImage::asLegacyBitmap). |
| 381 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry); |
| 382 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
| 383 device_bounds.Width(), device_bounds.Height(), &surface_props); |
| 384 if (!surface) |
| 385 return nullptr; |
| 386 |
| 387 SkiaPaintCanvas skia_paint_canvas(surface->getCanvas()); |
| 388 skia_paint_canvas.concat(AffineTransformToSkMatrix(transform)); |
| 389 builder.EndRecording(skia_paint_canvas, property_tree_state); |
| 390 |
| 391 RefPtr<Image> image = StaticBitmapImage::Create(surface->makeImageSnapshot()); |
| 392 float screen_device_scale_factor = |
| 393 frame.GetPage()->GetChromeClient().GetScreenInfo().device_scale_factor; |
| 394 |
| 395 return DragImage::Create(image.Get(), image_orientation, |
| 396 screen_device_scale_factor, kInterpolationHigh, |
| 397 opacity); |
| 398 } |
| 399 |
| 400 // static |
| 401 std::unique_ptr<DragImage> DataTransfer::NodeImage(const LocalFrame& frame, |
| 402 Node& node) { |
| 403 DraggedNodeImageBuilder image_node(frame, node); |
| 404 return image_node.CreateImage(); |
| 405 } |
| 406 |
258 std::unique_ptr<DragImage> DataTransfer::CreateDragImage( | 407 std::unique_ptr<DragImage> DataTransfer::CreateDragImage( |
259 IntPoint& loc, | 408 IntPoint& loc, |
260 LocalFrame* frame) const { | 409 LocalFrame* frame) const { |
261 if (drag_image_element_) { | 410 if (drag_image_element_) { |
262 loc = drag_loc_; | 411 loc = drag_loc_; |
263 | 412 |
264 return NodeImage(*frame, *drag_image_element_); | 413 return NodeImage(*frame, *drag_image_element_); |
265 } | 414 } |
266 if (drag_image_) { | 415 if (drag_image_) { |
267 loc = drag_loc_; | 416 loc = drag_loc_; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 } | 647 } |
499 } | 648 } |
500 | 649 |
501 DEFINE_TRACE(DataTransfer) { | 650 DEFINE_TRACE(DataTransfer) { |
502 visitor->Trace(data_object_); | 651 visitor->Trace(data_object_); |
503 visitor->Trace(drag_image_); | 652 visitor->Trace(drag_image_); |
504 visitor->Trace(drag_image_element_); | 653 visitor->Trace(drag_image_element_); |
505 } | 654 } |
506 | 655 |
507 } // namespace blink | 656 } // namespace blink |
OLD | NEW |