Chromium Code Reviews| 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::CreateDragImage( | |
| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 clean_type.StartsWith(kMimeTypeTextPlainEtc)) | 191 clean_type.StartsWith(kMimeTypeTextPlainEtc)) |
| 104 return kMimeTypeTextPlain; | 192 return kMimeTypeTextPlain; |
| 105 if (clean_type == kMimeTypeURL) { | 193 if (clean_type == kMimeTypeURL) { |
| 106 if (convert_to_url) | 194 if (convert_to_url) |
| 107 *convert_to_url = true; | 195 *convert_to_url = true; |
| 108 return kMimeTypeTextURIList; | 196 return kMimeTypeTextURIList; |
| 109 } | 197 } |
| 110 return clean_type; | 198 return clean_type; |
| 111 } | 199 } |
| 112 | 200 |
| 201 // Converts from bounds in CSS space to device space based on the given | |
| 202 // frame. | |
| 203 FloatRect DataTransfer::DeviceSpaceBounds(const FloatRect css_bounds, | |
| 204 const LocalFrame& frame) { | |
| 205 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); | |
| 206 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); | |
| 207 FloatRect device_bounds(css_bounds); | |
| 208 device_bounds.SetWidth(css_bounds.Width() * device_scale_factor * | |
| 209 page_scale_factor); | |
| 210 device_bounds.SetHeight(css_bounds.Height() * device_scale_factor * | |
| 211 page_scale_factor); | |
| 212 return device_bounds; | |
| 213 } | |
| 214 | |
| 215 // Returns a DragImage whose bitmap contains |contents|, positioned and scaled | |
| 216 // in device space. | |
| 217 std::unique_ptr<DragImage> DataTransfer::CreateDragImage( | |
|
Xiaocheng
2017/06/02 18:26:47
Let's renamed it into |CreateDragImageForFrame| to
| |
| 218 const LocalFrame& frame, | |
| 219 float opacity, | |
| 220 RespectImageOrientationEnum image_orientation, | |
| 221 const FloatRect& css_bounds, | |
| 222 PaintRecordBuilder& builder, | |
| 223 const PropertyTreeState& property_tree_state) { | |
| 224 float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated(); | |
| 225 float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale(); | |
| 226 | |
| 227 FloatRect device_bounds = DeviceSpaceBounds(css_bounds, frame); | |
| 228 | |
| 229 AffineTransform transform; | |
| 230 transform.Scale(device_scale_factor * page_scale_factor); | |
| 231 transform.Translate(-device_bounds.X(), -device_bounds.Y()); | |
| 232 | |
| 233 // Rasterize upfront, since DragImage::create() is going to do it anyway | |
| 234 // (SkImage::asLegacyBitmap). | |
| 235 SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry); | |
| 236 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( | |
| 237 device_bounds.Width(), device_bounds.Height(), &surface_props); | |
| 238 if (!surface) | |
| 239 return nullptr; | |
| 240 | |
| 241 SkiaPaintCanvas skia_paint_canvas(surface->getCanvas()); | |
| 242 skia_paint_canvas.concat(AffineTransformToSkMatrix(transform)); | |
| 243 builder.EndRecording(skia_paint_canvas, property_tree_state); | |
| 244 | |
| 245 RefPtr<Image> image = StaticBitmapImage::Create(surface->makeImageSnapshot()); | |
| 246 float screen_device_scale_factor = | |
| 247 frame.GetPage()->GetChromeClient().GetScreenInfo().device_scale_factor; | |
| 248 | |
| 249 return DragImage::Create(image.Get(), image_orientation, | |
| 250 screen_device_scale_factor, kInterpolationHigh, | |
| 251 opacity); | |
| 252 } | |
| 253 | |
| 113 DataTransfer* DataTransfer::Create() { | 254 DataTransfer* DataTransfer::Create() { |
| 114 DataTransfer* data = | 255 DataTransfer* data = |
| 115 Create(kCopyAndPaste, kDataTransferWritable, DataObject::Create()); | 256 Create(kCopyAndPaste, kDataTransferWritable, DataObject::Create()); |
| 116 data->drop_effect_ = "none"; | 257 data->drop_effect_ = "none"; |
| 117 data->effect_allowed_ = "none"; | 258 data->effect_allowed_ = "none"; |
| 118 return data; | 259 return data; |
| 119 } | 260 } |
| 120 | 261 |
| 121 DataTransfer* DataTransfer::Create(DataTransferType type, | 262 DataTransfer* DataTransfer::Create(DataTransferType type, |
| 122 DataTransferAccessPolicy policy, | 263 DataTransferAccessPolicy policy, |
| (...skipping 125 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 DataTransfer::NodeImage(*frame, *drag_image_element_); |
|
Xiaocheng
2017/06/02 18:26:47
nit: Remove |DataTransfer|
| |
| 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 |