1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/cocoa/web_drop_target.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_node_data.h"
#include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#import "third_party/mozilla/NSPasteboard+Utils.h"
#include "webkit/glue/webdropdata.h"
#include "webkit/glue/window_open_disposition.h"
using WebKit::WebDragOperationsMask;
@implementation WebDropTarget
// |contents| is the TabContents representing this tab, used to communicate
// drag&drop messages to WebCore and handle navigation on a successful drop
// (if necessary).
- (id)initWithTabContents:(TabContents*)contents {
if ((self = [super init])) {
tabContents_ = contents;
}
return self;
}
// Call to set whether or not we should allow the drop. Takes effect the
// next time |-draggingUpdated:| is called.
- (void)setCurrentOperation: (NSDragOperation)operation {
current_operation_ = operation;
}
// Given a point in window coordinates and a view in that window, return a
// flipped point in the coordinate system of |view|.
- (NSPoint)flipWindowPointToView:(const NSPoint&)windowPoint
view:(NSView*)view {
DCHECK(view);
NSPoint viewPoint = [view convertPoint:windowPoint fromView:nil];
NSRect viewFrame = [view frame];
viewPoint.y = viewFrame.size.height - viewPoint.y;
return viewPoint;
}
// Given a point in window coordinates and a view in that window, return a
// flipped point in screen coordinates.
- (NSPoint)flipWindowPointToScreen:(const NSPoint&)windowPoint
view:(NSView*)view {
DCHECK(view);
NSPoint screenPoint = [[view window] convertBaseToScreen:windowPoint];
NSScreen* screen = [[view window] screen];
NSRect screenFrame = [screen frame];
screenPoint.y = screenFrame.size.height - screenPoint.y;
return screenPoint;
}
// Return YES if the drop site only allows drops that would navigate. If this
// is the case, we don't want to pass messages to the renderer because there's
// really no point (i.e., there's nothing that cares about the mouse position or
// entering and exiting). One example is an interstitial page (e.g., safe
// browsing warning).
- (BOOL)onlyAllowsNavigation {
return tabContents_->showing_interstitial_page();
}
// Messages to send during the tracking of a drag, ususally upon recieving
// calls from the view system. Communicates the drag messages to WebCore.
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info
view:(NSView*)view {
// Save off the RVH so we can tell if it changes during a drag. If it does,
// we need to send a new enter message in draggingUpdated:.
currentRVH_ = tabContents_->render_view_host();
if ([self onlyAllowsNavigation]) {
if ([[info draggingPasteboard] containsURLData])
return NSDragOperationCopy;
return NSDragOperationNone;
}
// If the tab is showing the boomark manager, send BookmarkDrag events
RenderViewHostDelegate::BookmarkDrag* dragDelegate =
tabContents_->GetBookmarkDragDelegate();
BookmarkNodeData dragData;
if(dragDelegate && dragData.ReadFromDragClipboard())
dragDelegate->OnDragEnter(dragData);
// Fill out a WebDropData from pasteboard.
WebDropData data;
[self populateWebDropData:&data fromPasteboard:[info draggingPasteboard]];
// Create the appropriate mouse locations for WebCore. The draggingLocation
// is in window coordinates. Both need to be flipped.
NSPoint windowPoint = [info draggingLocation];
NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view];
NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view];
NSDragOperation mask = [info draggingSourceOperationMask];
tabContents_->render_view_host()->DragTargetDragEnter(data,
gfx::Point(viewPoint.x, viewPoint.y),
gfx::Point(screenPoint.x, screenPoint.y),
static_cast<WebDragOperationsMask>(mask));
// We won't know the true operation (whether the drag is allowed) until we
// hear back from the renderer. For now, be optimistic:
current_operation_ = NSDragOperationCopy;
return current_operation_;
}
- (void)draggingExited:(id<NSDraggingInfo>)info {
DCHECK(currentRVH_);
if (currentRVH_ != tabContents_->render_view_host())
return;
// Nothing to do in the interstitial case.
tabContents_->render_view_host()->DragTargetDragLeave();
}
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)info
view:(NSView*)view {
DCHECK(currentRVH_);
if (currentRVH_ != tabContents_->render_view_host())
[self draggingEntered:info view:view];
if ([self onlyAllowsNavigation]) {
if ([[info draggingPasteboard] containsURLData])
return NSDragOperationCopy;
return NSDragOperationNone;
}
// Create the appropriate mouse locations for WebCore. The draggingLocation
// is in window coordinates.
NSPoint windowPoint = [info draggingLocation];
NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view];
NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view];
NSDragOperation mask = [info draggingSourceOperationMask];
tabContents_->render_view_host()->DragTargetDragOver(
gfx::Point(viewPoint.x, viewPoint.y),
gfx::Point(screenPoint.x, screenPoint.y),
static_cast<WebDragOperationsMask>(mask));
// If the tab is showing the boomark manager, send BookmarkDrag events
RenderViewHostDelegate::BookmarkDrag* dragDelegate =
tabContents_->GetBookmarkDragDelegate();
BookmarkNodeData dragData;
if(dragDelegate && dragData.ReadFromDragClipboard())
dragDelegate->OnDragOver(dragData);
return current_operation_;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)info
view:(NSView*)view {
if (currentRVH_ != tabContents_->render_view_host())
[self draggingEntered:info view:view];
// Check if we only allow navigation and navigate to a url on the pasteboard.
if ([self onlyAllowsNavigation]) {
NSPasteboard* pboard = [info draggingPasteboard];
if ([pboard containsURLData]) {
GURL url;
[self populateURL:&url
andTitle:NULL
fromPasteboard:pboard
convertingFilenames:YES];
tabContents_->OpenURL(url, GURL(), CURRENT_TAB,
PageTransition::AUTO_BOOKMARK);
return YES;
}
return NO;
}
// If the tab is showing the boomark manager, send BookmarkDrag events
RenderViewHostDelegate::BookmarkDrag* dragDelegate =
tabContents_->GetBookmarkDragDelegate();
BookmarkNodeData dragData;
if(dragDelegate && dragData.ReadFromDragClipboard())
dragDelegate->OnDrop(dragData);
currentRVH_ = NULL;
// Create the appropriate mouse locations for WebCore. The draggingLocation
// is in window coordinates. Both need to be flipped.
NSPoint windowPoint = [info draggingLocation];
NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view];
NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view];
tabContents_->render_view_host()->DragTargetDrop(
gfx::Point(viewPoint.x, viewPoint.y),
gfx::Point(screenPoint.x, screenPoint.y));
return YES;
}
// Populate the |url| and |title| with URL data in |pboard|. There may be more
// than one, but we only handle dropping the first. |url| must not be |NULL|;
// |title| is an optional parameter. Returns |YES| if URL data was obtained from
// the pasteboard, |NO| otherwise. If |convertFilenames| is |YES|, the function
// will also attempt to convert filenames in |pboard| to file URLs.
- (BOOL)populateURL:(GURL*)url
andTitle:(string16*)title
fromPasteboard:(NSPasteboard*)pboard
convertingFilenames:(BOOL)convertFilenames {
DCHECK(url);
DCHECK(title);
// Bail out early if there's no URL data.
if (![pboard containsURLData])
return NO;
// |-getURLs:andTitles:convertingFilenames:| will already validate URIs so we
// don't need to again. The arrays returned are both of NSString's.
NSArray* urls = nil;
NSArray* titles = nil;
[pboard getURLs:&urls andTitles:&titles convertingFilenames:convertFilenames];
DCHECK_EQ([urls count], [titles count]);
// It's possible that no URLs were actually provided!
if (![urls count])
return NO;
NSString* urlString = [urls objectAtIndex:0];
if ([urlString length]) {
// Check again just to make sure to not assign NULL into a std::string,
// which throws an exception.
const char* utf8Url = [urlString UTF8String];
if (utf8Url) {
*url = GURL(utf8Url);
// Extra paranoia check.
if (title && [titles count])
*title = base::SysNSStringToUTF16([titles objectAtIndex:0]);
}
}
return YES;
}
// Given |data|, which should not be nil, fill it in using the contents of the
// given pasteboard.
- (void)populateWebDropData:(WebDropData*)data
fromPasteboard:(NSPasteboard*)pboard {
DCHECK(data);
DCHECK(pboard);
NSArray* types = [pboard types];
// Get URL if possible. To avoid exposing file system paths to web content,
// filenames in the drag are not converted to file URLs.
[self populateURL:&data->url
andTitle:&data->url_title
fromPasteboard:pboard
convertingFilenames:NO];
// Get plain text.
if ([types containsObject:NSStringPboardType]) {
data->plain_text =
base::SysNSStringToUTF16([pboard stringForType:NSStringPboardType]);
}
// Get HTML. If there's no HTML, try RTF.
if ([types containsObject:NSHTMLPboardType]) {
data->text_html =
base::SysNSStringToUTF16([pboard stringForType:NSHTMLPboardType]);
} else if ([types containsObject:NSRTFPboardType]) {
NSString* html = [pboard htmlFromRtf];
data->text_html = base::SysNSStringToUTF16(html);
}
// Get files.
if ([types containsObject:NSFilenamesPboardType]) {
NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
if ([files isKindOfClass:[NSArray class]] && [files count]) {
for (NSUInteger i = 0; i < [files count]; i++) {
NSString* filename = [files objectAtIndex:i];
BOOL isDir = NO;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filename
isDirectory:&isDir];
if (exists && !isDir)
data->filenames.push_back(base::SysNSStringToUTF16(filename));
}
}
}
// TODO(pinkerton): Get file contents. http://crbug.com/34661
}
@end
|