- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / tab_contents / web_drag_bookmark_handler_gtk.cc
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 #include "chrome/browser/ui/gtk/tab_contents/web_drag_bookmark_handler_gtk.h"
6
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"
15
16 using content::WebContents;
17
18 WebDragBookmarkHandlerGtk::WebDragBookmarkHandlerGtk()
19     : bookmark_tab_helper_(NULL),
20       web_contents_(NULL) {
21 }
22
23 WebDragBookmarkHandlerGtk::~WebDragBookmarkHandlerGtk() {}
24
25 void WebDragBookmarkHandlerGtk::DragInitialize(WebContents* contents) {
26   bookmark_drag_data_.Clear();
27
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);
35 }
36
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);
41 }
42
43 void WebDragBookmarkHandlerGtk::OnReceiveDataFromGtk(GtkSelectionData* data) {
44   Profile* profile =
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);
49 }
50
51 void WebDragBookmarkHandlerGtk::OnReceiveProcessedData(const GURL& url,
52                                                        const string16& title) {
53   bookmark_drag_data_.ReadFromTuple(url, title);
54 }
55
56 void WebDragBookmarkHandlerGtk::OnDragOver() {
57   if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
58     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragOver(
59         bookmark_drag_data_);
60   }
61 }
62
63 void WebDragBookmarkHandlerGtk::OnDragEnter() {
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_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
67     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragEnter(
68         bookmark_drag_data_);
69   }
70 }
71
72 void WebDragBookmarkHandlerGtk::OnDrop() {
73   // This is non-null if web_contents_ is showing an ExtensionWebUI with
74   // support for (at the moment experimental) drag and drop extensions.
75   if (bookmark_tab_helper_) {
76     if (bookmark_tab_helper_->bookmark_drag_delegate()) {
77       bookmark_tab_helper_->bookmark_drag_delegate()->OnDrop(
78           bookmark_drag_data_);
79     }
80
81     // Focus the target browser.
82     Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
83     if (browser)
84       browser->window()->Show();
85   }
86 }
87
88 void WebDragBookmarkHandlerGtk::OnDragLeave() {
89   if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
90     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragLeave(
91         bookmark_drag_data_);
92   }
93 }