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/aura/tab_contents/web_drag_bookmark_handler_aura.h"
6
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 "content/public/browser/web_contents.h"
13#include "ui/base/dragdrop/os_exchange_data.h"
14
15using content::WebContents;
16
17WebDragBookmarkHandlerAura::WebDragBookmarkHandlerAura()
18    : bookmark_tab_helper_(NULL),
19      web_contents_(NULL) {
20}
21
22WebDragBookmarkHandlerAura::~WebDragBookmarkHandlerAura() {
23}
24
25void WebDragBookmarkHandlerAura::DragInitialize(WebContents* contents) {
26  // Ideally we would want to initialize the the BookmarkTabHelper member in
27  // the constructor. We cannot do that as the WebDragDest object is
28  // created during the construction of the WebContents object.  The
29  // BookmarkTabHelper is created much later.
30  web_contents_ = contents;
31  if (!bookmark_tab_helper_)
32    bookmark_tab_helper_ = BookmarkTabHelper::FromWebContents(contents);
33}
34
35void WebDragBookmarkHandlerAura::OnDragOver() {
36  if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
37    if (bookmark_drag_data_.is_valid())
38      bookmark_tab_helper_->bookmark_drag_delegate()->OnDragOver(
39          bookmark_drag_data_);
40  }
41}
42
43void WebDragBookmarkHandlerAura::OnReceiveDragData(
44    const ui::OSExchangeData& data) {
45  if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
46    // Read the bookmark drag data and save it for use in later events in this
47    // drag.
48    bookmark_drag_data_.Read(data);
49  }
50}
51
52void WebDragBookmarkHandlerAura::OnDragEnter() {
53  if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
54    if (bookmark_drag_data_.is_valid())
55      bookmark_tab_helper_->bookmark_drag_delegate()->OnDragEnter(
56          bookmark_drag_data_);
57  }
58}
59
60void WebDragBookmarkHandlerAura::OnDrop() {
61  if (bookmark_tab_helper_) {
62    if (bookmark_tab_helper_->bookmark_drag_delegate()) {
63      if (bookmark_drag_data_.is_valid()) {
64        bookmark_tab_helper_->bookmark_drag_delegate()->OnDrop(
65            bookmark_drag_data_);
66      }
67    }
68
69    // Focus the target browser.
70    Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
71    if (browser)
72      browser->window()->Show();
73  }
74
75  bookmark_drag_data_.Clear();
76}
77
78void WebDragBookmarkHandlerAura::OnDragLeave() {
79  if (bookmark_tab_helper_ && bookmark_tab_helper_->bookmark_drag_delegate()) {
80    if (bookmark_drag_data_.is_valid())
81      bookmark_tab_helper_->bookmark_drag_delegate()->OnDragLeave(
82          bookmark_drag_data_);
83  }
84
85  bookmark_drag_data_.Clear();
86}
87