1 // Copyright 2015 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/passwords/manage_credential_item_view.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
9 #include "chrome/browser/ui/views/passwords/credentials_item_view.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/autofill/core/common/password_form.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/resources/grit/ui_resources.h"
15 #include "ui/views/controls/button/image_button.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/controls/link.h"
18 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/layout/layout_constants.h"
27 void BuildColumnSet(views::GridLayout
* layout
) {
28 views::ColumnSet
* column_set
= layout
->AddColumnSet(TWO_COLUMN_SET
);
30 // The credential or "Deleted!" field.
31 column_set
->AddColumn(views::GridLayout::FILL
,
32 views::GridLayout::CENTER
,
34 views::GridLayout::USE_PREF
,
38 // The delete button or "Undo!" field.
39 column_set
->AddPaddingColumn(0, views::kItemLabelSpacing
);
40 column_set
->AddColumn(views::GridLayout::TRAILING
,
41 views::GridLayout::CENTER
,
43 views::GridLayout::USE_PREF
,
50 ManageCredentialItemView::ManageCredentialItemView(
51 ManagePasswordsBubbleModel
* model
,
52 const autofill::PasswordForm
* password_form
)
53 : form_(password_form
),
54 delete_button_(nullptr),
57 form_deleted_(false) {
58 net::URLRequestContextGetter
* request_context
=
59 model_
->GetProfile()->GetRequestContext();
60 credential_button_
.reset(new CredentialsItemView(
61 this, form_
, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD
,
62 CredentialsItemView::ACCOUNT_CHOOSER
, request_context
));
63 credential_button_
->set_owned_by_client();
64 credential_button_
->SetEnabled(false);
68 ManageCredentialItemView::~ManageCredentialItemView() {
71 void ManageCredentialItemView::Refresh() {
72 RemoveAllChildViews(true);
73 views::GridLayout
* layout
= new views::GridLayout(this);
74 SetLayoutManager(layout
);
75 BuildColumnSet(layout
);
76 layout
->StartRow(1, TWO_COLUMN_SET
);
77 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
79 delete_button_
= nullptr;
80 views::Label
* removed_account
= new views::Label(
81 l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_DELETED_ACCOUNT
),
82 rb
->GetFontList(ui::ResourceBundle::SmallFont
));
83 removed_account
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
84 layout
->AddView(removed_account
);
86 new views::Link(l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_UNDO
));
87 undo_link_
->SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
88 undo_link_
->set_listener(this);
89 undo_link_
->SetUnderline(false);
90 undo_link_
->SetFontList(rb
->GetFontList(ui::ResourceBundle::BoldFont
));
91 layout
->AddView(undo_link_
);
94 layout
->AddView(credential_button_
.get());
95 delete_button_
= new views::ImageButton(this);
96 delete_button_
->SetImage(views::ImageButton::STATE_NORMAL
,
97 rb
->GetImageNamed(IDR_CLOSE_2
).ToImageSkia());
98 delete_button_
->SetImage(views::ImageButton::STATE_HOVERED
,
99 rb
->GetImageNamed(IDR_CLOSE_2_H
).ToImageSkia());
100 delete_button_
->SetImage(views::ImageButton::STATE_PRESSED
,
101 rb
->GetImageNamed(IDR_CLOSE_2_P
).ToImageSkia());
102 layout
->AddView(delete_button_
);
105 GetLayoutManager()->Layout(this);
108 void ManageCredentialItemView::ButtonPressed(views::Button
* sender
,
109 const ui::Event
& event
) {
110 DCHECK_EQ(delete_button_
, sender
);
111 form_deleted_
= true;
112 model_
->OnPasswordAction(*form_
, ManagePasswordsBubbleModel::REMOVE_PASSWORD
);
116 void ManageCredentialItemView::LinkClicked(views::Link
* source
,
118 DCHECK_EQ(undo_link_
, source
);
119 form_deleted_
= false;
120 model_
->OnPasswordAction(*form_
, ManagePasswordsBubbleModel::ADD_PASSWORD
);