1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/frame/browser_root_view.h"
#include <cmath>
#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/touch_uma/touch_uma.h"
#include "components/metrics/proto/omnibox_event.pb.h"
#include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/hit_test.h"
// static
const char BrowserRootView::kViewClassName[] =
"browser/ui/views/frame/BrowserRootView";
BrowserRootView::BrowserRootView(BrowserView* browser_view,
views::Widget* widget)
: views::internal::RootView(widget),
browser_view_(browser_view),
forwarding_to_tab_strip_(false),
scroll_remainder_x_(0),
scroll_remainder_y_(0) {}
bool BrowserRootView::GetDropFormats(
int* formats,
std::set<ui::Clipboard::FormatType>* format_types) {
if (tabstrip() && tabstrip()->visible()) {
*formats = ui::OSExchangeData::URL | ui::OSExchangeData::STRING;
return true;
}
return false;
}
bool BrowserRootView::AreDropTypesRequired() {
return true;
}
bool BrowserRootView::CanDrop(const ui::OSExchangeData& data) {
if (!tabstrip() || !tabstrip()->visible())
return false;
// If there is a URL, we'll allow the drop.
if (data.HasURL(ui::OSExchangeData::CONVERT_FILENAMES))
return true;
// If there isn't a URL, see if we can 'paste and go'.
return GetPasteAndGoURL(data, nullptr);
}
void BrowserRootView::OnDragEntered(const ui::DropTargetEvent& event) {
if (ShouldForwardToTabStrip(event)) {
forwarding_to_tab_strip_ = true;
scoped_ptr<ui::DropTargetEvent> mapped_event(
MapEventToTabStrip(event, event.data()));
tabstrip()->OnDragEntered(*mapped_event.get());
}
}
int BrowserRootView::OnDragUpdated(const ui::DropTargetEvent& event) {
if (ShouldForwardToTabStrip(event)) {
scoped_ptr<ui::DropTargetEvent> mapped_event(
MapEventToTabStrip(event, event.data()));
if (!forwarding_to_tab_strip_) {
tabstrip()->OnDragEntered(*mapped_event.get());
forwarding_to_tab_strip_ = true;
}
return tabstrip()->OnDragUpdated(*mapped_event.get());
} else if (forwarding_to_tab_strip_) {
forwarding_to_tab_strip_ = false;
tabstrip()->OnDragExited();
}
return ui::DragDropTypes::DRAG_NONE;
}
void BrowserRootView::OnDragExited() {
if (forwarding_to_tab_strip_) {
forwarding_to_tab_strip_ = false;
tabstrip()->OnDragExited();
}
}
int BrowserRootView::OnPerformDrop(const ui::DropTargetEvent& event) {
if (!forwarding_to_tab_strip_)
return ui::DragDropTypes::DRAG_NONE;
// Extract the URL and create a new ui::OSExchangeData containing the URL. We
// do this as the TabStrip doesn't know about the autocomplete edit and needs
// to know about it to handle 'paste and go'.
GURL url;
base::string16 title;
ui::OSExchangeData mapped_data;
if (!event.data().GetURLAndTitle(
ui::OSExchangeData::CONVERT_FILENAMES, &url, &title) ||
!url.is_valid()) {
// The url isn't valid. Use the paste and go url.
if (GetPasteAndGoURL(event.data(), &url))
mapped_data.SetURL(url, base::string16());
// else case: couldn't extract a url or 'paste and go' url. This ends up
// passing through an ui::OSExchangeData with nothing in it. We need to do
// this so that the tab strip cleans up properly.
} else {
mapped_data.SetURL(url, base::string16());
}
forwarding_to_tab_strip_ = false;
scoped_ptr<ui::DropTargetEvent> mapped_event(
MapEventToTabStrip(event, mapped_data));
return tabstrip()->OnPerformDrop(*mapped_event);
}
const char* BrowserRootView::GetClassName() const {
return kViewClassName;
}
bool BrowserRootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
if (browser_defaults::kScrollEventChangesTab) {
// Switch to the left/right tab if the wheel-scroll happens over the
// tabstrip, or the empty space beside the tabstrip.
views::View* hit_view = GetEventHandlerForPoint(event.location());
int hittest =
GetWidget()->non_client_view()->NonClientHitTest(event.location());
if (tabstrip()->Contains(hit_view) ||
hittest == HTCAPTION ||
hittest == HTTOP) {
scroll_remainder_x_ += event.x_offset();
scroll_remainder_y_ += event.y_offset();
// Number of integer scroll events that have passed in each direction.
int whole_scroll_amount_x =
std::lround(static_cast<double>(scroll_remainder_x_) /
ui::MouseWheelEvent::kWheelDelta);
int whole_scroll_amount_y =
std::lround(static_cast<double>(scroll_remainder_y_) /
ui::MouseWheelEvent::kWheelDelta);
// Adjust the remainder such that any whole scrolls we have taken action
// for don't count towards the scroll remainder.
scroll_remainder_x_ -=
whole_scroll_amount_x * ui::MouseWheelEvent::kWheelDelta;
scroll_remainder_y_ -=
whole_scroll_amount_y * ui::MouseWheelEvent::kWheelDelta;
// Count a scroll in either axis - summing the axes works for this.
int whole_scroll_offset = whole_scroll_amount_x + whole_scroll_amount_y;
Browser* browser = browser_view_->browser();
TabStripModel* model = browser->tab_strip_model();
// Switch to the next tab only if not at the end of the tab-strip.
if (whole_scroll_offset < 0 &&
model->active_index() + 1 < model->count()) {
chrome::SelectNextTab(browser);
return true;
}
// Switch to the previous tab only if not at the beginning of the
// tab-strip.
if (whole_scroll_offset > 0 && model->active_index() > 0) {
chrome::SelectPreviousTab(browser);
return true;
}
}
}
return RootView::OnMouseWheel(event);
}
void BrowserRootView::OnMouseExited(const ui::MouseEvent& event) {
// Reset the remainders so tab switches occur halfway through a smooth scroll.
scroll_remainder_x_ = 0;
scroll_remainder_y_ = 0;
RootView::OnMouseExited(event);
}
void BrowserRootView::OnEventProcessingStarted(ui::Event* event) {
if (event->IsGestureEvent()) {
ui::GestureEvent* gesture_event = event->AsGestureEvent();
if (gesture_event->type() == ui::ET_GESTURE_TAP &&
gesture_event->location().y() <= 0 &&
gesture_event->location().x() <= browser_view_->GetBounds().width()) {
TouchUMA::RecordGestureAction(TouchUMA::GESTURE_ROOTVIEWTOP_TAP);
}
}
RootView::OnEventProcessingStarted(event);
}
bool BrowserRootView::ShouldForwardToTabStrip(
const ui::DropTargetEvent& event) {
if (!tabstrip()->visible())
return false;
// Allow the drop as long as the mouse is over the tabstrip or vertically
// before it.
gfx::Point tab_loc_in_host;
ConvertPointToTarget(tabstrip(), this, &tab_loc_in_host);
return event.y() < tab_loc_in_host.y() + tabstrip()->height();
}
ui::DropTargetEvent* BrowserRootView::MapEventToTabStrip(
const ui::DropTargetEvent& event,
const ui::OSExchangeData& data) {
gfx::Point tab_strip_loc(event.location());
ConvertPointToTarget(this, tabstrip(), &tab_strip_loc);
return new ui::DropTargetEvent(data, tab_strip_loc, tab_strip_loc,
event.source_operations());
}
TabStrip* BrowserRootView::tabstrip() const {
return browser_view_->tabstrip();
}
bool BrowserRootView::GetPasteAndGoURL(const ui::OSExchangeData& data,
GURL* url) {
if (!data.HasString())
return false;
base::string16 text;
if (!data.GetString(&text) || text.empty())
return false;
text = AutocompleteMatch::SanitizeString(text);
AutocompleteMatch match;
AutocompleteClassifierFactory::GetForProfile(
browser_view_->browser()->profile())->Classify(
text, false, false, metrics::OmniboxEventProto::INVALID_SPEC, &match,
nullptr);
if (!match.destination_url.is_valid())
return false;
if (url)
*url = match.destination_url;
return true;
}
|