1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/url_drop_target.h"
7 #include "base/basictypes.h"
8 #include "chrome/browser/ui/cocoa/drag_util.h"
9 #import "third_party/mozilla/NSPasteboard+Utils.h"
14 // Mac WebKit uses this type, declared in
15 // WebKit/mac/History/WebURLsWithTitles.h.
16 NSString* const kCrWebURLsWithTitlesPboardType =
17 @"WebURLsWithTitlesPboardType";
21 @interface URLDropTargetHandler(Private)
23 // Gets the appropriate drag operation given the |NSDraggingInfo|.
24 - (NSDragOperation)getDragOperation:(id<NSDraggingInfo>)sender;
26 // Tell the window controller to hide the drop indicator.
27 - (void)hideIndicator;
29 @end // @interface URLDropTargetHandler(Private)
31 @implementation URLDropTargetHandler
33 + (NSArray*)handledDragTypes {
34 return [NSArray arrayWithObjects:kCrWebURLsWithTitlesPboardType,
37 NSFilenamesPboardType,
41 - (id)initWithView:(NSView<URLDropTarget>*)view {
42 if ((self = [super init])) {
44 [view_ registerForDraggedTypes:[URLDropTargetHandler handledDragTypes]];
49 // The following four methods implement parts of the |NSDraggingDestination|
50 // protocol, which the owner should "forward" to its |URLDropTargetHandler|
53 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
54 if ([[view_ urlDropController] isUnsupportedDropData:sender])
55 return NSDragOperationNone;
57 return [self getDragOperation:sender];
60 - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
61 NSDragOperation dragOp = NSDragOperationNone;
62 BOOL showIndicator = NO;
63 // Show indicator for drag data supported for tab contents as well as for
64 // local file drags that may not be viewable in tab contents, but should
65 // still trigger hover tab selection.
66 if (![[view_ urlDropController] isUnsupportedDropData:sender]) {
67 dragOp = [self getDragOperation:sender];
68 if (dragOp == NSDragOperationCopy)
70 } else if (!drag_util::GetFileURLFromDropData(sender).is_empty()) {
75 // Just tell the window controller to update the indicator.
76 NSPoint hoverPoint = [view_ convertPoint:[sender draggingLocation]
78 [[view_ urlDropController] indicateDropURLsInView:view_ at:hoverPoint];
83 - (void)draggingExited:(id<NSDraggingInfo>)sender {
87 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
90 NSPasteboard* pboard = [sender draggingPasteboard];
91 NSArray* supportedTypes = [NSArray arrayWithObjects:NSStringPboardType, nil];
92 NSString* bestType = [pboard availableTypeFromArray:supportedTypes];
95 [view_ convertPoint:[sender draggingLocation] fromView:nil];
96 // Tell the window controller about the dropped URL(s).
97 if ([pboard containsURLData]) {
99 NSArray* titles; // discarded
100 [pboard getURLs:&urls andTitles:&titles convertingFilenames:YES];
103 [[view_ urlDropController] dropURLs:urls inView:view_ at:dropPoint];
106 } else if (NSString* text = [pboard stringForType:bestType]) {
107 // This does not include any URLs, so treat it as plain text if we can
109 [[view_ urlDropController] dropText:text inView:view_ at:dropPoint];
116 @end // @implementation URLDropTargetHandler
118 @implementation URLDropTargetHandler(Private)
120 - (NSDragOperation)getDragOperation:(id<NSDraggingInfo>)sender {
121 NSPasteboard* pboard = [sender draggingPasteboard];
122 NSArray *supportedTypes = [NSArray arrayWithObjects:NSStringPboardType, nil];
123 NSString *bestType = [pboard availableTypeFromArray:supportedTypes];
124 if (![pboard containsURLData] && ![pboard stringForType:bestType])
125 return NSDragOperationNone;
127 // Only allow the copy operation.
128 return [sender draggingSourceOperationMask] & NSDragOperationCopy;
131 - (void)hideIndicator {
132 [[view_ urlDropController] hideDropURLsIndicatorInView:view_];
135 @end // @implementation URLDropTargetHandler(Private)