manage_passwords_icon_view.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright 2013 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/views/passwords/manage_passwords_icon_view.h"
6
7#include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h"
8#include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
9#include "grit/generated_resources.h"
10#include "grit/theme_resources.h"
11#include "ui/base/l10n/l10n_util.h"
12#include "ui/base/resource/resource_bundle.h"
13
14ManagePasswordsIconView::ManagePasswordsIconView(
15    LocationBarView::Delegate* location_bar_delegate)
16    : location_bar_delegate_(location_bar_delegate) {
17  SetAccessibilityFocusable(true);
18  Update(NULL);
19  LocationBarView::InitTouchableLocationBarChildView(this);
20}
21
22ManagePasswordsIconView::~ManagePasswordsIconView() {}
23
24void ManagePasswordsIconView::Update(
25    ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller) {
26  SetVisible(manage_passwords_bubble_ui_controller &&
27      manage_passwords_bubble_ui_controller->
28          manage_passwords_icon_to_be_shown() &&
29      !location_bar_delegate_->GetToolbarModel()->input_in_progress());
30  if (!visible()) {
31    ManagePasswordsBubbleView::CloseBubble(
32        password_manager::metrics_util::NOT_DISPLAYED);
33    return;
34  }
35  int icon_to_display =
36      manage_passwords_bubble_ui_controller->autofill_blocked()
37          ? IDR_SAVE_PASSWORD_BLACKLISTED
38          : IDR_SAVE_PASSWORD;
39  SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
40      icon_to_display));
41  SetTooltip(manage_passwords_bubble_ui_controller->password_to_be_saved());
42}
43
44void ManagePasswordsIconView::ShowBubbleIfNeeded(
45    ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller) {
46  if (manage_passwords_bubble_ui_controller->
47          manage_passwords_bubble_needs_showing() &&
48      visible() &&
49      !ManagePasswordsBubbleView::IsShowing()) {
50    ManagePasswordsBubbleView::ShowBubble(
51        location_bar_delegate_->GetWebContents(),
52        this,
53        ManagePasswordsBubbleView::AUTOMATIC);
54    manage_passwords_bubble_ui_controller->OnBubbleShown();
55  }
56}
57
58void ManagePasswordsIconView::SetTooltip(bool password_to_be_saved) {
59  SetTooltipText(l10n_util::GetStringUTF16(
60      (password_to_be_saved ?
61          IDS_PASSWORD_MANAGER_TOOLTIP_SAVE :
62          IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE)));
63}
64
65bool ManagePasswordsIconView::GetTooltipText(const gfx::Point& p,
66                                             base::string16* tooltip) const {
67  // Don't show tooltip if the password bubble is displayed.
68  return !ManagePasswordsBubbleView::IsShowing() &&
69      ImageView::GetTooltipText(p, tooltip);
70}
71
72void ManagePasswordsIconView::OnGestureEvent(ui::GestureEvent* event) {
73  if (event->type() == ui::ET_GESTURE_TAP) {
74    ManagePasswordsBubbleView::ShowBubble(
75        location_bar_delegate_->GetWebContents(),
76        this,
77        ManagePasswordsBubbleView::USER_ACTION);
78    event->SetHandled();
79  }
80}
81
82bool ManagePasswordsIconView::OnMousePressed(const ui::MouseEvent& event) {
83  // Do nothing until the mouse button is released.
84  return true;
85}
86
87void ManagePasswordsIconView::OnMouseReleased(const ui::MouseEvent& event) {
88  if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location())) {
89    ManagePasswordsBubbleView::ShowBubble(
90        location_bar_delegate_->GetWebContents(),
91        this,
92        ManagePasswordsBubbleView::USER_ACTION);
93  }
94}
95