Home | History | Annotate | Download | only in tab_contents
      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 bookmark_utils::WriteBookmarksToSelection()
     40   // for details.
     41   return ui::GetAtomForTarget(ui::CHROME_BOOKMARK_ITEM);
     42 }
     43 
     44 void WebDragBookmarkHandlerGtk::OnReceiveDataFromGtk(GtkSelectionData* data) {
     45   Profile* profile =
     46       Profile::FromBrowserContext(web_contents_->GetBrowserContext());
     47   bookmark_drag_data_.ReadFromVector(
     48       bookmark_utils::GetNodesFromSelection(
     49           NULL, data,
     50           ui::CHROME_BOOKMARK_ITEM,
     51           profile, NULL, NULL));
     52   bookmark_drag_data_.SetOriginatingProfile(profile);
     53 }
     54 
     55 void WebDragBookmarkHandlerGtk::OnReceiveProcessedData(const GURL& url,
     56                                                        const string16& title) {
     57   bookmark_drag_data_.ReadFromTuple(url, title);
     58 }
     59 
     60 void WebDragBookmarkHandlerGtk::OnDragOver() {
     61   if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
     62     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragOver(
     63         bookmark_drag_data_);
     64   }
     65 }
     66 
     67 void WebDragBookmarkHandlerGtk::OnDragEnter() {
     68   // This is non-null if the web_contents_ is showing an ExtensionWebUI with
     69   // support for (at the moment experimental) drag and drop extensions.
     70   if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
     71     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragEnter(
     72         bookmark_drag_data_);
     73   }
     74 }
     75 
     76 void WebDragBookmarkHandlerGtk::OnDrop() {
     77   // This is non-null if web_contents_ is showing an ExtensionWebUI with
     78   // support for (at the moment experimental) drag and drop extensions.
     79   if (bookmark_tab_helper_) {
     80     if (bookmark_tab_helper_->bookmark_drag_delegate()) {
     81       bookmark_tab_helper_->bookmark_drag_delegate()->OnDrop(
     82           bookmark_drag_data_);
     83     }
     84 
     85     // Focus the target browser.
     86     Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
     87     if (browser)
     88       browser->window()->Show();
     89   }
     90 }
     91 
     92 void WebDragBookmarkHandlerGtk::OnDragLeave() {
     93   if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
     94     bookmark_tab_helper_->bookmark_drag_delegate()->OnDragLeave(
     95         bookmark_drag_data_);
     96   }
     97 }
     98