Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / location_bar / manage_passwords_decoration.mm
1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
6
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/command_updater.h"
9 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
10 #include "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.h"
11 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13
14 // ManagePasswordsIconCocoa
15
16 ManagePasswordsIconCocoa::ManagePasswordsIconCocoa(
17     ManagePasswordsDecoration* decoration)
18     : decoration_(decoration) {
19 }
20
21 ManagePasswordsIconCocoa::~ManagePasswordsIconCocoa() {
22 }
23
24 void ManagePasswordsIconCocoa::UpdateVisibleUI() {
25   decoration_->UpdateVisibleUI();
26 }
27
28 // ManagePasswordsDecoration
29
30 ManagePasswordsDecoration::ManagePasswordsDecoration(
31     CommandUpdater* command_updater,
32     LocationBarViewMac* location_bar)
33     : command_updater_(command_updater),
34       location_bar_(location_bar),
35       icon_(new ManagePasswordsIconCocoa(this)) {
36   UpdateUIState();
37 }
38
39 ManagePasswordsDecoration::~ManagePasswordsDecoration() {
40   if (ManagePasswordsBubbleCocoa::instance())
41     ManagePasswordsBubbleCocoa::instance()->SetIcon(NULL);
42 }
43
44 NSPoint ManagePasswordsDecoration::GetBubblePointInFrame(NSRect frame) {
45   const NSRect draw_frame = GetDrawRectInFrame(frame);
46   return NSMakePoint(NSMidX(draw_frame), NSMaxY(draw_frame));
47 }
48
49 bool ManagePasswordsDecoration::AcceptsMousePress() {
50   return true;
51 }
52
53 bool ManagePasswordsDecoration::OnMousePressed(NSRect frame, NSPoint location) {
54   bool result = ImageDecoration::OnMousePressed(frame, location);
55   if (ManagePasswordsBubbleCocoa::instance())
56     ManagePasswordsBubbleCocoa::instance()->Close();
57   else
58     command_updater_->ExecuteCommand(IDC_MANAGE_PASSWORDS_FOR_PAGE);
59   return result;
60 }
61
62 NSString* ManagePasswordsDecoration::GetToolTip() {
63   return icon_->tooltip_text_id()
64              ? l10n_util::GetNSStringWithFixup(icon_->tooltip_text_id())
65              : nil;
66 }
67
68 void ManagePasswordsDecoration::OnChange() {
69   // |location_bar_| can be NULL in tests.
70   if (location_bar_)
71     location_bar_->OnDecorationsChanged();
72 }
73
74 void ManagePasswordsDecoration::UpdateUIState() {
75   if (icon_->state() == password_manager::ui::INACTIVE_STATE) {
76     SetVisible(false);
77     SetImage(nil);
78     if (ManagePasswordsBubbleCocoa::instance())
79       ManagePasswordsBubbleCocoa::instance()->Close();
80     return;
81   }
82   SetVisible(true);
83   SetImage(OmniboxViewMac::ImageForResource(icon_->icon_id()));
84 }
85
86 void ManagePasswordsDecoration::UpdateVisibleUI() {
87   UpdateUIState();
88   OnChange();
89 }