Commit ee841c26 authored by tanvir.rizvi's avatar tanvir.rizvi Committed by Commit Bot

Move drag related functions present in LocalFrame.cpp to correct files.

Move class DraggedNodeImageBuilder, DataTransfer::NodeImage(),
DataTransfer::DeviceSpaceBounds(), DataTransfer::CreateDragImageForFrame()
to DataTransfer.cpp.
Move DragController::DragImageForSelection() to DragController.cpp.

BUG=727981

Review-Url: https://codereview.chromium.org/2950793002
Cr-Commit-Position: refs/heads/master@{#481067}
parent 019b7355
......@@ -25,6 +25,7 @@
#include "core/clipboard/DataTransfer.h"
#include <memory>
#include "core/HTMLNames.h"
#include "core/clipboard/DataObject.h"
#include "core/clipboard/DataTransferItem.h"
......@@ -34,19 +35,105 @@
#include "core/editing/serializers/Serialization.h"
#include "core/fileapi/FileList.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/VisualViewport.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/TextControlElement.h"
#include "core/layout/LayoutImage.h"
#include "core/layout/LayoutObject.h"
#include "core/loader/resource/ImageResourceContent.h"
#include "core/page/ChromeClient.h"
#include "core/page/Page.h"
#include "core/paint/PaintInfo.h"
#include "core/paint/PaintLayer.h"
#include "core/paint/PaintLayerPainter.h"
#include "platform/DragImage.h"
#include "platform/clipboard/ClipboardMimeTypes.h"
#include "platform/clipboard/ClipboardUtilities.h"
#include "platform/graphics/StaticBitmapImage.h"
#include "platform/graphics/paint/PaintRecordBuilder.h"
#include "platform/network/mime/MIMETypeRegistry.h"
#include <memory>
#include "public/platform/WebScreenInfo.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace blink {
namespace {
class DraggedNodeImageBuilder {
STACK_ALLOCATED();
public:
DraggedNodeImageBuilder(const LocalFrame& local_frame, Node& node)
: local_frame_(&local_frame),
node_(&node)
#if DCHECK_IS_ON()
,
dom_tree_version_(node.GetDocument().DomTreeVersion())
#endif
{
for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_))
descendant.SetDragged(true);
}
~DraggedNodeImageBuilder() {
#if DCHECK_IS_ON()
DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion());
#endif
for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_))
descendant.SetDragged(false);
}
std::unique_ptr<DragImage> CreateImage() {
#if DCHECK_IS_ON()
DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion());
#endif
// Construct layout object for |m_node| with pseudo class "-webkit-drag"
local_frame_->View()->UpdateAllLifecyclePhasesExceptPaint();
LayoutObject* const dragged_layout_object = node_->GetLayoutObject();
if (!dragged_layout_object)
return nullptr;
// Paint starting at the nearest stacking context, clipped to the object
// itself. This will also paint the contents behind the object if the
// object contains transparency and there are other elements in the same
// stacking context which stacked below.
PaintLayer* layer = dragged_layout_object->EnclosingLayer();
if (!layer->StackingNode()->IsStackingContext())
layer = layer->StackingNode()->AncestorStackingContextNode()->Layer();
IntRect absolute_bounding_box =
dragged_layout_object->AbsoluteBoundingBoxRectIncludingDescendants();
FloatRect bounding_box =
layer->GetLayoutObject()
.AbsoluteToLocalQuad(FloatQuad(absolute_bounding_box),
kUseTransforms)
.BoundingBox();
PaintLayerPaintingInfo painting_info(layer, LayoutRect(bounding_box),
kGlobalPaintFlattenCompositingLayers,
LayoutSize());
PaintLayerFlags flags = kPaintLayerHaveTransparency |
kPaintLayerAppliedTransform |
kPaintLayerUncachedClipRects;
PaintRecordBuilder builder(
DataTransfer::DeviceSpaceBounds(bounding_box, *local_frame_));
PaintLayerPainter(*layer).Paint(builder.Context(), painting_info, flags);
PropertyTreeState border_box_properties = PropertyTreeState::Root();
if (RuntimeEnabledFeatures::SlimmingPaintV2Enabled()) {
border_box_properties =
*layer->GetLayoutObject().LocalBorderBoxProperties();
}
return DataTransfer::CreateDragImageForFrame(
*local_frame_, 1.0f,
LayoutObject::ShouldRespectImageOrientation(dragged_layout_object),
bounding_box, builder, border_box_properties);
}
private:
const Member<const LocalFrame> local_frame_;
const Member<Node> node_;
#if DCHECK_IS_ON()
const uint64_t dom_tree_version_;
#endif
};
} // namespace
static DragOperation ConvertEffectAllowedToDragOperation(const String& op) {
// Values specified in
// http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-datatransfer-effectallowed
......@@ -255,6 +342,68 @@ void DataTransfer::SetDragImageElement(Node* node, const IntPoint& loc) {
setDragImage(0, node, loc);
}
// static
// Converts from bounds in CSS space to device space based on the given
// frame.
FloatRect DataTransfer::DeviceSpaceBounds(const FloatRect css_bounds,
const LocalFrame& frame) {
float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated();
float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale();
FloatRect device_bounds(css_bounds);
device_bounds.SetWidth(css_bounds.Width() * device_scale_factor *
page_scale_factor);
device_bounds.SetHeight(css_bounds.Height() * device_scale_factor *
page_scale_factor);
return device_bounds;
}
// static
// Returns a DragImage whose bitmap contains |contents|, positioned and scaled
// in device space.
std::unique_ptr<DragImage> DataTransfer::CreateDragImageForFrame(
const LocalFrame& frame,
float opacity,
RespectImageOrientationEnum image_orientation,
const FloatRect& css_bounds,
PaintRecordBuilder& builder,
const PropertyTreeState& property_tree_state) {
float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated();
float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale();
FloatRect device_bounds = DeviceSpaceBounds(css_bounds, frame);
AffineTransform transform;
transform.Scale(device_scale_factor * page_scale_factor);
transform.Translate(-device_bounds.X(), -device_bounds.Y());
// Rasterize upfront, since DragImage::create() is going to do it anyway
// (SkImage::asLegacyBitmap).
SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(
device_bounds.Width(), device_bounds.Height(), &surface_props);
if (!surface)
return nullptr;
SkiaPaintCanvas skia_paint_canvas(surface->getCanvas());
skia_paint_canvas.concat(AffineTransformToSkMatrix(transform));
builder.EndRecording(skia_paint_canvas, property_tree_state);
RefPtr<Image> image = StaticBitmapImage::Create(surface->makeImageSnapshot());
float screen_device_scale_factor =
frame.GetPage()->GetChromeClient().GetScreenInfo().device_scale_factor;
return DragImage::Create(image.Get(), image_orientation,
screen_device_scale_factor, kInterpolationHigh,
opacity);
}
// static
std::unique_ptr<DragImage> DataTransfer::NodeImage(const LocalFrame& frame,
Node& node) {
DraggedNodeImageBuilder image_node(frame, node);
return image_node.CreateImage();
}
std::unique_ptr<DragImage> DataTransfer::CreateDragImage(
IntPoint& loc,
LocalFrame* frame) const {
......
......@@ -52,7 +52,6 @@
#include "core/frame/LocalFrameView.h"
#include "core/frame/PerformanceMonitor.h"
#include "core/frame/Settings.h"
#include "core/frame/VisualViewport.h"
#include "core/html/HTMLFrameElementBase.h"
#include "core/html/HTMLPlugInElement.h"
#include "core/html/PluginDocument.h"
......@@ -66,32 +65,23 @@
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoadRequest.h"
#include "core/loader/NavigationScheduler.h"
#include "core/page/ChromeClient.h"
#include "core/page/DragController.h"
#include "core/page/FocusController.h"
#include "core/page/Page.h"
#include "core/page/scrolling/ScrollingCoordinator.h"
#include "core/paint/ObjectPainter.h"
#include "core/paint/PaintInfo.h"
#include "core/paint/PaintLayer.h"
#include "core/paint/PaintLayerPainter.h"
#include "core/paint/TransformRecorder.h"
#include "core/plugins/PluginView.h"
#include "core/probe/CoreProbes.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "core/timing/Performance.h"
#include "platform/DragImage.h"
#include "platform/Histogram.h"
#include "platform/PluginScriptForbiddenScope.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/ScriptForbiddenScope.h"
#include "platform/WebFrameScheduler.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/StaticBitmapImage.h"
#include "platform/graphics/paint/ClipRecorder.h"
#include "platform/graphics/paint/PaintCanvas.h"
#include "platform/graphics/paint/PaintController.h"
#include "platform/graphics/paint/PaintRecordBuilder.h"
#include "platform/graphics/paint/TransformDisplayItem.h"
#include "platform/instrumentation/resource_coordinator/FrameResourceCoordinator.h"
#include "platform/json/JSONValues.h"
......@@ -105,156 +95,14 @@
#include "platform/wtf/StdLibExtras.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/InterfaceRegistry.h"
#include "public/platform/WebScreenInfo.h"
#include "public/platform/WebURLRequest.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace blink {
using namespace HTMLNames;
// static
// Converts from bounds in CSS space to device space based on the given
// frame.
// TODO(tanvir.rizvi): DeviceSpaceBounds is used for drag related functionality
// and is irrelevant to core functionality of LocalFrame. This should be moved
// out of LocalFrame to appropriate place.
FloatRect DataTransfer::DeviceSpaceBounds(const FloatRect css_bounds,
const LocalFrame& frame) {
float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated();
float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale();
FloatRect device_bounds(css_bounds);
device_bounds.SetWidth(css_bounds.Width() * device_scale_factor *
page_scale_factor);
device_bounds.SetHeight(css_bounds.Height() * device_scale_factor *
page_scale_factor);
return device_bounds;
}
// static
// Returns a DragImage whose bitmap contains |contents|, positioned and scaled
// in device space.
// TODO(tanvir.rizvi): CreateDragImageForFrame is used for drag related
// functionality and is irrelevant to core functionality of LocalFrame. This
// should be moved out of LocalFrame to appropriate place.
std::unique_ptr<DragImage> DataTransfer::CreateDragImageForFrame(
const LocalFrame& frame,
float opacity,
RespectImageOrientationEnum image_orientation,
const FloatRect& css_bounds,
PaintRecordBuilder& builder,
const PropertyTreeState& property_tree_state) {
float device_scale_factor = frame.GetPage()->DeviceScaleFactorDeprecated();
float page_scale_factor = frame.GetPage()->GetVisualViewport().Scale();
FloatRect device_bounds = DeviceSpaceBounds(css_bounds, frame);
AffineTransform transform;
transform.Scale(device_scale_factor * page_scale_factor);
transform.Translate(-device_bounds.X(), -device_bounds.Y());
// Rasterize upfront, since DragImage::create() is going to do it anyway
// (SkImage::asLegacyBitmap).
SkSurfaceProps surface_props(0, kUnknown_SkPixelGeometry);
sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(
device_bounds.Width(), device_bounds.Height(), &surface_props);
if (!surface)
return nullptr;
SkiaPaintCanvas skia_paint_canvas(surface->getCanvas());
skia_paint_canvas.concat(AffineTransformToSkMatrix(transform));
builder.EndRecording(skia_paint_canvas, property_tree_state);
RefPtr<Image> image = StaticBitmapImage::Create(surface->makeImageSnapshot());
float screen_device_scale_factor =
frame.GetPage()->GetChromeClient().GetScreenInfo().device_scale_factor;
return DragImage::Create(image.Get(), image_orientation,
screen_device_scale_factor, kInterpolationHigh,
opacity);
}
namespace {
// TODO(tanvir.rizvi): DraggedNodeImageBuilder is used for drag related
// functionality and is irrelevant to core functionality of LocalFrame. This
// should be moved out of LocalFrame to appropriate place.
class DraggedNodeImageBuilder {
STACK_ALLOCATED();
public:
DraggedNodeImageBuilder(const LocalFrame& local_frame, Node& node)
: local_frame_(&local_frame),
node_(&node)
#if DCHECK_IS_ON()
,
dom_tree_version_(node.GetDocument().DomTreeVersion())
#endif
{
for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_))
descendant.SetDragged(true);
}
~DraggedNodeImageBuilder() {
#if DCHECK_IS_ON()
DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion());
#endif
for (Node& descendant : NodeTraversal::InclusiveDescendantsOf(*node_))
descendant.SetDragged(false);
}
std::unique_ptr<DragImage> CreateImage() {
#if DCHECK_IS_ON()
DCHECK_EQ(dom_tree_version_, node_->GetDocument().DomTreeVersion());
#endif
// Construct layout object for |m_node| with pseudo class "-webkit-drag"
local_frame_->View()->UpdateAllLifecyclePhasesExceptPaint();
LayoutObject* const dragged_layout_object = node_->GetLayoutObject();
if (!dragged_layout_object)
return nullptr;
// Paint starting at the nearest stacking context, clipped to the object
// itself. This will also paint the contents behind the object if the
// object contains transparency and there are other elements in the same
// stacking context which stacked below.
PaintLayer* layer = dragged_layout_object->EnclosingLayer();
if (!layer->StackingNode()->IsStackingContext())
layer = layer->StackingNode()->AncestorStackingContextNode()->Layer();
IntRect absolute_bounding_box =
dragged_layout_object->AbsoluteBoundingBoxRectIncludingDescendants();
FloatRect bounding_box =
layer->GetLayoutObject()
.AbsoluteToLocalQuad(FloatQuad(absolute_bounding_box),
kUseTransforms)
.BoundingBox();
PaintLayerPaintingInfo painting_info(layer, LayoutRect(bounding_box),
kGlobalPaintFlattenCompositingLayers,
LayoutSize());
PaintLayerFlags flags = kPaintLayerHaveTransparency |
kPaintLayerAppliedTransform |
kPaintLayerUncachedClipRects;
PaintRecordBuilder builder(
DataTransfer::DeviceSpaceBounds(bounding_box, *local_frame_));
PaintLayerPainter(*layer).Paint(builder.Context(), painting_info, flags);
PropertyTreeState border_box_properties = PropertyTreeState::Root();
if (RuntimeEnabledFeatures::SlimmingPaintV2Enabled()) {
border_box_properties =
*layer->GetLayoutObject().LocalBorderBoxProperties();
}
return DataTransfer::CreateDragImageForFrame(
*local_frame_, 1.0f,
LayoutObject::ShouldRespectImageOrientation(dragged_layout_object),
bounding_box, builder, border_box_properties);
}
private:
const Member<const LocalFrame> local_frame_;
const Member<Node> node_;
#if DCHECK_IS_ON()
const uint64_t dom_tree_version_;
#endif
};
inline float ParentPageZoomFactor(LocalFrame* frame) {
Frame* parent = frame->Tree().Parent();
if (!parent || !parent->IsLocalFrame())
......@@ -768,42 +616,6 @@ double LocalFrame::DevicePixelRatio() const {
return ratio;
}
// static
// TODO(tanvir.rizvi): NodeImage is used only by DataTransfer,
// and is irrelevant to LocalFrame core functionality, so it can be moved to
// DataTransfer.
std::unique_ptr<DragImage> DataTransfer::NodeImage(const LocalFrame& frame,
Node& node) {
DraggedNodeImageBuilder image_node(frame, node);
return image_node.CreateImage();
}
// static
// TODO(tanvir.rizvi): DragImageForSelection is used only by DragController,
// and is irrelevant to LocalFrame core functionality, so it can be moved to
// DragController.
std::unique_ptr<DragImage> DragController::DragImageForSelection(
const LocalFrame& frame,
float opacity) {
if (!frame.Selection().ComputeVisibleSelectionInDOMTreeDeprecated().IsRange())
return nullptr;
frame.View()->UpdateAllLifecyclePhasesExceptPaint();
DCHECK(frame.GetDocument()->IsActive());
FloatRect painting_rect = FloatRect(frame.Selection().Bounds());
GlobalPaintFlags paint_flags =
kGlobalPaintSelectionOnly | kGlobalPaintFlattenCompositingLayers;
PaintRecordBuilder builder(
DataTransfer::DeviceSpaceBounds(painting_rect, frame));
frame.View()->PaintContents(builder.Context(), paint_flags,
EnclosingIntRect(painting_rect));
return DataTransfer::CreateDragImageForFrame(
frame, opacity, kDoNotRespectImageOrientation, painting_rect, builder,
PropertyTreeState::Root());
}
String LocalFrame::SelectedText() const {
return Selection().SelectedText();
}
......
......@@ -76,6 +76,7 @@
#include "platform/graphics/BitmapImage.h"
#include "platform/graphics/Image.h"
#include "platform/graphics/ImageOrientation.h"
#include "platform/graphics/paint/PaintRecordBuilder.h"
#include "platform/loader/fetch/ResourceFetcher.h"
#include "platform/loader/fetch/ResourceRequest.h"
#include "platform/weborigin/SecurityOrigin.h"
......@@ -1080,6 +1081,29 @@ static std::unique_ptr<DragImage> DragImageForLink(
return drag_image;
}
// static
std::unique_ptr<DragImage> DragController::DragImageForSelection(
const LocalFrame& frame,
float opacity) {
if (!frame.Selection().ComputeVisibleSelectionInDOMTreeDeprecated().IsRange())
return nullptr;
frame.View()->UpdateAllLifecyclePhasesExceptPaint();
DCHECK(frame.GetDocument()->IsActive());
FloatRect painting_rect = FloatRect(frame.Selection().Bounds());
GlobalPaintFlags paint_flags =
kGlobalPaintSelectionOnly | kGlobalPaintFlattenCompositingLayers;
PaintRecordBuilder builder(
DataTransfer::DeviceSpaceBounds(painting_rect, frame));
frame.View()->PaintContents(builder.Context(), paint_flags,
EnclosingIntRect(painting_rect));
return DataTransfer::CreateDragImageForFrame(
frame, opacity, kDoNotRespectImageOrientation, painting_rect, builder,
PropertyTreeState::Root());
}
bool DragController::StartDrag(LocalFrame* src,
const DragState& state,
const WebMouseEvent& drag_event,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment