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/gtk/tab_contents/web_drag_bookmark_handler_gtk.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.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 "ui/base/dragdrop/gtk_dnd_util.h"
16 using content::WebContents
;
18 WebDragBookmarkHandlerGtk::WebDragBookmarkHandlerGtk()
19 : bookmark_tab_helper_(NULL
),
23 WebDragBookmarkHandlerGtk::~WebDragBookmarkHandlerGtk() {}
25 void WebDragBookmarkHandlerGtk::DragInitialize(WebContents
* contents
) {
26 bookmark_drag_data_
.Clear();
28 // Ideally we would want to initialize the the BookmarkTabHelper member in
29 // the constructor. We cannot do that as the WebDragDestGtk object is
30 // created during the construction of the WebContents object. The
31 // BookmarkTabHelper is created much later.
32 web_contents_
= contents
;
33 if (!bookmark_tab_helper_
)
34 bookmark_tab_helper_
= BookmarkTabHelper::FromWebContents(contents
);
37 GdkAtom
WebDragBookmarkHandlerGtk::GetBookmarkTargetAtom() const {
38 // For GTK, bookmark drag data is encoded as pickle and associated with
39 // ui::CHROME_BOOKMARK_ITEM. See WriteBookmarksToSelection() for details.
40 return ui::GetAtomForTarget(ui::CHROME_BOOKMARK_ITEM
);
43 void WebDragBookmarkHandlerGtk::OnReceiveDataFromGtk(GtkSelectionData
* data
) {
45 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
46 bookmark_drag_data_
.ReadFromVector(GetNodesFromSelection(
47 NULL
, data
, ui::CHROME_BOOKMARK_ITEM
, profile
, NULL
, NULL
));
48 bookmark_drag_data_
.SetOriginatingProfile(profile
);
51 void WebDragBookmarkHandlerGtk::OnReceiveProcessedData(
53 const base::string16
& title
) {
54 bookmark_drag_data_
.ReadFromTuple(url
, title
);
57 void WebDragBookmarkHandlerGtk::OnDragOver() {
58 if (bookmark_tab_helper_
&& bookmark_tab_helper_
->bookmark_drag_delegate()) {
59 bookmark_tab_helper_
->bookmark_drag_delegate()->OnDragOver(
64 void WebDragBookmarkHandlerGtk::OnDragEnter() {
65 // This is non-null if the web_contents_ is showing an ExtensionWebUI with
66 // support for (at the moment experimental) drag and drop extensions.
67 if (bookmark_tab_helper_
&& bookmark_tab_helper_
->bookmark_drag_delegate()) {
68 bookmark_tab_helper_
->bookmark_drag_delegate()->OnDragEnter(
73 void WebDragBookmarkHandlerGtk::OnDrop() {
74 // This is non-null if web_contents_ is showing an ExtensionWebUI with
75 // support for (at the moment experimental) drag and drop extensions.
76 if (bookmark_tab_helper_
) {
77 if (bookmark_tab_helper_
->bookmark_drag_delegate()) {
78 bookmark_tab_helper_
->bookmark_drag_delegate()->OnDrop(
82 // Focus the target browser.
83 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
85 browser
->window()->Show();
89 void WebDragBookmarkHandlerGtk::OnDragLeave() {
90 if (bookmark_tab_helper_
&& bookmark_tab_helper_
->bookmark_drag_delegate()) {
91 bookmark_tab_helper_
->bookmark_drag_delegate()->OnDragLeave(