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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
// Copyright 2022 The Chromium Authors
// 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/passwords/manage_passwords_view.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/passwords/bubble_controllers/manage_passwords_bubble_controller.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate.h"
#include "chrome/browser/ui/passwords/ui_utils.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/controls/page_switcher_view.h"
#include "chrome/browser/ui/views/passwords/manage_passwords_details_view.h"
#include "chrome/browser/ui/views/passwords/manage_passwords_list_view.h"
#include "chrome/browser/ui/views/passwords/views_utils.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/password_sync_util.h"
#include "components/password_manager/core/common/password_manager_constants.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/gfx/favicon_size.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/layout/table_layout_view.h"
#include "ui/views/view_class_properties.h"
using password_manager::metrics_util::PasswordManagementBubbleInteractions;
ManagePasswordsView::ManagePasswordsView(content::WebContents* web_contents,
views::View* anchor_view)
: PasswordBubbleViewBase(web_contents,
anchor_view,
/*easily_dismissable=*/true),
controller_(PasswordsModelDelegateFromWebContents(web_contents)) {
SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
// Title insets assume there is content (and thus have no bottom padding). Use
// dialog insets to get the bottom margin back.
set_title_margins(
ChromeLayoutProvider::Get()->GetInsetsMetric(views::INSETS_DIALOG));
// Set the right and left margins to 0 such that the `page_container_` fills
// the whole page bubble width. Top margin is handled by the title above, and
// remove bottom margin such that `page_container_` can assign it if needed.
set_margins(gfx::Insets());
page_container_ = AddChildView(
std::make_unique<PageSwitcherView>(std::make_unique<views::View>()));
page_container_->SetProperty(
views::kMarginsKey,
gfx::Insets().set_bottom(ChromeLayoutProvider::Get()->GetDistanceMetric(
DISTANCE_CONTENT_LIST_VERTICAL_SINGLE)));
if (!controller_.GetCredentials().empty()) {
// The request is cancelled when the |controller_| is destroyed.
// |controller_| has the same lifetime as |this| and hence it's safe to use
// base::Unretained(this).
controller_.RequestFavicon(base::BindOnce(
&ManagePasswordsView::OnFaviconReady, base::Unretained(this)));
}
set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_BUBBLE_PREFERRED_WIDTH));
SetProperty(views::kElementIdentifierKey, kTopView);
}
ManagePasswordsView::~ManagePasswordsView() = default;
void ManagePasswordsView::DisplayDetailsOfPasswordForTesting(
password_manager::PasswordForm password_form) {
controller_.set_details_bubble_credential(std::move(password_form));
RecreateLayout();
}
PasswordBubbleControllerBase* ManagePasswordsView::GetController() {
return &controller_;
}
const PasswordBubbleControllerBase* ManagePasswordsView::GetController() const {
return &controller_;
}
ui::ImageModel ManagePasswordsView::GetWindowIcon() {
return ui::ImageModel::FromVectorIcon(GooglePasswordManagerVectorIcon(),
ui::kColorIcon);
}
void ManagePasswordsView::AddedToWidget() {
if (controller_.bubble_mode() ==
ManagePasswordsBubbleController::BubbleMode::kSingleCredentialDetails) {
// The user is expected to be authenticated before showing the bubble in
// the single credential mode. Analogous to authentication expiration
// after clicking on a credentail from the list, start the timer to close
// the bubble.
auth_timer_.Start(FROM_HERE,
password_manager::constants::kPasswordManagerAuthValidity,
base::BindRepeating(&ManagePasswordsView::CloseBubble,
base::Unretained(this)));
}
RecreateLayout();
}
bool ManagePasswordsView::Accept() {
// Accept button is only visible in the details page.
DCHECK(password_details_view_);
DCHECK(controller_.get_details_bubble_credential().has_value());
password_manager::PasswordForm updated_form =
controller_.get_details_bubble_credential().value();
std::optional<std::u16string> updated_username =
password_details_view_->GetUserEnteredUsernameValue();
if (updated_username.has_value()) {
updated_form.username_value = updated_username.value();
}
std::optional<std::u16string> updated_note =
password_details_view_->GetUserEnteredPasswordNoteValue();
if (updated_note.has_value()) {
updated_form.SetNoteWithEmptyUniqueDisplayName(updated_note.value());
}
controller_.UpdateDetailsBubbleCredentialInPasswordStore(
std::move(updated_form));
SwitchToReadingMode();
// Return false such that the bubble doesn't get closed upon clicking the
// button.
return false;
}
bool ManagePasswordsView::Cancel() {
// Cancel button is only visible in the details page.
DCHECK(controller_.get_details_bubble_credential().has_value());
SwitchToReadingMode();
// Return false such that the bubble doesn't get closed upon clicking the
// button.
return false;
}
std::unique_ptr<ManagePasswordsListView>
ManagePasswordsView::CreatePasswordListView() {
return std::make_unique<ManagePasswordsListView>(
controller_.GetCredentials(), GetFaviconImageModel(),
base::BindRepeating(
&ManagePasswordsView::AuthenticateUserAndDisplayDetailsOf,
base::Unretained(this)),
base::BindRepeating(
[](ManagePasswordsView* view) {
view->controller_.OnManageClicked(
password_manager::ManagePasswordsReferrer::
kManagePasswordsBubble);
view->CloseBubble();
// TODO(b/329572483): move this logging to the controller.
password_manager::metrics_util::
LogUserInteractionsInPasswordManagementBubble(
PasswordManagementBubbleInteractions::
kManagePasswordsButtonClicked);
},
base::Unretained(this)),
controller_.IsAccountStorageEnabled());
}
std::unique_ptr<ManagePasswordsDetailsView>
ManagePasswordsView::CreatePasswordDetailsView() {
DCHECK(controller_.get_details_bubble_credential().has_value());
return std::make_unique<ManagePasswordsDetailsView>(
controller_.get_details_bubble_credential().value(),
/*allow_empty_username_edit=*/controller_.bubble_mode() ==
ManagePasswordsBubbleController::BubbleMode::kCredentialList,
base::BindRepeating(&ManagePasswordsBubbleController::UsernameExists,
base::Unretained(&controller_)),
base::BindRepeating(
[](ManagePasswordsView* view) {
view->SetButtons(
static_cast<int>(ui::mojom::DialogButton::kOk) |
static_cast<int>(ui::mojom::DialogButton::kCancel));
view->SetButtonLabel(
ui::mojom::DialogButton::kOk,
l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_UPDATE));
view->GetBubbleFrameView()->SetFootnoteView(
view->CreateFooterView());
// TODO(crbug.com/41493925): Remove this SizeToContents().
// This SizeToContent() is used for immediate layout to ensure that
// a subsequent RequestFocus() sets the correct focus.
view->SizeToContents();
},
base::Unretained(this)),
base::BindRepeating(&ManagePasswordsView::ExtendAuthValidity,
base::Unretained(this)),
base::BindRepeating(
[](ManagePasswordsView* view, bool is_invalid) {
view->SetButtonEnabled(ui::mojom::DialogButton::kOk, !is_invalid);
},
base::Unretained(this)),
base::BindRepeating(
[](ManagePasswordsView* view) {
view->controller_.OnManagePasswordClicked(
password_manager::ManagePasswordsReferrer::
kManagePasswordDetailsBubble);
view->CloseBubble();
// TODO(b/329572483): move this logging to the controller.
password_manager::metrics_util::
LogUserInteractionsInPasswordManagementBubble(
PasswordManagementBubbleInteractions::
kManagePasswordButtonClicked);
},
base::Unretained(this)));
}
std::unique_ptr<views::View> ManagePasswordsView::CreateFooterView() {
base::RepeatingClosure open_password_manager_closure = base::BindRepeating(
[](ManagePasswordsView* dialog) {
dialog->controller_.OnGooglePasswordManagerLinkClicked();
// TODO(b/329572483): move this logging to the controller.
password_manager::metrics_util::
LogUserInteractionsInPasswordManagementBubble(
PasswordManagementBubbleInteractions::
kGooglePasswordManagerLinkClicked);
},
base::Unretained(this));
switch (controller_.GetPasswordSyncState()) {
case ManagePasswordsBubbleController::SyncState::kNotActive:
return CreateGooglePasswordManagerLabel(
/*text_message_id=*/
IDS_PASSWORD_BUBBLES_FOOTER_SAVING_ON_DEVICE,
/*link_message_id=*/
IDS_PASSWORD_BUBBLES_PASSWORD_MANAGER_LINK_TEXT_SAVING_ON_DEVICE,
open_password_manager_closure, views::style::CONTEXT_BUBBLE_FOOTER);
case ManagePasswordsBubbleController::SyncState::
kActiveWithSyncFeatureEnabled:
return CreateGooglePasswordManagerLabel(
/*text_message_id=*/
IDS_PASSWORD_BUBBLES_FOOTER_SYNCED_TO_ACCOUNT,
/*link_message_id=*/
IDS_PASSWORD_BUBBLES_PASSWORD_MANAGER_LINK_TEXT_SYNCED_TO_ACCOUNT,
controller_.GetPrimaryAccountEmail(), open_password_manager_closure,
views::style::CONTEXT_BUBBLE_FOOTER);
case ManagePasswordsBubbleController::SyncState::
kActiveWithAccountPasswords:
// Account store users have a special footer in the management bubble
// since they might have a mix of synced and non-synced passwords.
return CreateGooglePasswordManagerLabel(
/*text_message_id=*/
IDS_PASSWORD_MANAGEMENT_BUBBLE_FOOTER_ACCOUNT_STORE_USERS,
/*link_message_id=*/
IDS_PASSWORD_BUBBLES_PASSWORD_MANAGER_LINK_TEXT_SYNCED_TO_ACCOUNT,
open_password_manager_closure, views::style::CONTEXT_BUBBLE_FOOTER);
}
}
std::unique_ptr<views::View>
ManagePasswordsView::CreateMovePasswordFooterView() {
const ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
base::RepeatingClosure move_password_closure = base::BindRepeating(
[](ManagePasswordsView* dialog) {
dialog->controller_.OnMovePasswordLinkClicked();
},
base::Unretained(this));
auto footer = std::make_unique<views::FlexLayoutView>();
views::ImageView* icon_view = footer->AddChildView(
std::make_unique<views::ImageView>(ui::ImageModel::FromVectorIcon(
vector_icons::kSaveCloudIcon, ui::kColorIcon,
layout_provider->GetDistanceMetric(
views::DISTANCE_BUBBLE_HEADER_VECTOR_ICON_SIZE))));
icon_view->SetVerticalAlignment(views::ImageView::Alignment::kLeading);
icon_view->SetProperty(
views::kMarginsKey,
gfx::Insets::TLBR(
0, 0, 0,
layout_provider->GetInsetsMetric(views::INSETS_DIALOG_TITLE).left()));
views::StyledLabel* footer_label =
footer->AddChildView(CreateGooglePasswordManagerLabel(
/*text_message_id=*/
IDS_PASSWORD_MANAGER_MANAGEMENT_BUBBLE_FOOTER_MOVE_PASSWORD,
/*link_message_id=*/
IDS_PASSWORD_MANAGER_MANAGEMENT_BUBBLE_LINK_TEXT_MOVE_PASSWORD,
move_password_closure, views::style::CONTEXT_BUBBLE_FOOTER));
const int footer_label_width =
layout_provider->GetDistanceMetric(
views::DISTANCE_BUBBLE_PREFERRED_WIDTH) -
2 * layout_provider->GetInsetsMetric(views::INSETS_DIALOG).width();
footer_label->SizeToFit(footer_label_width);
return footer;
}
void ManagePasswordsView::RecreateLayout() {
views::BubbleFrameView* frame_view = GetBubbleFrameView();
CHECK(frame_view);
frame_view->SetFootnoteView(nullptr);
if (controller_.get_details_bubble_credential().has_value()) {
bool has_back_button =
controller_.bubble_mode() ==
ManagePasswordsBubbleController::BubbleMode::kCredentialList;
frame_view->SetTitleView(ManagePasswordsDetailsView::CreateTitleView(
controller_.get_details_bubble_credential().value(),
has_back_button ? std::make_optional(base::BindRepeating(
&ManagePasswordsView::SwitchToListView,
base::Unretained(this)))
: std::nullopt));
std::unique_ptr<ManagePasswordsDetailsView> details_view =
CreatePasswordDetailsView();
password_details_view_ = details_view.get();
page_container_->SwitchToPage(std::move(details_view));
if (controller_.IsAccountStorageEnabled() &&
!controller_.get_details_bubble_credential()
.value()
.IsUsingAccountStore()) {
frame_view->SetFootnoteView(CreateMovePasswordFooterView());
frame_view->SetProperty(views::kElementIdentifierKey, kFooterId);
}
} else {
password_details_view_ = nullptr;
frame_view->SetTitleView(CreateTitleView(controller_.GetTitle()));
page_container_->SwitchToPage(CreatePasswordListView());
}
SetTitle(controller_.GetTitle());
PreferredSizeChanged();
}
void ManagePasswordsView::SwitchToReadingMode() {
password_details_view_->SwitchToReadingMode();
SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
RecreateLayout();
}
void ManagePasswordsView::SwitchToListView() {
auth_timer_.Stop();
SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
controller_.set_details_bubble_credential(std::nullopt);
RecreateLayout();
}
void ManagePasswordsView::ExtendAuthValidity() {
if (auth_timer_.IsRunning()) {
auth_timer_.Reset();
}
}
void ManagePasswordsView::OnFaviconReady(const gfx::Image& favicon) {
if (!favicon.IsEmpty()) {
favicon_ = favicon;
RecreateLayout();
}
}
ui::ImageModel ManagePasswordsView::GetFaviconImageModel() const {
// Use a globe fallback icon until the actual favicon is loaded.
return favicon_.IsEmpty() ? ui::ImageModel::FromVectorIcon(
kGlobeIcon, ui::kColorIcon, gfx::kFaviconSize)
: ui::ImageModel::FromImage(favicon_);
}
void ManagePasswordsView::AuthenticateUserAndDisplayDetailsOf(
password_manager::PasswordForm password_form) {
// Prevent the bubble from closing for the duration of the lifetime of the
// `pin`. This is to keep it open while the user authentication is in action.
std::unique_ptr<CloseOnDeactivatePin> pin = PreventCloseOnDeactivate();
// Pass `pin` to the callback to keep it alive till the completion of the
// authentication process.
controller_.AuthenticateUserAndDisplayDetailsOf(
std::move(password_form),
base::BindOnce(
[](ManagePasswordsView* view,
std::unique_ptr<CloseOnDeactivatePin> pin,
bool authentication_result) {
// If the authentication is successful, navigate to the details page
// by recreating the layout.
if (authentication_result) {
view->RecreateLayout();
view->auth_timer_.Start(
FROM_HERE,
password_manager::constants::kPasswordManagerAuthValidity,
base::BindRepeating(&ManagePasswordsView::SwitchToListView,
base::Unretained(view)));
}
// This is necessary on Windows since the bubble isn't activated
// again after the conlusion of the auth flow.
view->GetWidget()->Activate();
// Delay the destruction of `pin` for 1 sec to make sure the bubble
// remains open till the OS closes the authentication dialog and
// reactivates the bubble.
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce([](std::unique_ptr<CloseOnDeactivatePin> pin) {},
std::move(pin)),
base::Seconds(1));
},
base::Unretained(this), std::move(pin)));
}
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ManagePasswordsView, kTopView);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ManagePasswordsView, kFooterId);
BEGIN_METADATA(ManagePasswordsView)
END_METADATA
|