// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_WEB_CONTENTS_WEB_DRAG_DEST_MAC_H_
#define CONTENT_BROWSER_WEB_CONTENTS_WEB_DRAG_DEST_MAC_H_

#import <Cocoa/Cocoa.h>

#include "base/memory/weak_ptr.h"
#include "content/browser/web_contents/web_contents_view_drag_security_info.h"
#include "content/common/content_export.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/common/drop_data.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/gfx/geometry/point_f.h"

namespace content {
class RenderViewHost;
class RenderWidgetHostImpl;
class RenderWidgetHostViewBase;
class WebContentsImpl;
class WebDragDestDelegate;
}  // namespace content

// A typedef for a RenderViewHost used for comparison purposes only.
using RenderViewHostIdentifier = content::RenderViewHost*;

namespace remote_cocoa::mojom {
class DraggingInfo;
}  // namespace remote_cocoa::mojom

namespace content {
class WebContentsViewDelegate;

// A structure used to keep drop context for asynchronously finishing a drop
// operation. This is required because some drop event data can change before
// finishDropWithData:context: is called.
struct CONTENT_EXPORT DropContext {
  DropContext(const DropData drop_data,
              const gfx::PointF client_pt,
              const gfx::PointF screen_pt,
              int modifier_flags,
              base::WeakPtr<RenderWidgetHostImpl> target_rwh);
  DropContext(const DropContext& other);
  DropContext(DropContext&& other);
  ~DropContext();

  DropData drop_data;
  const gfx::PointF client_pt;
  const gfx::PointF screen_pt;
  const int modifier_flags;
  base::WeakPtr<RenderWidgetHostImpl> target_rwh;
};

// Given a pasteboard, returns a `DropData` filled using its contents. The types
// handled by this method must be kept in sync with `-[WebContentsViewCocoa
// registerDragTypes]`.
DropData CONTENT_EXPORT PopulateDropDataFromPasteboard(NSPasteboard* pboard);

}  // namespace content

// A class that handles tracking and event processing for a drag and drop
// over the content area. Assumes something else initiates the drag, this is
// only for processing during a drag.
CONTENT_EXPORT
@interface WebDragDest : NSObject

// |contents| is the WebContentsImpl representing this tab, used to communicate
// drag&drop messages to WebCore and handle navigation on a successful drop
// (if necessary).
- (id)initWithWebContentsImpl:(content::WebContentsImpl*)contents;

- (content::DropData*)currentDropData;

- (void)setDragDelegate:(content::WebDragDestDelegate*)delegate;

// Sets the current operation negotiated by the source and destination,
// which determines whether or not we should allow the drop. Takes effect the
// next time |-draggingUpdated:| is called.
//
// See mojo method FrameWidget::DragTargetDragEnter() for a discussion of
// |operation| and |documentOperation|.
- (void)setCurrentOperation:(ui::mojom::DragOperation)operation
     documentIsHandlingDrag:(bool)documentIsHandlingDrag;

// Messages to send during the tracking of a drag, usually upon receiving
// calls from the view system. Communicates the drag messages to WebCore.
- (void)setDropData:(const content::DropData&)dropData;
- (NSDragOperation)draggingEntered:
    (const remote_cocoa::mojom::DraggingInfo*)info;
- (void)draggingExited;
- (NSDragOperation)draggingUpdated:
    (const remote_cocoa::mojom::DraggingInfo*)info;
- (BOOL)performDragOperation:(const remote_cocoa::mojom::DraggingInfo*)info
    withWebContentsViewDelegate:
        (content::WebContentsViewDelegate*)webContentsViewDelegate;
- (void)finishDropWithData:(std::optional<content::DropData>)dropData
                   context:(const content::DropContext)context;

// Called to indicate that the owning WebContents has initiated a drag.
- (void)initiateDragWithRenderWidgetHost:(content::RenderWidgetHostImpl*)rwhi
                                dropData:(const content::DropData&)dropData;

// Called to indicate that, if the owning WebContents has initiated a drag, that
// drag has ended. The closure will be called synchronously if the "dragend"
// event can be fired, or asynchronously if a "drop" event is still pending
// since it should be fired first.
- (void)endDrag:(base::OnceClosure)closure;

// Async drag callbacks (called from async helpers).
- (void)dragEnterHitTestDidCompleteForView:
            (base::WeakPtr<content::RenderWidgetHostViewBase>)targetView
                          transformedPoint:(const gfx::PointF&)transformedPoint
                            sequenceNumber:(uint64_t)sequenceNumber;

- (void)dragUpdateHitTestDidCompleteForView:
            (base::WeakPtr<content::RenderWidgetHostViewBase>)targetView
                           transformedPoint:(const gfx::PointF&)transformedPoint
                             sequenceNumber:(uint64_t)sequenceNumber;

- (void)dropHitTestDidCompleteForView:
            (base::WeakPtr<content::RenderWidgetHostViewBase>)targetView
                     transformedPoint:(const gfx::PointF&)transformedPoint
                       sequenceNumber:(uint64_t)sequenceNumber;

// Helper method to cleanup drag state consistently across error paths.
- (void)cleanupDragState;

- (bool)dropInProgressForTesting;
- (void)setDropInProgressForTesting;

@end

#endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_DRAG_DEST_MAC_H_
