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 #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_BROWSER_ROOT_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_FRAME_BROWSER_ROOT_VIEW_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "chrome/browser/ui/views/frame/browser_view.h"
12 #include "ui/views/widget/root_view.h"
13 
14 class ToolbarView;
15 
16 namespace ui {
17 class OSExchangeData;
18 }
19 
20 // RootView implementation used by BrowserFrame. This forwards drop events to
21 // the TabStrip. Visually the tabstrip extends to the top of the frame, but in
22 // actually it doesn't. The tabstrip is only as high as a tab. To enable
23 // dropping above the tabstrip BrowserRootView forwards drop events to the
24 // TabStrip.
25 class BrowserRootView : public views::internal::RootView {
26  public:
27   struct DropIndex {
28     // The index within the tabstrip to drop on/before (see
29     // |insert_before_index| below).
30     int value = 0;
31 
32     // If true, the dropped item should be inserted before |tab_index|.
33     // If false, the dropped item should replace the tab at |tab_index|.
34     bool drop_before = false;
35 
36     bool operator==(const DropIndex& other) const {
37       return value == other.value && drop_before == other.drop_before;
38     }
39   };
40 
41   class DropTarget {
42    public:
43     virtual DropIndex GetDropIndex(const ui::DropTargetEvent& event) = 0;
44     virtual views::View* GetViewForDrop() = 0;
45 
HandleDragUpdate(const base::Optional<DropIndex> & index)46     virtual void HandleDragUpdate(const base::Optional<DropIndex>& index) {}
HandleDragExited()47     virtual void HandleDragExited() {}
48 
49    protected:
50     DropTarget() = default;
51     virtual ~DropTarget() = default;
52 
53    private:
54     DISALLOW_COPY_AND_ASSIGN(DropTarget);
55   };
56 
57   // Internal class name.
58   static const char kViewClassName[];
59 
60   // You must call set_tabstrip before this class will accept drops.
61   BrowserRootView(BrowserView* browser_view, views::Widget* widget);
62   ~BrowserRootView() override;
63 
64   // views::View:
65   bool GetDropFormats(int* formats,
66                       std::set<ui::ClipboardFormatType>* format_types) override;
67   bool AreDropTypesRequired() override;
68   bool CanDrop(const ui::OSExchangeData& data) override;
69   void OnDragEntered(const ui::DropTargetEvent& event) override;
70   int OnDragUpdated(const ui::DropTargetEvent& event) override;
71   void OnDragExited() override;
72   int OnPerformDrop(const ui::DropTargetEvent& event) override;
73   const char* GetClassName() const override;
74   bool OnMouseWheel(const ui::MouseWheelEvent& event) override;
75   void OnMouseExited(const ui::MouseEvent& event) override;
76 
77  protected:
78   // views::View:
79   void PaintChildren(const views::PaintInfo& paint_info) override;
80 
81  private:
82   FRIEND_TEST_ALL_PREFIXES(BrowserRootViewBrowserTest, ClearDropInfo);
83 
84   // Used during a drop session of a url. Tracks the position of the drop.
85   struct DropInfo {
86     DropInfo();
87     ~DropInfo();
88 
89     DropTarget* target = nullptr;
90 
91     // Where to drop the url.
92     base::Optional<DropIndex> index;
93 
94     // The URL for the drop event.
95     GURL url;
96 
97     // Whether the MIME type of the file pointed to by |url| is supported.
98     // TODO(sangwoo108) Try removing this memeber.
99     bool file_supported = true;
100   };
101 
102   // ui::EventProcessor:
103   void OnEventProcessingStarted(ui::Event* event) override;
104 
105   // Converts the event from the hosts coordinate system to the view's
106   // coordinate system.
107   DropIndex GetDropIndexForEvent(const ui::DropTargetEvent& event,
108                                  const ui::OSExchangeData& data,
109                                  DropTarget* target);
110 
111   DropTarget* GetDropTarget(const ui::DropTargetEvent& event);
112 
113   // Called to indicate whether the given URL is a supported file.
114   void OnFileSupported(const GURL& url, bool supported);
115 
tabstrip()116   TabStrip* tabstrip() { return browser_view_->tabstrip(); }
toolbar()117   ToolbarView* toolbar() { return browser_view_->toolbar(); }
118 
119   // Returns true if |data| has string contents and the user can "paste and go".
120   // If |url| is non-null and the user can "paste and go", |url| is set to the
121   // desired destination.
122   bool GetPasteAndGoURL(const ui::OSExchangeData& data, GURL* url);
123 
124   // The BrowserView.
125   BrowserView* browser_view_ = nullptr;
126 
127   // Used to calculate partial offsets in scrolls that occur for a smooth
128   // scroll device.
129   int scroll_remainder_x_ = 0;
130   int scroll_remainder_y_ = 0;
131 
132   std::unique_ptr<DropInfo> drop_info_;
133 
134   base::WeakPtrFactory<BrowserRootView> weak_ptr_factory_{this};
135 
136   DISALLOW_COPY_AND_ASSIGN(BrowserRootView);
137 };
138 
139 #endif  // CHROME_BROWSER_UI_VIEWS_FRAME_BROWSER_ROOT_VIEW_H_
140