Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / browser / ui / views / exclusive_access_bubble_views.cc
blob902bdc2f55390f5e57b08486aac51f5ce219217d
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.
5 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
12 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
15 #include "chrome/browser/ui/views/frame/top_container_view.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/notification_service.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/events/keycodes/keyboard_codes.h"
21 #include "ui/gfx/animation/slide_animation.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/strings/grit/ui_strings.h"
25 #include "ui/views/bubble/bubble_border.h"
26 #include "ui/views/controls/button/label_button.h"
27 #include "ui/views/controls/link.h"
28 #include "ui/views/controls/link_listener.h"
29 #include "ui/views/layout/box_layout.h"
30 #include "ui/views/layout/grid_layout.h"
31 #include "ui/views/view.h"
32 #include "ui/views/widget/widget.h"
33 #include "url/gurl.h"
35 #if defined(OS_WIN)
36 #include "ui/base/l10n/l10n_util_win.h"
37 #endif
39 // ExclusiveAccessView ---------------------------------------------------------
41 namespace {
43 // Space between the site info label and the buttons / link.
44 const int kMiddlePaddingPx = 30;
46 class ButtonView : public views::View {
47 public:
48 ButtonView(views::ButtonListener* listener, int between_button_spacing);
49 ~ButtonView() override;
51 // Returns an empty size when the view is not visible.
52 gfx::Size GetPreferredSize() const override;
54 views::LabelButton* accept_button() const { return accept_button_; }
55 views::LabelButton* deny_button() const { return deny_button_; }
57 private:
58 views::LabelButton* accept_button_;
59 views::LabelButton* deny_button_;
61 DISALLOW_COPY_AND_ASSIGN(ButtonView);
64 ButtonView::ButtonView(views::ButtonListener* listener,
65 int between_button_spacing)
66 : accept_button_(NULL), deny_button_(NULL) {
67 accept_button_ = new views::LabelButton(listener, base::string16());
68 accept_button_->SetStyle(views::Button::STYLE_BUTTON);
69 accept_button_->SetFocusable(false);
70 AddChildView(accept_button_);
72 deny_button_ = new views::LabelButton(listener, base::string16());
73 deny_button_->SetStyle(views::Button::STYLE_BUTTON);
74 deny_button_->SetFocusable(false);
75 AddChildView(deny_button_);
77 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
78 between_button_spacing));
81 ButtonView::~ButtonView() {
84 gfx::Size ButtonView::GetPreferredSize() const {
85 return visible() ? views::View::GetPreferredSize() : gfx::Size();
88 } // namespace
90 class ExclusiveAccessBubbleViews::ExclusiveAccessView
91 : public views::View,
92 public views::ButtonListener,
93 public views::LinkListener {
94 public:
95 ExclusiveAccessView(ExclusiveAccessBubbleViews* bubble,
96 const base::string16& accelerator,
97 const GURL& url,
98 ExclusiveAccessBubbleType bubble_type);
99 ~ExclusiveAccessView() override;
101 // views::ButtonListener
102 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
104 // views::LinkListener
105 void LinkClicked(views::Link* source, int event_flags) override;
107 void UpdateContent(const GURL& url, ExclusiveAccessBubbleType bubble_type);
109 private:
110 ExclusiveAccessBubbleViews* bubble_;
112 // Clickable hint text for exiting fullscreen mode.
113 views::Link* link_;
114 // Instruction for exiting mouse lock.
115 views::Label* mouse_lock_exit_instruction_;
116 // Informational label: 'www.foo.com has gone fullscreen'.
117 views::Label* message_label_;
118 ButtonView* button_view_;
119 const base::string16 browser_fullscreen_exit_accelerator_;
121 DISALLOW_COPY_AND_ASSIGN(ExclusiveAccessView);
124 ExclusiveAccessBubbleViews::ExclusiveAccessView::ExclusiveAccessView(
125 ExclusiveAccessBubbleViews* bubble,
126 const base::string16& accelerator,
127 const GURL& url,
128 ExclusiveAccessBubbleType bubble_type)
129 : bubble_(bubble),
130 link_(NULL),
131 mouse_lock_exit_instruction_(NULL),
132 message_label_(NULL),
133 button_view_(NULL),
134 browser_fullscreen_exit_accelerator_(accelerator) {
135 scoped_ptr<views::BubbleBorder> bubble_border(
136 new views::BubbleBorder(views::BubbleBorder::NONE,
137 views::BubbleBorder::BIG_SHADOW, SK_ColorWHITE));
138 set_background(new views::BubbleBackground(bubble_border.get()));
139 SetBorder(bubble_border.Pass());
140 SetFocusable(false);
142 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
143 const gfx::FontList& medium_font_list =
144 rb.GetFontList(ui::ResourceBundle::MediumFont);
145 message_label_ = new views::Label(base::string16(), medium_font_list);
147 mouse_lock_exit_instruction_ =
148 new views::Label(bubble_->GetInstructionText(), medium_font_list);
149 mouse_lock_exit_instruction_->set_collapse_when_hidden(true);
151 link_ = new views::Link();
152 link_->set_collapse_when_hidden(true);
153 link_->SetFocusable(false);
154 #if defined(OS_CHROMEOS)
155 // On CrOS, the link text doesn't change, since it doesn't show the shortcut.
156 link_->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE));
157 #endif
158 link_->set_listener(this);
159 link_->SetFontList(medium_font_list);
160 link_->SetPressedColor(message_label_->enabled_color());
161 link_->SetEnabledColor(message_label_->enabled_color());
162 link_->SetVisible(false);
164 link_->SetBackgroundColor(background()->get_color());
165 message_label_->SetBackgroundColor(background()->get_color());
166 mouse_lock_exit_instruction_->SetBackgroundColor(background()->get_color());
168 button_view_ = new ButtonView(this, kPaddingPx);
169 button_view_->accept_button()->SetText(bubble->GetAllowButtonText());
171 views::GridLayout* layout = new views::GridLayout(this);
172 views::ColumnSet* columns = layout->AddColumnSet(0);
173 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
174 views::GridLayout::USE_PREF, 0, 0);
175 columns->AddPaddingColumn(1, kMiddlePaddingPx);
176 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
177 views::GridLayout::USE_PREF, 0, 0);
178 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
179 views::GridLayout::USE_PREF, 0, 0);
180 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
181 views::GridLayout::USE_PREF, 0, 0);
183 layout->StartRow(0, 0);
184 layout->AddView(message_label_);
185 layout->AddView(button_view_);
186 layout->AddView(mouse_lock_exit_instruction_);
187 layout->AddView(link_);
189 gfx::Insets padding(kPaddingPx, kPaddingPx, kPaddingPx, kPaddingPx);
190 padding += GetInsets();
191 layout->SetInsets(padding);
192 SetLayoutManager(layout);
194 UpdateContent(url, bubble_type);
197 ExclusiveAccessBubbleViews::ExclusiveAccessView::~ExclusiveAccessView() {
200 void ExclusiveAccessBubbleViews::ExclusiveAccessView::ButtonPressed(
201 views::Button* sender,
202 const ui::Event& event) {
203 if (sender == button_view_->accept_button())
204 bubble_->Accept();
205 else
206 bubble_->Cancel();
209 void ExclusiveAccessBubbleViews::ExclusiveAccessView::LinkClicked(
210 views::Link* link,
211 int event_flags) {
212 bubble_->ToggleFullscreen();
215 void ExclusiveAccessBubbleViews::ExclusiveAccessView::UpdateContent(
216 const GURL& url,
217 ExclusiveAccessBubbleType bubble_type) {
218 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE, bubble_type);
220 message_label_->SetText(bubble_->GetCurrentMessageText());
221 if (exclusive_access_bubble::ShowButtonsForType(bubble_type)) {
222 link_->SetVisible(false);
223 mouse_lock_exit_instruction_->SetVisible(false);
224 button_view_->SetVisible(true);
225 button_view_->deny_button()->SetText(bubble_->GetCurrentDenyButtonText());
226 button_view_->deny_button()->SetMinSize(gfx::Size());
227 } else {
228 bool link_visible = true;
229 base::string16 accelerator;
230 if (bubble_type ==
231 EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION ||
232 bubble_type ==
233 EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION) {
234 accelerator = browser_fullscreen_exit_accelerator_;
235 } else if (bubble_type ==
236 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION) {
237 accelerator = l10n_util::GetStringUTF16(IDS_APP_ESC_KEY);
238 } else {
239 link_visible = false;
241 #if !defined(OS_CHROMEOS)
242 if (link_visible) {
243 link_->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE) +
244 base::UTF8ToUTF16(" ") +
245 l10n_util::GetStringFUTF16(
246 IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR, accelerator));
248 #endif
249 link_->SetVisible(link_visible);
250 mouse_lock_exit_instruction_->SetVisible(!link_visible);
251 button_view_->SetVisible(false);
255 // ExclusiveAccessBubbleViews --------------------------------------------------
257 ExclusiveAccessBubbleViews::ExclusiveAccessBubbleViews(
258 BrowserView* browser_view,
259 const GURL& url,
260 ExclusiveAccessBubbleType bubble_type)
261 : ExclusiveAccessBubble(browser_view->browser(), url, bubble_type),
262 browser_view_(browser_view),
263 popup_(NULL),
264 animation_(new gfx::SlideAnimation(this)),
265 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS) {
266 animation_->Reset(1);
268 // Create the contents view.
269 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE);
270 bool got_accelerator =
271 browser_view_->GetWidget()->GetAccelerator(IDC_FULLSCREEN, &accelerator);
272 DCHECK(got_accelerator);
273 view_ = new ExclusiveAccessView(this, accelerator.GetShortcutText(), url,
274 bubble_type_);
276 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView.
277 // TODO(pkotwicz): When this becomes a views bubble, make sure that this
278 // bubble is ignored by ImmersiveModeControllerAsh::BubbleManager.
279 // Initialize the popup.
280 popup_ = new views::Widget;
281 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
282 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
283 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
284 params.parent = browser_view_->GetWidget()->GetNativeView();
285 params.bounds = GetPopupRect(false);
286 popup_->Init(params);
287 gfx::Size size = GetPopupRect(true).size();
288 popup_->SetContentsView(view_);
289 // We set layout manager to NULL to prevent the widget from sizing its
290 // contents to the same size as itself. This prevents the widget contents from
291 // shrinking while we animate the height of the popup to give the impression
292 // that it is sliding off the top of the screen.
293 popup_->GetRootView()->SetLayoutManager(NULL);
294 view_->SetBounds(0, 0, size.width(), size.height());
295 popup_->ShowInactive(); // This does not activate the popup.
297 popup_->AddObserver(this);
299 registrar_.Add(
300 this, chrome::NOTIFICATION_FULLSCREEN_CHANGED,
301 content::Source<FullscreenController>(browser_view_->browser()
302 ->exclusive_access_manager()
303 ->fullscreen_controller()));
305 UpdateForImmersiveState();
308 ExclusiveAccessBubbleViews::~ExclusiveAccessBubbleViews() {
309 popup_->RemoveObserver(this);
311 // This is tricky. We may be in an ATL message handler stack, in which case
312 // the popup cannot be deleted yet. We also can't set the popup's ownership
313 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab
314 // while in fullscreen mode, Windows has already destroyed the popup HWND by
315 // the time we get here, and thus either the popup will already have been
316 // deleted (if we set this in our constructor) or the popup will never get
317 // another OnFinalMessage() call (if not, as currently). So instead, we tell
318 // the popup to synchronously hide, and then asynchronously close and delete
319 // itself.
320 popup_->Close();
321 base::MessageLoop::current()->DeleteSoon(FROM_HERE, popup_);
324 void ExclusiveAccessBubbleViews::UpdateContent(
325 const GURL& url,
326 ExclusiveAccessBubbleType bubble_type) {
327 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE, bubble_type);
328 if (bubble_type_ == bubble_type && url_ == url)
329 return;
331 url_ = url;
332 bubble_type_ = bubble_type;
333 view_->UpdateContent(url_, bubble_type_);
335 gfx::Size size = GetPopupRect(true).size();
336 view_->SetSize(size);
337 popup_->SetBounds(GetPopupRect(false));
338 Show();
340 // Stop watching the mouse even if UpdateMouseWatcher() will start watching
341 // it again so that the popup with the new content is visible for at least
342 // |kInitialDelayMs|.
343 StopWatchingMouse();
345 UpdateMouseWatcher();
348 void ExclusiveAccessBubbleViews::RepositionIfVisible() {
349 if (popup_->IsVisible())
350 UpdateBounds();
353 void ExclusiveAccessBubbleViews::UpdateMouseWatcher() {
354 bool should_watch_mouse = false;
355 if (popup_->IsVisible())
356 should_watch_mouse =
357 !exclusive_access_bubble::ShowButtonsForType(bubble_type_);
358 else
359 should_watch_mouse = CanMouseTriggerSlideIn();
361 if (should_watch_mouse == IsWatchingMouse())
362 return;
364 if (should_watch_mouse)
365 StartWatchingMouse();
366 else
367 StopWatchingMouse();
370 void ExclusiveAccessBubbleViews::UpdateForImmersiveState() {
371 AnimatedAttribute expected_animated_attribute =
372 browser_view_->immersive_mode_controller()->IsEnabled()
373 ? ANIMATED_ATTRIBUTE_OPACITY
374 : ANIMATED_ATTRIBUTE_BOUNDS;
375 if (animated_attribute_ != expected_animated_attribute) {
376 // If an animation is currently in progress, skip to the end because
377 // switching the animated attribute midway through the animation looks
378 // weird.
379 animation_->End();
381 animated_attribute_ = expected_animated_attribute;
383 // We may have finished hiding |popup_|. However, the bounds animation
384 // assumes |popup_| has the opacity when it is fully shown and the opacity
385 // animation assumes |popup_| has the bounds when |popup_| is fully shown.
386 if (animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS)
387 popup_->SetOpacity(255);
388 else
389 UpdateBounds();
392 UpdateMouseWatcher();
395 void ExclusiveAccessBubbleViews::UpdateBounds() {
396 gfx::Rect popup_rect(GetPopupRect(false));
397 if (!popup_rect.IsEmpty()) {
398 popup_->SetBounds(popup_rect);
399 view_->SetY(popup_rect.height() - view_->height());
403 views::View* ExclusiveAccessBubbleViews::GetBrowserRootView() const {
404 return browser_view_->GetWidget()->GetRootView();
407 void ExclusiveAccessBubbleViews::AnimationProgressed(
408 const gfx::Animation* animation) {
409 if (animated_attribute_ == ANIMATED_ATTRIBUTE_OPACITY) {
410 int opacity = animation_->CurrentValueBetween(0, 255);
411 if (opacity == 0) {
412 popup_->Hide();
413 } else {
414 popup_->Show();
415 popup_->SetOpacity(opacity);
417 } else {
418 if (GetPopupRect(false).IsEmpty()) {
419 popup_->Hide();
420 } else {
421 UpdateBounds();
422 popup_->Show();
427 void ExclusiveAccessBubbleViews::AnimationEnded(
428 const gfx::Animation* animation) {
429 AnimationProgressed(animation);
432 gfx::Rect ExclusiveAccessBubbleViews::GetPopupRect(
433 bool ignore_animation_state) const {
434 gfx::Size size(view_->GetPreferredSize());
435 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window
436 // size is async. Instead we use the size of the screen.
437 gfx::Screen* screen =
438 gfx::Screen::GetScreenFor(browser_view_->GetWidget()->GetNativeView());
439 gfx::Rect screen_bounds =
440 screen->GetDisplayNearestWindow(
441 browser_view_->GetWidget()->GetNativeView()).bounds();
442 int x = screen_bounds.x() + (screen_bounds.width() - size.width()) / 2;
444 int top_container_bottom = screen_bounds.y();
445 if (browser_view_->immersive_mode_controller()->IsEnabled()) {
446 // Skip querying the top container height in non-immersive fullscreen
447 // because:
448 // - The top container height is always zero in non-immersive fullscreen.
449 // - Querying the top container height may return the height before entering
450 // fullscreen because layout is disabled while entering fullscreen.
451 // A visual glitch due to the delayed layout is avoided in immersive
452 // fullscreen because entering fullscreen starts with the top container
453 // revealed. When revealed, the top container has the same height as before
454 // entering fullscreen.
455 top_container_bottom =
456 browser_view_->top_container()->GetBoundsInScreen().bottom();
458 int y = top_container_bottom + kPopupTopPx;
460 if (!ignore_animation_state &&
461 animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) {
462 int total_height = size.height() + kPopupTopPx;
463 int popup_bottom = animation_->CurrentValueBetween(total_height, 0);
464 int y_offset = std::min(popup_bottom, kPopupTopPx);
465 size.set_height(size.height() - popup_bottom + y_offset);
466 y -= y_offset;
468 return gfx::Rect(gfx::Point(x, y), size);
471 gfx::Point ExclusiveAccessBubbleViews::GetCursorScreenPoint() {
472 gfx::Point cursor_pos =
473 gfx::Screen::GetScreenFor(browser_view_->GetWidget()->GetNativeView())
474 ->GetCursorScreenPoint();
475 views::View::ConvertPointFromScreen(GetBrowserRootView(), &cursor_pos);
476 return cursor_pos;
479 bool ExclusiveAccessBubbleViews::WindowContainsPoint(gfx::Point pos) {
480 return GetBrowserRootView()->HitTestPoint(pos);
483 bool ExclusiveAccessBubbleViews::IsWindowActive() {
484 return browser_view_->GetWidget()->IsActive();
487 void ExclusiveAccessBubbleViews::Hide() {
488 animation_->SetSlideDuration(kSlideOutDurationMs);
489 animation_->Hide();
492 void ExclusiveAccessBubbleViews::Show() {
493 animation_->SetSlideDuration(kSlideInDurationMs);
494 animation_->Show();
497 bool ExclusiveAccessBubbleViews::IsAnimating() {
498 return animation_->is_animating();
501 bool ExclusiveAccessBubbleViews::CanMouseTriggerSlideIn() const {
502 return !browser_view_->immersive_mode_controller()->IsEnabled();
505 void ExclusiveAccessBubbleViews::Observe(
506 int type,
507 const content::NotificationSource& source,
508 const content::NotificationDetails& details) {
509 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
510 UpdateForImmersiveState();
513 void ExclusiveAccessBubbleViews::OnWidgetVisibilityChanged(
514 views::Widget* widget,
515 bool visible) {
516 UpdateMouseWatcher();