| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "content/browser/web_contents/web_drag_dest_mac.h" | 5 #import "content/browser/web_contents/web_drag_dest_mac.h" |
| 6 | 6 |
| 7 #import <Carbon/Carbon.h> | 7 #import <Carbon/Carbon.h> |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "content/browser/renderer_host/render_view_host_impl.h" | 10 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 11 #include "content/browser/renderer_host/render_widget_host_impl.h" | 11 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_input_event_router.h" | 12 #include "content/browser/renderer_host/render_widget_host_input_event_router.h" |
| 13 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
| 14 #include "content/public/browser/web_contents_delegate.h" | 14 #include "content/public/browser/web_contents_delegate.h" |
| 15 #include "content/public/browser/web_drag_dest_delegate.h" | 15 #include "content/public/browser/web_drag_dest_delegate.h" |
| 16 #include "content/public/common/child_process_host.h" |
| 16 #include "content/public/common/drop_data.h" | 17 #include "content/public/common/drop_data.h" |
| 17 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 18 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 18 #import "third_party/mozilla/NSPasteboard+Utils.h" | 19 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 19 #include "ui/base/clipboard/custom_data_helper.h" | 20 #include "ui/base/clipboard/custom_data_helper.h" |
| 20 #include "ui/base/cocoa/cocoa_base_utils.h" | 21 #include "ui/base/cocoa/cocoa_base_utils.h" |
| 21 #import "ui/base/dragdrop/cocoa_dnd_util.h" | 22 #import "ui/base/dragdrop/cocoa_dnd_util.h" |
| 22 #include "ui/base/window_open_disposition.h" | 23 #include "ui/base/window_open_disposition.h" |
| 23 #include "ui/gfx/geometry/point.h" | 24 #include "ui/gfx/geometry/point.h" |
| 24 | 25 |
| 25 using blink::WebDragOperationsMask; | 26 using blink::WebDragOperationsMask; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 if (pressedButtons & (1 << 0)) | 51 if (pressedButtons & (1 << 0)) |
| 51 modifier_state |= blink::WebInputEvent::LeftButtonDown; | 52 modifier_state |= blink::WebInputEvent::LeftButtonDown; |
| 52 if (pressedButtons & (1 << 1)) | 53 if (pressedButtons & (1 << 1)) |
| 53 modifier_state |= blink::WebInputEvent::RightButtonDown; | 54 modifier_state |= blink::WebInputEvent::RightButtonDown; |
| 54 if (pressedButtons & (1 << 2)) | 55 if (pressedButtons & (1 << 2)) |
| 55 modifier_state |= blink::WebInputEvent::MiddleButtonDown; | 56 modifier_state |= blink::WebInputEvent::MiddleButtonDown; |
| 56 | 57 |
| 57 return modifier_state; | 58 return modifier_state; |
| 58 } | 59 } |
| 59 | 60 |
| 61 content::GlobalRoutingID GetRenderViewHostID(content::RenderViewHost* rvh) { |
| 62 return content::GlobalRoutingID(rvh->GetProcess()->GetID(), |
| 63 rvh->GetRoutingID()); |
| 64 } |
| 65 |
| 60 } // namespace | 66 } // namespace |
| 61 | 67 |
| 62 @implementation WebDragDest | 68 @implementation WebDragDest |
| 63 | 69 |
| 64 // |contents| is the WebContentsImpl representing this tab, used to communicate | 70 // |contents| is the WebContentsImpl representing this tab, used to communicate |
| 65 // drag&drop messages to WebCore and handle navigation on a successful drop | 71 // drag&drop messages to WebCore and handle navigation on a successful drop |
| 66 // (if necessary). | 72 // (if necessary). |
| 67 - (id)initWithWebContentsImpl:(WebContentsImpl*)contents { | 73 - (id)initWithWebContentsImpl:(WebContentsImpl*)contents { |
| 68 if ((self = [super init])) { | 74 if ((self = [super init])) { |
| 69 webContents_ = contents; | 75 webContents_ = contents; |
| 70 canceled_ = false; | 76 canceled_ = false; |
| 77 dragStartProcessID_ = content::ChildProcessHost::kInvalidUniqueID; |
| 78 dragStartViewID_ = content::GlobalRoutingID( |
| 79 content::ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); |
| 71 } | 80 } |
| 72 return self; | 81 return self; |
| 73 } | 82 } |
| 74 | 83 |
| 75 - (DropData*)currentDropData { | 84 - (DropData*)currentDropData { |
| 76 return dropData_.get(); | 85 return dropData_.get(); |
| 77 } | 86 } |
| 78 | 87 |
| 79 - (void)setDragDelegate:(content::WebDragDestDelegate*)delegate { | 88 - (void)setDragDelegate:(content::WebDragDestDelegate*)delegate { |
| 80 delegate_ = delegate; | 89 delegate_ = delegate; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | 143 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; |
| 135 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | 144 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; |
| 136 gfx::Point transformedPt; | 145 gfx::Point transformedPt; |
| 137 if (!webContents_->GetRenderWidgetHostView()) { | 146 if (!webContents_->GetRenderWidgetHostView()) { |
| 138 // TODO(ekaramad, paulmeyer): Find a better way than toggling |canceled_|. | 147 // TODO(ekaramad, paulmeyer): Find a better way than toggling |canceled_|. |
| 139 // This could happen when the renderer process for the top-level RWH crashes | 148 // This could happen when the renderer process for the top-level RWH crashes |
| 140 // (see https://crbug.com/670645). | 149 // (see https://crbug.com/670645). |
| 141 canceled_ = true; | 150 canceled_ = true; |
| 142 return NSDragOperationNone; | 151 return NSDragOperationNone; |
| 143 } | 152 } |
| 144 currentRWHForDrag_ = | 153 |
| 145 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt] | 154 content::RenderWidgetHostImpl* targetRWH = |
| 146 ->GetWeakPtr(); | 155 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt]; |
| 156 if (![self isValidDragTarget:targetRWH]) |
| 157 return NSDragOperationNone; |
| 158 |
| 159 currentRWHForDrag_ = targetRWH->GetWeakPtr(); |
| 147 | 160 |
| 148 // Fill out a DropData from pasteboard. | 161 // Fill out a DropData from pasteboard. |
| 149 std::unique_ptr<DropData> dropData; | 162 std::unique_ptr<DropData> dropData; |
| 150 dropData.reset(new DropData()); | 163 dropData.reset(new DropData()); |
| 151 [self populateDropData:dropData.get() | 164 [self populateDropData:dropData.get() |
| 152 fromPasteboard:[info draggingPasteboard]]; | 165 fromPasteboard:[info draggingPasteboard]]; |
| 153 // TODO(paulmeyer): Data may be pulled from the pasteboard multiple times per | 166 // TODO(paulmeyer): Data may be pulled from the pasteboard multiple times per |
| 154 // drag. Ideally, this should only be done once, and filtered as needed. | 167 // drag. Ideally, this should only be done once, and filtered as needed. |
| 155 currentRWHForDrag_->FilterDropData(dropData.get()); | 168 currentRWHForDrag_->FilterDropData(dropData.get()); |
| 156 | 169 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 230 |
| 218 // Create the appropriate mouse locations for WebCore. The draggingLocation | 231 // Create the appropriate mouse locations for WebCore. The draggingLocation |
| 219 // is in window coordinates. Both need to be flipped. | 232 // is in window coordinates. Both need to be flipped. |
| 220 NSPoint windowPoint = [info draggingLocation]; | 233 NSPoint windowPoint = [info draggingLocation]; |
| 221 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | 234 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; |
| 222 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | 235 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; |
| 223 gfx::Point transformedPt; | 236 gfx::Point transformedPt; |
| 224 content::RenderWidgetHostImpl* targetRWH = | 237 content::RenderWidgetHostImpl* targetRWH = |
| 225 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt]; | 238 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt]; |
| 226 | 239 |
| 240 if (![self isValidDragTarget:targetRWH]) |
| 241 return NSDragOperationNone; |
| 242 |
| 227 // TODO(paulmeyer): The dragging delegates may now by invoked multiple times | 243 // TODO(paulmeyer): The dragging delegates may now by invoked multiple times |
| 228 // per drag, even without the drag ever leaving the window. | 244 // per drag, even without the drag ever leaving the window. |
| 229 if (targetRWH != currentRWHForDrag_.get()) { | 245 if (targetRWH != currentRWHForDrag_.get()) { |
| 230 if (currentRWHForDrag_) | 246 if (currentRWHForDrag_) |
| 231 currentRWHForDrag_->DragTargetDragLeave(); | 247 currentRWHForDrag_->DragTargetDragLeave(); |
| 232 [self draggingEntered:info view:view]; | 248 [self draggingEntered:info view:view]; |
| 233 } | 249 } |
| 234 | 250 |
| 235 if (canceled_) | 251 if (canceled_) |
| 236 return NSDragOperationNone; | 252 return NSDragOperationNone; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 256 view:(NSView*)view { | 272 view:(NSView*)view { |
| 257 // Create the appropriate mouse locations for WebCore. The draggingLocation | 273 // Create the appropriate mouse locations for WebCore. The draggingLocation |
| 258 // is in window coordinates. Both need to be flipped. | 274 // is in window coordinates. Both need to be flipped. |
| 259 NSPoint windowPoint = [info draggingLocation]; | 275 NSPoint windowPoint = [info draggingLocation]; |
| 260 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; | 276 NSPoint viewPoint = [self flipWindowPointToView:windowPoint view:view]; |
| 261 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; | 277 NSPoint screenPoint = [self flipWindowPointToScreen:windowPoint view:view]; |
| 262 gfx::Point transformedPt; | 278 gfx::Point transformedPt; |
| 263 content::RenderWidgetHostImpl* targetRWH = | 279 content::RenderWidgetHostImpl* targetRWH = |
| 264 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt]; | 280 [self GetRenderWidgetHostAtPoint:viewPoint transformedPt:&transformedPt]; |
| 265 | 281 |
| 282 if (![self isValidDragTarget:targetRWH]) |
| 283 return NO; |
| 284 |
| 266 if (targetRWH != currentRWHForDrag_.get()) { | 285 if (targetRWH != currentRWHForDrag_.get()) { |
| 267 if (currentRWHForDrag_) | 286 if (currentRWHForDrag_) |
| 268 currentRWHForDrag_->DragTargetDragLeave(); | 287 currentRWHForDrag_->DragTargetDragLeave(); |
| 269 [self draggingEntered:info view:view]; | 288 [self draggingEntered:info view:view]; |
| 270 } | 289 } |
| 271 | 290 |
| 272 // Check if we only allow navigation and navigate to a url on the pasteboard. | 291 // Check if we only allow navigation and navigate to a url on the pasteboard. |
| 273 if ([self onlyAllowsNavigation]) { | 292 if ([self onlyAllowsNavigation]) { |
| 274 NSPasteboard* pboard = [info draggingPasteboard]; | 293 NSPasteboard* pboard = [info draggingPasteboard]; |
| 275 if ([pboard containsURLDataConvertingTextToURL:YES]) { | 294 if ([pboard containsURLDataConvertingTextToURL:YES]) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 299 } | 318 } |
| 300 | 319 |
| 301 - (content::RenderWidgetHostImpl*) | 320 - (content::RenderWidgetHostImpl*) |
| 302 GetRenderWidgetHostAtPoint:(const NSPoint&)viewPoint | 321 GetRenderWidgetHostAtPoint:(const NSPoint&)viewPoint |
| 303 transformedPt:(gfx::Point*)transformedPt { | 322 transformedPt:(gfx::Point*)transformedPt { |
| 304 return webContents_->GetInputEventRouter()->GetRenderWidgetHostAtPoint( | 323 return webContents_->GetInputEventRouter()->GetRenderWidgetHostAtPoint( |
| 305 webContents_->GetRenderViewHost()->GetWidget()->GetView(), | 324 webContents_->GetRenderViewHost()->GetWidget()->GetView(), |
| 306 gfx::Point(viewPoint.x, viewPoint.y), transformedPt); | 325 gfx::Point(viewPoint.x, viewPoint.y), transformedPt); |
| 307 } | 326 } |
| 308 | 327 |
| 328 - (void)setDragStartTrackersForProcess:(int)processID { |
| 329 dragStartProcessID_ = processID; |
| 330 dragStartViewID_ = GetRenderViewHostID(webContents_->GetRenderViewHost()); |
| 331 } |
| 332 |
| 333 - (bool)isValidDragTarget:(content::RenderWidgetHostImpl*)targetRWH { |
| 334 return targetRWH->GetProcess()->GetID() == dragStartProcessID_ || |
| 335 GetRenderViewHostID(webContents_->GetRenderViewHost()) != |
| 336 dragStartViewID_; |
| 337 } |
| 338 |
| 309 // Given |data|, which should not be nil, fill it in using the contents of the | 339 // Given |data|, which should not be nil, fill it in using the contents of the |
| 310 // given pasteboard. The types handled by this method should be kept in sync | 340 // given pasteboard. The types handled by this method should be kept in sync |
| 311 // with [WebContentsViewCocoa registerDragTypes]. | 341 // with [WebContentsViewCocoa registerDragTypes]. |
| 312 - (void)populateDropData:(DropData*)data | 342 - (void)populateDropData:(DropData*)data |
| 313 fromPasteboard:(NSPasteboard*)pboard { | 343 fromPasteboard:(NSPasteboard*)pboard { |
| 314 DCHECK(data); | 344 DCHECK(data); |
| 315 DCHECK(pboard); | 345 DCHECK(pboard); |
| 316 NSArray* types = [pboard types]; | 346 NSArray* types = [pboard types]; |
| 317 | 347 |
| 318 data->did_originate_from_renderer = | 348 data->did_originate_from_renderer = |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // Get custom MIME data. | 396 // Get custom MIME data. |
| 367 if ([types containsObject:ui::kWebCustomDataPboardType]) { | 397 if ([types containsObject:ui::kWebCustomDataPboardType]) { |
| 368 NSData* customData = [pboard dataForType:ui::kWebCustomDataPboardType]; | 398 NSData* customData = [pboard dataForType:ui::kWebCustomDataPboardType]; |
| 369 ui::ReadCustomDataIntoMap([customData bytes], | 399 ui::ReadCustomDataIntoMap([customData bytes], |
| 370 [customData length], | 400 [customData length], |
| 371 &data->custom_data); | 401 &data->custom_data); |
| 372 } | 402 } |
| 373 } | 403 } |
| 374 | 404 |
| 375 @end | 405 @end |
| OLD | NEW |