| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #import "content/browser/tab_contents/web_drag_dest_mac.h" | |
| 6 | |
| 7 #include "base/sys_string_conversions.h" | |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 9 #include "content/browser/tab_contents/tab_contents.h" | |
| 10 #include "content/public/browser/web_drag_dest_delegate.h" | |
| 11 #import "third_party/mozilla/NSPasteboard+Utils.h" | |
| 12 #include "ui/base/clipboard/custom_data_helper.h" | |
| 13 #import "ui/base/dragdrop/cocoa_dnd_util.h" | |
| 14 #include "webkit/glue/webdropdata.h" | |
| 15 #include "webkit/glue/window_open_disposition.h" | |
| 16 | |
| 17 using WebKit::WebDragOperationsMask; | |
| 18 using content::OpenURLParams; | |
| 19 using content::Referrer; | |
| 20 | |
| 21 @implementation WebDragDest | |
| 22 | |
| 23 // |contents| is the WebContentsImpl representing this tab, used to communicate | |
| 24 // drag&drop messages to WebCore and handle navigation on a successful drop | |
| 25 // (if necessary). | |
| 26 - (id)initWithWebContentsImpl:(WebContentsImpl*)contents { | |
| 27 if ((self = [super init])) { | |
| 28 webContents_ = contents; | |
| 29 } | |
| 30 return self; | |
| 31 } | |
| 32 | |
| 33 - (void)setDragDelegate:(content::WebDragDestDelegate*)delegate { | |
| 34 delegate_ = delegate; | |
| 35 } | |
| 36 | |
| 37 // Call to set whether or not we should allow the drop. Takes effect the | |
| 38 // next time |-draggingUpdated:| is called. | |
| 39 - (void)setCurrentOperation:(NSDragOperation)operation { | |
| 40 current_operation_ = operation; | |
| 41 } | |
| 42 | |
| 43 // Given a point in window coordinates and a view in that window, return a | |
| 44 // flipped point in the coordinate system of |view|. | |
| 45 - (NSPoint)flipWindowPointToView:(const NSPoint&)windowPoint | |
| 46 view:(NSView*)view { | |
| 47 DCHECK(view); | |
| 48 NSPoint viewPoint = [view convertPoint:windowPoint fromView:nil]; | |
| 49 NSRect viewFrame = [view frame]; | |
| 50 viewPoint.y = viewFrame.size.height - viewPoint.y; | |
| 51 return viewPoint; | |
| 52 } | |
| 53 | |
| 54 // Given a point in window coordinates and a view in that window, return a | |
| 55 // flipped point in screen coordinates. | |
| 56 - (NSPoint)flipWindowPointToScreen:(const NSPoint&)windowPoint | |
| 57 view:(NSView*)view { | |
| 58 DCHECK(view); | |
| 59 NSPoint screenPoint = [[view window] convertBaseToScreen:windowPoint]; | |
| 60 NSScreen* screen = [[view window] screen]; | |
| 61 NSRect screenFrame = [screen frame]; | |
| 62 screenPoint.y = screenFrame.size.height - screenPoint.y; | |
| 63 return screenPoint; | |
| 64 } | |
| 65 | |
| 66 // Return YES if the drop site only allows drops that would navigate. If this | |
| 67 // is the case, we don't want to pass messages to the renderer because there's | |
| 68 // really no point (i.e., there's nothing that cares about the mouse position or | |
| 69 // entering and exiting). One example is an interstitial page (e.g., safe | |
| 70 // browsing warning). | |
| 71 - (BOOL)onlyAllowsNavigation { | |
| 72 return webContents_->ShowingInterstitialPage(); | |
| 73 } | |
| 74 | |
| 75 // Messages to send during the tracking of a drag, usually upon receiving | |
| 76 // calls from the view system. Communicates the drag messages to WebCore. | |
| 77 | |
| 78 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info | |
| 79 view:(NSView*)view { | |
| 80 // Save off the RVH so we can tell if it changes during a drag. If it does, | |
| 81 // we need to send a new enter message in draggingUpdated:. | |
| 82 currentRVH_ = webContents_->GetRenderViewHost(); | |
| 83 | |
| 84 if ([self onlyAllowsNavigation]) { | |
| 85 if ([[info draggingPasteboard] containsURLData]) | |
| 86 return NSDragOperationCopy; | |
| 87 return NSDragOperationNone; | |
| 88 } | |
| 89 | |
| 90 if (delegate_) { | |
| 91 delegate_->DragInitialize(webContents_); | |
| 92 delegate_->OnDragEnter(); | |
| 93 } | |
| 94 | |
| 95 // Fill out a WebDropData from pasteboard. | |
| 96 WebDropData data; | |
| 97 [self populateWebDropData:&data fromPasteboard:[info draggingPasteboard]]; | |
| 98 | |
| 99 // Create the appropriate mouse locations for WebCore. The draggingLocation | |
| 100 // is in window coordinates. Both need to be flipped. | |
| 101 NSPoint windowPoint = [info draggingLocation]; | |
| 102 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | |
| 103 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | |
| 104 NSDragOperation mask = [info draggingSourceOperationMask]; | |
| 105 webContents_->GetRenderViewHost()->DragTargetDragEnter( | |
| 106 data, | |
| 107 gfx::Point(viewPoint.x, viewPoint.y), | |
| 108 gfx::Point(screenPoint.x, screenPoint.y), | |
| 109 static_cast<WebDragOperationsMask>(mask)); | |
| 110 | |
| 111 // We won't know the true operation (whether the drag is allowed) until we | |
| 112 // hear back from the renderer. For now, be optimistic: | |
| 113 current_operation_ = NSDragOperationCopy; | |
| 114 return current_operation_; | |
| 115 } | |
| 116 | |
| 117 - (void)draggingExited:(id<NSDraggingInfo>)info { | |
| 118 DCHECK(currentRVH_); | |
| 119 if (currentRVH_ != webContents_->GetRenderViewHost()) | |
| 120 return; | |
| 121 | |
| 122 // Nothing to do in the interstitial case. | |
| 123 | |
| 124 if (delegate_) | |
| 125 delegate_->OnDragLeave(); | |
| 126 | |
| 127 webContents_->GetRenderViewHost()->DragTargetDragLeave(); | |
| 128 } | |
| 129 | |
| 130 - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)info | |
| 131 view:(NSView*)view { | |
| 132 DCHECK(currentRVH_); | |
| 133 if (currentRVH_ != webContents_->GetRenderViewHost()) | |
| 134 [self draggingEntered:info view:view]; | |
| 135 | |
| 136 if ([self onlyAllowsNavigation]) { | |
| 137 if ([[info draggingPasteboard] containsURLData]) | |
| 138 return NSDragOperationCopy; | |
| 139 return NSDragOperationNone; | |
| 140 } | |
| 141 | |
| 142 // Create the appropriate mouse locations for WebCore. The draggingLocation | |
| 143 // is in window coordinates. | |
| 144 NSPoint windowPoint = [info draggingLocation]; | |
| 145 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | |
| 146 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | |
| 147 NSDragOperation mask = [info draggingSourceOperationMask]; | |
| 148 webContents_->GetRenderViewHost()->DragTargetDragOver( | |
| 149 gfx::Point(viewPoint.x, viewPoint.y), | |
| 150 gfx::Point(screenPoint.x, screenPoint.y), | |
| 151 static_cast<WebDragOperationsMask>(mask)); | |
| 152 | |
| 153 if (delegate_) | |
| 154 delegate_->OnDragOver(); | |
| 155 | |
| 156 return current_operation_; | |
| 157 } | |
| 158 | |
| 159 - (BOOL)performDragOperation:(id<NSDraggingInfo>)info | |
| 160 view:(NSView*)view { | |
| 161 if (currentRVH_ != webContents_->GetRenderViewHost()) | |
| 162 [self draggingEntered:info view:view]; | |
| 163 | |
| 164 // Check if we only allow navigation and navigate to a url on the pasteboard. | |
| 165 if ([self onlyAllowsNavigation]) { | |
| 166 NSPasteboard* pboard = [info draggingPasteboard]; | |
| 167 if ([pboard containsURLData]) { | |
| 168 GURL url; | |
| 169 ui::PopulateURLAndTitleFromPasteboard(&url, NULL, pboard, YES); | |
| 170 webContents_->OpenURL(OpenURLParams( | |
| 171 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_AUTO_BOOKMARK, | |
| 172 false)); | |
| 173 return YES; | |
| 174 } | |
| 175 return NO; | |
| 176 } | |
| 177 | |
| 178 if (delegate_) | |
| 179 delegate_->OnDrop(); | |
| 180 | |
| 181 currentRVH_ = NULL; | |
| 182 | |
| 183 // Create the appropriate mouse locations for WebCore. The draggingLocation | |
| 184 // is in window coordinates. Both need to be flipped. | |
| 185 NSPoint windowPoint = [info draggingLocation]; | |
| 186 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | |
| 187 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | |
| 188 webContents_->GetRenderViewHost()->DragTargetDrop( | |
| 189 gfx::Point(viewPoint.x, viewPoint.y), | |
| 190 gfx::Point(screenPoint.x, screenPoint.y)); | |
| 191 | |
| 192 return YES; | |
| 193 } | |
| 194 | |
| 195 // Given |data|, which should not be nil, fill it in using the contents of the | |
| 196 // given pasteboard. | |
| 197 - (void)populateWebDropData:(WebDropData*)data | |
| 198 fromPasteboard:(NSPasteboard*)pboard { | |
| 199 DCHECK(data); | |
| 200 DCHECK(pboard); | |
| 201 NSArray* types = [pboard types]; | |
| 202 | |
| 203 // Get URL if possible. To avoid exposing file system paths to web content, | |
| 204 // filenames in the drag are not converted to file URLs. | |
| 205 ui::PopulateURLAndTitleFromPasteboard(&data->url, | |
| 206 &data->url_title, | |
| 207 pboard, | |
| 208 NO); | |
| 209 | |
| 210 // Get plain text. | |
| 211 if ([types containsObject:NSStringPboardType]) { | |
| 212 data->plain_text = | |
| 213 base::SysNSStringToUTF16([pboard stringForType:NSStringPboardType]); | |
| 214 } | |
| 215 | |
| 216 // Get HTML. If there's no HTML, try RTF. | |
| 217 if ([types containsObject:NSHTMLPboardType]) { | |
| 218 data->text_html = | |
| 219 base::SysNSStringToUTF16([pboard stringForType:NSHTMLPboardType]); | |
| 220 } else if ([types containsObject:NSRTFPboardType]) { | |
| 221 NSString* html = [pboard htmlFromRtf]; | |
| 222 data->text_html = base::SysNSStringToUTF16(html); | |
| 223 } | |
| 224 | |
| 225 // Get files. | |
| 226 if ([types containsObject:NSFilenamesPboardType]) { | |
| 227 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType]; | |
| 228 if ([files isKindOfClass:[NSArray class]] && [files count]) { | |
| 229 for (NSUInteger i = 0; i < [files count]; i++) { | |
| 230 NSString* filename = [files objectAtIndex:i]; | |
| 231 BOOL isDir = NO; | |
| 232 BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filename | |
| 233 isDirectory:&isDir]; | |
| 234 if (exists && !isDir) | |
| 235 data->filenames.push_back(base::SysNSStringToUTF16(filename)); | |
| 236 } | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 // TODO(pinkerton): Get file contents. http://crbug.com/34661 | |
| 241 | |
| 242 // Get custom MIME data. | |
| 243 if ([types containsObject:ui::kWebCustomDataPboardType]) { | |
| 244 NSData* customData = [pboard dataForType:ui::kWebCustomDataPboardType]; | |
| 245 ui::ReadCustomDataIntoMap([customData bytes], | |
| 246 [customData length], | |
| 247 &data->custom_data); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 @end | |
| OLD | NEW |