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 #include "chrome/browser/ui/views/tab_contents/web_drag_bookmark_handler_win.h"
7 #include "chrome/browser/bookmarks/bookmark_node_data.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/drop_data.h"
15 #include "content/public/common/url_constants.h"
16 #include "ui/base/dragdrop/os_exchange_data.h"
17 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
19 using content::WebContents;
21 WebDragBookmarkHandlerWin::WebDragBookmarkHandlerWin()
22 : bookmark_tab_helper_(NULL),
26 WebDragBookmarkHandlerWin::~WebDragBookmarkHandlerWin() {
29 void WebDragBookmarkHandlerWin::DragInitialize(WebContents* contents) {
30 // Ideally we would want to initialize the the BookmarkTabHelper member in
31 // the constructor. We cannot do that as the WebDragTargetWin object is
32 // created during the construction of the WebContents object. The
33 // BookmarkTabHelper is created much later.
34 web_contents_ = contents;
35 if (!bookmark_tab_helper_)
36 bookmark_tab_helper_ = BookmarkTabHelper::FromWebContents(contents);
39 void WebDragBookmarkHandlerWin::OnDragOver(IDataObject* data_object) {
40 if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
41 ui::OSExchangeData os_exchange_data(
42 new ui::OSExchangeDataProviderWin(data_object));
43 BookmarkNodeData bookmark_drag_data;
44 if (bookmark_drag_data.Read(os_exchange_data))
45 bookmark_tab_helper_->bookmark_drag_delegate()->OnDragOver(
50 void WebDragBookmarkHandlerWin::OnDragEnter(IDataObject* data_object) {
51 // This is non-null if the web_contents_ is showing an ExtensionWebUI with
52 // support for (at the moment experimental) drag and drop extensions.
53 if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
54 ui::OSExchangeData os_exchange_data(
55 new ui::OSExchangeDataProviderWin(data_object));
56 BookmarkNodeData bookmark_drag_data;
57 if (bookmark_drag_data.Read(os_exchange_data))
58 bookmark_tab_helper_->bookmark_drag_delegate()->OnDragEnter(
63 void WebDragBookmarkHandlerWin::OnDrop(IDataObject* data_object) {
64 // This is non-null if the web_contents_ is showing an ExtensionWebUI with
65 // support for (at the moment experimental) drag and drop extensions.
66 if (bookmark_tab_helper_) {
67 if (bookmark_tab_helper_->bookmark_drag_delegate()) {
68 ui::OSExchangeData os_exchange_data(
69 new ui::OSExchangeDataProviderWin(data_object));
70 BookmarkNodeData bookmark_drag_data;
71 if (bookmark_drag_data.Read(os_exchange_data)) {
72 bookmark_tab_helper_->bookmark_drag_delegate()->OnDrop(
77 // Focus the target browser.
78 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
80 browser->window()->Show();
84 void WebDragBookmarkHandlerWin::OnDragLeave(IDataObject* data_object) {
85 if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
86 ui::OSExchangeData os_exchange_data(
87 new ui::OSExchangeDataProviderWin(data_object));
88 BookmarkNodeData bookmark_drag_data;
89 if (bookmark_drag_data.Read(os_exchange_data))
90 bookmark_tab_helper_->bookmark_drag_delegate()->OnDragLeave(
95 bool WebDragBookmarkHandlerWin::AddDragData(const content::DropData& drop_data,
96 ui::OSExchangeData* data) {
97 if (!drop_data.url.SchemeIs(content::kJavaScriptScheme))
100 // We don't want to allow javascript URLs to be dragged to the desktop,
101 // but we do want to allow them to be added to the bookmarks bar
102 // (bookmarklets). So we create a fake bookmark entry (BookmarkNodeData
103 // object) which explorer.exe cannot handle, and write the entry to data.
104 BookmarkNodeData::Element bm_elt;
105 bm_elt.is_url = true;
106 bm_elt.url = drop_data.url;
107 bm_elt.title = drop_data.url_title;
109 BookmarkNodeData bm_drag_data;
110 bm_drag_data.elements.push_back(bm_elt);
112 // Pass in NULL as the profile so that the bookmark always adds the url
113 // rather than trying to move an existing url.
114 bm_drag_data.Write(NULL, data);